| Current Path : /var/www/consult-e-syn/public_html/components/com_ats/Model/ |
| Current File : /var/www/consult-e-syn/public_html/components/com_ats/Model/InstantReplies.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\Model\DefaultDataModel;
use FOF40\Container\Container;
use FOF40\Model\DataModel\Exception\RecordNotLoaded;
class InstantReplies extends DefaultDataModel
{
public function __construct(Container $container, array $config = [])
{
// Let's fake a table for this model, so it won't complain
$config['idFieldName'] = 'ats_attempt_id';
$config['tableName'] = '#__ats_attempts';
parent::__construct($container, $config);
}
public function &getItemsArray($limitstart = 0, $limit = 0, $overrideLimits = false)
{
// We need to do that since we are returning by reference.
$blankResults = [];
// Make sure our search string is at least 6 characters long (it's unusable if it's less than that)
$search = $this->getState('search', '');
if (empty($search) || (strlen($search) < 6))
{
return $blankResults;
}
// Get the Category I am fetching results for
$catId = $this->getState('catid', 0);
/** @var Categories $category */
$category = $this->container->factory->model('Categories')->tmpInstance();
try
{
$category->findOrFail($catId);
}
catch (RecordNotLoaded $e)
{
return $blankResults;
}
// Should I display the site's template chrome?
$alwaysShowChrome = $this->getState('alwaysShowChrome', 0);
$showChrome = $this->container->params->get('instantreplies_sitechrome', 1);
$showChrome = $alwaysShowChrome ? 1 : $showChrome;
// Search using plugins
$platform = $this->container->platform;
$oldAllowPluginsValue = $platform->isAllowPluginsInCli();
$platform->setAllowPluginsInCli(true);
$platform->importPlugin('atsinstantreply');
$pluginResults = $platform->runPlugins('onATSInstantReply', [$category, $search, $showChrome]);
$platform->setAllowPluginsInCli($oldAllowPluginsValue);
$sort_results = [];
foreach ($pluginResults as $someResults)
{
if (!is_array($someResults) || empty($someResults))
{
continue;
}
$sort_results = array_merge($sort_results, $someResults);
}
if (empty($sort_results))
{
return $blankResults;
}
// Merge the results ordered by score and pick only the first 10 items, re-keying them with integer keys.
usort($sort_results, function ($a, $b) {
if ($a->score == $b->score)
{
return 0;
}
return ($a->score > $b->score) ? -1 : 1;
});
$results = array_values(array_slice($sort_results, 0, 15));
return $results;
}
}