Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert mod_articles_archive to new structure #39535

Merged
merged 38 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c72f39d
services/provider file
carlitorweb Dec 31, 2022
ba7cfd5
dispatcher file
carlitorweb Dec 31, 2022
f82a2ed
services/provider content
carlitorweb Dec 31, 2022
0e4bdfe
Dispatcher content
carlitorweb Jan 1, 2023
ad15461
Helper class adaptation
carlitorweb Jan 1, 2023
3edcf32
xml entry point
carlitorweb Jan 1, 2023
91edceb
not used
carlitorweb Jan 1, 2023
98b68bb
fixed wrong namespace
carlitorweb Jan 1, 2023
a1c1fa1
Changed model for use countItemsByMonth() method
carlitorweb Jan 1, 2023
45845c6
Display amount of archived articles in each month
carlitorweb Jan 1, 2023
105f817
phpcs fixes
carlitorweb Jan 1, 2023
067128b
fixed wrong created model
carlitorweb Jan 1, 2023
698ad27
removed unused property
carlitorweb Jan 1, 2023
7947117
describe better the purpose of the method
carlitorweb Jan 1, 2023
9f47bd1
Merge branch '4.3-dev' into convert_mod_articles_archive
carlitorweb Jan 9, 2023
ddaa05c
more verbose variables and methods names
carlitorweb Jan 10, 2023
4c4daff
cs fixes
carlitorweb Jan 10, 2023
7b2cb5d
Reviewers fixes
carlitorweb Jan 10, 2023
5f22075
Merge branch '4.3-dev' into convert_mod_articles_archive
carlitorweb Jan 22, 2023
2990530
Merge branch '4.3-dev' into convert_mod_articles_archive
carlitorweb Mar 11, 2023
9ce6e2a
Merge branch '4.4-dev' into convert_mod_articles_archive
carlitorweb Mar 11, 2023
b198849
Merge branch '4.4-dev' into convert_mod_articles_archive
laoneo Mar 28, 2023
de2f425
update
carlitorweb Mar 28, 2023
9a2da10
Merge remote-tracking branch 'origin/convert_mod_articles_archive' in…
carlitorweb Mar 28, 2023
225860d
Merge branch '4.4-dev' into convert_mod_articles_archive
carlitorweb Mar 31, 2023
c5044e6
update
carlitorweb Mar 31, 2023
93023c9
cy test
carlitorweb Mar 31, 2023
2919703
revert Helper class
carlitorweb Mar 31, 2023
5e8f2da
changed deprecated getLanguage
carlitorweb Mar 31, 2023
bebe264
cy test
carlitorweb Mar 31, 2023
32801fa
Revert output
laoneo Mar 31, 2023
5a26250
Update modules/mod_articles_archive/src/Helper/ArticlesArchiveHelper.php
laoneo Mar 31, 2023
ef98fc0
Update modules/mod_articles_archive/services/provider.php
laoneo Mar 31, 2023
ce78d68
Update modules/mod_articles_archive/services/provider.php
laoneo Mar 31, 2023
84e08af
Update modules/mod_articles_archive/src/Dispatcher/Dispatcher.php
laoneo Mar 31, 2023
01a9506
removing $data variable from getHelper call
carlitorweb Apr 1, 2023
d04c5be
test if an archived article is displayed
carlitorweb Apr 1, 2023
33b15f4
Merge branch '4.4-dev' into convert_mod_articles_archive
laoneo Apr 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions modules/mod_articles_archive/mod_articles_archive.php

This file was deleted.

