From 15eb8f7676f7a478c42f6e7c280a1c13da9f4f3d Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Tue, 24 Oct 2023 22:55:12 +0200 Subject: [PATCH] Code review --- CHANGELOG.md | 6 + README.md | 7 +- _define.php | 2 +- dcstore.xml | 4 +- src/ActivityReport.php | 67 ++++++-- src/ActivityReportAction.php | 3 + src/Backend.php | 160 ++---------------- src/BackendBehaviors.php | 181 +++++++++++++++++++++ src/Config.php | 4 +- src/Frontend.php | 26 ++- src/{Context.php => FrontendContext.php} | 2 +- src/{Template.php => FrontendTemplate.php} | 32 ++-- src/{UrlHandler.php => FrontendUrl.php} | 2 +- src/Manage.php | 10 +- src/ManageList.php | 140 ++++++++++------ src/Prepend.php | 2 +- 16 files changed, 392 insertions(+), 256 deletions(-) create mode 100644 src/BackendBehaviors.php rename src/{Context.php => FrontendContext.php} (99%) rename src/{Template.php => FrontendTemplate.php} (81%) rename src/{UrlHandler.php => FrontendUrl.php} (98%) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf1a926..bb3eae2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +activityReport 3.4.1 - 2023.10.24 +=========================================================== +* Require Dotclear 2.28 +* Require PHP 8.1 +* Code review + activityReport 3.4 - 2023.10.18 =========================================================== * Require Dotclear 2.28 diff --git a/README.md b/README.md index 4291d41..b2e62fd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # README -[![Release](https://img.shields.io/badge/release-3.4-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/activityReport/releases) -![Date](https://img.shields.io/badge/date-2023.10.18-c44d58.svg) +[![Release](https://img.shields.io/badge/release-3.4.1-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/activityReport/releases) +![Date](https://img.shields.io/badge/date-2023.10.24-c44d58.svg) [![Dotclear](https://img.shields.io/badge/dotclear-v2.28-137bbb.svg)](https://fr.dotclear.org/download) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-9ac123.svg)](https://plugins.dotaddict.org/dc2/details/activityReport) [![License](https://img.shields.io/badge/license-GPL--2.0-ececec.svg)](https://git.dotclear.watch/JcDenis/activityReport/src/branch/master/LICENSE) @@ -17,7 +17,7 @@ _activityReport_ is a plugin for the open-source web publishing software called * Dotclear 2.28 * PHP 8.1+ * System permissions to add table on database -* System permissions to send email +* System permissions to send email (optionnal) * Dotclear admin permissions ## USAGE @@ -37,6 +37,7 @@ Once it's done you can manage your reports from menu * [Packages & details](https://git.dotclear.watch/JcDenis/activityReport/releases) (or on [Dotaddict](https://plugins.dotaddict.org/dc2/details/activityReport)) * [Sources & contributions](https://git.dotclear.watch/JcDenis/activityReport) (or on [GitHub](https://github.com/JcDenis/activityReport)) * [Issues & security](https://git.dotclear.watch/JcDenis/activityReport/issues) (or on [GitHub](https://github.com/JcDenis/activityReport/issues)) +* [Discuss & help](https://forum.dotclear.org/viewtopic.php?id=40504) ## CONTRIBUTORS diff --git a/_define.php b/_define.php index 7726e0a..69c7930 100644 --- a/_define.php +++ b/_define.php @@ -17,7 +17,7 @@ $this->registerModule( 'Activity log', 'Log and receive your blog activity by email, feed, or on dashboard', 'Jean-Christian Denis and contributors', - '3.4', + '3.4.1', [ 'requires' => [['core', '2.28']], 'permissions' => 'My', diff --git a/dcstore.xml b/dcstore.xml index e698f08..b55bcbe 100644 --- a/dcstore.xml +++ b/dcstore.xml @@ -2,10 +2,10 @@ Activity log - 3.4 + 3.4.1 Jean-Christian Denis and contributors Log and receive your blog activity by email, feed, or on dashboard - https://git.dotclear.watch/JcDenis/activityReport/releases/download/v3.4/plugin-activityReport.zip + https://git.dotclear.watch/JcDenis/activityReport/releases/download/v3.4.1/plugin-activityReport.zip 2.28 https://git.dotclear.watch/JcDenis/activityReport/src/branch/master/README.md https://git.dotclear.watch/JcDenis/activityReport/issues diff --git a/src/ActivityReport.php b/src/ActivityReport.php index 48dab77..04155a8 100644 --- a/src/ActivityReport.php +++ b/src/ActivityReport.php @@ -41,28 +41,60 @@ class ActivityReport */ public const ACTIVITYREPORT_CLASS_NAME = 'ActivityReportAction'; - /** @var int activity marked as pending mail report */ + /** + * Activity marked as pending mail report. + * + * @var int STATUS_PENDING + */ public const STATUS_PENDING = 0; - /** @var int activity marked as reported by mail */ + /** + * Activity marked as reported by mail. + * + * @var int STATUS_REPORTED + */ public const STATUS_REPORTED = 1; - /** @var string $type Activity report type (by default activityReport) */ + /** + * Activity report type (by default activityReport). + * + * @var string $type + */ public readonly string $type; - /** @var Settings $settings Activity report settings for current blog */ + /** + * Activity report settings for current blog. + * + * @var Settings $settings + */ public readonly Settings $settings; - /** @var Groups $groups Groups of actions */ + /** + * Groups of actions. + * + * @var Groups $groups + */ public readonly Groups $groups; - /** @var Formats $formats Export available formats */ + /** + * Export available formats. + * + * @var Formats $formats + */ public readonly Formats $formats; - /** @var ActivityReport $instance ActivityReport instance */ + /** + * ActivityReport instance. + * + * @var ActivityReport $instance + */ private static $instance; - /** @var null|string $lock File lock for update */ + /** + * File lock for update. + * + * @var null|string $lock + */ private static $lock = null; /** @@ -115,13 +147,13 @@ class ActivityReport /** * Get logs record. * - * @param null|ArrayObject $params The query params - * @param bool $count_only Count only - * @param null|SelectStatement $ext_sql The sql select statement + * @param null|ArrayObject $params The query params + * @param bool $count_only Count only + * @param null|SelectStatement $ext_sql The sql select statement * - * @return null|MetaRecord The logs record + * @return MetaRecord The logs record */ - public function getLogs(ArrayObject $params = null, bool $count_only = false, ?SelectStatement $ext_sql = null): ?MetaRecord + public function getLogs(ArrayObject $params = null, bool $count_only = false, ?SelectStatement $ext_sql = null): MetaRecord { if (is_null($params)) { $params = new ArrayObject(); @@ -179,7 +211,7 @@ class ActivityReport $sql->where('E.activity_type = ' . $sql->quote($this->type)); } - if (isset($params['blog_id']) && is_null($params['blog_id'])) { + if (isset($params['blog_id']) && $params['blog_id'] != '') { $sql->and('E.blog_id IS NOT NULL'); } elseif (!empty($params['blog_id'])) { if (!is_array($params['blog_id'])) { @@ -243,9 +275,10 @@ class ActivityReport if (!$count_only && !empty($params['limit'])) { $sql->limit($params['limit']); } + $rs = $sql->select(); - return $sql->select(); + return is_null($rs) ? MetaRecord::newFromArray([]) : $rs; } /** @@ -253,7 +286,7 @@ class ActivityReport * * @param string $group The group * @param string $action The action - * @param array $logs The logs values + * @param array $logs The logs values */ public function addLog(string $group, string $action, array $logs): void { @@ -616,7 +649,7 @@ class ActivityReport ]); $logs = $this->getLogs($params); - if (!is_null($logs) && !$logs->isEmpty()) { + if (!$logs->isEmpty()) { // Datas to readable text $content = $this->parseLogs($logs); if (!empty($content)) { diff --git a/src/ActivityReportAction.php b/src/ActivityReportAction.php index 8d9c3d5..8b2b9d1 100644 --- a/src/ActivityReportAction.php +++ b/src/ActivityReportAction.php @@ -330,6 +330,9 @@ class ActivityReportAction extends Process ActivityReport::instance()->addLog('post', 'delete', $logs); } + /** + * @param ArrayObject $result + */ public static function postPasswordAttempt(ArrayObject $result): void { if ($result['tpl'] != 'password-form.html' || empty($_POST['password'])) { diff --git a/src/Backend.php b/src/Backend.php index a304737..2f2c820 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -4,18 +4,8 @@ declare(strict_types=1); namespace Dotclear\Plugin\activityReport; -use ArrayObject; use Dotclear\App; -use Dotclear\Core\Backend\Favorites; use Dotclear\Core\Process; -use Dotclear\Helper\Date; -use Dotclear\Helper\Html\Form\{ - Div, - Label, - Para, - Select, - Text -}; /** * @brief activityReport backend class. @@ -33,142 +23,22 @@ class Backend extends Process public static function process(): bool { - if (!self::status()) { - return false; + if (self::status()) { + // be sure to init report + ActivityReport::init(); + + My::addBackendMenuItem(); + + App::behavior()->addBehaviors([ + 'adminDashboardFavoritesV2' => BackendBehaviors::adminDashboardFavoritesV2(...), + 'adminDashboardContentsV2' => BackendBehaviors::adminDashboardContentsV2(...), + 'adminDashboardOptionsFormV2' => BackendBehaviors::adminDashboardOptionsFormV2(...), + 'adminAfterDashboardOptionsUpdate' => BackendBehaviors::adminAfterDashboardOptionsUpdate(...), + 'adminFiltersListsV2' => BackendBehaviors::adminFiltersListsV2(...), + 'adminColumnsListsV2' => BackendBehaviors::adminColumnsListsV2(...), + ]); } - // be sure to init report - ActivityReport::init(); - - My::addBackendMenuItem(); - - App::behavior()->addBehaviors([ - // dashboard favorites icon - 'adminDashboardFavoritesV2' => function (Favorites $favs): void { - $favs->register(My::id(), [ - 'title' => My::name(), - 'url' => My::manageUrl(), - 'small-icon' => My::icons(), - 'large-icon' => My::icons(), - 'permissions' => App::auth()->makePermissions([ - App::auth()::PERMISSION_ADMIN, - ]), - ]); - }, - // dashboard content display - 'adminDashboardContentsV2' => function (ArrayObject $items): void { - $db = App::auth()->prefs()->get(My::id())->get('dashboard_item'); - $limit = abs(is_numeric($db) ? (int) $db : 0); - if (!$limit) { - return ; - } - - $params = new ArrayObject([ - 'limit' => $limit, - 'requests' => true, - ]); - $rs = ActivityReport::instance()->getLogs($params); - if (!$rs || $rs->isEmpty()) { - return; - } - - $lines = []; - $groups = ActivityReport::instance()->groups; - while ($rs->fetch()) { - $row = new ActivityRow($rs); - if (!$groups->has($row->group)) { - continue; - } - $group = $groups->get($row->group); - $lines[] = '
' . - '' . __($group->get($row->action)->title) . '' . - '
' . Date::str( - App::blog()->settings()->get('system')->get('date_format') . ', ' . App::blog()->settings()->get('system')->get('time_format'), - (int) strtotime($row->dt), - is_string(App::auth()->getInfo('user_tz')) ? App::auth()->getInfo('user_tz') : 'UTC' - ) . '
' . - '

