From 5c20252fa9b215de7d0625e8150d9c8021cdb31a Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 19 Mar 2023 22:51:59 +0100 Subject: [PATCH] add container for admin vars (and fix phpstan lvl 8 warnings) --- src/Backend.php | 8 +++--- src/BackendList.php | 4 +-- src/Manage.php | 58 ++++++++++++++------------------------- src/ManageVars.php | 67 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 src/ManageVars.php diff --git a/src/Backend.php b/src/Backend.php index bedd1e1..8f479f0 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -43,10 +43,10 @@ class Backend extends dcNsProcess dcCore::app()->menu[dcAdmin::MENU_SYSTEM]->addItem( My::name(), - dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), dcPage::getPF(My::id() . '/icon.svg'), - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), - dcCore::app()->auth->isSuperAdmin() + preg_match('/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth?->isSuperAdmin() ); dcCore::app()->addBehaviors([ @@ -84,7 +84,7 @@ class Backend extends dcNsProcess 'adminDashboardFavoritesV2' => function (dcFavorites $favs): void { $favs->register(My::BACKEND_LIST_ID, [ 'title' => My::name(), - 'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + 'url' => dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), 'small-icon' => dcPage::getPF(My::id() . '/icon.svg'), 'large-icon' => dcPage::getPF(My::id() . '/icon.svg'), //'permissions' => null, diff --git a/src/BackendList.php b/src/BackendList.php index c35ef94..6597062 100644 --- a/src/BackendList.php +++ b/src/BackendList.php @@ -23,7 +23,7 @@ use html; class BackendList extends adminGenericListV2 { - public function display($page, $nb_per_page, $enclose_block = '', $filter = false) + public function display(int $page, int $nb_per_page, string $enclose_block = '', bool $filter = false): void { if ($this->rs->isEmpty()) { echo $filter ? @@ -73,7 +73,7 @@ class BackendList extends adminGenericListV2 } } - private function logLine($checked) + private function logLine(bool $checked): void { $cols = [ 'check' => '' . diff --git a/src/Manage.php b/src/Manage.php index 2367e70..db8a69e 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -14,8 +14,6 @@ declare(strict_types=1); namespace Dotclear\Plugin\dcLog; -use adminGenericFilterV2; -use dcAdminFilters; use dcCore; use dcNsProcess; use dcPage; @@ -42,41 +40,23 @@ class Manage extends dcNsProcess return false; } - dcCore::app()->admin->logs = dcCore::app()->admin->logs_list = dcCore::app()->admin->logs_filter = null; - - $entries = $_POST['entries'] ?? null; - $del_all_log = isset($_POST['del_all_logs']) ? true : false; + $current = ManageVars::init(); # Delete logs - if (isset($_POST['del_logs']) || isset($_POST['del_all_logs'])) { + if ($current->selected_logs || $current->all_logs) { try { - dcCore::app()->log->delLogs($entries, $del_all_log); + dcCore::app()->log->delLogs($current->entries, $current->all_logs); dcPage::addSuccessNotice( - $del_all_log ? + $current->all_logs ? __('All logs have been successfully deleted') : __('Selected logs have been successfully deleted') ); - dcCore::app()->adminurl->redirect('admin.plugin.' . My::id()); + dcCore::app()->adminurl?->redirect('admin.plugin.' . My::id()); } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); } } - dcCore::app()->admin->logs_filter = new adminGenericFilterV2('dcloglist'); - dcCore::app()->admin->logs_filter->add(dcAdminFilters::getPageFilter()); - dcCore::app()->admin->logs_filter->add(dcAdminFilters::getInputFilter('blog_id', __('Blog:'))); - dcCore::app()->admin->logs_filter->add(dcAdminFilters::getInputFilter('user_id', __('User:'))); - dcCore::app()->admin->logs_filter->add(dcAdminFilters::getInputFilter('log_table', __('Component:'))); - dcCore::app()->admin->logs_filter->add(dcAdminFilters::getInputFilter('log_ip', __('IP:'))); - $params = dcCore::app()->admin->logs_filter->params(); - - try { - dcCore::app()->admin->logs = dcCore::app()->log->getLogs($params); - dcCore::app()->admin->logs_list = new BackendList(dcCore::app()->admin->logs, dcCore::app()->admin->logs->count()); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - } - return true; } @@ -86,13 +66,15 @@ class Manage extends dcNsProcess return; } + $current = ManageVars::init(); + dcPage::openModule( __('Pings'), dcPage::jsJson('dclog_list', [ 'confirm_delete_selected_log' => __('Are you sure you want to delete selected logs?'), 'confirm_delete_all_log' => __('Are you sure you want to delete all logs?'), ]) . - dcCore::app()->admin->logs_filter->js(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . + $current->filter->js((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . dcPage::jsLoad(dcPage::getPF(My::id() . '/js/backend.js')) ); @@ -100,23 +82,23 @@ class Manage extends dcNsProcess dcPage::breadcrumb( [ __('System') => '', - My::name() => dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + My::name() => dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), ] ) . dcPage::notices(); - if (isset(dcCore::app()->admin->logs) && isset(dcCore::app()->admin->logs_list)) { - if (dcCore::app()->admin->logs->isEmpty() && !dcCore::app()->admin->logs_filter->show()) { + if ($current->logs !== null && $current->list != null) { + if ($current->logs->isEmpty() && !$current->filter->show()) { echo '

' . __('There are no logs') . '

'; } else { - dcCore::app()->admin->logs_filter->display( + $current->filter->display( 'admin.plugin.' . My::id(), form::hidden('p', My::id()) ); - dcCore::app()->admin->logs_list->display( - dcCore::app()->admin->logs_filter->__get('page'), - dcCore::app()->admin->logs_filter->__get('nb'), - '
' . + $current->list->display( + (int) $current->filter->__get('page'), + (int) $current->filter->__get('nb'), + '' . '%s' . @@ -124,15 +106,15 @@ class Manage extends dcNsProcess '

' . '

' . - ' ' . - '' . + ' ' . + '' . '

' . - dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.' . My::id(), dcCore::app()->admin->logs_filter->values()) . + dcCore::app()->adminurl?->getHiddenFormFields('admin.plugin.' . My::id(), $current->filter->values()) . dcCore::app()->formNonce() . '' . '
', - dcCore::app()->admin->logs_filter->show() + $current->filter->show() ); } } diff --git a/src/ManageVars.php b/src/ManageVars.php new file mode 100644 index 0000000..5975e81 --- /dev/null +++ b/src/ManageVars.php @@ -0,0 +1,67 @@ +entries = !empty($_POST['entries']) && is_array($_POST['entries']) ? $_POST['entries'] : []; + $this->all_logs = isset($_POST['all_logs']); + $this->selected_logs = isset($_POST['selected_logs']); + + $this->filter = new adminGenericFilterV2('dcloglist'); + $this->filter->add(dcAdminFilters::getPageFilter()); + $this->filter->add(dcAdminFilters::getInputFilter('blog_id', __('Blog:'))); + $this->filter->add(dcAdminFilters::getInputFilter('user_id', __('User:'))); + $this->filter->add(dcAdminFilters::getInputFilter('log_table', __('Component:'))); + $this->filter->add(dcAdminFilters::getInputFilter('log_ip', __('IP:'))); + $params = $this->filter->params(); + + try { + $this->logs = dcCore::app()->log->getLogs($params); + $this->list = new BackendList($this->logs, $this->logs->count()); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + } + + public static function init(): ManageVars + { + if (!(self::$container instanceof self)) { + self::$container = new self(); + } + + return self::$container; + } +}