add activity record row class (type hinting)

This commit is contained in:
Jean-Christian Paul Denis 2023-05-12 01:23:16 +02:00
parent 4baa348e6a
commit 6ddfc37487
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
4 changed files with 117 additions and 38 deletions

View File

@ -303,21 +303,22 @@ class ActivityReport
{ {
$from = time(); $from = time();
$to = 0; $to = 0;
$res = $blog = $group = ''; $res = $blog_name = $blog_url = $group = '';
$tz = dcCore::app()->blog?->settings->get('system')->get('blog_timezone'); $tz = dcCore::app()->blog?->settings->get('system')->get('blog_timezone');
$dt = empty($this->settings->dateformat) ? '%Y-%m-%d %H:%M:%S' : $this->settings->dateformat; $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'); $format = $this->formats->get($this->formats->has($this->settings->mailformat) ? $this->settings->mailformat : 'plain');
$group_open = false; $group_open = false;
while ($rs->fetch()) { 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 // Type
if ($rs->f('activity_group') != $group) { if ($row->group != $group) {
if ($group_open) { if ($group_open) {
$res .= $format->group_close; $res .= $format->group_close;
} }
$group = $rs->f('activity_group'); $group = $row->group;
$res .= str_replace( $res .= str_replace(
'%TEXT%', '%TEXT%',
@ -329,12 +330,11 @@ class ActivityReport
} }
// Action // Action
$time = strtotime($rs->f('activity_dt')); $time = strtotime($row->dt);
$data = json_decode($rs->f('activity_logs'), true);
$res .= str_replace( $res .= str_replace(
['%TIME%', '%TEXT%'], ['%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 $format->action
); );
@ -346,6 +346,8 @@ class ActivityReport
$to = $time; $to = $time;
} }
} }
$blog_name = $row->blog_name;
$blog_url = $row->blog_url;
} }
if ($group_open) { if ($group_open) {
@ -372,8 +374,8 @@ class ActivityReport
$format->info $format->info
); );
$period .= str_replace('%TEXT%', $rs->f('blog_name'), $format->info); $period .= str_replace('%TEXT%', $blog_name, $format->info);
$period .= str_replace('%TEXT%', $rs->f('blog_url'), $format->info); $period .= str_replace('%TEXT%', $blog_url, $format->info);
$period .= str_replace( $period .= str_replace(
'%TEXT%', '%TEXT%',

71
src/ActivityRow.php Normal file
View File

@ -0,0 +1,71 @@
<?php
/**
* @brief activityReport, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis 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\activityReport;
use Dotclear\Database\MetaRecord;
/**
* Activity record row type hinting.
*/
class ActivityRow
{
/** @var int $id The activity ID */
public readonly int $id;
/** @var string $id The activity blog ID */
public readonly ?string $blog_id;
/** @var string $id The activity blog URL */
public readonly string $blog_url;
/** @var string $id The activity blog name */
public readonly string $blog_name;
/** @var string $id The activity group */
public readonly string $group;
/** @var string $id The activity action */
public readonly string $action;
/** @var string $id The activity date */
public readonly string $dt;
/** @var int $id The activity status */
public readonly int $status;
/** @var array<int,string> 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 : [];
}
}

View File

@ -19,6 +19,7 @@ use dcAdmin;
use dcAuth; use dcAuth;
use dcCore; use dcCore;
use dcFavorites; use dcFavorites;
use dcMenu;
use dcNsProcess; use dcNsProcess;
use dcPage; use dcPage;
use Dotclear\Helper\Date; use Dotclear\Helper\Date;
@ -50,18 +51,20 @@ class Backend extends dcNsProcess
return false; return false;
} }
if ((dcCore::app()->menu[dcAdmin::MENU_PLUGINS] instanceof dcMenu)) {
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
My::name(), My::name(),
dcCore::app()->adminurl?->get('admin.plugin.' . My::id()), (string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id()),
dcPage::getPF(My::id() . '/icon.svg'), dcPage::getPF(My::id() . '/icon.svg'),
preg_match( preg_match(
'/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/', '/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/',
$_SERVER['REQUEST_URI'] $_SERVER['REQUEST_URI']
), ),
dcCore::app()->auth?->check(dcCore::app()->auth->makePermissions([ (bool) dcCore::app()->auth?->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_ADMIN, dcAuth::PERMISSION_ADMIN,
]), dcCore::app()->blog?->id) ]), dcCore::app()->blog?->id)
); );
}
dcCore::app()->addBehaviors([ dcCore::app()->addBehaviors([
// dashboard favorites icon // dashboard favorites icon
@ -78,7 +81,8 @@ class Backend extends dcNsProcess
}, },
// dashboard content display // dashboard content display
'adminDashboardContentsV2' => function (ArrayObject $items): void { '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) { if (!$limit) {
return ; return ;
} }
@ -95,20 +99,20 @@ class Backend extends dcNsProcess
$lines = []; $lines = [];
$groups = ActivityReport::instance()->groups; $groups = ActivityReport::instance()->groups;
while ($rs->fetch()) { while ($rs->fetch()) {
if (!$groups->has($rs->f('activity_group'))) { $row = new ActivityRow($rs);
if (!$groups->has($row->group)) {
continue; continue;
} }
$group = $groups->get($rs->f('activity_group')); $group = $groups->get($row->group);
$data = json_decode($rs->f('activity_logs'), true);
$lines[] = '<dt title="' . __($group->title) . '">' . $lines[] = '<dt title="' . __($group->title) . '">' .
'<strong>' . __($group->get($rs->f('activity_action'))->title) . '</strong>' . '<strong>' . __($group->get($row->action)->title) . '</strong>' .
'<br />' . Date::str( '<br />' . Date::str(
dcCore::app()->blog?->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog?->settings->get('system')->get('time_format'), dcCore::app()->blog?->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog?->settings->get('system')->get('time_format'),
(int) strtotime($rs->f('activity_dt')), (int) strtotime($row->dt),
dcCore::app()->auth?->getInfo('user_tz') is_string(dcCore::app()->auth?->getInfo('user_tz')) ? dcCore::app()->auth->getInfo('user_tz') : 'UTC'
) . '<dt>' . ) . '<dt>' .
'<dd><p>' . '<dd><p>' .
'<em>' . ActivityReport::parseMessage(__($group->get($rs->f('activity_action'))->message), $data) . '</em></p></dd>'; '<em>' . ActivityReport::parseMessage(__($group->get($row->action)->message), $row->logs) . '</em></p></dd>';
} }
if (empty($lines)) { if (empty($lines)) {
return ; return ;
@ -132,12 +136,13 @@ class Backend extends dcNsProcess
}, },
// dashboard content user preference form // dashboard content user preference form
'adminDashboardOptionsFormV2' => function (): void { 'adminDashboardOptionsFormV2' => function (): void {
$db = dcCore::app()->auth?->user_prefs?->get(My::id())->get('dashboard_item');
echo echo
(new Div())->class('fieldset')->items([ (new Div())->class('fieldset')->items([
(new Text('h4', My::name())), (new Text('h4', My::name())),
(new Para())->items([ (new Para())->items([
(new Label(__('Number of activities to show on dashboard:'), Label::OUTSIDE_LABEL_BEFORE))->for(My::id() . '_dashboard_item'), (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, __('Do not show activity report') => 0,
5 => 5, 5 => 5,
10 => 10, 10 => 10,

View File

@ -74,17 +74,18 @@ class ManageList extends adminGenericList
private function logsLine(): string private function logsLine(): string
{ {
$offline = (int) $this->rs->f('activity_status') == ActivityReport::STATUS_REPORTED ? ' offline' : ''; $row = new ActivityRow($this->rs);
$group = ActivityReport::instance()->groups->get($this->rs->f('activity_group'));
$action = $group->get($this->rs->f('activity_action')); $offline = $row->status == ActivityReport::STATUS_REPORTED ? ' offline' : '';
$data = json_decode((string) $this->rs->f('activity_logs'), true); $group = ActivityReport::instance()->groups->get($row->group);
$message = ActivityReport::parseMessage(__($action->message), is_array($data) ? $data : []); $action = $group->get($row->action);
$message = ActivityReport::parseMessage(__($action->message), $row->logs);
$date = Date::str( $date = Date::str(
dcCore::app()->blog?->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog?->settings->get('system')->get('time_format'), 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' 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([ $cols = new ArrayObject([
'activity_group' => '<td class="nowrap">' . __($group->title) . '</td>', 'activity_group' => '<td class="nowrap">' . __($group->title) . '</td>',
@ -97,7 +98,7 @@ class ManageList extends adminGenericList
$this->userColumns(My::id(), $cols); $this->userColumns(My::id(), $cols);
return return
'<tr class="line ' . $offline . '" id="l' . $this->rs->f('activity_id') . '">' . '<tr class="line ' . $offline . '" id="l' . $row->id . '">' .
implode(iterator_to_array($cols)) . implode(iterator_to_array($cols)) .
'</tr>'; '</tr>';
} }