first commit
This commit is contained in:
87
modules/mod_related_items/mod_related_items.xml
Normal file
87
modules/mod_related_items/mod_related_items.xml
Normal file
@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="module" client="site" method="upgrade">
|
||||
<name>mod_related_items</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2004-07</creationDate>
|
||||
<copyright>(C) 2005 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>3.0.0</version>
|
||||
<description>MOD_RELATED_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Module\RelatedItems</namespace>
|
||||
<files>
|
||||
<folder module="mod_related_items">services</folder>
|
||||
<folder>src</folder>
|
||||
<folder>tmpl</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/mod_related_items.ini</language>
|
||||
<language tag="en-GB">language/en-GB/mod_related_items.sys.ini</language>
|
||||
</languages>
|
||||
<help key="Site_Modules:_Articles_-_Related" />
|
||||
<config>
|
||||
<fields name="params">
|
||||
<fieldset name="basic">
|
||||
<field
|
||||
name="showDate"
|
||||
type="radio"
|
||||
layout="joomla.form.field.radio.switcher"
|
||||
label="MOD_RELATED_FIELD_SHOWDATE_LABEL"
|
||||
default="0"
|
||||
filter="integer"
|
||||
>
|
||||
<option value="0">JHIDE</option>
|
||||
<option value="1">JSHOW</option>
|
||||
</field>
|
||||
|
||||
<field
|
||||
name="maximum"
|
||||
type="number"
|
||||
label="MOD_RELATED_FIELD_MAX_LABEL"
|
||||
default="5"
|
||||
filter="integer"
|
||||
min="1"
|
||||
validate="number"
|
||||
/>
|
||||
</fieldset>
|
||||
<fieldset name="advanced">
|
||||
<field
|
||||
name="layout"
|
||||
type="modulelayout"
|
||||
label="JFIELD_ALT_LAYOUT_LABEL"
|
||||
class="form-select"
|
||||
validate="moduleLayout"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="moduleclass_sfx"
|
||||
type="textarea"
|
||||
label="COM_MODULES_FIELD_MODULECLASS_SFX_LABEL"
|
||||
rows="3"
|
||||
validate="CssIdentifier"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="owncache"
|
||||
type="list"
|
||||
label="COM_MODULES_FIELD_CACHING_LABEL"
|
||||
default="1"
|
||||
filter="integer"
|
||||
validate="options"
|
||||
>
|
||||
<option value="1">JGLOBAL_USE_GLOBAL</option>
|
||||
<option value="0">COM_MODULES_FIELD_VALUE_NOCACHING</option>
|
||||
</field>
|
||||
|
||||
<field
|
||||
name="cache_time"
|
||||
type="number"
|
||||
label="COM_MODULES_FIELD_CACHE_TIME_LABEL"
|
||||
default="900"
|
||||
filter="integer"
|
||||
/>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</config>
|
||||
</extension>
|
||||
41
modules/mod_related_items/services/provider.php
Normal file
41
modules/mod_related_items/services/provider.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_related_items
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
|
||||
use Joomla\CMS\Extension\Service\Provider\Module;
|
||||
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
|
||||
/**
|
||||
* The articles related module service provider.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\RelatedItems'));
|
||||
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\RelatedItems\\Site\\Helper'));
|
||||
|
||||
$container->registerServiceProvider(new Module());
|
||||
}
|
||||
};
|
||||
55
modules/mod_related_items/src/Dispatcher/Dispatcher.php
Normal file
55
modules/mod_related_items/src/Dispatcher/Dispatcher.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_related_items
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Module\RelatedItems\Site\Dispatcher;
|
||||
|
||||
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
|
||||
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
|
||||
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
|
||||
use Joomla\CMS\Helper\ModuleHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Dispatcher class for mod_articles_popular
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
|
||||
{
|
||||
use HelperFactoryAwareTrait;
|
||||
|
||||
/**
|
||||
* Returns the layout data.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
protected function getLayoutData(): array
|
||||
{
|
||||
$data = parent::getLayoutData();
|
||||
$params = $data['params'];
|
||||
|
||||
$cacheParams = new \stdClass();
|
||||
$cacheParams->cachemode = 'safeuri';
|
||||
$cacheParams->class = $this->getHelperFactory()->getHelper('RelatedItemsHelper');
|
||||
$cacheParams->method = 'getRelatedArticles';
|
||||
$cacheParams->methodparams = [$params, $data['app']];
|
||||
$cacheParams->modeparams = ['id' => 'int', 'Itemid' => 'int'];
|
||||
|
||||
$data['list'] = ModuleHelper::moduleCache($this->module, $params, $cacheParams);
|
||||
$data['showDate'] = $params->get('showDate', 0);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
202
modules/mod_related_items/src/Helper/RelatedItemsHelper.php
Normal file
202
modules/mod_related_items/src/Helper/RelatedItemsHelper.php
Normal file
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_related_items
|
||||
*
|
||||
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Module\RelatedItems\Site\Helper;
|
||||
|
||||
use Joomla\CMS\Application\SiteApplication;
|
||||
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;
|
||||
use Joomla\Component\Content\Site\Helper\RouteHelper;
|
||||
use Joomla\Component\Content\Site\Model\ArticlesModel;
|
||||
use Joomla\Database\DatabaseAwareInterface;
|
||||
use Joomla\Database\DatabaseAwareTrait;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\Registry\Registry;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Helper for mod_related_items
|
||||
*
|
||||
* @since 1.5
|
||||
*/
|
||||
class RelatedItemsHelper implements DatabaseAwareInterface
|
||||
{
|
||||
use DatabaseAwareTrait;
|
||||
|
||||
/**
|
||||
* Retrieve a list of related articles based on the metakey field
|
||||
*
|
||||
* @param Registry $params The module parameters.
|
||||
* @param SiteApplication $app The current application.
|
||||
*
|
||||
* @return \stdClass[]
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
public function getRelatedArticles(Registry $params, SiteApplication $app): array
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
$input = $app->getInput();
|
||||
$groups = $app->getIdentity()->getAuthorisedViewLevels();
|
||||
$maximum = (int) $params->get('maximum', 5);
|
||||
$factory = $app->bootComponent('com_content')->getMVCFactory();
|
||||
|
||||
// Get an instance of the generic articles model
|
||||
/** @var 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 [];
|
||||
}
|
||||
|
||||
$temp = $input->getString('id');
|
||||
$temp = explode(':', $temp);
|
||||
$id = (int) $temp[0];
|
||||
|
||||
$now = Factory::getDate()->toSql();
|
||||
$related = [];
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
if ($id) {
|
||||
// Select the meta keywords from the item
|
||||
$query->select($db->quoteName('metakey'))
|
||||
->from($db->quoteName('#__content'))
|
||||
->where($db->quoteName('id') . ' = :id')
|
||||
->bind(':id', $id, ParameterType::INTEGER);
|
||||
$db->setQuery($query);
|
||||
|
||||
try {
|
||||
$metakey = trim($db->loadResult());
|
||||
} catch (\RuntimeException $e) {
|
||||
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
// 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($db->quoteName('a.id'))
|
||||
->from($db->quoteName('#__content', 'a'))
|
||||
->where($db->quoteName('a.id') . ' != :id')
|
||||
->where($db->quoteName('a.state') . ' = ' . ContentComponent::CONDITION_PUBLISHED)
|
||||
->whereIn($db->quoteName('a.access'), $groups)
|
||||
->bind(':id', $id, ParameterType::INTEGER);
|
||||
|
||||
$binds = [];
|
||||
$wheres = [];
|
||||
|
||||
foreach ($likes as $keyword) {
|
||||
$binds[] = '%' . $keyword . '%';
|
||||
}
|
||||
|
||||
$bindNames = $query->bindArray($binds, ParameterType::STRING);
|
||||
|
||||
foreach ($bindNames as $keyword) {
|
||||
$wheres[] = $db->quoteName('a.metakey') . ' LIKE ' . $keyword;
|
||||
}
|
||||
|
||||
$query->extendWhere('AND', $wheres, 'OR')
|
||||
->extendWhere('AND', [ $db->quoteName('a.publish_up') . ' IS NULL', $db->quoteName('a.publish_up') . ' <= :nowDate1'], 'OR')
|
||||
->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.publish_down') . ' IS NULL',
|
||||
$db->quoteName('a.publish_down') . ' >= :nowDate2',
|
||||
],
|
||||
'OR'
|
||||
)
|
||||
->bind([':nowDate1', ':nowDate2'], $now);
|
||||
|
||||
// Filter by language
|
||||
if (Multilanguage::isEnabled()) {
|
||||
$query->whereIn($db->quoteName('a.language'), [$app->getLanguage()->getTag(), '*'], ParameterType::STRING);
|
||||
}
|
||||
|
||||
$query->setLimit($maximum);
|
||||
$db->setQuery($query);
|
||||
|
||||
try {
|
||||
$articleIds = $db->loadColumn();
|
||||
} catch (\RuntimeException $e) {
|
||||
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
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::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
|
||||
}
|
||||
}
|
||||
|
||||
return $related;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of related articles
|
||||
*
|
||||
* @param Registry &$params module parameters
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.6
|
||||
*
|
||||
* @deprecated 4.4.0 will be removed in 6.0
|
||||
* Use the non-static method getRelatedArticles
|
||||
* Example: Factory::getApplication()->bootModule('mod_related_items', 'site')
|
||||
* ->getHelper('RelatedItemsHelper')
|
||||
* ->getRelatedArticles($params, Factory::getApplication())
|
||||
*/
|
||||
public static function getList(&$params)
|
||||
{
|
||||
/** @var SiteApplication $app */
|
||||
$app = Factory::getApplication();
|
||||
|
||||
return (new self())->getRelatedArticles($params, $app);
|
||||
}
|
||||
}
|
||||
27
modules/mod_related_items/tmpl/default.php
Normal file
27
modules/mod_related_items/tmpl/default.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_related_items
|
||||
*
|
||||
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
|
||||
?>
|
||||
<ul class="mod-relateditems relateditems mod-list">
|
||||
<?php foreach ($list as $item) : ?>
|
||||
<li>
|
||||
<a href="<?php echo $item->route; ?>">
|
||||
<?php if ($showDate) {
|
||||
echo HTMLHelper::_('date', $item->created, Text::_('DATE_FORMAT_LC4')) . ' - ';
|
||||
} ?>
|
||||
<?php echo $item->title; ?></a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
Reference in New Issue
Block a user