add default selection for user action form, fix #2

This commit is contained in:
Jean-Christian Paul Denis 2023-05-09 10:17:34 +02:00
parent 78b483775e
commit 910aeee188
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
12 changed files with 60 additions and 33 deletions

View File

@ -28,6 +28,7 @@ class ActionDescriptor
* @param string $error The error message * @param string $error The error message
* @param string $ns The namespace (for defined action) * @param string $ns The namespace (for defined action)
* @param string $select The generic message (used for self::values() management) * @param string $select The generic message (used for self::values() management)
* @param bool $default The default state of action form field (checked or not)
*/ */
public function __construct( public function __construct(
public readonly string $id, public readonly string $id,
@ -35,7 +36,8 @@ class ActionDescriptor
public readonly string $success, public readonly string $success,
public readonly string $error, public readonly string $error,
public readonly string $ns = '', public readonly string $ns = '',
public readonly string $select = '' public readonly string $select = '',
public readonly bool $default = true
) { ) {
} }

View File

@ -46,7 +46,8 @@ class Caches extends CleanerParent
select: __('delete selected cache directories'), select: __('delete selected cache directories'),
query: __('delete "%s" cache directory'), query: __('delete "%s" cache directory'),
success: __('"%s" cache directory deleted'), success: __('"%s" cache directory deleted'),
error: __('Failed to delete "%s" cache directory') error: __('Failed to delete "%s" cache directory'),
default: true
), ),
// delete $ns folder files but keep folder // delete $ns folder files but keep folder
new ActionDescriptor( new ActionDescriptor(
@ -54,7 +55,8 @@ class Caches extends CleanerParent
select: __('empty selected cache directories'), select: __('empty selected cache directories'),
query: __('empty "%s" cache directory'), query: __('empty "%s" cache directory'),
success: __('"%s" cache directory emptied'), success: __('"%s" cache directory emptied'),
error: __('Failed to empty "%s" cache directory') error: __('Failed to empty "%s" cache directory'),
default: true
), ),
] ]
)); ));

View File

@ -48,7 +48,8 @@ class Logs extends CleanerParent
select: __('delete selected logs tables'), select: __('delete selected logs tables'),
query: __('delete "%s" logs table'), query: __('delete "%s" logs table'),
success: __('"%s" logs table deleted'), success: __('"%s" logs table deleted'),
error: __('Failed to delete "%s" logs table') error: __('Failed to delete "%s" logs table'),
default: true
), ),
] ]
)); ));

View File

@ -44,7 +44,8 @@ class Plugins extends CleanerParent
select: __('delete selected plugins files and directories'), select: __('delete selected plugins files and directories'),
query: __('delete "%s" plugin files and directories'), query: __('delete "%s" plugin files and directories'),
success: __('"%s" plugin files and directories deleted'), success: __('"%s" plugin files and directories deleted'),
error: __('Failed to delete "%s" plugin files and directories') error: __('Failed to delete "%s" plugin files and directories'),
default: true
), ),
] ]
)); ));

View File

@ -48,7 +48,8 @@ class Preferences extends CleanerParent
select: __('delete selected global preferences workspaces'), select: __('delete selected global preferences workspaces'),
query: __('delete "%s" global preferences workspace'), query: __('delete "%s" global preferences workspace'),
success: __('"%s" global preferences workspace deleted'), success: __('"%s" global preferences workspace deleted'),
error: __('Failed to delete "%s" global preferences workspace') error: __('Failed to delete "%s" global preferences workspace'),
default: false
), ),
// delete users $ns preferences workspace // delete users $ns preferences workspace
new ActionDescriptor( new ActionDescriptor(
@ -56,7 +57,8 @@ class Preferences extends CleanerParent
select: __('delete selected users preferences workspaces'), select: __('delete selected users preferences workspaces'),
query: __('delete "%s" users preferences workspace'), query: __('delete "%s" users preferences workspace'),
success: __('"%s" users preferences workspace deleted'), success: __('"%s" users preferences workspace deleted'),
error: __('Failed to delete "%s" users preferences workspace') error: __('Failed to delete "%s" users preferences workspace'),
default: false
), ),
// delete user and global $ns preferences workspace // delete user and global $ns preferences workspace
new ActionDescriptor( new ActionDescriptor(
@ -64,14 +66,16 @@ class Preferences extends CleanerParent
select: __('delete selected preferences workspaces'), select: __('delete selected preferences workspaces'),
query: __('delete "%s" preferences workspace'), query: __('delete "%s" preferences workspace'),
success: __('"%s" preferences workspace deleted'), success: __('"%s" preferences workspace deleted'),
error: __('Failed to delete "%s" preferences workspace') error: __('Failed to delete "%s" preferences workspace'),
default: false
), ),
// delete users and globals specific $ws:$id settings using 'pref_ws:pref_id;pref_ws:pref_id;' as $ns // delete users and globals specific $ws:$id settings using 'pref_ws:pref_id;pref_ws:pref_id;' as $ns
new ActionDescriptor( new ActionDescriptor(
id: 'delete_related', id: 'delete_related',
query: __('delete related preferences'), query: __('delete related preferences'),
success: __('related preferences deleted'), success: __('related preferences deleted'),
error: __('Failed to delete related preferences') error: __('Failed to delete related preferences'),
default: false
), ),
] ]
)); ));

