- md5
- d0fff6ae02f3d0243164f3a47a425922
- sha1
- 894137561e6218987243c3ebafa5d0ab306a548d
- sha256
- 64a1ce63c898e7e7493b27b99b414dcd5a84dc1f6d5722801547f17e40e4d047
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
<?php
/**
* @package Joomla.Site
* @subpackage mod_related_items
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Module\RelatedItems\Site\Helper;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
/**
* Helper for mod_related_items
*
* @since 1.5
*/
abstract class RelatedItemsHelper
{
/**
* Get a list of related articles
*
* @param \Joomla\Registry\Registry &$params module parameters
*
* @return array
*/
public static function getList(&$params)
{
$db = Factory::getDbo();
$app = Factory::getApplication();
$input = $app->input;
$groups = implode(',', Factory::getUser()->getAuthorisedViewLevels());
$maximum = (int) $params->get('maximum', 5);
$factory = $app->bootComponent('com_content')->getMVCFactory();
// Get an instance of the generic articles model
/** @var \Joomla\Component\Content\Site\Model\ArticlesModel $articles */
$articles = $factory->createModel('Articles', 'Site', ['ignore_request' => true]);
// Set application parameters in model
$articles->setState('params', $app->getParams());
$option = $input->get('option');
$view = $input->get('view');
if (!($option === 'com_content' && $view === 'article'))
{
return array();
}
$temp = $input->getString('id');
$temp = explode(':', $temp);
$id = $temp[0];
$nullDate = $db->getNullDate();
$now = Factory::getDate()->toSql();
$related = [];
$query = $db->getQuery(true);
if ($id)
{
// Select the meta keywords from the item
$query->select('metakey')
->from('#__content')
->where('id = ' . (int) $id);
$db->setQuery($query);
try
{
$metakey = trim($db->loadResult());
}
catch (\RuntimeException $e)
{
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return array();
}
// Explode the meta keys on a comma
$keys = explode(',', $metakey);
$likes = [];
// Assemble any non-blank word(s)
foreach ($keys as $key)
{
$key = trim($key);
if ($key)
{
$likes[] = $db->escape($key);
}
}
if (count($likes))
{
// Select other items based on the metakey field 'like' the keys found
$query->clear()
->select('a.id')
->from('#__content AS a')
->where('a.id != ' . (int) $id)
->where('ws.condition = ' . ContentComponent::CONDITION_PUBLISHED)
->where('a.access IN (' . $groups . ')');
$wheres = array();
foreach ($likes as $keyword)
{
$wheres[] = 'a.metakey LIKE ' . $db->quote('%' . $keyword . '%');
}
$query->where('(' . implode(' OR ', $wheres) . ')')
->where('(a.publish_up = ' . $db->quote($nullDate) . ' OR a.publish_up <= ' . $db->quote($now) . ')')
->where('(a.publish_down = ' . $db->quote($nullDate) . ' OR a.publish_down >= ' . $db->quote($now) . ')');
// Filter by language
if (Multilanguage::isEnabled())
{
$query->where('a.language in (' . $db->quote(Factory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
}
$db->setQuery($query, 0, $maximum);
try
{
$articleIds = $db->loadColumn();
}
catch (\RuntimeException $e)
{
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return array();
}
if (count($articleIds))
{
$articles->setState('filter.article_id', $articleIds);
$articles->setState('filter.published', 1);
$related = $articles->getItems();
}
unset($articleIds);
}
}
if (count($related))
{
// Prepare data for display using display options
foreach ($related as &$item)
{
$item->slug = $item->id . ':' . $item->alias;
$item->route = Route::_(\ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
}
}
return $related;
}
}
The same content is in 2 releases, Joomla 4.0.0-alpha10 to 4.0.0-alpha11. See them all, or every version of this file.