' . - '' . ActivityReport::parseMessage(__($group->get($row->action)->message), $row->logs) . '

'; - } - if (empty($lines)) { - return ; - } - - $items[] = new ArrayObject([ - '
' . - '

' . My::name() . '

' . - '
' . implode('', $lines) . '
' . - '

' . - __('View all logs') . ' - ' . - __('Configure plugin') . '

' . - '
', - ]); - }, - // dashboard content user preference form - 'adminDashboardOptionsFormV2' => function (): void { - $db = App::auth()->prefs()->get(My::id())->get('dashboard_item'); - echo - (new Div())->class('fieldset')->items([ - (new Text('h4', My::name())), - (new Para())->items([ - (new Label(__('Number of activities to show on dashboard:'), Label::OUTSIDE_LABEL_BEFORE))->for(My::id() . '_dashboard_item'), - (new Select(My::id() . '_dashboard_item'))->default(is_string($db) ? $db : '')->items([ - __('Do not show activity report') => 0, - 5 => 5, - 10 => 10, - 15 => 15, - 20 => 20, - 50 => 50, - 100 => 100, - ]), - ]), - ])->render(); - }, - // save dashboard content user preference - 'adminAfterDashboardOptionsUpdate' => function (?string $user_id = null): void { - if (!is_null($user_id)) { - App::auth()->prefs()->get(My::id())->put( - 'dashboard_item', - (int) $_POST[My::id() . '_dashboard_item'], - 'integer' - ); - } - }, - // list filters - 'adminFiltersListsV2' => function (ArrayObject $sorts): void { - $sorts[My::id()] = [ - My::name(), - [ - __('Group') => 'activity_group', - __('Action') => 'activity_action', - __('Date') => 'activity_dt', - __('Status') => 'activity_status', - ], - 'activity_dt', - 'desc', - [__('logs per page'), 30], - ]; - }, - // list columns user preference - 'adminColumnsListsV2' => function (ArrayObject $cols): void { - $cols[My::id()] = [ - My::name(), - [ - 'activity_group' => [true, __('Group')], - 'activity_action' => [true, __('Action')], - 'activity_dt' => [true, __('Date')], - 'activity_status' => [false, __('Status')], - ], - ]; - }, - ]); - - return true; + return self::status(); } } diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php new file mode 100644 index 0000000..138bfe0 --- /dev/null +++ b/src/BackendBehaviors.php @@ -0,0 +1,181 @@ +register(My::id(), [ + 'title' => My::name(), + 'url' => My::manageUrl(), + 'small-icon' => My::icons(), + 'large-icon' => My::icons(), + 'permissions' => App::auth()->makePermissions([ + App::auth()::PERMISSION_ADMIN, + ]), + ]); + } + + /** + * Dashboard content display. + * + * @param ArrayObject $items + */ + public static function adminDashboardContentsV2(ArrayObject $items): void + { + $db = App::auth()->prefs()->get(My::id())->get('dashboard_item'); + $limit = abs(is_numeric($db) ? (int) $db : 0); + if (!$limit) { + return ; + } + + $params = new ArrayObject([ + 'limit' => $limit, + 'requests' => true, + ]); + $rs = ActivityReport::instance()->getLogs($params); + if ($rs->isEmpty()) { + return; + } + + $lines = []; + $groups = ActivityReport::instance()->groups; + while ($rs->fetch()) { + $row = new ActivityRow($rs); + if (!$groups->has($row->group)) { + continue; + } + $group = $groups->get($row->group); + $lines[] = '
' . + '' . __($group->get($row->action)->title) . '' . + '
' . Date::str( + App::blog()->settings()->get('system')->get('date_format') . ', ' . App::blog()->settings()->get('system')->get('time_format'), + (int) strtotime($row->dt), + is_string(App::auth()->getInfo('user_tz')) ? App::auth()->getInfo('user_tz') : 'UTC' + ) . '
' . + '