View File

@ -48,7 +48,8 @@ class Settings extends CleanerParent
select: __('delete selected global settings namespaces'), select: __('delete selected global settings namespaces'),
query: __('delete "%s" global settings namespace'), query: __('delete "%s" global settings namespace'),
success: __('"%s" global settings namespace deleted'), success: __('"%s" global settings namespace deleted'),
error: __('Failed to delete "%s" global settings namespace') error: __('Failed to delete "%s" global settings namespace'),
default: false
), ),
// delete blogs $ns settings namespace // delete blogs $ns settings namespace
new ActionDescriptor( new ActionDescriptor(
@ -56,7 +57,8 @@ class Settings extends CleanerParent
select: __('delete selected blog settings namespaces'), select: __('delete selected blog settings namespaces'),
query: __('delete "%s" blog settings namespace'), query: __('delete "%s" blog settings namespace'),
success: __('"%s" blog settings namespace deleted'), success: __('"%s" blog settings namespace deleted'),
error: __('Failed to delete "%s" blog settings namespace') error: __('Failed to delete "%s" blog settings namespace'),
default: false
), ),
// delete blogs and global settings namespace // delete blogs and global settings namespace
new ActionDescriptor( new ActionDescriptor(
@ -64,14 +66,16 @@ class Settings extends CleanerParent
select: __('delete selected settings namespaces'), select: __('delete selected settings namespaces'),
query: __('delete "%s" settings namespace'), query: __('delete "%s" settings namespace'),
success: __('"%s" settings namespace deleted'), success: __('"%s" settings namespace deleted'),
error: __('Failed to delete "%s" settings namespace') error: __('Failed to delete "%s" settings namespace'),
default: false
), ),
// delete blogs and globals specific $ns:$id settings using 'setting_ns:setting_id;setting_ns:setting_id;' as $ns // delete blogs and globals specific $ns:$id settings using 'setting_ns:setting_id;setting_ns:setting_id;' as $ns
new ActionDescriptor( new ActionDescriptor(
id: 'delete_related', id: 'delete_related',
query: __('delete related settings'), query: __('delete related settings'),
success: __('related settings deleted'), success: __('related settings deleted'),
error: __('Failed to delete related settings') error: __('Failed to delete related settings'),
default: false
), ),
] ]
)); ));

View File

@ -51,7 +51,8 @@ class Tables extends CleanerParent
select: __('delete selected tables'), select: __('delete selected tables'),
query: __('delete "%s" table'), query: __('delete "%s" table'),
success: __('"%s" table deleted'), success: __('"%s" table deleted'),
error: __('Failed to delete "%s" table') error: __('Failed to delete "%s" table'),
default: false
), ),
// truncate (empty) $ns database table // truncate (empty) $ns database table
new ActionDescriptor( new ActionDescriptor(
@ -59,7 +60,8 @@ class Tables extends CleanerParent
select: __('empty selected tables'), select: __('empty selected tables'),
query: __('empty "%s" table'), query: __('empty "%s" table'),
success: __('"%s" table emptied'), success: __('"%s" table emptied'),
error: __('Failed to empty "%s" table') error: __('Failed to empty "%s" table'),
default: false
), ),
] ]
)); ));

View File

@ -45,7 +45,8 @@ class Themes extends CleanerParent
select: __('delete selected themes files and directories'), select: __('delete selected themes files and directories'),
query: __('delete "%s" theme files and directories'), query: __('delete "%s" theme files and directories'),
success: __('"%s" theme files and directories deleted'), success: __('"%s" theme files and directories deleted'),
error: __('Failed to delete "%s" theme files and directories') error: __('Failed to delete "%s" theme files and directories'),
default: true
), ),
] ]
)); ));

View File

@ -45,7 +45,8 @@ class Vars extends CleanerParent
select: __('delete selected var directories'), select: __('delete selected var directories'),
query: __('delete "%s" var directory'), query: __('delete "%s" var directory'),
success: __('"%s" var directory deleted'), success: __('"%s" var directory deleted'),
error: __('Failed to delete "%s" var directory') error: __('Failed to delete "%s" var directory'),
default: true
), ),
] ]
)); ));

View File

@ -44,7 +44,8 @@ class Versions extends CleanerParent
select: __('delete selected versions numbers'), select: __('delete selected versions numbers'),
query: __('delete "%s" version number'), query: __('delete "%s" version number'),
success: __('"%s" version number deleted'), success: __('"%s" version number deleted'),
error: __('Failed to delete "%s" version number') error: __('Failed to delete "%s" version number'),
default: true
), ),
] ]
)); ));

View File

