use namespace

master
Jean-Christian Paul Denis 2023-04-10 23:26:28 +02:00
parent dd618e229e
commit 114d443f88
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
6 changed files with 465 additions and 353 deletions

View File

@ -1,48 +0,0 @@
<?php
/**
* @brief arlequin, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors
*
* @copyright Jean-Crhistian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return;
}
$mt_models = [];
/* Syntaxe pour ajouter vos propres modèles prédéfinis :
$mt_models[] = array(
'name'=>__('Model name'), // Nom du modèle prédéfini, éventuellement
// traduit dans un fichier de langue
's_html'=>'[HTML code]', // Code HTML du sélecteur de thème
'e_html'=>'[HTML code]', // Code HTML d'un item pouvant être sélectionné
'a_html'=>'[HTML code]' // Code HTML d'un item actif (thème sélectionné)
);
//*/
$mt_models[] = [
'name' => __('Bullets list'),
's_html' => '<ul>%2$s</ul>',
'e_html' => '<li><a href="%1$s%2$s%3$s">%4$s</a></li>',
'a_html' => '<li><strong>%4$s</strong></li>',
];
$mt_models[] = [
'name' => __('Scrolled list'),
's_html' => '<form action="%1$s" method="post">' . "\n" .
'<p><select name="theme">' . "\n" .
'%2$s' . "\n" .
'</select>' . "\n" .
'<input type="submit" value="' . __('ok') . '"/></p>' . "\n" .
'</form>',
'e_html' => '<option value="%3$s">%4$s</option>',
'a_html' => '<option value="%3$s" selected="selected" disabled="disabled">%4$s (' . __('active theme') . ')</option>',
];

View File

