use namespace

master
Jean-Christian Paul Denis 2023-03-26 23:55:27 +02:00
parent a25d7eabec
commit 5cbc52307e
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 225 additions and 147 deletions

View File

@ -10,20 +10,46 @@
* @copyright Jean-Christian 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\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;
} }
require __DIR__ . '/_widgets.php'; public static function process(): bool
{
dcCore::app()->addBehavior('adminDashboardItemsV2', function ($__dashboard_items) { if (!static::$init) {
if (!dcCore::app()->auth->user_prefs->dashboard->get('dcLatestVersionsItems')) { return false;
return null;
} }
$builds = explode(',', (string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('builds')); 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])) { if (empty($builds[0])) {
return null; return;
} }
$li = []; $li = [];
@ -55,40 +81,44 @@ dcCore::app()->addBehavior('adminDashboardItemsV2', function ($__dashboard_items
} }
if (empty($li)) { if (empty($li)) {
return null; return;
} }
# Display # Display
$__dashboard_items[0][] = '<div class="box small" id="udclatestversionsitems">' . $__dashboard_items[0][] = '<div class="box small" id="udclatestversionsitems">' .
'<h3>' . html::escapeHTML(__("Dotclear's latest versions")) . '</h3>' . '<h3>' . Html::escapeHTML(My::name()) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' . '<ul>' . implode('', $li) . '</ul>' .
'</div>'; '</div>';
}); },
dcCore::app()->addBehavior('adminDashboardOptionsFormV2', function () { 'adminDashboardOptionsFormV2' => function (): void {
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('dcLatestVersionsItems')) { if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('dcLatestVersionsItems')) {
dcCore::app()->auth->user_prefs->dashboard->put( dcCore::app()->auth->user_prefs->get('dashboard')->put(
'dcLatestVersionsItems', 'dcLatestVersionsItems',
false, false,
'boolean' 'boolean'
); );
} }
$pref = dcCore::app()->auth->user_prefs->dashboard->get('dcLatestVersionsItems');
echo echo
'<div class="fieldset">' . '<div class="fieldset">' .
'<h4>' . __("Dotclear's latest versions") . '</h4>' . '<h4>' . Html::escapeHTML(My::name()) . '</h4>' .
'<p><label class="classic" for="dcLatestVersionsItems">' . (new Para())->items([
form::checkbox('dcLatestVersionsItems', 1, $pref) . ' ' . (new Checkbox('dcLatestVersionsItems', (bool) dcCore::app()->auth->user_prefs->get('dashboard')->get('dcLatestVersionsItems')))->value(1),
__("Show Dotclear's latest versions on dashboards.") . (new Label(__("Show Dotclear's latest versions on dashboards."), Label::OUTSIDE_LABEL_AFTER))->for('dcLatestVersionsItems')->class('classic'),
'</label></p>' . ])->render() .
'</div>'; '</div>';
}); },
dcCore::app()->addBehavior('adminAfterDashboardOptionsUpdate', function ($user_id) { 'adminAfterDashboardOptionsUpdate' => function (?string $user_id): void {
dcCore::app()->auth->user_prefs->dashboard->put( dcCore::app()->auth->user_prefs->get('dashboard')->put(
'dcLatestVersionsItems', 'dcLatestVersionsItems',
!empty($_POST['dcLatestVersionsItems']), !empty($_POST['dcLatestVersionsItems']),
'boolean' 'boolean'
); );
}); },
]);
return true;
}
}

View File

@ -10,8 +10,32 @@
* @copyright Jean-Christian 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\dcLatestVersions;
use dcCore;
use dcNsProcess;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_RC_PATH');
return static::$init;
} }
require dirname(__FILE__) . '/_widgets.php'; public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -10,43 +10,38 @@
* @copyright Jean-Christian 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\dcLatestVersions;
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;
} }
# -- Module specs -- public static function process(): bool
$mod_conf = [[ {
if (!static::$init) {
return false;
}
dcCore::app()->blog->settings->get(My::id())->put(
'builds', 'builds',
"List of Dotclear's builds", 'stable,unstable,testing',
'stable,unstable,testing,sexy',
'string', 'string',
]]; "List of Dotclear's builds",
# -- Nothing to change below --
try {
# Check module version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
}
# 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],
false, false,
true true
); );
}
return true; return true;
} catch (Exception $e) { }
dcCore::app()->error->add($e->getMessage());
return false;
} }

39
src/My.php 100644
View File

@ -0,0 +1,39 @@
<?php
/**
* @brief dcLatestVersions, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis, Pierre Van Glabeke
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\dcLatestVersions;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/**
* 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'));
}
}

View File

@ -10,36 +10,30 @@
* @copyright Jean-Christian 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;
}
dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); namespace Dotclear\Plugin\dcLatestVersions;
dcCore::app()->addBehavior( use dcCore;
'initWidgets', use dcUpdate;
['dcLatestVersionsWidget', 'adminWidget'] use Dotclear\Helper\Html\Html;
); use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
/** class Widgets
* @ingroup DC_PLUGIN_DCLATESTVERSIONS
* @brief Display latest versions of Dotclear - widget methods.
* @since 2.6
*/
class dcLatestVersionsWidget
{ {
public static function adminWidget($w) public static function initWidgets(WidgetsStack $w): void
{ {
$w $w
->create( ->create(
'dclatestversionswidget', 'dclatestversionswidget',
__("Dotclear's latest versions"), My::name(),
['dcLatestVersionsWidget','publicWidget'], [self::class, 'parseWidget'],
null, null,
__('Show the latest available versions of Dotclear') __('Show the latest available versions of Dotclear')
) )
->addTitle( ->addTitle(
__("Dotclear's latest versions") My::name()
) )
->setting( ->setting(
'text', 'text',
@ -53,20 +47,16 @@ class dcLatestVersionsWidget
->addOffline(); ->addOffline();
} }
public static function publicWidget($w) public static function parseWidget(WidgetsElement $w): string
{ {
if ($w->offline) { if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type) || $w->text == '') {
return null; return '';
}
if (!$w->checkHomeOnly(dcCore::app()->url->type) || $w->text == '') {
return null;
} }
# Builds to check # 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])) { if (empty($builds[0])) {
return null; return '';
} }
$li = []; $li = [];
@ -103,15 +93,15 @@ class dcLatestVersionsWidget
} }
if (empty($li)) { if (empty($li)) {
return null; return '';
} }
# Display # Display
return $w->renderDiv( return $w->renderDiv(
$w->content_only, (bool) $w->content_only,
'dclatestversionswidget ' . $w->class, 'dclatestversionswidget ' . $w->class,
'', '',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . sprintf('<ul>%s</ul>', implode('', $li)) ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . sprintf('<ul>%s</ul>', implode('', $li))
); );
} }
} }