From 910aeee1885945eab20ea050b901c97de297d11d Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Tue, 9 May 2023 10:17:34 +0200 Subject: [PATCH] add default selection for user action form, fix #2 --- src/ActionDescriptor.php | 4 +++- src/Cleaner/Caches.php | 6 ++++-- src/Cleaner/Logs.php | 3 ++- src/Cleaner/Plugins.php | 3 ++- src/Cleaner/Preferences.php | 12 ++++++++---- src/Cleaner/Settings.php | 12 ++++++++---- src/Cleaner/Tables.php | 6 ++++-- src/Cleaner/Themes.php | 3 ++- src/Cleaner/Vars.php | 3 ++- src/Cleaner/Versions.php | 3 ++- src/Manage.php | 2 +- src/Uninstaller.php | 36 ++++++++++++++++++++++-------------- 12 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/ActionDescriptor.php b/src/ActionDescriptor.php index 1a59533..deba4b4 100644 --- a/src/ActionDescriptor.php +++ b/src/ActionDescriptor.php @@ -28,6 +28,7 @@ class ActionDescriptor * @param string $error The error message * @param string $ns The namespace (for defined action) * @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 readonly string $id, @@ -35,7 +36,8 @@ class ActionDescriptor public readonly string $success, public readonly string $error, public readonly string $ns = '', - public readonly string $select = '' + public readonly string $select = '', + public readonly bool $default = true ) { } diff --git a/src/Cleaner/Caches.php b/src/Cleaner/Caches.php index 084a404..4fc9248 100644 --- a/src/Cleaner/Caches.php +++ b/src/Cleaner/Caches.php @@ -46,7 +46,8 @@ class Caches extends CleanerParent select: __('delete selected cache directories'), query: __('delete "%s" cache directory'), 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 new ActionDescriptor( @@ -54,7 +55,8 @@ class Caches extends CleanerParent select: __('empty selected cache directories'), query: __('empty "%s" cache directory'), success: __('"%s" cache directory emptied'), - error: __('Failed to empty "%s" cache directory') + error: __('Failed to empty "%s" cache directory'), + default: true ), ] )); diff --git a/src/Cleaner/Logs.php b/src/Cleaner/Logs.php index b55fca8..8529dc4 100644 --- a/src/Cleaner/Logs.php +++ b/src/Cleaner/Logs.php @@ -48,7 +48,8 @@ class Logs extends CleanerParent select: __('delete selected logs tables'), query: __('delete "%s" logs table'), success: __('"%s" logs table deleted'), - error: __('Failed to delete "%s" logs table') + error: __('Failed to delete "%s" logs table'), + default: true ), ] )); diff --git a/src/Cleaner/Plugins.php b/src/Cleaner/Plugins.php index e1ac308..a3f021a 100644 --- a/src/Cleaner/Plugins.php +++ b/src/Cleaner/Plugins.php @@ -44,7 +44,8 @@ class Plugins extends CleanerParent select: __('delete selected plugins files and directories'), query: __('delete "%s" plugin files and directories'), 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 ), ] )); diff --git a/src/Cleaner/Preferences.php b/src/Cleaner/Preferences.php index 3a83d17..80287d4 100644 --- a/src/Cleaner/Preferences.php +++ b/src/Cleaner/Preferences.php @@ -48,7 +48,8 @@ class Preferences extends CleanerParent select: __('delete selected global preferences workspaces'), query: __('delete "%s" global preferences workspace'), 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 new ActionDescriptor( @@ -56,7 +57,8 @@ class Preferences extends CleanerParent select: __('delete selected users preferences workspaces'), query: __('delete "%s" users preferences workspace'), 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 new ActionDescriptor( @@ -64,14 +66,16 @@ class Preferences extends CleanerParent select: __('delete selected preferences workspaces'), query: __('delete "%s" preferences workspace'), 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 new ActionDescriptor( id: 'delete_related', query: __('delete related preferences'), success: __('related preferences deleted'), - error: __('Failed to delete related preferences') + error: __('Failed to delete related preferences'), + default: false ), ] )); diff --git a/src/Cleaner/Settings.php b/src/Cleaner/Settings.php index 2fe63b1..051c0aa 100644 --- a/src/Cleaner/Settings.php +++ b/src/Cleaner/Settings.php @@ -48,7 +48,8 @@ class Settings extends CleanerParent select: __('delete selected global settings namespaces'), query: __('delete "%s" global settings namespace'), 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 new ActionDescriptor( @@ -56,7 +57,8 @@ class Settings extends CleanerParent select: __('delete selected blog settings namespaces'), query: __('delete "%s" blog settings namespace'), 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 new ActionDescriptor( @@ -64,14 +66,16 @@ class Settings extends CleanerParent select: __('delete selected settings namespaces'), query: __('delete "%s" settings namespace'), 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 new ActionDescriptor( id: 'delete_related', query: __('delete related settings'), success: __('related settings deleted'), - error: __('Failed to delete related settings') + error: __('Failed to delete related settings'), + default: false ), ] )); diff --git a/src/Cleaner/Tables.php b/src/Cleaner/Tables.php index 79d44a0..0697767 100644 --- a/src/Cleaner/Tables.php +++ b/src/Cleaner/Tables.php @@ -51,7 +51,8 @@ class Tables extends CleanerParent select: __('delete selected tables'), query: __('delete "%s" table'), success: __('"%s" table deleted'), - error: __('Failed to delete "%s" table') + error: __('Failed to delete "%s" table'), + default: false ), // truncate (empty) $ns database table new ActionDescriptor( @@ -59,7 +60,8 @@ class Tables extends CleanerParent select: __('empty selected tables'), query: __('empty "%s" table'), success: __('"%s" table emptied'), - error: __('Failed to empty "%s" table') + error: __('Failed to empty "%s" table'), + default: false ), ] )); diff --git a/src/Cleaner/Themes.php b/src/Cleaner/Themes.php index a9f65c5..3655aca 100644 --- a/src/Cleaner/Themes.php +++ b/src/Cleaner/Themes.php @@ -45,7 +45,8 @@ class Themes extends CleanerParent select: __('delete selected themes files and directories'), query: __('delete "%s" theme files and directories'), 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 ), ] )); diff --git a/src/Cleaner/Vars.php b/src/Cleaner/Vars.php index 40af05e..7f595c4 100644 --- a/src/Cleaner/Vars.php +++ b/src/Cleaner/Vars.php @@ -45,7 +45,8 @@ class Vars extends CleanerParent select: __('delete selected var directories'), query: __('delete "%s" var directory'), success: __('"%s" var directory deleted'), - error: __('Failed to delete "%s" var directory') + error: __('Failed to delete "%s" var directory'), + default: true ), ] )); diff --git a/src/Cleaner/Versions.php b/src/Cleaner/Versions.php index b84d40d..fed4799 100644 --- a/src/Cleaner/Versions.php +++ b/src/Cleaner/Versions.php @@ -44,7 +44,8 @@ class Versions extends CleanerParent select: __('delete selected versions numbers'), query: __('delete "%s" version number'), success: __('"%s" version number deleted'), - error: __('Failed to delete "%s" version number') + error: __('Failed to delete "%s" version number'), + default: true ), ] )); diff --git a/src/Manage.php b/src/Manage.php index 4f52f91..1bf043c 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -145,7 +145,7 @@ class Manage extends dcNsProcess foreach ($uninstaller->getUserActions($define->getId()) as $cleaner => $stack) { foreach ($stack as $action) { $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'), ]); } diff --git a/src/Uninstaller.php b/src/Uninstaller.php index fe43ac4..333a9e0 100644 --- a/src/Uninstaller.php +++ b/src/Uninstaller.php @@ -134,15 +134,18 @@ class Uninstaller * This method should be called from module Uninstall::proces() method. * User will be prompted before doing these actions. * - * @param string $cleaner The cleaner ID - * @param string $action The action ID - * @param string $ns Name of setting related to module + * Leave $default param to null to let Cleaner decide. + * + * @param string $cleaner The cleaner ID + * @param string $action The action ID + * @param string $ns Name of setting related to module + * @param null|null $default The default state of form field (checked) * * @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()])) { $this->user_actions[$this->module->getId()] = new ActionsStack(); } @@ -160,15 +163,18 @@ class Uninstaller * user will NOT be prompted before these actions execution. * Note: If module is disabled, direct actions are not executed. * - * @param string $cleaner The cleaner ID - * @param string $action The action ID - * @param string $ns Name of setting related to module. + * Leave $default param to null to let Cleaner decide. + * + * @param string $cleaner The cleaner ID + * @param string $action The action ID + * @param string $ns Name of setting related to module + * @param null|null $default The default state of form field (checked) * * @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()])) { $this->direct_actions[$this->module->getId()] = new ActionsStack(); } @@ -254,13 +260,14 @@ class Uninstaller /** * Add a predefined action to unsintall features. * - * @param string $cleaner The cleaner ID - * @param string $action The action ID - * @param string $ns Name of setting related to module. + * @param string $cleaner The cleaner ID + * @param string $action The action ID + * @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 */ - 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 if (null === $this->module @@ -279,6 +286,7 @@ class Uninstaller query: sprintf($this->cleaners->get($cleaner)->actions[$action]->query, $ns), success: sprintf($this->cleaners->get($cleaner)->actions[$action]->success, $ns), error: sprintf($this->cleaners->get($cleaner)->actions[$action]->error, $ns), + default: is_null($default) ? $this->cleaners->get($cleaner)->actions[$action]->default : $default ); } }