Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/plugins/atsinstantreply/docimport/
Upload File :
Current File : /var/www/consult-e-syn/public_html/plugins/atsinstantreply/docimport/docimport.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\Model\Categories;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;

/**
 * Akeeba Ticket System - InstantReplies from DocImport documentation
 */
class plgAtsinstantreplyDocimport extends CMSPlugin
{
	/**
	 * The site's database driver object. The property's presence causes the CMSPlugin to magically populate it.
	 *
	 * @var   JDatabaseDriver
	 * @since 3.2.0
	 */
	protected $db;

	/**
	 * Return InstantReply results to ATS
	 *
	 * @param   Categories  $category    The ATS category in which we are performing the search
	 * @param   string      $search      The search string (partial ticket title)
	 * @param   bool        $siteChrome  Should I display the full site template when a search result link is clicked?
	 *
	 * @return  array
	 *
	 * @since   3.2.0
	 */
	public function onATSInstantReply(Categories $category, $search, $siteChrome = true)
	{
		// Get the unique DocImport categories to search into
		$docImportCategories = $category->params->get('dicats', []);
		$docImportCategories = array_map(function ($x) {
			if (empty($x))
			{
				return 0;
			}

			return (int) $x;
		}, $docImportCategories);
		array_filter($docImportCategories, function ($x) {
			return !empty($x);
		});
		$docImportCategories = array_unique($docImportCategories);

		// Make sure we have at least one category to look into
		if (empty($docImportCategories))
		{
			return [];
		}

		// Fetching results from DocImport pages
		$db          = $this->db;
		$dicImploded = implode(', ', array_map([$db, 'q'], $docImportCategories));
		$query       = $db->getQuery(true)
			->select([
				$db->qn('docimport_article_id') . ' AS ' . $db->qn('id'),
				$db->qn('docimport_category_id') . ' AS ' . $db->qn('catid'),
				$db->qn('title'),
				$db->qn('fulltext'),
				'MATCH(' . $db->qn('fulltext') . ') AGAINST (' . $db->q($search) . ') as ' . $db->qn('score'),
			])->from($db->qn('#__docimport_articles'))
			->where($db->qn('docimport_category_id') . ' IN (' . $dicImploded . ')')
			->where('MATCH(' . $db->qn('fulltext') . ') AGAINST (' . $db->q($search) . ')');

		try
		{
			$rawResults = $db->setQuery($query, 0, 10)->loadObjectList();
		}
		catch (\Exception $e)
		{
			return [];
		}

		// Additional query parameters based on whether to show the site's template chrome
		$additionalQueryParams = $siteChrome ? '' : '&tmpl=component';
		$results               = [];
		$factor                = (float) $this->params->get('factor', 1.0);

		foreach ($rawResults as $row)
		{
			$introtext = substr(strip_tags($row->fulltext), 0, 150) . '&hellip;';
			$url       = Route::_('index.php?option=com_docimport&view=article&id=' . $row->id . $additionalQueryParams);

			$results[] = (object) [
				'score'   => $factor * $row->score,
				'title'   => $row->title,
				'preview' => $introtext,
				'url'     => $url,
				'source'  => 'docimport',
			];
		}

		return $results;
	}

	/**
	 * Runs when preparing the ATS Category form in com_categories.
	 *
	 * We use this method to load our partial form definition which adds fields to the InstantReply fieldset.
	 *
	 * @param   Form  $form
	 *
	 * @return  void
	 *
	 * @since   3.2.0
	 *
	 * @see     AtsHelperCategory::onPrepareForm()
	 */
	public function onATSCategoryFormPrepare(Form &$form)
	{
		$file = __DIR__ . '/form/form.xml';
		$xml = simplexml_load_file($file);

		if ($xml === false)
		{
			// Failure is always an option.
			return;
		}

		$form->load($xml, false);
	}
}