@ -145,7 +145,7 @@ class Manage extends dcNsProcess
foreach ($uninstaller->getUserActions($define->getId()) as $cleaner => $stack) { foreach ($uninstaller->getUserActions($define->getId()) as $cleaner => $stack) {
foreach ($stack as $action) { foreach ($stack as $action) {
$fields[] = (new Para())->items([ $fields[] = (new Para())->items([
(new Checkbox(['action[' . $cleaner . '][' . $action->id . ']', 'action_' . $cleaner . '_' . $action->id], true))->value($action->ns), (new Checkbox(['action[' . $cleaner . '][' . $action->id . ']', 'action_' . $cleaner . '_' . $action->id], $action->default))->value($action->ns),
(new Label($action->query, Label::OUTSIDE_LABEL_AFTER))->for('action_' . $cleaner . '_' . $action->id)->class('classic'), (new Label($action->query, Label::OUTSIDE_LABEL_AFTER))->for('action_' . $cleaner . '_' . $action->id)->class('classic'),
]); ]);
} }

View File

@ -134,15 +134,18 @@ class Uninstaller
* This method should be called from module Uninstall::proces() method. * This method should be called from module Uninstall::proces() method.
* User will be prompted before doing these actions. * User will be prompted before doing these actions.
* *
* Leave $default param to null to let Cleaner decide.
*
* @param string $cleaner The cleaner ID * @param string $cleaner The cleaner ID
* @param string $action The action ID * @param string $action The action ID
* @param string $ns Name of setting related to module * @param string $ns Name of setting related to module
* @param null|null $default The default state of form field (checked)
* *
* @return Uninstaller Uninstaller instance * @return Uninstaller Uninstaller instance
*/ */
public function addUserAction(string $cleaner, string $action, string $ns): Uninstaller public function addUserAction(string $cleaner, string $action, string $ns, ?bool $default = null): Uninstaller
{ {
if (null !== $this->module && null !== ($res = $this->addAction($cleaner, $action, $ns))) { if (null !== $this->module && null !== ($res = $this->addAction($cleaner, $action, $ns, $default))) {
if (!isset($this->user_actions[$this->module->getId()])) { if (!isset($this->user_actions[$this->module->getId()])) {
$this->user_actions[$this->module->getId()] = new ActionsStack(); $this->user_actions[$this->module->getId()] = new ActionsStack();
} }
@ -160,15 +163,18 @@ class Uninstaller
* user will NOT be prompted before these actions execution. * user will NOT be prompted before these actions execution.
* Note: If module is disabled, direct actions are not executed. * Note: If module is disabled, direct actions are not executed.
* *
* Leave $default param to null to let Cleaner decide.
*
* @param string $cleaner The cleaner ID * @param string $cleaner The cleaner ID
* @param string $action The action ID * @param string $action The action ID
* @param string $ns Name of setting related to module. * @param string $ns Name of setting related to module
* @param null|null $default The default state of form field (checked)
* *
* @return Uninstaller Uninstaller instance * @return Uninstaller Uninstaller instance
*/ */
public function addDirectAction(string $cleaner, string $action, string $ns): Uninstaller public function addDirectAction(string $cleaner, string $action, string $ns, ?bool $default = null): Uninstaller
{ {
if (null !== $this->module && null !== ($res = $this->addAction($cleaner, $action, $ns))) { if (null !== $this->module && null !== ($res = $this->addAction($cleaner, $action, $ns, $default))) {
if (!isset($this->direct_actions[$this->module->getId()])) { if (!isset($this->direct_actions[$this->module->getId()])) {
$this->direct_actions[$this->module->getId()] = new ActionsStack(); $this->direct_actions[$this->module->getId()] = new ActionsStack();
} }
@ -256,11 +262,12 @@ class Uninstaller
* *
* @param string $cleaner The cleaner ID * @param string $cleaner The cleaner ID
* @param string $action The action ID * @param string $action The action ID
* @param string $ns Name of setting related to module. * @param string $ns Name of setting related to module
* @param null|null $default The default state of form field (checked)
* *
* @return null|ActionDescriptor The action description * @return null|ActionDescriptor The action description
*/ */
private function addAction(string $cleaner, string $action, string $ns): ?ActionDescriptor private function addAction(string $cleaner, string $action, string $ns, ?bool $default): ?ActionDescriptor
{ {
// no current module or no cleaner id or no ns or unknown cleaner action // no current module or no cleaner id or no ns or unknown cleaner action
if (null === $this->module if (null === $this->module
@ -279,6 +286,7 @@ class Uninstaller
query: sprintf($this->cleaners->get($cleaner)->actions[$action]->query, $ns), query: sprintf($this->cleaners->get($cleaner)->actions[$action]->query, $ns),
success: sprintf($this->cleaners->get($cleaner)->actions[$action]->success, $ns), success: sprintf($this->cleaners->get($cleaner)->actions[$action]->success, $ns),
error: sprintf($this->cleaners->get($cleaner)->actions[$action]->error, $ns), error: sprintf($this->cleaners->get($cleaner)->actions[$action]->error, $ns),
default: is_null($default) ? $this->cleaners->get($cleaner)->actions[$action]->default : $default
); );
} }
} }