' . + '' . ActivityReport::parseMessage(__($group->get($row->action)->message), $row->logs) . '

'; + } + if (empty($lines)) { + return ; + } + + $items[] = new ArrayObject([ + '
' . + '

' . My::name() . '

' . + '
' . implode('', $lines) . '
' . + '

' . + __('View all logs') . ' - ' . + __('Configure plugin') . '

' . + '
', + ]); + } + + /** + * Dashboard content user preference form. + */ + public static function adminDashboardOptionsFormV2(): void + { + $db = App::auth()->prefs()->get(My::id())->get('dashboard_item'); + echo + (new Div())->class('fieldset')->items([ + (new Text('h4', My::name())), + (new Para())->items([ + (new Label(__('Number of activities to show on dashboard:'), Label::OUTSIDE_LABEL_BEFORE))->for(My::id() . '_dashboard_item'), + (new Select(My::id() . '_dashboard_item'))->default(is_string($db) ? $db : '')->items([ + __('Do not show activity report') => 0, + 5 => 5, + 10 => 10, + 15 => 15, + 20 => 20, + 50 => 50, + 100 => 100, + ]), + ]), + ])->render(); + } + + /** + * Save dashboard content user preference. + */ + public static function adminAfterDashboardOptionsUpdate(?string $user_id = null): void + { + if (!is_null($user_id)) { + App::auth()->prefs()->get(My::id())->put( + 'dashboard_item', + (int) $_POST[My::id() . '_dashboard_item'], + 'integer' + ); + } + } + + /** + * List filters. + * + * @param ArrayObject $sorts + */ + public static function adminFiltersListsV2(ArrayObject $sorts): void + { + $sorts[My::id()] = [ + My::name(), + [ + __('Group') => 'activity_group', + __('Action') => 'activity_action', + __('Date') => 'activity_dt', + __('Status') => 'activity_status', + ], + 'activity_dt', + 'desc', + [__('logs per page'), 30], + ]; + } + + /** + * List columns user preference. + * + * @param ArrayObject $cols + */ + public static function adminColumnsListsV2(ArrayObject $cols): void + { + $cols[My::id()] = [ + My::name(), + [ + 'activity_group' => [true, __('Group')], + 'activity_action' => [true, __('Action')], + 'activity_dt' => [true, __('Date')], + 'activity_status' => [false, __('Status')], + ], + ]; + } +} diff --git a/src/Config.php b/src/Config.php index a25fa5a..74704ce 100644 --- a/src/Config.php +++ b/src/Config.php @@ -131,13 +131,13 @@ class Config extends Process ]), (new Para())->items([ (new Label(__('Recipients:'), Label::OUTSIDE_LABEL_BEFORE))->for('mailinglist'), - (new Input('mailinglist'))->size(60)->maxlenght(255)->value(implode(';', $s->mailinglist)), + (new Input('mailinglist'))->size(60)->maxlength(255)->value(implode(';', $s->mailinglist)), ]), (new Note())->class('form-note')->text(__('Separate multiple email addresses with a semicolon ";"')), (new Note())->class('form-note')->text(__('Leave it empty to disable mail report.')), (new Para())->items([ (new Label(__('Date format:'), Label::OUTSIDE_LABEL_BEFORE))->for('dateformat'), - (new Input('dateformat'))->size(60)->maxlenght(255)->value($s->dateformat), + (new Input('dateformat'))->size(60)->maxlength(255)->value($s->dateformat), ]), (new Note())->class('form-note')->text(__('Use Dotclear date formaters. ex: %B %d at %H:%M')), (new Para())->items([ diff --git a/src/Frontend.php b/src/Frontend.php index 1b1eefc..39c7ec5 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -23,21 +23,19 @@ class Frontend extends Process public static function process(): bool { - if (!self::status()) { - return false; + if (self::status()) { + // be sure to init report + ActivityReport::init(); + + $tpl = App::frontend()->template(); + $tpl->setPath($tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::path(), 'default-templates', 'tpl'])); + $tpl->addBlock('activityReports', FrontendTemplate::activityReports(...)); + $tpl->addValue('activityReportFeedID', FrontendTemplate::activityReportFeedID(...)); + $tpl->addValue('activityReportTitle', FrontendTemplate::activityReportTitle(...)); + $tpl->addValue('activityReportDate', FrontendTemplate::activityReportDate(...)); + $tpl->addValue('activityReportContent', FrontendTemplate::activityReportContent(...)); } - // be sure to init report - ActivityReport::init(); - - App::frontend()->template()->setPath(App::frontend()->template()->getPath(), implode(DIRECTORY_SEPARATOR, [My::path(), 'default-templates', 'tpl'])); - - App::frontend()->template()->addBlock('activityReports', Template::activityReports(...)); - App::frontend()->template()->addValue('activityReportFeedID', Template::activityReportFeedID(...)); - App::frontend()->template()->addValue('activityReportTitle', Template::activityReportTitle(...)); - App::frontend()->template()->addValue('activityReportDate', Template::activityReportDate(...)); - App::frontend()->template()->addValue('activityReportContent', Template::activityReportContent(...)); - - return true; + return self::status(); } } diff --git a/src/Context.php b/src/FrontendContext.php similarity index 99% rename from src/Context.php rename to src/FrontendContext.php index 2edac4c..f578bf7 100644 --- a/src/Context.php +++ b/src/FrontendContext.php @@ -14,7 +14,7 @@ use Dotclear\Database\MetaRecord; * @author Jean-Christian Denis (author) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class Context +class FrontendContext { /** * Parse title. diff --git a/src/Template.php b/src/FrontendTemplate.php similarity index 81% rename from src/Template.php rename to src/FrontendTemplate.php index b399e03..1086058 100644 --- a/src/Template.php +++ b/src/FrontendTemplate.php @@ -15,7 +15,7 @@ use Dotclear\Helper\Date; * @author Jean-Christian Denis (author) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class Template +class FrontendTemplate { /** * tpl:activityReports [attributes] : Activity report logs (tpl block) @@ -25,10 +25,10 @@ class Template * - lastn integer Limit to last n logs * - ingnore_pagination 1|0 Ignore pagination paramaters * - * @param ArrayObject $attr The attributes - * @param string $content The content + * @param ArrayObject $attr The attributes + * @param string $content The content * - * @return string The code + * @return string The code */ public static function activityReports(ArrayObject $attr, string $content): string { @@ -37,7 +37,7 @@ class Template $lastn = abs((int) $attr['lastn']) + 0; } - $p = '$_page_number = App::frontend()->getPageNumber(); if ($_page_number < 1) { $_page_number = 1; }' . "\n\$params = new ArrayObject();\n"; + $p = '$page_number = App::frontend()->getPageNumber(); if ($page_number < 1) { $page_number = 1; }' . "\n\$params = new ArrayObject();\n"; if ($lastn > 0) { $p .= "\$params['limit'] = " . $lastn . ";\n"; @@ -46,7 +46,7 @@ class Template } if (!isset($attr['ignore_pagination']) || $attr['ignore_pagination'] == '0') { - $p .= "\$params['limit'] = array(((\$_page_number-1)*\$params['limit']),\$params['limit']);\n"; + $p .= "\$params['limit'] = array(((\$page_number-1)*\$params['limit']),\$params['limit']);\n"; } else { $p .= "\$params['limit'] = array(0, \$params['limit']);\n"; } @@ -68,9 +68,9 @@ class Template * * - any filters See self::getFilters() * - * @param ArrayObject $attr The attributes + * @param ArrayObject $attr The attributes * - * @return string The code + * @return string The code */ public static function activityReportFeedID(ArrayObject $attr): string { @@ -87,15 +87,15 @@ class Template * * - any filters See self::getFilters() * - * @param ArrayObject $attr The attributes + * @param ArrayObject $attr The attributes * - * @return string The code + * @return string The code */ public static function activityReportTitle(ArrayObject $attr): string { $f = App::frontend()->template()->getFilters($attr); - return ''; + return ''; } /** @@ -105,15 +105,15 @@ class Template * * - any filters See self::getFilters() * - * @param ArrayObject $attr The attributes + * @param ArrayObject $attr The attributes * - * @return string The code + * @return string The code */ public static function activityReportContent(ArrayObject $attr): string { $f = App::frontend()->template()->getFilters($attr); - return ''; + return ''; } /** @@ -126,9 +126,9 @@ class Template * - rfc822 (1|0) Use Date::rfc822() * - any filters See self::getFilters() * - * @param ArrayObject $attr The attributes + * @param ArrayObject $attr The attributes * - * @return string The code + * @return string The code */ public static function activityReportDate(ArrayObject $attr): string { diff --git a/src/UrlHandler.php b/src/FrontendUrl.php similarity index 98% rename from src/UrlHandler.php rename to src/FrontendUrl.php index bcd5f51..725238c 100644 --- a/src/UrlHandler.php +++ b/src/FrontendUrl.php @@ -14,7 +14,7 @@ use Dotclear\Core\Frontend\Url; * @author Jean-Christian Denis (author) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class UrlHandler extends Url +class FrontendUrl extends Url { /** * Get activity logs feed. diff --git a/src/Manage.php b/src/Manage.php index 725b782..2554cc6 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -6,7 +6,10 @@ namespace Dotclear\Plugin\activityReport; use ArrayObject; use Dotclear\App; -use Dotclear\Core\Backend\Filter\Filters; +use Dotclear\Core\Backend\Filter\{ + Filters, + FiltersLibrary +}; use Dotclear\Core\Backend\{ Notices, Page @@ -62,14 +65,13 @@ class Manage extends Process $logs = $counter = $list = null; $filter = new Filters(My::id()); + $filter->add(FiltersLibrary::getPageFilter()); $params = new ArrayObject($filter->params()); try { $logs = ActivityReport::instance()->getLogs($params); $counter = ActivityReport::instance()->getLogs($params, true); - if (!is_null($logs) && !is_null($counter)) { - $list = new ManageList($logs, $counter->f(0)); - } + $list = new ManageList($logs, $counter->f(0)); } catch (Exception $e) { App::error()->add($e->getMessage()); } diff --git a/src/ManageList.php b/src/ManageList.php index e36848a..fe6284d 100644 --- a/src/ManageList.php +++ b/src/ManageList.php @@ -12,6 +12,18 @@ use Dotclear\Core\Backend\Listing\{ Pager }; use Dotclear\Helper\Date; +use Dotclear\Helper\Html\Form\{ + Caption, + Div, + Note, + Table, + Tbody, + Td, + Text, + Th, + Tr +}; +use Dotclear\Helper\Html\Html; /** * @brief activityReport manage logs list class. @@ -25,50 +37,71 @@ class ManageList extends Listing public function logsDisplay(Filters $filter, string $enclose_block = ''): void { if ($this->rs->isEmpty()) { - if ($filter->show()) { - echo '

