use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-04-11 00:09:04 +02:00
parent 92e9631e63
commit 31d713a804
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
4 changed files with 140 additions and 41 deletions

View File

@ -7,11 +7,35 @@
* *
* @author annso, Pierre Van Glabeke and Contributors * @author annso, Pierre Van Glabeke and Contributors
* *
* @copyright Jean-Crhistian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
namespace Dotclear\Plugin\shortArchives;
use dcCore;
use dcNsProcess;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN') && My::phpCompliant();
return static::$init;
} }
require __DIR__ . '/_widgets.php'; public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -7,17 +7,41 @@
* *
* @author annso, Pierre Van Glabeke and Contributors * @author annso, Pierre Van Glabeke and Contributors
* *
* @copyright Jean-Crhistian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
namespace Dotclear\Plugin\shortArchives;
use dcCore;
use dcNsProcess;
use dcUtils;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = My::phpCompliant();
return static::$init;
} }
require __DIR__ . '/_widgets.php'; public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehavior('publicHeadContent', function () { dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
'publicHeadContent', function (): void {
echo echo
dcUtils::jsModuleLoad(basename(__DIR__) . '/js/accordion.js') . dcUtils::jsModuleLoad(My::id() . '/js/accordion.js') .
dcUtils::cssModuleLoad(basename(__DIR__) . '/css/shortArchives.css'); dcUtils::cssModuleLoad(My::id() . '/css/frontend.css');
}); },
]);
return true;
}
}

50
src/My.php Normal file
View File

@ -0,0 +1,50 @@
<?php
/**
* @brief shortArchives, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author annso, Pierre Van Glabeke and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\shortArchives;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
/**
* Check php version
*/
public static function phpCompliant(): bool
{
return version_compare(phpversion(), self::PHP_MIN, '>=');
}
}

View File

@ -7,23 +7,28 @@
* *
* @author annso, Pierre Van Glabeke and Contributors * @author annso, Pierre Van Glabeke and Contributors
* *
* @copyright Jean-Crhistian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return;
}
dcCore::app()->addBehavior('initWidgets', ['shortArchivesWidgets','initWidgets']); namespace Dotclear\Plugin\shortArchives;
class shortArchivesWidgets use dcCore;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
use dt;
class Widgets
{ {
public static function initWidgets($w) public static function initWidgets(WidgetsStack $w): void
{ {
#Top c
$w->create( $w->create(
'shortArchives', My::id(),
__('Short Archives'), My::name(),
['shortArchivesWidgets', 'shortArchivesWidgets'], [self::class, 'parseWidget'],
null, null,
__('Blog Archive List an accordion menu, sorted by year') __('Blog Archive List an accordion menu, sorted by year')
) )
@ -36,19 +41,15 @@ class shortArchivesWidgets
->addOffline(); ->addOffline();
} }
public static function shortArchivesWidgets($w) public static function parseWidget(WidgetsElement $w): string
{ {
if ($w->offline) { if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return; return '';
}
if (!$w->checkHomeOnly(dcCore::app()->url->type)) {
return;
} }
$rs = dcCore::app()->blog->getDates(['type' => 'month']); $rs = dcCore::app()->blog->getDates(['type' => 'month']);
if ($rs->isEmpty()) { if ($rs->isEmpty()) {
return; return '';
} }
$active_year = null; $active_year = null;
@ -58,10 +59,10 @@ class shortArchivesWidgets
$posts = []; $posts = [];
while ($rs->fetch()) { while ($rs->fetch()) {
$posts[dt::dt2str(__('%Y'), $rs->dt)][] = [ $posts[dt::dt2str(__('%Y'), $rs->f('dt'))][] = [
'url' => $rs->url(), 'url' => $rs->url(),
'date' => html::escapeHTML(dt::dt2str(__('%B'), $rs->dt)), 'date' => Html::escapeHTML(dt::dt2str(__('%B'), $rs->f('dt'))),
'nbpost' => $rs->nb_post, 'nbpost' => $rs->f('nb_post'),
]; ];
} }
@ -85,14 +86,14 @@ class shortArchivesWidgets
if (dcCore::app()->url->getBase('archive') && !is_null($w->allarchivesslinktitle) && $w->allarchivesslinktitle !== '') { if (dcCore::app()->url->getBase('archive') && !is_null($w->allarchivesslinktitle) && $w->allarchivesslinktitle !== '') {
$res .= '<p><strong><a href="' . dcCore::app()->blog->url . dcCore::app()->url->getURLFor('archive') . '">' . $res .= '<p><strong><a href="' . dcCore::app()->blog->url . dcCore::app()->url->getURLFor('archive') . '">' .
html::escapeHTML($w->allarchivesslinktitle) . '</a></strong></p>'; Html::escapeHTML($w->allarchivesslinktitle) . '</a></strong></p>';
} }
return $w->renderDiv( return $w->renderDiv(
$w->content_only, (bool) $w->content_only,
'shortArchives ' . $w->class, My::id() . ' ' . $w->class,
'', '',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $res ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . $res
); );
} }
} }