From a0d854c9eaa78cae97bab3bb79ab93be74ced6ab Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Fri, 6 Jan 2023 09:31:44 +0100 Subject: [PATCH] NS Playground: fill structure --- _admin.php | 18 +++++++ _config.php | 19 +++++++ _prepend.php | 22 ++++++++ inc/Admin.php | 136 ++++++++++++++++++++++++++---------------------- inc/Config.php | 136 ++++++++++++++++++++++++++++-------------------- inc/Core.php | 15 +++++- inc/Prepend.php | 41 +++++++++++++-- js/admin.js | 4 +- 8 files changed, 269 insertions(+), 122 deletions(-) create mode 100644 _admin.php create mode 100644 _config.php create mode 100644 _prepend.php diff --git a/_admin.php b/_admin.php new file mode 100644 index 0000000..065bd9f --- /dev/null +++ b/_admin.php @@ -0,0 +1,18 @@ +auth->isSuperAdmin()) { - return null; -} +namespace Dotclear\Plugin\tweakStores; -# only if activated -dcCore::app()->blog->settings->addNamespace('tweakStores'); -if (!dcCore::app()->blog->settings->tweakStores->active) { - return null; -} +/* dotclear ns */ +use dcCore; +use dcPage; -# admin behaviors -if (dcCore::app()->blog->settings->tweakStores->packman) { - dcCore::app()->addBehavior('packmanBeforeCreatePackage', ['tweakStoresBehaviors', 'packmanBeforeCreatePackage']); -} -dcCore::app()->addBehavior('pluginsToolsHeadersV2', ['tweakStoresBehaviors', 'modulesToolsHeaders']); -dcCore::app()->addBehavior('themesToolsHeadersV2', ['tweakStoresBehaviors', 'modulesToolsHeaders']); -dcCore::app()->addBehavior('pluginsToolsTabsV2', ['tweakStoresBehaviors', 'pluginsToolsTabs']); -dcCore::app()->addBehavior('themesToolsTabsV2', ['tweakStoresBehaviors', 'themesToolsTabs']); +/* clearbricks ns */ +use form; +use html; -class tweakStoresBehaviors +/* php ns */ +use Exception; + +class Admin { - # create dcstore.xml file on the fly when pack a module - public static function packmanBeforeCreatePackage(array $module): void + protected static $init = false; + + public static function init(): bool { - tweakStores::writeXML($module['id'], $module, dcCore::app()->blog->settings->tweakStores->file_pattern); + if (defined('DC_CONTEXT_ADMIN') + && dcCore::app()->auth->isSuperAdmin() + && dcCore::app()->blog->settings->get(basename(__NAMESPACE__))->get('active') + ) { + dcCore::app()->auth->user_prefs->addWorkspace('interface'); + self::$init = true; + } + + return self::$init; + } + + public static function process(): ?bool + { + if (!self::$init) { + return false; + } + + if (dcCore::app()->blog->settings->get(basename(__NAMESPACE__))->get('packman')) { + // create dcstore.xml file on the fly when plugin packman pack a module + dcCore::app()->addBehavior('packmanBeforeCreatePackage', function (array $module): void { + Core::writeXML($module['id'], $module, dcCore::app()->blog->settings->get(basename(__NAMESPACE__))->get('file_pattern')); + }); + } + + dcCore::app()->addBehaviors([ + // addd some js + 'pluginsToolsHeadersV2' => [self::class, 'modulesToolsHeaders'], + 'themesToolsHeadersV2' => [self::class, 'modulesToolsHeaders'], + // admin plugins page tab + 'pluginsToolsTabsV2' => function (): void { + self::modulesToolsTabs(dcCore::app()->plugins->getModules(), explode(',', DC_DISTRIB_PLUGINS), dcCore::app()->adminurl->get('admin.plugins')); + }, + // admin themes page tab + 'themesToolsTabsV2' => function (): void { + self::modulesToolsTabs(dcCore::app()->themes->getModules(), explode(',', DC_DISTRIB_THEMES), dcCore::app()->adminurl->get('admin.blog.theme')); + }, + ]); + + return true; } - # addd some js public static function modulesToolsHeaders(bool $is_plugin): string { - dcCore::app()->auth->user_prefs->addWorkspace('interface'); - return - dcPage::jsVars(['dotclear.ts_copied' => __('Copied to clipboard')]) . - dcPage::jsLoad(dcPage::getPF('tweakStores/js/admin.js')) . + dcPage::jsJson('ts_copied', ['alert' => __('Copied to clipboard')]) . + dcPage::jsModuleLoad(basename(__NAMESPACE__) . '/js/admin.js') . ( !dcCore::app()->auth->user_prefs->interface->colorsyntax ? '' : dcPage::jsLoadCodeMirror(dcCore::app()->auth->user_prefs->interface->colorsyntax_theme) . - dcPage::jsLoad(dcPage::getPF('tweakStores/js/cms.js')) + dcPage::jsModuleLoad(basename(__NAMESPACE__) . '/js/cms.js') ); } - # admin plugins page tab - public static function pluginsToolsTabs(): void - { - self::modulesToolsTabs(dcCore::app()->plugins->getModules(), explode(',', DC_DISTRIB_PLUGINS), dcCore::app()->adminurl->get('admin.plugins') . '#tweakStores'); - } - - # admin themes page tab - public static function themesToolsTabs(): void - { - self::modulesToolsTabs(dcCore::app()->themes->getModules(), explode(',', DC_DISTRIB_THEMES), dcCore::app()->adminurl->get('admin.blog.theme') . '#tweakStores'); - } - - # generic page tab protected static function modulesToolsTabs(array $modules, array $excludes, string $page_url): void { - dcCore::app()->auth->user_prefs->addWorkspace('interface'); + $page_url .= '#' . basename(__NAMESPACE__); $user_ui_colorsyntax = dcCore::app()->auth->user_prefs->interface->colorsyntax; $user_ui_colorsyntax_theme = dcCore::app()->auth->user_prefs->interface->colorsyntax_theme; $combo = self::comboModules($modules, $excludes); - $file_pattern = dcCore::app()->blog->settings->tweakStores->file_pattern; + $file_pattern = dcCore::app()->blog->settings->get(basename(__NAMESPACE__))->get('file_pattern'); # check dcstore repo $url = ''; @@ -110,7 +124,7 @@ class tweakStoresBehaviors # generate xml code if (!empty($_POST['buildxml_id']) && in_array($_POST['buildxml_id'], $combo)) { - $xml_content = tweakStores::generateXML($_POST['buildxml_id'], $modules[$_POST['buildxml_id']], $file_pattern); + $xml_content = Core::generateXML($_POST['buildxml_id'], $modules[$_POST['buildxml_id']], $file_pattern); } # write dcstore.xml file @@ -118,14 +132,14 @@ class tweakStoresBehaviors if (empty($_POST['your_pwd']) || !dcCore::app()->auth->checkPassword($_POST['your_pwd'])) { dcCore::app()->error->add(__('Password verification failed')); } else { - $ret = tweakStores::writeXML($_POST['buildxml_id'], $modules[$_POST['buildxml_id']], $file_pattern); - if (!empty(tweakStores::$failed)) { - dcCore::app()->error->add(implode(' ', tweakStores::$failed)); + $ret = Core::writeXML($_POST['buildxml_id'], $modules[$_POST['buildxml_id']], $file_pattern); + if (!empty(Core::$failed)) { + dcCore::app()->error->add(implode(' ', Core::$failed)); } } } echo - '
' . + '
' . '

