add settings to disable (hide) modules

This commit is contained in:
Jean-Christian Denis 2021-11-05 22:52:46 +01:00
parent e127b460e9
commit 4c0e33c83f
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
5 changed files with 237 additions and 5 deletions

View file

@ -4,6 +4,10 @@ dev
- [ ] add module to check directory structure
- [ ] write documentation of php class
0.5 - 2021.11.05
- add settings to disable (hide) modules
- fix dcstore xml rendering (thanks Franck Paul)
0.4 - 2021.11.02
- add module to use php-cs-fixer

57
_config.php Normal file
View file

@ -0,0 +1,57 @@
<?php
/**
* @brief improve, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return;
}
# Check user perms
dcPage::checkSuper();
$improve = new Improve($core);
$combo_actions = [];
foreach ($improve->modules() as $action) {
$combo_actions[$action->name] = $action->id;
}
$disabled = $improve->disabled();
if (!empty($disabled)) {
$combo_actions = array_merge($combo_actions, array_flip($disabled));
}
if (!empty($_POST['save'])) {
try {
$pdisabled = '';
if (!empty($_POST['disabled'])) {
$pdisabled = implode(';', $_POST['disabled']);
}
$core->blog->settings->improve->put('disabled', $pdisabled);
dcPage::addSuccessNotice(__('Configuration successfully updated.'));
$core->adminurl->redirect(
'admin.plugins',
['module' => 'improve', 'conf' => 1, 'chk' => 1, 'redir' => $list->getRedir()]
);
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
}
echo '<div class="fieldset"><h4>' . __('List of disabled actions:') . '</h4>';
foreach ($combo_actions as $name => $id) {
echo
'<p><label class="classic" title="' . $id . '">' .
form::checkbox(['disabled[]'], $id, ['checked' => isset($disabled[$id])]) .
__($name) . '</label></p>';
}
echo '</div>';

77
_install.php Normal file
View file

@ -0,0 +1,77 @@
<?php
/**
* @brief improve, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
# -- Module specs --
$dc_min = '2.19';
$mod_id = 'improve';
$mod_conf = [
[
'disabled',
'List of hidden action modules',
'tab;newline;endoffile',
'string'
]
];
# -- Nothing to change below --
try {
# Check module version
if (version_compare(
$core->getVersion($mod_id),
$core->plugins->moduleInfo($mod_id, 'version'),
'>='
)) {
return null;
}
# Check Dotclear version
if (!method_exists('dcUtils', 'versionsCompare')
|| dcUtils::versionsCompare(DC_VERSION, $dc_min, '<', false)) {
throw new Exception(sprintf(
'%s requires Dotclear %s',
$mod_id,
$dc_min
));
}
# Set module settings
$core->blog->settings->addNamespace($mod_id);
foreach ($mod_conf as $v) {
$core->blog->settings->{$mod_id}->put(
$v[0],
$v[2],
$v[3],
$v[1],
false,
true
);
}
# Set module version
$core->setVersion(
$mod_id,
$core->plugins->moduleInfo($mod_id, 'version')
);
return true;
} catch (Exception $e) {
$core->error->add($e->getMessage());
return false;
}

83
_uninstall.php Normal file
View file

@ -0,0 +1,83 @@
<?php
/**
* @brief improve, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
$mod_id = 'improve';
$this->addUserAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
$mod_id,
/* desc */
__('delete all settings')
);
$this->addUserAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
$mod_id,
/* desc */
__('delete plugin files')
);
$this->addUserAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
$mod_id,
/* desc */
__('delete the version number')
);
$this->addDirectAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
$mod_id,
/* desc */
sprintf(__('delete all %s settings'), $mod_id)
);
$this->addDirectAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
$mod_id,
/* desc */
sprintf(__('delete %s plugin files'), $mod_id)
);
$this->addDirectAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
$mod_id,
/* desc */
sprintf(__('delete %s version number'), $mod_id)
);

View file

@ -19,22 +19,28 @@ class Improve
'php', 'xml', 'js', 'css', 'csv', 'html', 'htm', 'txt', 'md'
];
private $core;
private $actions = [];
private $logs = [];
private $has_log = ['success' => false, 'warning' => false, 'error' => false];
private $actions = [];
private $disabled = [];
private $logs = [];
private $has_log = ['success' => false, 'warning' => false, 'error' => false];
public function __construct(dcCore $core)
{
$this->core = &$core;
$core->blog->settings->addNamespace('improve');
$list = new arrayObject();
$disabled = explode(';', (string) $core->blog->settings->improve->disabled);
$list = new arrayObject();
try {
$this->core->callBehavior('improveAddAction', $list, $this->core);
foreach ($list as $action) {
if ($action instanceof ImproveAction && !isset($this->actions[$action->id])) {
$this->actions[$action->id] = $action;
if (in_array($action->id, $disabled)) {
$this->disabled[$action->id] = $action->name;
} else {
$this->actions[$action->id] = $action;
}
}
}
} catch (Exception $e) {
@ -129,6 +135,11 @@ class Improve
return $this->actions[$id] ?? null;
}
public function disabled(): array
{
return $this->disabled;
}
public function fixModule(string $type, string $id, array $properties, array $actions): float
{
$time_start = microtime(true);