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();
$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%',

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 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[] = '<dt title="' . __($group->title) . '">' .
'<strong>' . __($group->get($rs->f('activity_action'))->title) . '</strong>' .
'<strong>' . __($group->get($row->action)->title) . '</strong>' .
'<br />' . 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'
) . '<dt>' .
'<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)) {
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,

View File

@ -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' => '<td class="nowrap">' . __($group->title) . '</td>',
@ -97,7 +98,7 @@ class ManageList extends adminGenericList
$this->userColumns(My::id(), $cols);
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)) .
'</tr>';
}