' . __('Tweak third-party repositories') . '

'; if (!empty($_POST['write_xml'])) { @@ -162,7 +176,7 @@ class tweakStoresBehaviors ( empty($file_content) ? '' : '
' . form::textArea('file_xml', 165, 14, [
-                    'default'    => html::escapeHTML(tweakStores::prettyXML($file_content)),
+                    'default'    => html::escapeHTML(Core::prettyXML($file_content)),
                     'class'      => 'maximal',
                     'extra_html' => 'readonly="true"',
                 ]) . '
' . @@ -177,7 +191,7 @@ class tweakStoresBehaviors if (empty($file_pattern)) { echo sprintf( '

' . __('Generate xml code') . '

%s

', - dcCore::app()->adminurl->get('admin.plugins', ['module' => 'tweakStores', 'conf' => 1, 'redir' => $page_url]), + dcCore::app()->adminurl->get('admin.plugins', ['module' => basename(__NAMESPACE__), 'conf' => 1, 'redir' => $page_url]), __('You must configure zip file pattern to complete xml code automatically.') ); } else { @@ -197,19 +211,19 @@ class tweakStoresBehaviors '
' . '

' . sprintf(__('Generated code for module: %s'), html::escapeHTML($_POST['buildxml_id'])) . '

'; - if (!empty(tweakStores::$failed)) { - echo '

