diff --git a/src/Backend.php b/src/Backend.php index 0448cf0..375bc5b 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -7,11 +7,35 @@ * * @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 */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -require __DIR__ . '/_widgets.php'; +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; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } +} diff --git a/src/Frontend.php b/src/Frontend.php index 1b8fd4e..90cee31 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -7,17 +7,41 @@ * * @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 */ -if (!defined('DC_RC_PATH')) { - return null; +declare(strict_types=1); + +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; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + 'publicHeadContent', function (): void { + echo + dcUtils::jsModuleLoad(My::id() . '/js/accordion.js') . + dcUtils::cssModuleLoad(My::id() . '/css/frontend.css'); + }, + ]); + + return true; + } } - -require __DIR__ . '/_widgets.php'; - -dcCore::app()->addBehavior('publicHeadContent', function () { - echo - dcUtils::jsModuleLoad(basename(__DIR__) . '/js/accordion.js') . - dcUtils::cssModuleLoad(basename(__DIR__) . '/css/shortArchives.css'); -}); diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..fcb9703 --- /dev/null +++ b/src/My.php @@ -0,0 +1,50 @@ +plugins->moduleInfo(self::id(), 'name')); + } + + /** + * Check php version + */ + public static function phpCompliant(): bool + { + return version_compare(phpversion(), self::PHP_MIN, '>='); + } +} diff --git a/src/Widgets.php b/src/Widgets.php index 93cc9be..79eb719 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -7,23 +7,28 @@ * * @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 */ -if (!defined('DC_RC_PATH')) { - return; -} +declare(strict_types=1); -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( - 'shortArchives', - __('Short Archives'), - ['shortArchivesWidgets', 'shortArchivesWidgets'], + My::id(), + My::name(), + [self::class, 'parseWidget'], null, __('Blog Archive List an accordion menu, sorted by year') ) @@ -36,19 +41,15 @@ class shortArchivesWidgets ->addOffline(); } - public static function shortArchivesWidgets($w) + public static function parseWidget(WidgetsElement $w): string { - if ($w->offline) { - return; - } - - if (!$w->checkHomeOnly(dcCore::app()->url->type)) { - return; + if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) { + return ''; } $rs = dcCore::app()->blog->getDates(['type' => 'month']); if ($rs->isEmpty()) { - return; + return ''; } $active_year = null; @@ -58,10 +59,10 @@ class shortArchivesWidgets $posts = []; while ($rs->fetch()) { - $posts[dt::dt2str(__('%Y'), $rs->dt)][] = [ + $posts[dt::dt2str(__('%Y'), $rs->f('dt'))][] = [ 'url' => $rs->url(), - 'date' => html::escapeHTML(dt::dt2str(__('%B'), $rs->dt)), - 'nbpost' => $rs->nb_post, + 'date' => Html::escapeHTML(dt::dt2str(__('%B'), $rs->f('dt'))), + 'nbpost' => $rs->f('nb_post'), ]; } @@ -85,14 +86,14 @@ class shortArchivesWidgets if (dcCore::app()->url->getBase('archive') && !is_null($w->allarchivesslinktitle) && $w->allarchivesslinktitle !== '') { $res .= '

' . - html::escapeHTML($w->allarchivesslinktitle) . '

'; + Html::escapeHTML($w->allarchivesslinktitle) . '

'; } return $w->renderDiv( - $w->content_only, - 'shortArchives ' . $w->class, + (bool) $w->content_only, + My::id() . ' ' . $w->class, '', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $res + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . $res ); } }