add container for admin vars (and fix phpstan lvl 8 warnings)

This commit is contained in:
Jean-Christian Denis 2023-03-19 22:51:59 +01:00
parent cb52f33cd1
commit 5c20252fa9
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
4 changed files with 93 additions and 44 deletions

View file

@ -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,

View file

@ -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' => '<td class="nowrap minimal">' .

View file

@ -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 '<p>' . __('There are no logs') . '</p>';
} 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'),
'<form action="' . dcCore::app()->adminurl->get('admin.plugin.' . My::id()) . '" method="post" id="form-entries">' .
$current->list->display(
(int) $current->filter->__get('page'),
(int) $current->filter->__get('nb'),
'<form action="' . dcCore::app()->adminurl?->get('admin.plugin.' . My::id()) . '" method="post" id="form-entries">' .
'%s' .
@ -124,15 +106,15 @@ class Manage extends dcNsProcess
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' .
'<input type="submit" value="' . __('Delete selected logs') . '" name="del_logs" />&nbsp;' .
'<input type="submit" value="' . __('Delete all logs') . '" name="del_all_logs" />' .
'<input type="submit" value="' . __('Delete selected logs') . '" name="selected_logs" />&nbsp;' .
'<input type="submit" value="' . __('Delete all logs') . '" name="all_logs" />' .
'</p>' .
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() .
'</div>' .
'</form>',
dcCore::app()->admin->logs_filter->show()
$current->filter->show()
);
}
}

67
src/ManageVars.php Normal file
View file

@ -0,0 +1,67 @@
<?php
/**
* @brief dcLog, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Tomtom and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\dcLog;
use adminGenericFilterV2;
use dcAdminFilters;
use dcCore;
use dcRecord;
use Exception;
class ManageVars
{
/**
* @var ManageVars self instance
*/
private static $container;
public readonly adminGenericFilterV2 $filter;
public readonly ?dcRecord $logs;
public readonly ?BackendList $list;
public readonly array $entries;
public readonly bool $selected_logs;
public readonly bool $all_logs;
protected function __construct()
{
$this->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;
}
}