' . sprintf(__('Failed to parse XML code: %s'), implode(', ', tweakStores::$failed)) . '

'; + if (!empty(Core::$failed)) { + echo '

' . sprintf(__('Failed to parse XML code: %s'), implode(', ', Core::$failed)) . '

'; } - if (!empty(tweakStores::$notice)) { - echo '

' . sprintf(__('Code is not fully filled: %s'), implode(', ', tweakStores::$notice)) . '

'; + if (!empty(Core::$notice)) { + echo '

' . sprintf(__('Code is not fully filled: %s'), implode(', ', Core::$notice)) . '

'; } if (!empty($xml_content)) { - if (empty(tweakStores::$failed) && empty(tweakStores::$notice)) { + if (empty(Core::$failed) && empty(Core::$notice)) { echo '

' . __('Code is complete') . '

'; } echo '
' . form::textArea('gen_xml', 165, 14, [
-                    'default'    => html::escapeHTML(tweakStores::prettyXML($xml_content)),
+                    'default'    => html::escapeHTML(Core::prettyXML($xml_content)),
                     'class'      => 'maximal',
                     'extra_html' => 'readonly="true"',
                 ]) . '
' . @@ -218,7 +232,7 @@ class tweakStoresBehaviors dcPage::jsRunCodeMirror('editor', 'gen_xml', 'dotclear', $user_ui_colorsyntax_theme) ); - if (empty(tweakStores::$failed) + if (empty(Core::$failed) && $modules[$_POST['buildxml_id']]['root_writable'] && dcCore::app()->auth->isSuperAdmin() ) { @@ -234,13 +248,13 @@ class tweakStoresBehaviors ] ) . '

' . '

' . - '' . __('Copy to clipboard') . '' . + '' . __('Copy to clipboard') . '' . form::hidden('buildxml_id', $_POST['buildxml_id']) . dcCore::app()->formNonce() . '

'; } echo sprintf( '

%s

', - dcCore::app()->adminurl->get('admin.plugins', ['module' => 'tweakStores', 'conf' => 1, 'redir' => $page_url]), + dcCore::app()->adminurl->get('admin.plugins', ['module' => basename(__NAMESPACE__), 'conf' => 1, 'redir' => $page_url]), __('You can edit zip file pattern from configuration page.') ); } diff --git a/inc/Config.php b/inc/Config.php index d2427ed..99c6665 100644 --- a/inc/Config.php +++ b/inc/Config.php @@ -10,67 +10,93 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_MODULE')) { - return null; -} +declare(strict_types=1); -dcPage::checkSuper(); +namespace Dotclear\Plugin\tweakStores; -$redir = empty($_REQUEST['redir']) ? - dcCore::app()->admin->list->getURL() . '#plugins' : $_REQUEST['redir']; +/* dotclear ns */ +use dcCore; +use dcPage; -# -- Get settings -- -dcCore::app()->blog->settings->addNamespace('tweakStores'); -$s = dcCore::app()->blog->settings->tweakStores; +/* clearbricks ns */ +use form; +use http; -$tweakStores_active = $s->active; -$tweakStores_packman = $s->packman; -$tweakStores_file_pattern = $s->file_pattern; +/* php ns */ +use Exception; -# -- Set settings -- -if (!empty($_POST['save'])) { - try { - $tweakStores_active = !empty($_POST['tweakStores_active']); - $tweakStores_packman = !empty($_POST['tweakStores_packman']); - $tweakStores_file_pattern = $_POST['tweakStores_file_pattern']; +class Config +{ + protected static $init = false; - $s->put('active', $tweakStores_active); - $s->put('packman', $tweakStores_packman); - $s->put('file_pattern', $tweakStores_file_pattern); + public static function init(): bool + { + if (defined('DC_CONTEXT_ADMIN') && defined('DC_CONTEXT_MODULE')) { + dcPage::checkSuper(); + self::$init = true; + } - dcPage::addSuccessNotice( - __('Configuration successfully updated') - ); - http::redirect( - dcCore::app()->admin->list->getURL('module=tweakStores&conf=1&redir=' . dcCore::app()->admin->list->getRedir()) - ); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + return self::$init; + } + + public static function process(): ?bool + { + if (!self::$init) { + return false; + } + + if (empty($_POST['save'])) { + return null; + } + + try { + $s = dcCore::app()->blog->settings->get(basename(__NAMESPACE__)); + $s->put('active', !empty($_POST['s_active'])); + $s->put('packman', !empty($_POST['s_packman'])); + $s->put('file_pattern', $_POST['s_file_pattern']); + + dcPage::addSuccessNotice( + __('Configuration successfully updated') + ); + http::redirect( + dcCore::app()->admin->__get('list')->getURL('module=' . basename(__NAMESPACE__) . '&conf=1&redir=' . dcCore::app()->admin->__get('list')->getRedir()) + ); + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + return null; + } + + public static function render(): void + { + $s = dcCore::app()->blog->settings->get(basename(__NAMESPACE__)); + + echo ' +
+