@ -7,38 +7,66 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors * @author Oleksandr Syenchuk, 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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return;
namespace Dotclear\Plugin\arlequin;
use dcAdmin;
use dcCore;
use dcFavorites;
use dcNsProcess;
use dcPage;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id);
return static::$init;
} }
require __DIR__ . '/_widgets.php'; public static function process(): bool
{
if (!static::$init) {
return false;
}
// Admin sidebar menu
dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem( dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem(
__('Arlequin'), My::name(),
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
dcPage::getPF(basename(__DIR__) . '/icon.png'), dcPage::getPF(My::id() . '/icon.png'),
preg_match( preg_match(
'/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', '/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/',
$_SERVER['REQUEST_URI'] $_SERVER['REQUEST_URI']
), ),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_CONTENT_ADMIN, dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id) ]), dcCore::app()->blog->id)
); );
// Admin dashbaord favorite dcCore::app()->addBehaviors([
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function ($favs) { 'adminDashboardFavoritesV2' => function (dcFavorites $favs): void {
$favs->register(basename(__DIR__), [ $favs->register(My::id(), [
'title' => __('Arlequin'), 'title' => My::name(),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), 'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(basename(__DIR__) . '/icon.png'), 'small-icon' => dcPage::getPF(My::id() . '/icon.png'),
'large-icon' => dcPage::getPF(basename(__DIR__) . '/icon-big.png'), 'large-icon' => dcPage::getPF(My::id() . '/icon-big.png'),
'permissions' => dcCore::app()->auth->makePermissions([ 'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_CONTENT_ADMIN, dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), ]),
]); ]);
}); },
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -7,168 +7,81 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors * @author Oleksandr Syenchuk, 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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return;
namespace Dotclear\Plugin\arlequin;
use dcCore;
use dcNsProcess;
use Dotclear\Helper\Network\Http;
class Frontend extends dcNsProcess
{
public const COOKIE_THEME_PREFIX = 'dc_theme_';
public const COOKIE_UPDDT_PREFIX = 'dc_user_upddt_';
public static function init(): bool
{
static::$init = true;
return static::$init;
} }
require __DIR__ . '/_widgets.php'; public static function process(): bool
publicArlequinEngine::init();
dcCore::app()->addBehavior('publicBeforeDocumentV2', ['publicArlequinEngine','adjustCache']);
dcCore::app()->tpl->addValue('themesList', ['publicArlequinInterface','template']);
class publicArlequinEngine
{ {
public static $cookie_theme; if (!static::$init) {
public static $cookie_upddt; return false;
}
public static function init()
{
$cname = base_convert(dcCore::app()->blog->uid, 16, 36);
self::$cookie_theme = 'dc_theme_' . $cname;
self::$cookie_upddt = 'dc_user_upddt_' . $cname;
if (!empty($_REQUEST['theme'])) { if (!empty($_REQUEST['theme'])) {
# Set cookie for 365 days # Set cookie for 365 days
setcookie(self::$cookie_theme, $_REQUEST['theme'], time() + 31536000, '/'); setcookie(self::COOKIE_THEME_PREFIX . self::cookieSuffix(), $_REQUEST['theme'], time() + 31536000, '/');
setcookie(self::$cookie_upddt, (string) time(), time() + 31536000, '/'); setcookie(self::COOKIE_UPDDT_PREFIX . self::cookieSuffix(), (string) time(), time() + 31536000, '/');
# Redirect if needed # Redirect if needed
if (isset($_GET['theme'])) { if (isset($_GET['theme'])) {
$p = '/(\?|&)theme(=.*)?$/'; $p = '/(\?|&)theme(=.*)?$/';
http::redirect(preg_replace($p, '', http::getSelfURI())); Http::redirect(preg_replace($p, '', Http::getSelfURI()));
} }
# Switch theme # Switch theme
self::switchTheme($_REQUEST['theme']); self::switchTheme($_REQUEST['theme']);
} elseif (!empty($_COOKIE[self::$cookie_theme])) { } elseif (!empty($_COOKIE[self::COOKIE_THEME_PREFIX . self::cookieSuffix()])) {
self::switchTheme($_COOKIE[self::$cookie_theme]); self::switchTheme($_COOKIE[self::COOKIE_THEME_PREFIX . self::cookieSuffix()]);
}
dcCore::app()->addBehaviors([
'publicBeforeDocumentV2' => [self::class, 'adjustCache'],
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
protected static function cookieSuffix(): string
{
return base_convert(dcCore::app()->blog->uid, 16, 36);
}
public static function adjustCache(): void
{
if (!empty($_COOKIE[self::COOKIE_UPDDT_PREFIX . self::cookieSuffix()])) {
dcCore::app()->cache['mod_ts'][] = (int) $_COOKIE[self::COOKIE_UPDDT_PREFIX . self::cookieSuffix()];
} }
} }
public static function adjustCache() public static function switchTheme(string $theme): void
{ {
if (!empty($_COOKIE[self::$cookie_upddt])) { if (dcCore::app()->blog->settings->get(My::id())->get('mt_exclude')) {
dcCore::app()->cache['mod_ts'][] = (int) $_COOKIE[self::$cookie_upddt]; if (in_array($theme, explode('/', dcCore::app()->blog->settings->get(My::id())->get('mt_exclude')))) {
}
}
public static function switchTheme($theme)
{
if (dcCore::app()->blog->settings->arlequinMulti->mt_exclude) {
if (in_array($theme, explode('/', dcCore::app()->blog->settings->arlequinMulti->mt_exclude))) {
return; return;
} }
} }
dcCore::app()->public->theme = dcCore::app()->blog->settings->system->theme = $theme; dcCore::app()->blog->settings->get('system')->set('theme', $theme);
} dcCore::app()->public->theme = $theme;
}
class publicArlequinInterface
{
public static function arlequinWidget($w)
{
return self::getHTML($w);
}
public static function template($attr)
{
return '<?php echo publicArlequinInterface::getHTML(); ?>';
}
public static function getHTML($w = false)
{
if ($w->offline) {
return;
}
if (!$w->checkHomeOnly(dcCore::app()->url->type)) {
return;
}
$cfg = @unserialize(dcCore::app()->blog->settings->arlequinMulti->get('mt_cfg'));
if ($cfg === false || ($names = self::getNames()) === false) {
return;
}
# Current page URL and the associated query string. Note : the URL for
# the switcher ($s_url) is different to the URL for an item ($e_url)
$s_url = $e_url = http::getSelfURI();
# If theme setting is already present in URL, we will replace its value
$replace = preg_match('/(\\?|&)theme\\=[^&]*/', $e_url);
# URI extension to send theme setting by query string
if ($replace) {
$ext = '';
} elseif (strpos($e_url, '?') === false) {
$ext = '?theme=';
} else {
$ext = (substr($e_url, -1) == '?' ? '' : '&amp;') . 'theme=';
}
$res = '';
foreach ($names as $k => $v) {
if ($k == dcCore::app()->public->theme) {
$format = $cfg['a_html'];
} else {
$format = $cfg['e_html'];
}
if ($replace) {
$e_url = preg_replace(
'/(\\?|&)(theme\\=)([^&]*)/',
'$1${2}' . addcslashes($k, '$\\'),
$e_url
);
$val = '';
} else {
$val = html::escapeHTML(rawurlencode($k));
}
$res .= sprintf(
$format,
$e_url,
$ext,
$val,
html::escapeHTML($v['name']),
html::escapeHTML($v['desc']),
html::escapeHTML($k)
);
}
# Nothing to display
if (!trim($res)) {
return;
}
$res = sprintf($cfg['s_html'], $s_url, $res);
if ($w) {
return $w->renderDiv(
$w->content_only,
'arlequin ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $res
);
}
return $res;
}
public static function getNames()
{
$mt_exclude = dcCore::app()->blog->settings->arlequinMulti->mt_exclude;
$exclude = [];
if (!empty($mt_exclude)) {
$exclude = array_flip(explode('/', dcCore::app()->blog->settings->arlequinMulti->mt_exclude));
}
$names = array_diff_key(dcCore::app()->themes->getModules(), $exclude);
return empty($names) ? false : $names;
} }
} }

View File

@ -7,109 +7,183 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors * @author Oleksandr Syenchuk, 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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return;
namespace Dotclear\Plugin\arlequin;
use ArrayObject;
use dcCore;
use dcNsProcess;
use dcPage;
use Dotclear\Helper\Html\Form\{
Div,
Form,
Input,
Label,
Note,
Para,
Submit,
Text,
Textarea
};
use Dotclear\Helper\Html\Html;
use Exception;
class Manage extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id);
return static::$init;
} }
$mt_models = []; public static function process(): bool
{
if (!static::$init) {
return false;
}
try { try {
include __DIR__ . '/inc/models.php'; $s = dcCore::app()->blog->settings->get(My::id());
// Initialisation $model = json_decode((string) $s->get('model'), true);
dcCore::app()->blog->settings->addNamespace('arlequinMulti'); $exclude = $s->get('exclude');
[$mt_cfg, $mt_exclude] = adminArlequin::loadSettings(dcCore::app()->blog->settings);
if (adminArlequin::$initialized) {
dcAdminNotices::AddSuccessNotice(__('Settings have been reinitialized.'));
}
// Enregistrement des données depuis les formulaires // initialize settings
if (isset($_POST['mt_action_config'])) { $initialized = false;
$mt_cfg['e_html'] = $_POST['e_html']; if ($model === false || $exclude === null || !(isset($model['e_html']) && isset($model['a_html']) && isset($model['s_html']))) {
$mt_cfg['a_html'] = $_POST['a_html']; $model = My::defaultModel();
$mt_cfg['s_html'] = $_POST['s_html']; $s->put('model', json_encode($model), 'string', 'Arlequin configuration');
$mt_exclude = $_POST['mt_exclude']; $s->put('exclude', 'customCSS', 'string', 'Excluded themes');
}
// Traitement des requêtes dcPage::AddSuccessNotice(__('Settings have been reinitialized.'));
if (isset($_POST['mt_action_config'])) {
dcCore::app()->blog->settings->arlequinMulti->put('mt_cfg', serialize($mt_cfg));
dcCore::app()->blog->settings->arlequinMulti->put('mt_exclude', $mt_exclude);
dcAdminNotices::AddSuccessNotice(__('System settings have been updated.'));
dcCore::app()->blog->triggerBlog(); dcCore::app()->blog->triggerBlog();
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), ['config' => 1]);
} }
// collect settings
if (isset($_POST['mt_action_config'])) {
$model['e_html'] = $_POST['e_html'];
$model['a_html'] = $_POST['a_html'];
$model['s_html'] = $_POST['s_html'];
$exclude = $_POST['exclude'];
}
// save settings
if (isset($_POST['mt_action_config'])) {
$s->put('model', json_encode($model));
$s->put('exclude', $exclude);
dcPage::AddSuccessNotice(__('System settings have been updated.'));
dcCore::app()->blog->triggerBlog();
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id(), ['config' => 1]);
}
// restore settings
if (isset($_POST['mt_action_restore'])) { if (isset($_POST['mt_action_restore'])) {
dcCore::app()->blog->settings->arlequinMulti->drop('mt_cfg'); $s->drop('model');
dcCore::app()->blog->settings->arlequinMulti->drop('mt_exclude'); $s->drop('exclude');
dcAdminNotices::AddSuccessNotice(__('Settings have been reinitialized.'));
dcPage::AddSuccessNotice(__('Settings have been reinitialized.'));
dcCore::app()->blog->triggerBlog(); dcCore::app()->blog->triggerBlog();
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), ['restore' => 1]); dcCore::app()->adminurl->redirect('admin.plugin.' . My::id(), ['restore' => 1]);
} }
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());
} }
// Headers return true;
$jsModels = ''; }
$cslashes = "\n\"\'";
foreach ($mt_models as $m) { // @phpstan-ignore-line public static function render(): void
$jsModels .= "\t" . {
if (!static::$init) {
return;
}
$models = new ArrayObject(My::distributedModels());
dcCore::app()->callBehavior('arlequinAddModels', $models);
$models = iterator_to_array($models);
$s = dcCore::app()->blog->settings->get(My::id());
$model = json_decode((string) $s->get('model'), true);
$header = '';
foreach ($models as $m) {
$header .= "\t" .
'arlequin.addModel(' . 'arlequin.addModel(' .
'"' . html::escapeJS($m['name']) . '",' . '"' . Html::escapeJS($m['name']) . '",' .
'"' . addcslashes($m['s_html'], $cslashes) . '",' . '"' . addcslashes($m['s_html'], "\n\"\'") . '",' .
'"' . addcslashes($m['e_html'], $cslashes) . '",' . '"' . addcslashes($m['e_html'], "\n\"\'") . '",' .
'"' . addcslashes($m['a_html'], $cslashes) . '"' . '"' . addcslashes($m['a_html'], "\n\"\'") . '"' .
");\n"; ");\n";
} }
// DISPLAY dcPage::openModule(
echo ' My::name(),
<html><head><title>' . __('Arlequin') . '</title>' . dcPage::jsModuleLoad(My::id() . '/js/models.js') . '
dcPage::jsLoad(dcPage::getPF(basename(__DIR__) . '/js/models.js')) . '
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ //<![CDATA[
arlequin.msg.predefined_models = "' . html::escapeJS(__('Predefined models')) . '"; arlequin.msg.predefined_models = "' . Html::escapeJS(__('Predefined models')) . '";
arlequin.msg.select_model = "' . html::escapeJS(__('Select a model')) . '"; arlequin.msg.select_model = "' . Html::escapeJS(__('Select a model')) . '";
arlequin.msg.user_defined = "' . html::escapeJS(__('User defined')) . '"; arlequin.msg.user_defined = "' . Html::escapeJS(__('User defined')) . '";
$(function() { $(function() {
arlequin.addDefault(); arlequin.addDefault();
' . $jsModels . ' ' . $header . '
}); });
//]]> //]]>
</script> </script>'
</head><body>' . );
echo
dcPage::breadcrumb([ dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '', Html::escapeHTML(dcCore::app()->blog->name) => '',
'<span class="page-title">' . __('Arlequin') . '</span>' => '', My::name() => '',
]) . ]) .
dcPage::notices() . ' dcPage::notices() .
<form action="' . dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '" method="post"> (new Form(My::id() . 'form'))->method('post')->action(dcCore::app()->adminurl->get('admin.plugin.' . My::id()))->fields([
<h4>' . __('Switcher display format') . '</h4> (new Text('h4', __('Switcher display format'))),
<div id="models"></div> (new Div())->id('models'),
(new Div())->class('two-boxes odd')->items([
(new Para())->items([
(new Label(__('Switcher HTML code:'), Label::OUTSIDE_LABEL_BEFORE))->for('s_html'),
(new Textarea('s_html', Html::escapeHTML($model['s_html'])))->cols(50)->rows(10),
]),
]),
(new Div())->class('two-boxes even')->items([
(new Para())->items([
(new Label(__('Item HTML code:'), Label::OUTSIDE_LABEL_BEFORE))->for('e_html'),
(new Input('e_html'))->size(50)->maxlenght(200)->value(Html::escapeHTML($model['e_html'])),
]),
(new Para())->items([
(new Label(__('Active item HTML code:'), Label::OUTSIDE_LABEL_BEFORE))->for('a_html'),
(new Input('a_html'))->size(50)->maxlenght(200)->value(Html::escapeHTML($model['a_html'])),
]),
]),
(new Div())->class('two-boxes odd')->items([
(new Para())->items([
(new Label(__('Excluded themes:'), Label::OUTSIDE_LABEL_BEFORE))->for('exclude'),
(new Input('exclude'))->size(50)->maxlenght(200)->value(Html::escapeHTML($s->exclude)),
]),
(new Note())->class('form-note')->text('Semicolon separated list of themes IDs (theme folder name). Ex: ductile;berlin'),
]),
(new Para())->separator(' ')->items([
dcCore::app()->formNonce(false),
(new Submit(['mt_action_config']))->value(__('Save')),
(new Submit(['mt_action_restore']))->value(__('Restore defaults')),
]),
])->render();
<div class="two-boxes odd"> dcPage::helpBlock('arlequin');
<p><label for="s_html">' . __('Switcher HTML code:') . '</label> ' . dcPage::closeModule();
form::textArea('s_html', 50, 10, html::escapeHTML($mt_cfg['s_html'])) . '</p> }
</div><div class="two-boxes even"> }
<p><label for="e_html">' . __('Item HTML code:') . '</label> ' .
form::field('e_html', 50, 200, html::escapeHTML($mt_cfg['e_html'])) . '</p>
<p><label for="a_html">' . __('Active item HTML code:') . '</label> ' .
form::field('a_html', 50, 200, html::escapeHTML($mt_cfg['a_html'])) . '</p>
</div><div class="two-boxes odd">
<p><label for="mt_exclude">' . __("Excluded themes (separated by slashs '/'):") . '</label> ' .
form::field('mt_exclude', 50, 200, html::escapeHTML($mt_exclude)) . '</p>
<p class="info">' . __('The names to be taken into account are those of the theme files.') . '</p>
</div>
<p>
<input type="submit" name="mt_action_config" value="' . __('Save') . '" />
<input type="submit" name="mt_action_restore" value="' . __('Restore defaults') . '" />' .
dcCore::app()->formNonce() . '</p>
</form>';
dcPage::helpBlock('arlequin'); ?>
</body></html>

