Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/administrator/components/com_ats/Controller/
Upload File :
Current File : /var/www/consult-e-syn/public_html/administrator/components/com_ats/Controller/ControlPanel.php

<?php
/**
 * @package   ats
 * @copyright Copyright (c)2011-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license   GNU General Public License version 3, or later
 */

namespace Akeeba\TicketSystem\Admin\Controller;

defined('_JEXEC') or die;

use Akeeba\TicketSystem\Admin\Helper\PluginParameters;
use Akeeba\TicketSystem\Admin\Model\Attachments;
use Akeeba\TicketSystem\Admin\Model\PostStatistics;
use Akeeba\TicketSystem\Admin\Model\TicketStatistics;
use Akeeba\TicketSystem\Admin\Model\Updates;
use DateInterval;
use FOF40\Controller\Controller;
use FOF40\Date\Date;
use FOF40\Utils\ViewManifestMigration;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;

class ControlPanel extends Controller
{
	/**
	 * Applies the Download ID when the user is prompted about it in the Control Panel
	 */
	public function applydlid()
	{
		$this->csrfProtection();

		$msg     = Text::_('COM_ATS_CPANEL_ERR_INVALIDDOWNLOADID');
		$msgType = 'error';
		$dlid    = $this->input->getString('dlid', '');

		/** @var Updates $updateModel */
		$updateModel = $this->container->factory->model('Updates')->tmpInstance();
		$dlid        = $updateModel->sanitizeLicenseKey($dlid);
		$isValidDLID = $updateModel->isValidLicenseKey($dlid);

		// If the Download ID seems legit let's apply it
		if ($isValidDLID)
		{
			$msg     = null;
			$msgType = null;

			$updateModel->setLicenseKey($dlid);
		}

		// Redirect back to the control panel
		$url       = '';
		$returnurl = $this->input->get('returnurl', '', 'base64');

		if (!empty($returnurl))
		{
			$url = base64_decode($returnurl);
		}

		if (empty($url))
		{
			$url = Uri::base() . 'index.php?option=com_ats';
		}

		$this->setRedirect($url, $msg, $msgType);
	}

	/**
	 * Resets the "updatedb" flag and forces the database updates
	 */
	public function forceUpdateDb()
	{
		// Reset the flag so the updates could take place
		$this->container->params->set('updatedb', null);
		$this->container->params->save();

		/** @var \Akeeba\TicketSystem\Admin\Model\ControlPanel $model */
		$model = $this->getModel();

		try
		{
			$model->checkAndFixDatabase();
		}
		catch (\RuntimeException $e)
		{
			// This should never happen, since we reset the flag before execute the update, but you never know
		}

		$this->setRedirect('index.php?option=com_ats');
	}

	/**
	 * Gets the ticket and posts statistics for rendering the control panel graphs
	 */
	public function getTicketStats()
	{
		// Create the search criteria: tickets/posts filed in the last month
		$earliestDate   = (new Date())->sub(new DateInterval('P30D'))->setTime(0, 0, 0, 0);
		$latestDate     = new Date();
		$searchCriteria = [
			"from" => $earliestDate->toSql(),
			"to"   => $latestDate->toSql(),
		];

		// Create a default results array filled with zeroes.
		$results  = [];
		$thisDate = $earliestDate;

		while (true)
		{
			$key           = $thisDate->format('Y-m-d');
			$results[$key] = [
				'tickets' => 0,
				'posts'   => 0,
			];

			$thisDate->add(new DateInterval('P1D'));

			if ($thisDate->getTimestamp() > $latestDate->getTimestamp())
			{
				break;
			}
		};

		/** @var TicketStatistics $ticketModel */
		$ticketModel = $this->container->factory->model('TicketStatistics')->tmpInstance();
		$ticketModel->setState('groupbydate', 1);
		$ticketModel->where('created_on', '[]', $searchCriteria);

		/** @var PostStatistics $postsModel */
		$postsModel = $this->container->factory->model('PostStatistics')->tmpInstance();
		$postsModel->where('created_on', '[]', $searchCriteria);
		$postsModel->setState('groupbydate', 1);

		// Fill in the results array with statistics results
		$ticketModel->get(true)->each(function ($item) use (&$results) {
			$results[$item->date]['tickets'] = $item->tickets;
		});

		$postsModel->get(true)->each(function ($item) use (&$results) {
			$results[$item->date]['posts'] = $item->posts;
		});

		$results = array_map(function ($date, $data) {
			return array_merge([
				'date' => $date,
			], $data);
		}, array_keys($results), $results);

		// Finally, render the results
		@ob_end_clean();

		echo json_encode(array_values($results));

		$this->container->platform->closeApplication();
	}

	/**
	 * Runs before the main task, used to perform housekeeping function automatically
	 */
	protected function onBeforeDefault()
	{
		/** @var \Akeeba\TicketSystem\Admin\Model\ControlPanel $model */
		$model = $this->getModel();

		try
		{
			$model->checkAndFixDatabase();
		}
		catch (\RuntimeException $e)
		{
			// The update is stuck. We will display a warning in the Control Panel
		}

		// Migrate options
		PluginParameters::migrate();

		// Secure the attachments directory
		Attachments::secureAttachmentsDirectory();

		// Migrate view manifests
		ViewManifestMigration::migrateJoomla4MenuXMLFiles($this->container);
		ViewManifestMigration::removeJoomla3LegacyViews($this->container);

		/** @var \Akeeba\TicketSystem\Admin\Model\Updates $updatesModel */
		$updatesModel = $this->getModel('Updates');
		$updatesModel->refreshUpdateSite();

		// Store the site URL in the component options, we're going to need it while running in CLI
		$db        = $this->container->db;
		$query     = $db->getQuery(true)
			->select('params')
			->from($db->qn('#__extensions'))
			->where($db->qn('element') . '=' . $db->q('com_ats'))
			->where($db->qn('type') . '=' . $db->q('component'));
		$rawparams = $db->setQuery($query)->loadResult();
		$params    = new Registry();
		$params->loadString($rawparams, 'JSON');

		$siteURL_stored  = $params->get('siteurl', '');
		$siteURL_target  = str_replace('/administrator', '', Uri::base());
		$sitePath_stored = $params->get('sitepath', '');
		$sitePath_target = str_replace('/administrator', '', Uri::base(true));

		if (($siteURL_target != $siteURL_stored) || ($sitePath_target != $sitePath_stored))
		{
			$params->set('siteurl', $siteURL_target);
			$params->set('sitepath', $sitePath_target);

			$query = $db->getQuery(true)
				->update($db->qn('#__extensions'))
				->set($db->qn('params') . '=' . $db->q($params->toString()))
				->where($db->qn('element') . '=' . $db->q('com_ats'))
				->where($db->qn('type') . '=' . $db->q('component'));
			$db->setQuery($query)->execute();
		}
	}
}