Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/components/com_ats/Helper/
Upload File :
Current File : /var/www/consult-e-syn/public_html/components/com_ats/Helper/Breadcrumbs.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\Site\Helper;

defined('_JEXEC') or die;

use Akeeba\TicketSystem\Site\Model\Tickets;
use FOF40\Container\Container;
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Language\Text;

class Breadcrumbs
{
	private static $iHaveRun = false;

	public static function buildCrumbs()
	{
		if (self::$iHaveRun)
		{
			return;
		}

		self::$iHaveRun = true;

		$container = Container::getInstance('com_ats');

		$mObject = JFactory::getApplication()->getMenu()->getActive();
		$mQuery  = is_object($mObject) ? $mObject->query : [];
		$Itemid  = is_object($mObject) ? $mObject->id : null;

		if (!array_key_exists('view', $mQuery))
		{
			$mQuery['view'] = '';
		}

		// Get the menu and actual view
		$mView = $mQuery['view'];
		$aView = $container->input->getCmd('view');

		// Do I have to leave breadcrumbs behind me?
		$leaveCatCrumbs  = in_array($aView, ['Ticket', 'Tickets', 'Newticket']);
		$leaveItemCrumbs = $mView != $aView;

		if ($leaveCatCrumbs)
		{
			// On these views we have to check the category IDs to make sure they match
			$mCategory = array_key_exists('category', $mQuery) ? $mQuery['category'] : 0;
			$catID     = $container->input->getInt('category', 0);

			// If it's a ticket view, I have to pick the catID from the ticket
			if ($aView == 'Ticket')
			{
				$ticketID = $container->input->getInt('id', 0);
				$ticket   = self::loadTicket($ticketID);

				$catID = $ticket->catid;
			}

			$leaveCatCrumbs = $mCategory != $catID;
		}

		// Don't continue if breadcrumbs are not necessary
		if (!$leaveItemCrumbs && !$leaveCatCrumbs)
		{
			return;
		}

		if ($leaveCatCrumbs)
		{
			// Get category
			$db   = $container->db;
			$q    = $db->getQuery(true)
				->select('path')
				->from($db->qn('#__categories'))
				->where($db->qn('id') . ' = ' . $db->q($catID))
				->where($db->qn('extension') . ' = ' . $db->q('com_ats'));
			$path = $db->setQuery($q)->loadResult();

			// Get all categories, by path
			$q = $db->getQuery(true)
				->select([
					$db->qn('path'), $db->qn('id'), $db->qn('title'),
				])
				->from($db->qn('#__categories'))
				->where($db->qn('id') . ' = ' . $db->q($catID))
				->where($db->qn('extension') . ' = ' . $db->q('com_ats'));

			$categoriesRaw = $db->setQuery($q)->loadAssocList('path');

			$categories       = [];
			$categoriesTitles = [];

			foreach ($categoriesRaw as $p => $c)
			{
				$categories[$p]       = $c['id'];
				$categoriesTitles[$p] = $c['title'];
			}

			// Break it down
			$pathParts = explode('/', $path);

			// Iterate it into an array cat ID => path
			$catParts = [];
			$temp     = [];

			foreach ($pathParts as $p)
			{
				$temp[]              = $p;
				$tempPath            = implode('/', $temp);
				$tempID              = array_key_exists($tempPath, $categories) ? $categories[$tempPath] : 0;
				$catParts[$tempPath] = $tempID;
			}

			// Check if the $mCategory is in the array keys. If so, remove
			// elements before it.
			$mCategory = array_key_exists('category', $mQuery) ? $mQuery['category'] : 0;

			if ($mCategory && array_key_exists($mCategory, $catParts))
			{
				$temp  = [];
				$start = false;

				foreach ($catParts as $p => $i)
				{
					if ($i == $mCategory)
					{
						$start = true;
					}

					if ($start)
					{
						$temp[$p] = $i;
					}
				}
				$catParts = $temp;
			}

			// If there are still elements, add them to the breadcrumbs
			if (!empty($catParts))
			{
				foreach ($catParts as $path => $catid)
				{
					if (!array_key_exists($path, $categoriesTitles))
					{
						continue;
					}

					$label = $categoriesTitles[$path];
					$url   = 'index.php?option=com_ats&view=Tickets&category=' . $catid;

					if ($Itemid)
					{
						$url .= '&Itemid=' . $Itemid;
					}

					$url = \JRoute::_($url);

					JFactory::getApplication()->getPathway()->addItem($label, $url);
				}
			}
		}

		if ($leaveItemCrumbs)
		{
			// This has to be a new or existing
			if ($aView == 'Newticket')
			{
				JFactory::getApplication()->getPathway()->addItem(Text::_('COM_ATS_BREADCRUMBS_NEWTICKET'));
			}
			elseif ($container->input->getString('view') == 'Ticket')
			{
				$ticketID = $container->input->getInt('id', 0);
				/** @var Tickets $ticket */
				$ticket = self::loadTicket($ticketID);

				JFactory::getApplication()->getPathway()->addItem(
					'#' . $ticket->ats_ticket_id . ': ' .
					$ticket->title
				);
			}
		}
	}

	/**
	 * Loads a minimal snapshot of a ticket.
	 *
	 * This method goes through the router's loadTicket() method if the router is already loaded. That method uses a
	 * cache to prevent loading the same ticket multiple times on each page, reducing the number of queries and the
	 * page load time.
	 *
	 * @param   int  $id  The ticket ID to load
	 *
	 * @return  object
	 *
	 * @since   4.0.0
	 */
	public static function loadTicket($id)
	{
		if (class_exists('AtsRouter'))
		{
			return \AtsRouter::loadTicket($id);
		}

		$container = Container::getInstance('com_ats');
		/** @var Tickets $ticket */
		$ticket = $container->factory->model('Tickets')->tmpInstance();

		return $ticket->load($id);
	}
}