' . __('No log matches the filter') . '

'; - } else { - echo '

' . __('No log') . '

'; - } - } else { - $page = $filter->value('page'); - $nbpp = $filter->value('nb'); - $pager = new Pager(is_numeric($page) ? (int) $page : 1, (int) $this->rs_count, is_numeric($nbpp) ? (int) $nbpp : 20, 10); - $pager->var_page = 'page'; + echo (new Note()) + ->text($filter->show() ? __('No log matches the filter') : __('No log')) + ->class('info') + ->render(); - $html_block = '
'; - - $cols = new ArrayObject([ - 'activity_group' => '', - 'activity_action' => '', - 'activity_logs' => '', - 'activity_date' => '', - 'activity_status' => '', - ]); - - $this->userColumns(My::id(), $cols); - - $html_block .= '' . implode(iterator_to_array($cols)) . '%s
' . ( - $filter->show() ? - sprintf(__('List of %s logs matching the filter.'), $this->rs_count) : - sprintf(__('List of %s logs.'), $this->rs_count) - ) . '
' . __('Group') . '' . __('Action') . '' . __('Message') . '' . __('Date') . '' . __('Status') . '
%s
'; - if ($enclose_block) { - $html_block = sprintf($enclose_block, $html_block); - } - $blocks = explode('%s', $html_block); - - echo $pager->getLinks() . $blocks[0]; - - while ($this->rs->fetch()) { - echo $this->logsLine(); - } - - echo $blocks[1] . $blocks[2] . $pager->getLinks(); + return; } + + $page = is_numeric($filter->value('page')) ? (int) $filter->value('page') : 1; + $nbpp = is_numeric($filter->value('nb')) ? (int) $filter->value('nb') : 20; + $count = (int) $this->rs_count; + $pager = new Pager($page, $count, $nbpp, 10); + $pager->var_page = 'page'; + + $cols = new ArrayObject([ + 'activity_group' => (new Th()) + ->text(__('Group')) + ->scope('col'), + 'activity_action' => (new Th()) + ->text(__('Action')) + ->scope('col'), + 'activity_logs' => (new Th()) + ->text(__('Message')) + ->scope('col'), + 'activity_date' => (new Th()) + ->text(__('Date')) + ->scope('col'), + 'activity_status' => (new Th()) + ->text(__('Status')) + ->scope('col'), + ]); + + $this->userColumns(My::id(), $cols); + + $lines = []; + while ($this->rs->fetch()) { + $lines[] = $this->line(); + } + + echo + $pager->getLinks() . + sprintf( + $enclose_block, + (new Div()) + ->class('table-outer') + ->items([ + (new Table()) + ->items([ + (new Caption( + $filter->show() ? + sprintf(__('List of %s logs matching the filter.'), $count) : + sprintf(__('List of logs. (%s)'), $count) + )), + (new Tr()) + ->items(iterator_to_array($cols)), + (new Tbody()) + ->items($lines), + ]), + ]) + ->render() + ) . + $pager->getLinks(); } - private function logsLine(): string + private function line(): Tr { $row = new ActivityRow($this->rs); @@ -84,18 +117,27 @@ class ManageList extends Listing $status = $row->status == ActivityReport::STATUS_PENDING ? __('pending') : __('reported'); $cols = new ArrayObject([ - 'activity_group' => '' . __($group->title) . '', - 'activity_action' => '' . __($action->title) . '', - 'activity_logs' => '' . $message . '', - 'activity_date' => '' . $date . '', - 'activity_status' => '' . $status . '', + 'activity_group' => (new Td()) + ->text(Html::escapeHTML(__($group->title))) + ->class('nowrap'), + 'activity_action' => (new Td()) + ->text(Html::escapeHTML(__($action->title))) + ->class('nowrap'), + 'activity_logs' => (new Td()) + ->text(Html::escapeHTML($message)) + ->class('maximal'), + 'activity_date' => (new Td()) + ->text(Html::escapeHTML($date)) + ->class('nowrap'), + 'activity_status' => (new Td()) + ->text(Html::escapeHTML($status)) + ->class('nowrap'), ]); $this->userColumns(My::id(), $cols); - return - '' . - implode(iterator_to_array($cols)) . - ''; + return (new Tr('l' . $row->id)) + ->class('line' . $offline) + ->items(iterator_to_array($cols)); } } diff --git a/src/Prepend.php b/src/Prepend.php index f0630a0..64f0c97 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -32,7 +32,7 @@ class Prepend extends Process My::id(), 'reports', '^reports/((atom|rss2)/(.+))$', - UrlHandler::feed(...) + FrontendUrl::feed(...) ); return true;