From 5cbc52307e35173ea4b3718f850aa82016f7f09b Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 26 Mar 2023 23:55:27 +0200 Subject: [PATCH] use namespace --- src/Backend.php | 192 +++++++++++++++++++++++++++-------------------- src/Frontend.php | 32 +++++++- src/Install.php | 59 +++++++-------- src/My.php | 39 ++++++++++ src/Widgets.php | 50 +++++------- 5 files changed, 225 insertions(+), 147 deletions(-) create mode 100644 src/My.php diff --git a/src/Backend.php b/src/Backend.php index 54155a5..e7123af 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,85 +10,115 @@ * @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); + +namespace Dotclear\Plugin\dcLatestVersions; + +use ArrayObject; +use dcCore; +use dcNsProcess; +use dcUpdate; +use Dotclear\Helper\Html\Form\{ + Checkbox, + Label, + Para +}; +use Dotclear\Helper\Html\Html; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN'); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + 'adminDashboardItemsV2' => function (ArrayObject $__dashboard_items): void { + if (!dcCore::app()->auth->user_prefs->get('dashboard')->get('dcLatestVersionsItems')) { + return; + } + + $builds = explode(',', (string) dcCore::app()->blog->settings->get(My::id())->get('builds')); + if (empty($builds[0])) { + return; + } + + $li = []; + + foreach ($builds as $build) { + $build = strtolower(trim($build)); + if (empty($build)) { + continue; + } + + $updater = new dcUpdate( + DC_UPDATE_URL, + 'dotclear', + $build, + DC_TPL_CACHE . '/versions' + ); + + if (false === $updater->check('0')) { + continue; + } + + $li[] = sprintf( + '
  • %3$s : %4$s
  • ', + $updater->getFileURL(), + sprintf(__('Download Dotclear %s'), $updater->getVersion()), + $build, + $updater->getVersion() + ); + } + + if (empty($li)) { + return; + } + + # Display + $__dashboard_items[0][] = '
    ' . + '

    ' . Html::escapeHTML(My::name()) . '

    ' . + '' . + '
    '; + }, + + 'adminDashboardOptionsFormV2' => function (): void { + if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('dcLatestVersionsItems')) { + dcCore::app()->auth->user_prefs->get('dashboard')->put( + 'dcLatestVersionsItems', + false, + 'boolean' + ); + } + + echo + '
    ' . + '

    ' . Html::escapeHTML(My::name()) . '

    ' . + (new Para())->items([ + (new Checkbox('dcLatestVersionsItems', (bool) dcCore::app()->auth->user_prefs->get('dashboard')->get('dcLatestVersionsItems')))->value(1), + (new Label(__("Show Dotclear's latest versions on dashboards."), Label::OUTSIDE_LABEL_AFTER))->for('dcLatestVersionsItems')->class('classic'), + ])->render() . + '
    '; + }, + + 'adminAfterDashboardOptionsUpdate' => function (?string $user_id): void { + dcCore::app()->auth->user_prefs->get('dashboard')->put( + 'dcLatestVersionsItems', + !empty($_POST['dcLatestVersionsItems']), + 'boolean' + ); + }, + ]); + + return true; + } } - -require __DIR__ . '/_widgets.php'; - -dcCore::app()->addBehavior('adminDashboardItemsV2', function ($__dashboard_items) { - if (!dcCore::app()->auth->user_prefs->dashboard->get('dcLatestVersionsItems')) { - return null; - } - - $builds = explode(',', (string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('builds')); - if (empty($builds[0])) { - return null; - } - - $li = []; - - foreach ($builds as $build) { - $build = strtolower(trim($build)); - if (empty($build)) { - continue; - } - - $updater = new dcUpdate( - DC_UPDATE_URL, - 'dotclear', - $build, - DC_TPL_CACHE . '/versions' - ); - - if (false === $updater->check('0')) { - continue; - } - - $li[] = sprintf( - '
  • %3$s : %4$s
  • ', - $updater->getFileURL(), - sprintf(__('Download Dotclear %s'), $updater->getVersion()), - $build, - $updater->getVersion() - ); - } - - if (empty($li)) { - return null; - } - - # Display - $__dashboard_items[0][] = '
    ' . - '

    ' . html::escapeHTML(__("Dotclear's latest versions")) . '

    ' . - '' . - '
    '; -}); - -dcCore::app()->addBehavior('adminDashboardOptionsFormV2', function () { - if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('dcLatestVersionsItems')) { - dcCore::app()->auth->user_prefs->dashboard->put( - 'dcLatestVersionsItems', - false, - 'boolean' - ); - } - $pref = dcCore::app()->auth->user_prefs->dashboard->get('dcLatestVersionsItems'); - - echo - '
    ' . - '

    ' . __("Dotclear's latest versions") . '

    ' . - '

    ' . - '
    '; -}); - -dcCore::app()->addBehavior('adminAfterDashboardOptionsUpdate', function ($user_id) { - dcCore::app()->auth->user_prefs->dashboard->put( - 'dcLatestVersionsItems', - !empty($_POST['dcLatestVersionsItems']), - 'boolean' - ); -}); diff --git a/src/Frontend.php b/src/Frontend.php index 8996bdf..10feba5 100644 --- a/src/Frontend.php +++ b/src/Frontend.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_RC_PATH')) { - return null; -} +declare(strict_types=1); -require dirname(__FILE__) . '/_widgets.php'; +namespace Dotclear\Plugin\dcLatestVersions; + +use dcCore; +use dcNsProcess; + +class Frontend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_RC_PATH'); + + 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/Install.php b/src/Install.php index 909c050..6580f57 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,43 +10,38 @@ * @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); -# -- Module specs -- -$mod_conf = [[ - 'builds', - "List of Dotclear's builds", - 'stable,unstable,testing,sexy', - 'string', -]]; +namespace Dotclear\Plugin\dcLatestVersions; -# -- Nothing to change below -- -try { - # Check module version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +use dcCore; +use dcNsProcess; + +class Install extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); + + return static::$init; } - # Set module settings - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - foreach ($mod_conf as $v) { - dcCore::app()->blog->settings->__get(basename(__DIR__))->put( - $v[0], - $v[2], - $v[3], - $v[1], + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->blog->settings->get(My::id())->put( + 'builds', + 'stable,unstable,testing', + 'string', + "List of Dotclear's builds", false, true ); + + return true; } - - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - - return false; } diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..6f342a3 --- /dev/null +++ b/src/My.php @@ -0,0 +1,39 @@ +plugins->moduleInfo(self::id(), 'name')); + } +} diff --git a/src/Widgets.php b/src/Widgets.php index 7d672aa..3438273 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -10,36 +10,30 @@ * @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()->blog->settings->addNamespace(basename(__DIR__)); +namespace Dotclear\Plugin\dcLatestVersions; -dcCore::app()->addBehavior( - 'initWidgets', - ['dcLatestVersionsWidget', 'adminWidget'] -); +use dcCore; +use dcUpdate; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsStack; +use Dotclear\Plugin\widgets\WidgetsElement; -/** - * @ingroup DC_PLUGIN_DCLATESTVERSIONS - * @brief Display latest versions of Dotclear - widget methods. - * @since 2.6 - */ -class dcLatestVersionsWidget +class Widgets { - public static function adminWidget($w) + public static function initWidgets(WidgetsStack $w): void { $w ->create( 'dclatestversionswidget', - __("Dotclear's latest versions"), - ['dcLatestVersionsWidget','publicWidget'], + My::name(), + [self::class, 'parseWidget'], null, __('Show the latest available versions of Dotclear') ) ->addTitle( - __("Dotclear's latest versions") + My::name() ) ->setting( 'text', @@ -53,20 +47,16 @@ class dcLatestVersionsWidget ->addOffline(); } - public static function publicWidget($w) + public static function parseWidget(WidgetsElement $w): string { - if ($w->offline) { - return null; - } - - if (!$w->checkHomeOnly(dcCore::app()->url->type) || $w->text == '') { - return null; + if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type) || $w->text == '') { + return ''; } # Builds to check - $builds = explode(',', (string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('builds')); + $builds = explode(',', (string) dcCore::app()->blog->settings->get(My::id())->get('builds')); if (empty($builds[0])) { - return null; + return ''; } $li = []; @@ -103,15 +93,15 @@ class dcLatestVersionsWidget } if (empty($li)) { - return null; + return ''; } # Display return $w->renderDiv( - $w->content_only, + (bool) $w->content_only, 'dclatestversionswidget ' . $w->class, '', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . sprintf('', implode('', $li)) + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . sprintf('', implode('', $li)) ); } }