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,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(
'<li><a href="%1$s" title="%2$s">%3$s</a> : %4$s</li>',
$updater->getFileURL(),
sprintf(__('Download Dotclear %s'), $updater->getVersion()),
$build,
$updater->getVersion()
);
}
if (empty($li)) {
return;
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="udclatestversionsitems">' .
'<h3>' . Html::escapeHTML(My::name()) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
},
'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
'<div class="fieldset">' .
'<h4>' . Html::escapeHTML(My::name()) . '</h4>' .
(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() .
'</div>';
},
'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(
'<li><a href="%1$s" title="%2$s">%3$s</a> : %4$s</li>',
$updater->getFileURL(),
sprintf(__('Download Dotclear %s'), $updater->getVersion()),
$build,
$updater->getVersion()
);
}
if (empty($li)) {
return null;
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="udclatestversionsitems">' .
'<h3>' . html::escapeHTML(__("Dotclear's latest versions")) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
});
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
'<div class="fieldset">' .
'<h4>' . __("Dotclear's latest versions") . '</h4>' .
'<p><label class="classic" for="dcLatestVersionsItems">' .
form::checkbox('dcLatestVersionsItems', 1, $pref) . ' ' .
__("Show Dotclear's latest versions on dashboards.") .
'</label></p>' .
'</div>';
});
dcCore::app()->addBehavior('adminAfterDashboardOptionsUpdate', function ($user_id) {
dcCore::app()->auth->user_prefs->dashboard->put(
'dcLatestVersionsItems',
!empty($_POST['dcLatestVersionsItems']),
'boolean'
);
});

View File

@ -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;
}
}

View File

@ -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;
}

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 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('<ul>%s</ul>', implode('', $li))
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . sprintf('<ul>%s</ul>', implode('', $li))
);
}
}