From 6ddfc37487f072cbc6e93b84b400f018f692d11e Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Fri, 12 May 2023 01:23:16 +0200 Subject: [PATCH] add activity record row class (type hinting) --- src/ActivityReport.php | 20 ++++++------ src/ActivityRow.php | 71 ++++++++++++++++++++++++++++++++++++++++++ src/Backend.php | 47 +++++++++++++++------------- src/ManageList.php | 17 +++++----- 4 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 src/ActivityRow.php diff --git a/src/ActivityReport.php b/src/ActivityReport.php index 09758c1..ead177b 100644 --- a/src/ActivityReport.php +++ b/src/ActivityReport.php @@ -303,21 +303,22 @@ class ActivityReport { $from = time(); $to = 0; - $res = $blog = $group = ''; + $res = $blog_name = $blog_url = $group = ''; $tz = dcCore::app()->blog?->settings->get('system')->get('blog_timezone'); $dt = empty($this->settings->dateformat) ? '%Y-%m-%d %H:%M:%S' : $this->settings->dateformat; $format = $this->formats->get($this->formats->has($this->settings->mailformat) ? $this->settings->mailformat : 'plain'); $group_open = false; while ($rs->fetch()) { - if ($this->groups->has($rs->f('activity_group')) && $this->groups->get($rs->f('activity_group'))->has($rs->f('activity_action'))) { + $row = new ActivityRow($rs); + if ($this->groups->has($row->group) && $this->groups->get($row->group)->has($row->action)) { // Type - if ($rs->f('activity_group') != $group) { + if ($row->group != $group) { if ($group_open) { $res .= $format->group_close; } - $group = $rs->f('activity_group'); + $group = $row->group; $res .= str_replace( '%TEXT%', @@ -329,12 +330,11 @@ class ActivityReport } // Action - $time = strtotime($rs->f('activity_dt')); - $data = json_decode($rs->f('activity_logs'), true); + $time = strtotime($row->dt); $res .= str_replace( ['%TIME%', '%TEXT%'], - [Date::str($dt, (int) $time, $tz), vsprintf(__($this->groups->get($group)->get($rs->f('activity_action'))->message), $data)], + [Date::str($dt, (int) $time, $tz), vsprintf(__($this->groups->get($group)->get($row->action)->message), $row->logs)], $format->action ); @@ -346,6 +346,8 @@ class ActivityReport $to = $time; } } + $blog_name = $row->blog_name; + $blog_url = $row->blog_url; } if ($group_open) { @@ -372,8 +374,8 @@ class ActivityReport $format->info ); - $period .= str_replace('%TEXT%', $rs->f('blog_name'), $format->info); - $period .= str_replace('%TEXT%', $rs->f('blog_url'), $format->info); + $period .= str_replace('%TEXT%', $blog_name, $format->info); + $period .= str_replace('%TEXT%', $blog_url, $format->info); $period .= str_replace( '%TEXT%', diff --git a/src/ActivityRow.php b/src/ActivityRow.php new file mode 100644 index 0000000..d4cb196 --- /dev/null +++ b/src/ActivityRow.php @@ -0,0 +1,71 @@ + The log data */ + public readonly array $logs; + + /** + * Constructor sets properties. + * + * @param MetaRecord $rs The record + */ + public function __construct( + public readonly MetaRecord $rs + ) { + $this->id = is_numeric($this->rs->f('activity_id')) ? (int) $this->rs->f('activity_id') : 0; + $this->blog_id = is_string($this->rs->f('blog_id')) ? $this->rs->f('blog_id') : null; + $this->blog_url = is_string($this->rs->f('blog_url')) ? $this->rs->f('blog_url') : ''; + $this->blog_name = is_string($this->rs->f('blog_name')) ? $this->rs->f('blog_name') : ''; + $this->group = is_string($this->rs->f('activity_group')) ? $this->rs->f('activity_group') : ''; + $this->action = is_string($this->rs->f('activity_action')) ? $this->rs->f('activity_action') : ''; + $this->dt = is_string($this->rs->f('activity_dt')) ? $this->rs->f('activity_dt') : ''; + $this->status = is_numeric($this->rs->f('activity_status')) ? (int) $this->rs->f('activity_status') : 0; + + $logs = json_decode(is_string($this->rs->f('activity_logs')) ? $this->rs->f('activity_logs') : '', true); + $this->logs = is_array($logs) ? $logs : []; + } +} diff --git a/src/Backend.php b/src/Backend.php index 850b2b6..8ef3d49 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -19,6 +19,7 @@ use dcAdmin; use dcAuth; use dcCore; use dcFavorites; +use dcMenu; use dcNsProcess; use dcPage; use Dotclear\Helper\Date; @@ -50,18 +51,20 @@ class Backend extends dcNsProcess return false; } - dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( - My::name(), - dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), - dcPage::getPF(My::id() . '/icon.svg'), - preg_match( - '/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/', - $_SERVER['REQUEST_URI'] - ), - dcCore::app()->auth?->check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_ADMIN, - ]), dcCore::app()->blog?->id) - ); + if ((dcCore::app()->menu[dcAdmin::MENU_PLUGINS] instanceof dcMenu)) { + dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( + My::name(), + (string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), + dcPage::getPF(My::id() . '/icon.svg'), + preg_match( + '/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/', + $_SERVER['REQUEST_URI'] + ), + (bool) dcCore::app()->auth?->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_ADMIN, + ]), dcCore::app()->blog?->id) + ); + } dcCore::app()->addBehaviors([ // dashboard favorites icon @@ -78,7 +81,8 @@ class Backend extends dcNsProcess }, // dashboard content display 'adminDashboardContentsV2' => function (ArrayObject $items): void { - $limit = abs((int) dcCore::app()->auth?->user_prefs?->get(My::id())->get('dashboard_item')); + $db = dcCore::app()->auth?->user_prefs?->get(My::id())->get('dashboard_item'); + $limit = abs(is_numeric($db) ? (int) $db : 0); if (!$limit) { return ; } @@ -95,20 +99,20 @@ class Backend extends dcNsProcess $lines = []; $groups = ActivityReport::instance()->groups; while ($rs->fetch()) { - if (!$groups->has($rs->f('activity_group'))) { + $row = new ActivityRow($rs); + if (!$groups->has($row->group)) { continue; } - $group = $groups->get($rs->f('activity_group')); - $data = json_decode($rs->f('activity_logs'), true); + $group = $groups->get($row->group); $lines[] = '
' . - '' . __($group->get($rs->f('activity_action'))->title) . '' . + '' . __($group->get($row->action)->title) . '' . '
' . Date::str( dcCore::app()->blog?->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog?->settings->get('system')->get('time_format'), - (int) strtotime($rs->f('activity_dt')), - dcCore::app()->auth?->getInfo('user_tz') + (int) strtotime($row->dt), + is_string(dcCore::app()->auth?->getInfo('user_tz')) ? dcCore::app()->auth->getInfo('user_tz') : 'UTC' ) . '
' . '

' . - '' . ActivityReport::parseMessage(__($group->get($rs->f('activity_action'))->message), $data) . '

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

'; } if (empty($lines)) { return ; @@ -132,12 +136,13 @@ class Backend extends dcNsProcess }, // dashboard content user preference form 'adminDashboardOptionsFormV2' => function (): void { + $db = dcCore::app()->auth?->user_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((string) dcCore::app()->auth?->user_prefs?->get(My::id())->get('dashboard_item'))->items([ + (new Select(My::id() . '_dashboard_item'))->default(is_string($db) ? $db : '')->items([ __('Do not show activity report') => 0, 5 => 5, 10 => 10, diff --git a/src/ManageList.php b/src/ManageList.php index 758b7ca..8257643 100644 --- a/src/ManageList.php +++ b/src/ManageList.php @@ -74,17 +74,18 @@ class ManageList extends adminGenericList private function logsLine(): string { - $offline = (int) $this->rs->f('activity_status') == ActivityReport::STATUS_REPORTED ? ' offline' : ''; - $group = ActivityReport::instance()->groups->get($this->rs->f('activity_group')); - $action = $group->get($this->rs->f('activity_action')); - $data = json_decode((string) $this->rs->f('activity_logs'), true); - $message = ActivityReport::parseMessage(__($action->message), is_array($data) ? $data : []); + $row = new ActivityRow($this->rs); + + $offline = $row->status == ActivityReport::STATUS_REPORTED ? ' offline' : ''; + $group = ActivityReport::instance()->groups->get($row->group); + $action = $group->get($row->action); + $message = ActivityReport::parseMessage(__($action->message), $row->logs); $date = Date::str( dcCore::app()->blog?->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog?->settings->get('system')->get('time_format'), - (int) strtotime((string) $this->rs->f('activity_dt')), + (int) strtotime($row->dt), is_string(dcCore::app()->auth?->getInfo('user_tz')) ? dcCore::app()->auth->getInfo('user_tz') : 'UTC' ); - $status = (int) $this->rs->f('activity_status') == ActivityReport::STATUS_PENDING ? __('pending') : __('reported'); + $status = $row->status == ActivityReport::STATUS_PENDING ? __('pending') : __('reported'); $cols = new ArrayObject([ 'activity_group' => '' . __($group->title) . '', @@ -97,7 +98,7 @@ class ManageList extends adminGenericList $this->userColumns(My::id(), $cols); return - '' . + '' . implode(iterator_to_array($cols)) . ''; }