use settings container

2.28
Jean-Christian Paul Denis 2023-03-16 23:56:53 +01:00
parent c21672138e
commit 9ff0b56fda
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 161 additions and 145 deletions

View File

@ -54,41 +54,22 @@ class Config extends dcNsProcess
return true; return true;
} }
$s = new Settings();
# -- Set settings -- # -- Set settings --
try { try {
$pack_nocomment = !empty($_POST['pack_nocomment']); foreach ($s->listSettings() as $key) {
$pack_fixnewline = !empty($_POST['pack_fixnewline']); $s->writeSetting($key, $_POST[$key] ?? '');
$pack_overwrite = !empty($_POST['pack_overwrite']);
$pack_filename = (string) $_POST['pack_filename'];
$secondpack_filename = (string) $_POST['secondpack_filename'];
$pack_repository = (string) $_POST['pack_repository'];
$pack_excludefiles = (string) $_POST['pack_excludefiles'];
$check = Utils::is_configured(
Utils::getRepositoryDir($pack_repository),
$pack_filename,
$secondpack_filename
);
if ($check) {
$s = dcCore::app()->blog->settings->get(My::id());
$s->put('pack_nocomment', $pack_nocomment);
$s->put('pack_fixnewline', $pack_fixnewline);
$s->put('pack_overwrite', $pack_overwrite);
$s->put('pack_filename', $pack_filename);
$s->put('secondpack_filename', $secondpack_filename);
$s->put('pack_repository', $pack_repository);
$s->put('pack_excludefiles', $pack_excludefiles);
dcPage::addSuccessNotice(
__('Configuration has been successfully updated.')
);
dcCore::app()->adminurl->redirect('admin.plugins', [
'module' => My::id(),
'conf' => '1',
'redir' => dcCore::app()->admin->__get('list')->getRedir(),
]);
} }
dcPage::addSuccessNotice(
__('Configuration has been successfully updated.')
);
dcCore::app()->adminurl->redirect('admin.plugins', [
'module' => My::id(),
'conf' => '1',
'redir' => dcCore::app()->admin->__get('list')->getRedir(),
]);
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());
} }
@ -103,7 +84,7 @@ class Config extends dcNsProcess
} }
# -- Get settings -- # -- Get settings --
$s = dcCore::app()->blog->settings->get(My::id()); $s = new Settings();
# -- Display form -- # -- Display form --
echo echo
@ -112,7 +93,7 @@ class Config extends dcNsProcess
// pack_repository // pack_repository
(new Para())->items([ (new Para())->items([
(new Label(__('Path to repository:')))->for('pack_repository'), (new Label(__('Path to repository:')))->for('pack_repository'),
(new Input('pack_repository'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_repository')), (new Input('pack_repository'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_repository),
]), ]),
(new Note())->class('form-note')->text( (new Note())->class('form-note')->text(
sprintf( sprintf(
@ -126,18 +107,18 @@ class Config extends dcNsProcess
// pack_filename // pack_filename
(new Para())->items([ (new Para())->items([
(new Label(__('Name of exported package:')))->for('pack_filename'), (new Label(__('Name of exported package:')))->for('pack_filename'),
(new Input('pack_filename'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_filename')), (new Input('pack_filename'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_filename),
]), ]),
(new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%'))->class('form-note'), (new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%'))->class('form-note'),
// secondpack_filename // secondpack_filename
(new Para())->items([ (new Para())->items([
(new Label(__('Name of second exported package:')))->for('secondpack_filename'), (new Label(__('Name of second exported package:')))->for('secondpack_filename'),
(new Input('secondpack_filename'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('secondpack_filename')), (new Input('secondpack_filename'))->class('maximal')->size(65)->maxlenght(255)->value($s->secondpack_filename),
]), ]),
(new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%-%version%'))->class('form-note'), (new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%-%version%'))->class('form-note'),
// pack_overwrite // pack_overwrite
(new Para())->items([ (new Para())->items([
(new Checkbox('pack_overwrite', (bool) $s->get('pack_overwrite')))->value(1), (new Checkbox('pack_overwrite', $s->pack_overwrite))->value(1),
(new Label(__('Overwrite existing package'), Label::OUTSIDE_LABEL_AFTER))->for('pack_overwrite')->class('classic'), (new Label(__('Overwrite existing package'), Label::OUTSIDE_LABEL_AFTER))->for('pack_overwrite')->class('classic'),
]), ]),
]), ]),
@ -145,17 +126,17 @@ class Config extends dcNsProcess
// pack_excludefiles // pack_excludefiles
(new Para())->items([ (new Para())->items([
(new Label(__('Extra files to exclude from package:')))->for('pack_excludefiles'), (new Label(__('Extra files to exclude from package:')))->for('pack_excludefiles'),
(new Input('pack_excludefiles'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_excludefiles')), (new Input('pack_excludefiles'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_excludefiles),
]), ]),
(new Note())->text(sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz'))->class('form-note'), (new Note())->text(sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz'))->class('form-note'),
// pack_nocomment // pack_nocomment
(new Para())->items([ (new Para())->items([
(new Checkbox('pack_nocomment', (bool) $s->get('pack_nocomment')))->value(1), (new Checkbox('pack_nocomment', $s->pack_nocomment))->value(1),
(new Label(__('Remove comments from files'), Label::OUTSIDE_LABEL_AFTER))->for('pack_nocomment')->class('classic'), (new Label(__('Remove comments from files'), Label::OUTSIDE_LABEL_AFTER))->for('pack_nocomment')->class('classic'),
]), ]),
// pack_fixnewline // pack_fixnewline
(new Para())->items([ (new Para())->items([
(new Checkbox('pack_fixnewline', (bool) $s->get('pack_fixnewline')))->value(1), (new Checkbox('pack_fixnewline', $s->pack_fixnewline))->value(1),
(new Label(__('Fix newline style from files content'), Label::OUTSIDE_LABEL_AFTER))->for('pack_fixnewline')->class('classic'), (new Label(__('Fix newline style from files content'), Label::OUTSIDE_LABEL_AFTER))->for('pack_fixnewline')->class('classic'),
]), ]),

View File

@ -21,55 +21,12 @@ use Exception;
class Install extends dcNsProcess class Install extends dcNsProcess
{ {
// Module specs
private static $mod_conf = [
[
'menu_plugins',
'Add link to pacKman in plugins page',
false,
'boolean',
],
[
'pack_nocomment',
'Remove comments from files',
false,
'boolean',
],
[
'pack_overwrite',
'Overwrite existing package',
false,
'boolean',
],
[
'pack_filename',
'Name of package',
'%type%-%id%',
'string',
],
[
'secondpack_filename',
'Name of second package',
'%type%-%id%-%version%',
'string',
],
[
'pack_repository',
'Path to package repository',
'',
'string',
],
[
'pack_excludefiles',
'Extra files to exclude from package',
'*.zip,*.tar,*.tar.gz,.directory,.hg',
'string',
],
];
public static function init(): bool public static function init(): bool
{ {
self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); if (defined('DC_CONTEXT_ADMIN')) {
self::$init = version_compare(phpversion(), My::PHP_MIN, '>=')
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
}
return self::$init; return self::$init;
} }
@ -84,18 +41,6 @@ class Install extends dcNsProcess
// Upgrade // Upgrade
self::growUp(); self::growUp();
// Set module settings
foreach (self::$mod_conf as $v) {
dcCore::app()->blog->settings->get(My::id())->put(
$v[0],
$v[2],
$v[3],
$v[1],
false,
true
);
}
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());

View File

@ -23,26 +23,16 @@ use dcNsProcess;
/* clearbricks ns */ /* clearbricks ns */
use files; use files;
use http; use http;
use path;
/* php ns */ /* php ns */
use Exception; use Exception;
class Manage extends dcNsProcess class Manage extends dcNsProcess
{ {
private static $plugins_path = '';
private static $themes_path = '';
public static function init(): bool public static function init(): bool
{ {
if (defined('DC_CONTEXT_ADMIN')) { if (defined('DC_CONTEXT_ADMIN')) {
dcPage::checkSuper(); self::$init = dcCore::app()->auth->isSuperAdmin() && version_compare(phpversion(), My::PHP_MIN, '>=');
# Paths
$e = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$p = array_pop($e);
self::$plugins_path = (string) path::real($p);
self::$themes_path = dcCore::app()->blog->themes_path;
} }
return self::$init; return self::$init;
@ -59,8 +49,8 @@ class Manage extends dcNsProcess
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : ''; $type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : '';
# Settings # Settings
$s = dcCore::app()->blog->settings->get(My::id()); $s = new Settings();
$dir = Utils::getRepositoryDir($s->get('pack_repository')); $dir = Utils::getRepositoryDir($s->pack_repository);
# Modules # Modules
if (!(dcCore::app()->themes instanceof dcThemes)) { if (!(dcCore::app()->themes instanceof dcThemes)) {
@ -73,7 +63,7 @@ class Manage extends dcNsProcess
# Rights # Rights
$is_writable = Utils::is_writable( $is_writable = Utils::is_writable(
$dir, $dir,
$s->get('pack_filename') $s->pack_filename
); );
$is_editable = !empty($type) $is_editable = !empty($type)
&& !empty($_POST['modules']) && !empty($_POST['modules'])
@ -85,13 +75,13 @@ class Manage extends dcNsProcess
if (isset($_REQUEST['package']) && empty($type)) { if (isset($_REQUEST['package']) && empty($type)) {
$modules = []; $modules = [];
if ($type == 'plugins') { if ($type == 'plugins') {
$modules = Core::getPackages(self::$plugins_path); $modules = Core::getPackages(Utils::getPluginsPath());
} elseif ($type == 'themes') { } elseif ($type == 'themes') {
$modules = Core::getPackages(self::$themes_path); $modules = Core::getPackages(Utils::getThemesPath());
} else { } else {
$modules = array_merge( $modules = array_merge(
Core::getPackages(dirname($dir . '/' . $s->get('pack_filename'))), Core::getPackages(dirname($dir . '/' . $s->pack_filename)),
Core::getPackages(dirname($dir . '/' . $s->get('secondpack_filename'))) Core::getPackages(dirname($dir . '/' . $s->secondpack_filename))
); );
} }
@ -140,19 +130,18 @@ class Manage extends dcNsProcess
$module['id'] = $id; $module['id'] = $id;
$module['type'] = $type == 'themes' ? 'theme' : 'plugin'; $module['type'] = $type == 'themes' ? 'theme' : 'plugin';
$files = [
(string) $s->get('pack_filename'),
(string) $s->get('secondpack_filename'),
];
$nocomment = (bool) $s->get('pack_nocomment');
$fixnewline = (bool) $s->get('pack_fixnewline');
$overwrite = (bool) $s->get('pack_overwrite');
$exclude = explode(',', (string) $s->get('pack_excludefiles'));
# --BEHAVIOR-- packmanBeforeCreatePackage # --BEHAVIOR-- packmanBeforeCreatePackage
dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module); dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module);
Core::pack($module, $dir, $files, $overwrite, $exclude, $nocomment, $fixnewline); Core::pack(
$module,
$dir,
[$s->pack_filename, $s->secondpack_filename],
$s->pack_overwrite,
explode(',', $s->pack_excludefiles),
$s->pack_nocomment,
$s->pack_fixnewline
);
# --BEHAVIOR-- packmanAfterCreatePackage # --BEHAVIOR-- packmanAfterCreatePackage
dcCore::app()->callBehavior('packmanAfterCreatePackage', $module); dcCore::app()->callBehavior('packmanAfterCreatePackage', $module);
@ -224,9 +213,9 @@ class Manage extends dcNsProcess
} elseif (strpos($action, 'copy_to_') !== false) { } elseif (strpos($action, 'copy_to_') !== false) {
$dest = (string) $dir; $dest = (string) $dir;
if ($action == 'copy_to_plugins') { if ($action == 'copy_to_plugins') {
$dest = self::$plugins_path; $dest = Utils::getPluginsPath();
} elseif ($action == 'copy_to_themes') { } elseif ($action == 'copy_to_themes') {
$dest = self::$themes_path; $dest = Utils::getThemesPath();
} }
foreach ($_POST['modules'] as $root => $id) { foreach ($_POST['modules'] as $root => $id) {
@ -250,9 +239,9 @@ class Manage extends dcNsProcess
} elseif (strpos($action, 'move_to_') !== false) { } elseif (strpos($action, 'move_to_') !== false) {
$dest = (string) $dir; $dest = (string) $dir;
if ($action == 'move_to_plugins') { if ($action == 'move_to_plugins') {
$dest = self::$plugins_path; $dest = Utils::getPluginsPath();
} elseif ($action == 'move_to_themes') { } elseif ($action == 'move_to_themes') {
$dest = self::$themes_path; $dest = Utils::getThemesPath();
} }
foreach ($_POST['modules'] as $root => $id) { foreach ($_POST['modules'] as $root => $id) {
@ -287,13 +276,13 @@ class Manage extends dcNsProcess
} }
# Settings # Settings
$s = dcCore::app()->blog->settings->get(My::id()); $s = new Settings();
$dir = Utils::getRepositoryDir($s->get('pack_repository')); $dir = Utils::getRepositoryDir($s->pack_repository);
$is_configured = Utils::is_configured( $is_configured = Utils::is_configured(
$dir, $dir,
$s->get('pack_filename'), $s->pack_filename,
$s->get('secondpack_filename') $s->secondpack_filename
); );
# Display # Display
@ -319,13 +308,6 @@ class Manage extends dcNsProcess
'<a href="' . dcCore::app()->adminurl->get('admin.plugins', ['module' => My::id(), 'conf' => '1', 'redir' => dcCore::app()->adminurl->get('admin.plugin.' . My::id())]) . '">' . __('Configuration') . '</a>' . '<a href="' . dcCore::app()->adminurl->get('admin.plugins', ['module' => My::id(), 'conf' => '1', 'redir' => dcCore::app()->adminurl->get('admin.plugin.' . My::id())]) . '">' . __('Configuration') . '</a>' .
'</div>'; '</div>';
} else { } else {
$repo_path_modules = array_merge(
Core::getPackages(dirname($dir . '/' . $s->get('pack_filename'))),
Core::getPackages(dirname($dir . '/' . $s->get('secondpack_filename')))
);
$plugins_path_modules = Core::getPackages(self::$plugins_path);
$themes_path_modules = Core::getPackages(self::$themes_path);
Utils::modules( Utils::modules(
Utils::getModules('plugins'), Utils::getModules('plugins'),
'plugins', 'plugins',
@ -339,19 +321,22 @@ class Manage extends dcNsProcess
); );
Utils::repository( Utils::repository(
$plugins_path_modules, Core::getPackages(Utils::getPluginsPath()),
'plugins', 'plugins',
__('Plugins root') __('Plugins root')
); );
Utils::repository( Utils::repository(
$themes_path_modules, Core::getPackages(Utils::getThemesPath()),
'themes', 'themes',
__('Themes root') __('Themes root')
); );
Utils::repository( Utils::repository(
$repo_path_modules, array_merge(
Core::getPackages(dirname($dir . '/' . $s->pack_filename)),
Core::getPackages(dirname($dir . '/' . $s->secondpack_filename))
),
'repository', 'repository',
__('Packages repository') __('Packages repository')
); );

92
src/Settings.php 100644
View File

@ -0,0 +1,92 @@
<?php
/**
* @brief pacKman, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\pacKman;
use dcCore;
class Settings
{
// Remove comments from files
public readonly bool $pack_nocomment;
// Remove comments from files
public readonly bool $pack_fixnewline;
// Overwrite existing package
public readonly bool $pack_overwrite;
// Name of package
public readonly string $pack_filename;
// Name of second package
public readonly string $secondpack_filename;
// Path to package repository
public readonly string $pack_repository;
// Extra files to exclude from package
public readonly string $pack_excludefiles;
/**
* Constructor set up plugin settings
*/
public function __construct()
{
$s = dcCore::app()->blog->settings->get(My::id());
$this->pack_nocomment = (bool) ($s->get('pack_nocomment') ?? false);
$this->pack_fixnewline = (bool) ($s->get('pack_fixnewline') ?? false);
$this->pack_overwrite = (bool) ($s->get('pack_overwrite') ?? false);
$this->pack_filename = (string) ($s->get('pack_filename') ?? '%type%-%id%');
$this->secondpack_filename = (string) ($s->get('secondpack_filename') ?? '%type%-%id%-%version%');
$this->pack_repository = (string) ($s->get('pack_repository') ?? '');
$this->pack_excludefiles = (string) ($s->get('pack_excludefiles') ?? '*.zip,*.tar,*.tar.gz,.directory,.hg');
}
public function getSetting(string $key): mixed
{
return $this->{$key} ?? null;
}
/**
* Overwrite a plugin settings (in db)
*
* @param string $key The setting ID
* @param mixed $value The setting value
*
* @return bool True on success
*/
public function writeSetting(string $key, mixed $value): bool
{
if (property_exists($this, $key) && settype($value, gettype($this->{$key})) === true) {
dcCore::app()->blog->settings->get(My::id())->drop($key);
dcCore::app()->blog->settings->get(My::id())->put($key, $value, gettype($this->{$key}), '', true, true);
return true;
}
return false;
}
/**
* List defined settings keys
*
* @return array The settings keys
*/
public function listSettings(): array
{
return array_keys(get_class_vars(Settings::class));
}
}

View File

@ -29,6 +29,19 @@ use Exception;
class Utils class Utils
{ {
public static function getPluginsPath(): string
{
$e = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$p = array_pop($e);
return (string) path::real($p);
}
public static function getThemesPath(): string
{
return dcCore::app()->blog->themes_path;
}
public static function is_configured(string $repo, string $file_a, string $file_b): bool public static function is_configured(string $repo, string $file_a, string $file_b): bool
{ {
if (!is_dir(DC_TPL_CACHE) || !is_writable(DC_TPL_CACHE)) { if (!is_dir(DC_TPL_CACHE) || !is_writable(DC_TPL_CACHE)) {