<?php
defined('_JEXEC') or die;
// --------------------------------------------------------------------------
// Software: Eorisis Pixel
// Software URI: https://eorisis.com/shop/software/joomla/pixels
// Type: Joomla Extension
// Author: Eorisis
// Author URI: https://eorisis.com
// Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
// License: GNU General Public License version 2; See https://eorisis.com/gpl
// Terms of Service: https://eorisis.com/tos
// --------------------------------------------------------------------------

use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\cms\language as cms_language;
use com_eorisis_pixels\framework\lib\core\cms\uri as cms_uri;
use com_eorisis_pixels\framework\lib\core\model\document as document_model;
use com_eorisis_pixels\framework\lib\core\model\clean;
use com_eorisis_pixels\framework\lib\core\model\valid;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisBaseController', '\\Joomla\\CMS\\MVC\\Controller\\BaseController');
}
else
{
	JLoader::registerAlias('EorisisBaseController', 'JControllerLegacy');
}

class Eorisis_pixelsController extends EorisisBaseController
{
	protected $default_view = 'dashboard';
	protected $errors = [];

	// --------------------------------------------------

	public function display($cachable = false, $urlparams = false)
	{
		document_model::set_component_layout();

		$view_name = $this->input->get('view', app_config::DEFAULT_VIEW);
		$view_name = valid::view($view_name);

		$layout_name = clean::string_mandatory($this->input->get('layout'), 'default');

		// Security checks
		if ($layout_name == 'edit' && in_array($view_name, app_config::EDIT_VIEWS))
		{
			$component_element = 'com_'.$this->getName();
			$id = (int)$this->input->getInt('id');

			if (!$this->checkEditId($component_element.'.edit.'.$view_name, $id))
			{
				// Somehow the person just went to the form - we don't allow that
				$txt = cms_language::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id);
				$this->setError($txt);
				$this->setMessage($this->getError(), 'error');

				$uri = cms_uri::route('index.php?option='.$component_element.'&view='.app_config::DEFAULT_VIEW, false);
				$this->setRedirect($uri);
				return false;
			}
		}

		return parent::display();
	}

	// --------------------------------------------------

	public function getError($i = null, $toString = true)
	{
		// Get the most recent error message
		// @param   integer  $i         Option error index
		// @param   boolean  $toString  Indicates if JError objects should return their error message
		// @return  string   Error message

		// Find the error
		if ($i === null)
		{
			// Default, return the last message
			$error = end($this->errors);
		}
		elseif (!array_key_exists($i, $this->errors))
		{
			// If $i has been specified but does not exist, return false
			return false;
		}
		else
		{
			$error = $this->errors[$i];
		}

		// Check if only the string is requested
		if ($error instanceof Exception && $toString)
		{
			return $error->getMessage();
		}

		return $error;
	}

	// --------------------------------------------------

	public function setError($error)
	{
		$this->errors[] = $error;
	}
}