2 changes: 1 addition & 1 deletion modules/mod_articles_archive/mod_articles_archive.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<description>MOD_ARTICLES_ARCHIVE_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Module\ArticlesArchive</namespace>
<files>
<filename module="mod_articles_archive">mod_articles_archive.php</filename>
<folder module="mod_articles_archive">services</folder>
<folder>src</folder>
<folder>tmpl</folder>
</files>
Expand Down
42 changes: 42 additions & 0 deletions modules/mod_articles_archive/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @package Joomla.Site
* @subpackage mod_articles_archive
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
laoneo marked this conversation as resolved.
Show resolved Hide resolved
* @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 archive module service provider.
*
* @since __DEPLOY_VERSION__
*/
return new class implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
laoneo marked this conversation as resolved.
Show resolved Hide resolved
{
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\ArticlesArchive'));
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\ArticlesArchive\\Site\\Helper'));

$container->registerServiceProvider(new Module());
}
};
45 changes: 45 additions & 0 deletions modules/mod_articles_archive/src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @package Joomla.Site
* @subpackage mod_articles_archive
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
laoneo marked this conversation as resolved.
Show resolved Hide resolved
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Module\ArticlesArchive\Site\Dispatcher;

use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
use Joomla\CMS\Helper\HelperFactoryAwareTrait;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
laoneo marked this conversation as resolved.
Show resolved Hide resolved
// phpcs:enable PSR1.Files.SideEffects

/**
* Dispatcher class for mod_articles_archive
*
* @since __DEPLOY_VERSION__
*/
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
use HelperFactoryAwareTrait;