' . dcCore::app()->plugins->moduleInfo(basename(__NAMESPACE__), 'name') . '

+ +

+

' . __('If enabled, new tab "Tweak stores" allows your to perfom actions relative to third-party repositories.') . '

+ +

+

' . __('If enabled, plugin pacKman will (re)generate on the fly dcstore.xml file at root directory of the module.') . '

+ +

+

' . + __('You can use widcard like %author%, %type%, %id%, %version%.') . '
' . + __('For example on github https://github.com/MyGitName/%id%/releases/download/v%version%/%type%-%id%.zip') . '
' . + __('Note: on github, you must create a release and join to it the module zip file.') . ' +

+ +
'; } } - -# -- Display form -- -echo ' -
-

' . __('Tweak store') . '

- -

-

' . __('If enabled, new tab "Tweak stores" allows your to perfom actions relative to third-party repositories.') . '

- -

-

' . __('If enabled, plugin pacKman will (re)generate on the fly dcstore.xml file at root directory of the module.') . '

- -

-

' . -__('You can use widcard like %author%, %type%, %id%, %version%.') . '
' . -__('For example on github https://github.com/MyGitName/%id%/releases/download/v%version%/%type%-%id%.zip') . '
' . -__('Note: on github, you must create a release and join to it the module zip file.') . ' -

- -
'; diff --git a/inc/Core.php b/inc/Core.php index 4c7f349..b4af237 100644 --- a/inc/Core.php +++ b/inc/Core.php @@ -10,7 +10,20 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class tweakStores +declare(strict_types=1); + +namespace Dotclear\Plugin\tweakStores; + +/* clearbricks ns */ +use files; +use text; +use xmlTag; + +/* php ns */ +use DOMDocument; +use Exception; + +class Core { /** @var array List of notice messages */ public static $notice = []; diff --git a/inc/Prepend.php b/inc/Prepend.php index 91883ac..abbda41 100644 --- a/inc/Prepend.php +++ b/inc/Prepend.php @@ -10,8 +10,41 @@ * @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); -Clearbricks::lib()->autoload(['tweakStores' => __DIR__ . '/inc/class.tweakstores.php']); +namespace Dotclear\Plugin\tweakStores; + +/* clearbricks ns */ +use Clearbricks; + +class Prepend +{ + private const LIBS = [ + 'Admin', + 'Config', + 'Core', + ]; + protected static $init = false; + + public static function init(): bool + { + self::$init = defined('DC_RC_PATH'); + + return self::$init; + } + + public static function process(): ?bool + { + if (!self::$init) { + return false; + } + + foreach (self::LIBS as $lib) { + Clearbricks::lib()->autoload([ + implode('\\', ['Dotclear','Plugin', basename(__NAMESPACE__), $lib]) => __DIR__ . DIRECTORY_SEPARATOR . $lib . '.php', + ]); + } + + return true; + } +} diff --git a/js/admin.js b/js/admin.js index 6205522..de98410 100644 --- a/js/admin.js +++ b/js/admin.js @@ -1,6 +1,8 @@ /*global $, dotclear */ 'use strict'; +Object.assign(dotclear.msg, dotclear.getData('ts_copied')); + $(function(){ $("#ts_copy_button").click(function() { var style = $("#gen_xml").attr('style'); @@ -14,7 +16,7 @@ $(function(){ $("#gen_xml").removeAttr("contenteditable").attr('style', style); $("#ts_copy_button").focus(); - alert(dotclear.ts_copied); + alert(dotclear.msg.alert); return false; }); }); \ No newline at end of file