93
src/My.php 100644
View File

@ -0,0 +1,93 @@
<?php
/**
* @brief arlequin, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\arlequin;
use dcCore;
/**
* Plugin definitions.
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/**
* 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'));
}
/**
* Check php version.
*/
public static function phpCompliant(): bool
{
return version_compare(phpversion(), self::PHP_MIN, '>=');
}
/**
* Get distributed models.
*
* Use Behavior arlequinAddModels to add models with synthax:
* [
* 'name'=>__('Model name'), // Nom du modèle prédéfini
* 's_html'=>'[HTML code]', // Code HTML du sélecteur de thème
* 'e_html'=>'[HTML code]', // Code HTML d'un item pouvant être sélectionné
* 'a_html'=>'[HTML code]' // Code HTML d'un item actif (thème sélectionné)
* ]
*/
public static function distributedModels(): array
{
return [
[
'name' => __('Bullets list'),
's_html' => '<ul>%2$s</ul>',
'e_html' => '<li><a href="%1$s%2$s%3$s">%4$s</a></li>',
'a_html' => '<li><strong>%4$s</strong></li>',
],
[
'name' => __('Scrolled list'),
's_html' => '<form action="%1$s" method="post">' . "\n" .
'<p><select name="theme">' . "\n" .
'%2$s' . "\n" .
'</select>' . "\n" .
'<input type="submit" value="' . __('ok') . '"/></p>' . "\n" .
'</form>',
'e_html' => '<option value="%3$s">%4$s</option>',
'a_html' => '<option value="%3$s" selected="selected" disabled="disabled">%4$s (' . __('active theme') . ')</option>',
],
];
}
public static function defaultModel(): array
{
return [
'e_html' => '<li><a href="%1$s%2$s%3$s">%4$s</a></li>',
'a_html' => '<li><strong>%4$s</strong></li>',
's_html' => '<ul>%2$s</ul>',
];
}
}