/**
* Returns the layout data.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();

$data['list'] = $this->getHelperFactory()->getHelper('ArticlesArchiveHelper', $data)->getArticles($data['params'], $data['app']);

return $data;
}
}
166 changes: 118 additions & 48 deletions modules/mod_articles_archive/src/Helper/ArticlesArchiveHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

namespace Joomla\Module\ArticlesArchive\Site\Helper;

use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\CMS\Cache\Controller\OutputController;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
use Joomla\Database\ParameterType;
use Joomla\Component\Content\Site\Model\ArticlesModel;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -24,73 +30,137 @@
/**
* Helper for mod_articles_archive
*
* @since 1.5
* @since __DEPLOY_VERSION__
carlitorweb marked this conversation as resolved.
Show resolved Hide resolved
*/
class ArticlesArchiveHelper
class ArticlesArchiveHelper implements DatabaseAwareInterface
{
use DatabaseAwareTrait;

/**
* Retrieve list of archived articles
* The module instance
*
* @param \Joomla\Registry\Registry &$params module parameters
* @var \stdClass
* @since __DEPLOY_VERSION__
*/
protected $module;

/**
* Constructor.
*
* @return array
* @param array $config An optional associative array of configuration settings.
*
* @since 1.5
* @since __DEPLOY_VERSION__
*/
public static function getList(&$params)
public function __construct($config = [])
{
$app = Factory::getApplication();
$db = Factory::getDbo();
$query = $db->getQuery(true);

$query->select($query->month($db->quoteName('created')) . ' AS created_month')
->select('MIN(' . $db->quoteName('created') . ') AS created')
->select($query->year($db->quoteName('created')) . ' AS created_year')
->from($db->quoteName('#__content', 'c'))
->where($db->quoteName('c.state') . ' = ' . ContentComponent::CONDITION_ARCHIVED)
->group($query->year($db->quoteName('c.created')) . ', ' . $query->month($db->quoteName('c.created')))
->order($query->year($db->quoteName('c.created')) . ' DESC, ' . $query->month($db->quoteName('c.created')) . ' DESC');

// Filter by language
if ($app->getLanguageFilter()) {
$query->whereIn($db->quoteName('language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
}
$this->module = $config['module'];
}

$query->setLimit((int) $params->get('count'));
$db->setQuery($query);
/**
* Retrieve a list of months with archived articles
*
* @param Registry $params The module parameters.
* @param SiteApplication $app The current application.
*
* @return object[]
*
* @since __DEPLOY_VERSION__
*/
public function getArticles(Registry $moduleParams, SiteApplication $app)
{
$cacheKey = md5(serialize(array ($moduleParams->toString(), $this->module->module, $this->module->id)));

try {
$rows = (array) $db->loadObjectList();
} catch (\RuntimeException $e) {
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
/** @var OutputController $cache */
$cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)
->createCacheController('output', ['defaultgroup' => 'mod_articles_archive']);

return [];
}
if (!$cache->contains($cacheKey)) {
$mvcContentFactory = $app->bootComponent('com_content')->getMVCFactory();

/** @var ArticlesModel $articlesModel */
$articlesModel = $mvcContentFactory->createModel('Articles', 'Site', ['ignore_request' => true]);

// Set application parameters in model
$appParams = $app->getParams();
$articlesModel->setState('params', $appParams);

$menu = $app->getMenu();
$item = $menu->getItems('link', 'index.php?option=com_content&view=archive', true);
$itemid = (isset($item) && !empty($item->id)) ? '&Itemid=' . $item->id : '';
// Filter on archived articles
$articlesModel->setState('filter.published', ContentComponent::CONDITION_ARCHIVED);

$i = 0;
$lists = array();
$articlesModel->setState('list.start', 0);

foreach ($rows as $row) {
$date = Factory::getDate($row->created);
// Set the filters based on the module params
$articlesModel->setState('list.limit', (int) $moduleParams->get('count', 1));

$createdMonth = $date->format('n');
$createdYear = $date->format('Y');
// This module does not use tags data
$articlesModel->setState('load_tags', false);

$createdYearCal = HTMLHelper::_('date', $row->created, 'Y');
$monthNameCal = HTMLHelper::_('date', $row->created, 'F');
// Filter by language
$articlesModel->setState('filter.language', $app->getLanguageFilter());

$lists[$i] = new \stdClass();
// Prepare the module output
$items = [];
$menu = $app->getMenu();

$lists[$i]->link = Route::_('index.php?option=com_content&view=archive&year=' . $createdYear . '&month=' . $createdMonth . $itemid);
$lists[$i]->text = Text::sprintf('MOD_ARTICLES_ARCHIVE_DATE', $monthNameCal, $createdYearCal);
$menuItem = $menu->getItems('link', 'index.php?option=com_content&view=archive', true);
$urlParamItemid = (isset($menuItem) && !empty($menuItem->id)) ? '&Itemid=' . $menuItem->id : '';

$i++;
foreach ($articlesModel->countItemsByMonth() as $month) {
$items[] = static::prepareItem($month, $urlParamItemid);
}

// Cache the output and return
$cache->store($items, $cacheKey);

return $items;
}

return $lists;
// Return the cached output
return $cache->get($cacheKey);
}

/**
* Prepare the month before render.
*
* @param object $month The month to prepare
* @param \stdClass $urlParamItemid The Itemid param of the URL
*
* @return \stdClass
*
* @since __DEPLOY_VERSION__
*/
protected static function prepareItem($month, $urlParamItemid): \stdClass
{
$date = Factory::getDate($month->d);

$createdMonth = $date->format('n');
$createdYear = $date->format('Y');

$createdYearCal = HTMLHelper::_('date', $month->d, 'Y');
$monthNameCal = HTMLHelper::_('date', $month->d, 'F');

$archivedArticlesMonth = new \stdClass();

$archivedArticlesMonth->link = Route::_('index.php?option=com_content&view=archive&year=' . $createdYear . '&month=' . $createdMonth . $urlParamItemid);
$archivedArticlesMonth->text = Text::sprintf('MOD_ARTICLES_ARCHIVE_DATE', $monthNameCal, $createdYearCal);
$archivedArticlesMonth->amount = $month->c;

return $archivedArticlesMonth;
}

/**
* Retrieve list of archived articles
*
* @param \Joomla\Registry\Registry &$params module parameters
*
* @return mixed
*
* @since __DEPLOY_VERSION__
*
* @deprecated 5.0 Use the none static function getArticles
*/
public static function getList(&$params)
{
return (new self())->getArticles($params, Factory::getApplication());
}
}
2 changes: 1 addition & 1 deletion modules/mod_articles_archive/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<?php foreach ($list as $item) : ?>
<li>
<a href="<?php echo $item->link; ?>">
<?php echo $item->text; ?>
<?php echo $item->text; ?> (<?php echo $item->amount; ?>)
carlitorweb marked this conversation as resolved.
Show resolved Hide resolved
</a>
</li>
<?php endforeach; ?>
Expand Down