<?php
/**
 * @package   DPCalendar
 * @copyright Copyright (C) 2014 Digital Peak GmbH. <https://www.digital-peak.com>
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
 */

namespace DigitalPeak\Component\DPCalendar\Site\Controller;

\defined('_JEXEC') or die();

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\User\CurrentUserInterface;
use Joomla\CMS\User\CurrentUserTrait;
use Joomla\Input\Input;

class DisplayController extends BaseController implements CurrentUserInterface
{
	use CurrentUserTrait;

	public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?CMSApplicationInterface $app = null, ?Input $input = null)
	{
		$input = Factory::getApplication()->getInput();

		if ($input->get('view') === 'events' && $input->get('layout') === 'modal') {
			// Event frontpage Editor article proxying:
			$config['base_path'] = JPATH_ADMINISTRATOR . '/components/com_dpcalendar';

			Form::addFormPath(JPATH_ADMINISTRATOR . '/components/com_dpcalendar/forms');
		}

		parent::__construct($config, $factory, $app, $input);
	}

	public function display($cachable = false, $urlparams = [])
	{
		$id    = $this->input->get('e_id');
		$vName = $this->input->getCmd('view', 'calendar');
		$this->input->set('view', $vName);

		$safeurlparams = [
			'id'               => 'STRING',
			'limit'            => 'UINT',
			'limitstart'       => 'UINT',
			'filter_order'     => 'CMD',
			'filter_order_Dir' => 'CMD',
			'lang'             => 'CMD'
		];

		// Check for edit form.
		if ($vName == 'form' && is_numeric($id) && !$this->checkEditId('com_dpcalendar.edit.event', (int)$id)) {
			// Somehow the person just went to the form - we don't allow that.
			throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id), 403);
		}

		// The views are not cacheable because the ids are stored in the params
		return parent::display(false, $safeurlparams);
	}
}
