<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\MVC\Controller\BaseController;

/**
 * Main controller of com_cmgiftcard.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardController extends BaseController
{
	/**
	 * Default view.
	 *
	 * @var    string
	 * @since  1.0.0
	 */
	protected $default_view = 'dashboard';

	/**
	 * Method to display a view.
	 *
	 * @param   boolean  $cachable   If true, the view output will be cached
	 * @param   array    $urlparams  An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return  BaseController
	 *
	 * @since   1.0.0
	 */
	public function display($cachable = false, $urlparams = false)
	{
		$view	= $this->input->get('view', 'dashboard');
		$layout	= $this->input->get('layout', 'default');
		$id		= $this->input->getInt('id');

		$views = array('card', 'code', 'design', 'order', 'template');

		// Check for edit form.
		// Somehow user just access the form directly, we don't allow that.
		if (in_array($view, $views) && $layout == 'edit' && !$this->checkEditId('com_cmgiftcard.edit.' . $view, $id))
		{
			$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id), 'error');
			$this->setRedirect(Route::_('index.php?option=com_cmgiftcard&view=' . $view . 's', false));

			return false;
		}

		parent::display();

		return $this;
	}
}
