Your IP : 216.73.217.142


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

defined('_JEXEC') or die;

use Akeeba\TicketSystem\Admin\Helper\Permissions;

/**
 * Class Tickets
 *
 * @method  $this   frontendfilter($bool)   Enable frontend filtering
 * @method  $this   filterNewest($bool)     Order by newest ticket
 * @method  $this   assignedtome($bool)     Only tickets assigned to me
 * @method  $this   categories($cats)       Filter by categories (array)
 *
 * @package Akeeba\TicketSystem\Site\Model
 */
class Tickets extends \Akeeba\TicketSystem\Admin\Model\Tickets
{
	public function buildQuery($override = false)
	{
		$db      = $this->getDbo();
		$user_id = Permissions::getUser()->id;

		// On frontend we use category instead of catid for filtering
		$this->setState('catid', $this->getState('category'));

		$query = parent::buildQuery($override);

		// Filter only public tickets and tickets visible to me
		if($this->getState('frontendfilter') == 1)
		{
			$category = $this->getState('category');

			$isManager = Permissions::isManager($category);

			if(!$isManager && ($user_id > 0))
			{
				// I am not a manager and I have a user ID
				$query->where(
				'('.
					$db->qn('public').' = '.$db->q(1).
					' OR ('.$db->qn('public').' = '.$db->q(0).' AND '.$db->qn('created_by').' = '.$db->q($user_id).')'.
				')'
				);
			}
			elseif(!$isManager)
			{
				// I am a guest
				$query->where($db->qn('public').' = '.$db->q(1));
			}
		}

		// Filter by category
		if($categories = $this->getState('categories', array(), 'array'))
		{
			$categories = array_map(array($db, 'quote'), $categories);

			$query->where($db->qn('catid').' IN ('.implode(',', $categories).')');
		}

		// Only tickets assigned to me
		if($this->getState('assignedtome', 0, 'int') == 1)
		{
			if($user_id > 0)
			{
				$query->where($db->qn('assigned_to').' = '.$db->q($user_id));
			}
		}

		// Apply custom ordering
		$query->clear('order');

		$dir = strtoupper($this->getState('filter_order_Dir', 'DESC', 'cmd'));

		if (!in_array($dir,array('ASC','DESC')))
		{
			$dir = 'ASC';
		}

		$filterNewest = $this->getState('filterNewest', false, 'bool');

		if ($filterNewest)
		{
			// The modified_on field is always filled thanks to our custom behavior
			$order = 'modified_on';
		}
		else
		{
			$order = $this->getState('filter_order', 'ats_ticket_id', 'cmd');

			if (!in_array($order, array_keys($this->knownFields)))
			{
				$order = 'ats_ticket_id';
			}
		}

		$query->order($order.' '.$dir);

		return $query;
	}

	/**
	 * Loads and decode ticket custom fields data, returning an array containing labels and values
	 *
	 * @return   array   Array list of array following this logic:
	 *                   array('label' => field label, 'value' => field value)
	 */
	public function loadCustomFields()
	{
		$customFields = [];
		$ticket_id    = $this->getId();

		// Got no ticket id, return an empty set
		if (!$ticket_id)
		{
			return $customFields;
		}

		// No fields to decode
		if (!$this->params)
		{
			return $customFields;
		}

		/**
		 * Should I only show public fields?
		 *
		 * If the current user is support staff OR the ticket owner we display all fields. In any other case we will only
		 * show the public fields.
		 */
		$privileges       = Permissions::getPrivileges($this);
		$isManager        = $privileges['admin'];
		$isOwner          = ($this->created_by != 0) && ($this->created_by == Permissions::getUser()->id);
		$onlyPublicFields = !$isManager && !$isOwner;

		/** @var CustomFields $customModel */
		$customModel = $this->container->factory->model('CustomFields')->tmpInstance();
		$fields      = $customModel->enabled(true)->get(true);

		// Organize the fields
		/** @var CustomFields $field */
		foreach ($fields as $field)
		{
			if ($onlyPublicFields && !$field->public)
			{
				continue;
			}

			$decoded[$field->slug] = $field;
		}

		$customFields = [];

		foreach ($this->params as $slug => $value)
		{
			if (!isset($decoded[$slug]))
			{
				continue;
			}

			$field       = $decoded[$slug];
			$jResponse   = $this->container->platform->runPlugins('onAtsDisplayCustomField', [$field, $value]);
			$currentItem = [];

			if (is_array($jResponse) && !empty($jResponse))
			{
				foreach ($jResponse as $pluginResponse)
				{
					if (!is_array($pluginResponse) || !isset($pluginResponse['label']) || !isset($pluginResponse['value']))
					{
						continue;
					}

					$currentItem = array_merge($currentItem, $pluginResponse);
				}
			}

			if (!empty($currentItem))
			{
				$customFields[] = $currentItem;
			}
		}

		// Add more fields defined by custom field plugins
		$jResponse = $this->container->platform->runPlugins('onAtsDisplayCustomFields', [$this]);

		if (is_array($jResponse) && !empty($jResponse))
		{
			foreach ($jResponse as $pluginResponse)
			{
				if (!is_array($pluginResponse) || empty($pluginResponse))
				{
					continue;
				}

				$customFields = array_merge($customFields, $pluginResponse);
			}
		}


		return $customFields;
	}

	/**
	 * Return the custom fields definitions, used to render the field inputs in the interface
	 *
	 * @param   $catID  int|null  Overrides the category ID of the current ticket (if $catID is not empty)
	 *
	 * @return  array[]
	 *
	 * @since   3.0.0
	 */
	public function getCustomFieldDefinitions($catID = null)
	{
		$catID     = empty($catID) ? $this->catid : $catID;
		$args      = array_merge($this->getData(), ['catid' => $catID]);
		$jResponse = $this->container->platform->runPlugins('onTicketFormRenderPerCatFields', [$args]);
		$ret       = [];

		if (!is_array($jResponse) || empty($jResponse))
		{
			return $ret;
		}

		foreach ($jResponse as $customFields)
		{
			if (!is_array($customFields) || empty($customFields))
			{
				continue;
			}

			$ret = array_merge($ret, $customFields);
		}

		return $ret;
	}
}