-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategory.php
More file actions
159 lines (131 loc) · 4.95 KB
/
Copy pathCategory.php
File metadata and controls
159 lines (131 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace FriendsOfRedaxo\ActivityLog\EP;
use FriendsOfRedaxo\ActivityLog\Activity;
use rex;
use rex_addon_interface;
use rex_category;
use rex_clang;
use rex_extension;
use rex_extension_point;
use rex_url;
use function is_bool;
class Category
{
use EpTrait;
/** @var rex_addon_interface */
private static $addon;
public function __construct()
{
self::$addon = $this->addon();
/** @phpstan-ignore-next-line */
if (is_bool(self::$addon->getConfig('category_added')) && self::$addon->getConfig('category_added')) {
$this->addCategory();
}
/** @phpstan-ignore-next-line */
if (is_bool(self::$addon->getConfig('category_updated')) && self::$addon->getConfig('category_updated')) {
$this->update('CAT_UPDATED', static::class . '::message');
}
/** @phpstan-ignore-next-line */
if (is_bool(self::$addon->getConfig('category_deleted')) && self::$addon->getConfig('category_deleted')) {
$this->deleteCategory();
}
/** @phpstan-ignore-next-line */
if (is_bool(self::$addon->getConfig('category_status')) && self::$addon->getConfig('category_status')) {
$this->status('CAT_STATUS', static::class . '::message');
}
}
protected function getSource(): string
{
return 'category';
}
/**
* Registriert den CAT_ADDED-Handler mit Deduplication:
* Da REDAXO CAT_ADDED einmal pro Sprache feuert, wird nur der erste Aufruf
* pro Kategorie-ID geloggt.
*/
private function addCategory(): void
{
$source = $this->getSource();
rex_extension::register('CAT_ADDED', static function (rex_extension_point $ep) use ($source): void {
/** @var array<string, mixed> $params */
$params = $ep->getParams();
$categoryId = (int) ($params['id'] ?? 0);
/** @var array<int, bool> $logged */
static $logged = [];
if (isset($logged[$categoryId])) {
return;
}
$logged[$categoryId] = true;
$message = static::message($params, Activity::TYPE_ADD);
Activity::message($message)
->type(Activity::TYPE_ADD)
->source($source)
->causer(rex::getUser())
->log();
});
}
/**
* Registriert den CAT_DELETED-Handler mit Deduplication:
* Da REDAXO CAT_DELETED einmal pro Sprache feuert, wird nur der erste Aufruf
* pro Kategorie-ID geloggt.
*/
private function deleteCategory(): void
{
$source = $this->getSource();
rex_extension::register('CAT_DELETED', static function (rex_extension_point $ep) use ($source): void {
/** @var array<string, mixed> $params */
$params = $ep->getParams();
$categoryId = (int) ($params['id'] ?? 0);
/** @var array<int, bool> $logged */
static $logged = [];
if (isset($logged[$categoryId])) {
return;
}
$logged[$categoryId] = true;
$message = static::message($params, Activity::TYPE_DELETE);
Activity::message($message)
->type(Activity::TYPE_DELETE)
->source($source)
->causer(rex::getUser())
->log();
});
}
/**
* @param array<string> $params
* @param array<string>|null $additionalParams
*/
public static function message(array $params, string $type, ?array $additionalParams = null): string
{
$message = '<strong>Category:</strong> ';
// Do NOT call rex_category::get() for delete – the record is already gone
// from DB when CAT_DELETED fires, causing an SQL conflict.
if ('delete' === $type) {
$message .= rex_escape($params['name'] ?? '[' . $params['id'] . ']');
$message .= ' - ' . self::$addon->i18n('type_' . $type);
return $message;
}
$category = rex_category::get((int) $params['id']);
if (null === $category) {
$message .= rex_escape($params['name'] ?? '[' . $params['id'] . ']');
} else {
$message .= '<a href="' . rex_url::backendController([
'page' => 'structure',
'article_id' => 0,
'category_id' => $params['id'],
'clang_id' => $params['clang'] ?? rex_clang::getCurrentId(),
]) . '">';
$message .= rex_escape($category->getName());
$message .= '</a>';
}
$message .= ' - ';
if (isset($additionalParams['type'])) {
$message .= self::$addon->i18n('type_' . $additionalParams['type']);
if (null !== $category) {
$message .= self::getStatus($category->isOnline(), $additionalParams);
}
} else {
$message .= self::$addon->i18n('type_' . $type);
}
return $message;
}
}