Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/plugins/search/ats/
Upload File :
Current File : /var/www/consult-e-syn/public_html/plugins/search/ats/ats.php

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

defined('_JEXEC') or die;

use Akeeba\TicketSystem\Admin\Helper\Permissions;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;

/**
 * ATS Search plugin
 */
class plgSearchAts extends CMSPlugin
{
	/** @var \FOF40\Container\Container The component container */
	protected $container;

	/**
	 * Constructor
	 *
	 * @access      protected
	 *
	 * @param   object  $subject  The object to observe
	 * @param   array   $config   An array that holds the plugin configuration
	 */
	public function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);

		$this->loadLanguage();
		$this->container = \FOF40\Container\Container::getInstance('com_ats');
	}

	/**
	 * @return array An array of search areas
	 */
	public function onContentSearchAreas()
	{
		static $areas = [
			'ats' => 'PLG_SEARCH_ATS_ATS',
		];

		return $areas;
	}

	/**
	 * ATS Search method
	 *
	 * The sql must return the following fields that are used in a common display
	 * routine: href, title, section, created, text, browsernav
	 *
	 * @param   string  $text      Target search string
	 * @param   string  $phrase    Matching option, exact|any|all
	 * @param   string  $ordering  Ordering option, newest|oldest|popular|alpha|category
	 * @param   mixed   $areas     An array if the search it to be restricted to areas, null if search all
	 *
	 * @return array
	 */
	public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
	{
		$db     = $this->container->db;
		$app    = Factory::getApplication();
		$user   = Permissions::getUser();
		$groups = implode(',', $user->getAuthorisedViewLevels());

		$searchText = $text;

		if (is_array($areas))
		{
			if (!array_intersect($areas, array_keys($this->onContentSearchAreas())))
			{
				return [];
			}
		}

		$limit = $this->params->get('search_limit', 50);
		$text  = trim($text);

		if ($text == '')
		{
			return [];
		}

		$section = JText::_('PLG_SEARCH_ATS_ATS');

		$query = $db->getQuery(true);

		switch ($phrase)
		{
			case 'exact':
				$text = $db->Quote('%' . $db->escape($text, true) . '%', false);
				$query->where(
					'((' . $db->qn('t') . '.' . $db->qn('title') . ' LIKE ' . $text . ')'
					. ' OR ' .
					'(' . $db->qn('p') . '.' . $db->qn('content_html') . ' LIKE ' . $text . '))'
				);
				break;

			case 'all':
			default:
				$words = explode(' ', $text);
				foreach ($words as $word)
				{
					$word = $db->Quote('%' . $db->escape($word, true) . '%', false);
					$query->where(
						'((' . $db->qn('t') . '.' . $db->qn('title') . ' LIKE ' . $word . ')'
						. ' OR ' .
						'(' . $db->qn('p') . '.' . $db->qn('content_html') . ' LIKE ' . $word . '))'
					);
				}
				break;

			case 'any':
				$words = explode(' ', $text);
				foreach ($words as $word)
				{
					$word = $db->Quote('%' . $db->escape($word, true) . '%', false);
					$query->where(
						'((' . $db->qn('t') . '.' . $db->qn('title') . ' LIKE ' . $word . ')'
						. ' OR ' .
						'(' . $db->qn('p') . '.' . $db->qn('content_html') . ' LIKE ' . $word . '))',
						'OR'
					);
				}
				break;
		}

		switch ($ordering)
		{
			case 'oldest':
				$order = 'p.created_on ASC';
				break;

			case 'popular':
				// @todo Implement a hits field
				$order = 'p.created_on DESC';
				break;

			case 'alpha':
				$order = 't.title ASC';
				break;

			case 'category':
				$order = 'c.title ASC, t.title ASC';
				break;

			case 'newest':
			default:
				$order = 'p.created_on DESC';
		}

		$query->select([
			$db->qn('p') . '.' . $db->qn('ats_post_id'),
			$db->qn('p') . '.' . $db->qn('ats_ticket_id'),
			$db->qn('t') . '.' . $db->qn('title') . ' AS ' . $db->qn('title'),
			'CONCAT_WS(" / ", ' . $db->q($section) . ', c.title) AS section',
			$db->qn('p') . '.' . $db->qn('created_on') . ' AS ' . $db->qn('created'),
			$db->qn('p') . '.' . $db->qn('content_html') . ' AS ' . $db->qn('text'),
			$db->q('1') . ' AS ' . $db->qn('browsernav'),
		])->from($db->qn('#__ats_posts') . ' AS ' . $db->qn('p'))
			->join('INNER',
				$db->qn('#__ats_tickets') . ' AS ' . $db->qn('t') . ' ON(' .
				$db->qn('t') . '.' . $db->qn('ats_ticket_id') . ' = ' .
				$db->qn('p') . '.' . $db->qn('ats_ticket_id') . ')'
			)->join('INNER',
				$db->qn('#__categories') . ' AS ' . $db->qn('c') . ' ON(' .
				$db->qn('c') . '.' . $db->qn('id') . ' = ' .
				$db->qn('t') . '.' . $db->qn('catid') . ')'
			)
			->where('p.enabled = 1')
			->where('t.enabled = 1')
			->where('c.published = 1')
			->where('t.public = 1')
			->order($order);;

		// Just checkin' that the user ain't a Suepr Administrator
		if (!$user->authorise('core.admin'))
		{
			$query->where($db->qn('c') . '.' . $db->qn('access') . ' IN (' . $groups . ')');
		}

		// Filter by language
		if ($app->isClient('site') && $app->getLanguageFilter())
		{
			$tag = Factory::getLanguage()->getTag();
			$query->where('c.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')');
		}

		$db->setQuery($query, 0, $limit);
		$rows = $db->loadObjectList();

		$return = [];

		if ($rows)
		{
			foreach ($rows as $key => $row)
			{
				$rows[$key]->href = Route::_('index.php?option=com_ats&view=ticket&id=' . $row->ats_ticket_id, true, Route::TLS_IGNORE, true) . '#p' . $row->ats_post_id;
			}

			foreach ($rows as $key => $article)
			{
				if (SearchHelper::checkNoHTML($article, $searchText, ['url', 'text', 'title']))
				{
					$return[] = $article;
				}
			}
		}

		return $return;
	}
}