View File

@ -7,25 +7,28 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke and contributors * @author Oleksandr Syenchuk, 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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return;
}
dcCore::app()->addBehavior('initWidgets', ['adminArlequin','initWidgets']); namespace Dotclear\Plugin\arlequin;
class adminArlequin use dcCore;
use dcModuleDefine;
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
class Widgets
{ {
public static $initialized = false; public static function initWidgets(WidgetsStack $w): void
public static function initWidgets($w)
{ {
$w->create( $w->create(
'arlequin', 'arlequin',
__('Arlequin'), My::name(),
['publicArlequinInterface','arlequinWidget'], [self::class,'parseWidget'],
null, null,
__('Theme switcher') __('Theme switcher')
) )
@ -36,31 +39,80 @@ class adminArlequin
->addOffline(); ->addOffline();
} }
public static function getDefaults() public static function parseWidget(WidgetsElement $w): string
{ {
return [ if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
'e_html' => '<li><a href="%1$s%2$s%3$s">%4$s</a></li>', return '';
'a_html' => '<li><strong>%4$s</strong></li>',
's_html' => '<ul>%2$s</ul>',
];
} }
public static function loadSettings($settings) $model = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('model'), true);
$names = self::getNames();
if (!is_array($model) || empty($names)) {
return '';
}
# Current page URL and the associated query string. Note : the URL for
# the switcher ($s_url) is different to the URL for an item ($e_url)
$s_url = $e_url = Http::getSelfURI();
# If theme setting is already present in URL, we will replace its value
$replace = preg_match('/(\\?|&)theme\\=[^&]*/', $e_url);
# URI extension to send theme setting by query string
if ($replace) {
$ext = '';
} elseif (strpos($e_url, '?') === false) {
$ext = '?theme=';
} else {
$ext = (substr($e_url, -1) == '?' ? '' : '&amp;') . 'theme=';
}
$res = '';
foreach ($names as $k => $v) {
if ($k == dcCore::app()->public->theme) {
$format = $model['a_html'];
} else {
$format = $model['e_html'];
}
if ($replace) {
$e_url = preg_replace(
'/(\\?|&)(theme\\=)([^&]*)/',
'$1${2}' . addcslashes($k, '$\\'),
$e_url
);
$val = '';
} else {
$val = Html::escapeHTML(rawurlencode($k));
}
$res .= sprintf(
$format,
$e_url,
$ext,
$val,
Html::escapeHTML($v['name']),
Html::escapeHTML($v['desc']),
Html::escapeHTML($k)
);
}
# Nothing to display
if (!trim($res)) {
return '';
}
return $w->renderDiv(
(bool) $w->content_only,
'arlequin ' . $w->class,
'',
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . sprintf($model['s_html'], $s_url, $res)
);
}
public static function getNames(): array
{ {
self::$initialized = false; $exclude = explode(';', (string) dcCore::app()->blog->settings->get(My::id())->get('exclude'));
$mt_cfg = @unserialize($settings->arlequinMulti->get('mt_cfg'));
$mt_exclude = $settings->arlequinMulti->get('mt_exclude');
// Paramètres corrompus ou inexistants return array_diff_key(dcCore::app()->themes->getDefines(['state' => dcModuleDefine::STATE_ENABLED], true), array_flip($exclude));
if ($mt_cfg === false || $mt_exclude === null || !(isset($mt_cfg['e_html']) && isset($mt_cfg['a_html']) && isset($mt_cfg['s_html']))) {
$mt_cfg = adminArlequin::getDefaults();
$settings->addNamespace('arlequinMulti');
$settings->arlequinMulti->put('mt_cfg', serialize($mt_cfg), 'string', 'Arlequin configuration');
$settings->arlequinMulti->put('mt_exclude', 'customCSS', 'string', 'Excluded themes');
self::$initialized = true;
dcCore::app()->blog->triggerBlog();
}
return [$mt_cfg,$mt_exclude];
} }
} }