From f21a5eecfe20dc294dc326b81c0c72efcfac7bfd Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Tue, 11 Apr 2023 00:47:02 +0200 Subject: [PATCH] use namespace --- src/Backend.php | 32 ++++++++++++++++--- src/Frontend.php | 80 +++++++++++++++++++++++++++++----------------- src/My.php | 58 +++++++++++++++++++++++++++++++++ src/Prepend.php | 34 +++++++++++++++----- src/UrlHandler.php | 26 +++++++++++++++ src/Widgets.php | 41 ++++++++++++------------ 6 files changed, 209 insertions(+), 62 deletions(-) create mode 100644 src/My.php create mode 100644 src/UrlHandler.php diff --git a/src/Backend.php b/src/Backend.php index daf4dd6..1bd9bf2 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,8 +10,32 @@ * @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\CategoriesPage; + +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 bacdbe3..a04b23f 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,33 +10,55 @@ * @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\CategoriesPage; + +use ArrayObject; +use dcCore; +use dcNsProcess; + +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([ + // template path + 'publicBeforeDocumentV2' => function (): void { + $tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset'); + if (!empty($tplset) && is_dir(implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', $tplset]))) { + dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', $tplset])); + } else { + dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', DC_DEFAULT_TPLSET])); + } + }, + // breacrumb addon + 'publicBreadcrumb' => function (string $context, string $separator): string { + return $context == 'categories' ? My::name() : ''; + }, + // widget + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + // tpl values + dcCore::app()->tpl->addValue('CategoryCount', function (ArrayObject $attr): string { + return 'tpl->getFilters($attr), 'dcCore::app()->ctx->categories->nb_post') . '; ?>'; + }); + dcCore::app()->tpl->addValue('CategoriesURL', function (ArrayObject $attr): string { + return 'tpl->getFilters($attr), 'dcCore::app()->blog->url.dcCore::app()->url->getBase("categories")') . '; ?>'; + }); + + return true; + } } - -require __DIR__ . '/_widgets.php'; - -// public behavior -dcCore::app()->addBehavior('publicBeforeDocumentV2', function () { - $tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->system->theme, 'tplset'); - if (!empty($tplset) && is_dir(__DIR__ . '/default-templates/' . $tplset)) { - dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates/' . $tplset); - } else { - dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates/' . DC_DEFAULT_TPLSET); - } -}); - -// breacrumb addon -dcCore::app()->addBehavior('publicBreadcrumb', function ($context, $separator) { - if ($context == 'categories') { - return __('Categories Page'); - } -}); - -// tpl values -dcCore::app()->tpl->addValue('CategoryCount', function ($attr) { - return 'tpl->getFilters($attr), 'dcCore::app()->ctx->categories->nb_post') . '; ?>'; -}); -dcCore::app()->tpl->addValue('CategoriesURL', function ($attr) { - return 'tpl->getFilters($attr), 'dcCore::app()->blog->url.dcCore::app()->url->getBase("categories")') . '; ?>'; -}); diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..1d284f2 --- /dev/null +++ b/src/My.php @@ -0,0 +1,58 @@ +plugins->moduleInfo(self::id(), 'name')); + } + + /** + * This module root + */ + public static function root(): string + { + return dirname(__DIR__); + } + + /** + * Check php version + */ + public static function phpCompliant(): bool + { + return version_compare(phpversion(), self::PHP_MIN, '>='); + } +} diff --git a/src/Prepend.php b/src/Prepend.php index 568d43d..0ac1dd0 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -10,17 +10,35 @@ * @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); -dcCore::app()->url->register('categories', 'categories', '^categories$', ['urlCategoriesPage', 'categories']); +namespace Dotclear\Plugin\CategoriesPage; -class urlCategoriesPage extends dcUrlHandlers +use dcCore; +use dcNsProcess; + +class Prepend extends dcNsProcess { - public static function categories($args) + public static function init(): bool { - self::serveDocument('categories.html'); - exit; + static::$init = My::phpCompliant(); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->url->register( + 'categories', + 'categories', + '^categories$', + [UrlHandler::class, 'categories'] + ); + + return true; } } diff --git a/src/UrlHandler.php b/src/UrlHandler.php new file mode 100644 index 0000000..8d94ae3 --- /dev/null +++ b/src/UrlHandler.php @@ -0,0 +1,26 @@ +addBehavior('initWidgets', ['widgetsCategoriesPage', 'initWidgets']); +namespace Dotclear\Plugin\CategoriesPage; -class widgetsCategoriesPage +use dcCore; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsStack; +use Dotclear\Plugin\widgets\WidgetsElement; + +class Widgets { - public static function initWidgets($w) + public static function initWidgets(WidgetsStack $w): void { $w ->create( - 'CategoriesPage', - __('Categories Page'), - ['widgetsCategoriesPage', 'categoriesPageWidgets'], + My::id(), + My::name(), + [self::class, 'parseWidget'], null, __('Link to categories') ) - ->addTitle(__('Categories Page')) + ->addTitle(My::name()) ->addHomeOnly() ->addContentOnly() ->addClass() ->addOffline(); } - public static function categoriesPageWidgets($w) + public static function parseWidget(WidgetsElement $w): string { - if ($w->offline) { - return; - } - - if (!$w->checkHomeOnly(dcCore::app()->url->type)) { - return null; + if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) { + return ''; } return $w->renderDiv( - $w->content_only, - 'categories ' . $w->class, + (bool) $w->content_only, + My::id() . ' ' . $w->class, '', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . '

' . - ($w->link_title ? html::escapeHTML($w->link_title) : __('All categories')) . + ($w->link_title ? Html::escapeHTML($w->link_title) : __('All categories')) . '

' ); }