Skip to content

Commit 35332cb

Browse files
feat: add news formatter for Event site
1 parent d20074f commit 35332cb

22 files changed

+1598
-51
lines changed

app/Audit/AbstractAuditLogFormatter.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,78 @@ protected function getUserInfo(): string
4141
return sprintf("%s (%s)", $user_name, $user_id);
4242
}
4343

44+
45+
protected function getIgnoredFields(): array
46+
{
47+
return [
48+
'last_created',
49+
'last_updated',
50+
'last_edited',
51+
'created_by',
52+
'updated_by'
53+
];
54+
}
55+
56+
protected function formatChangeValue($value): string
57+
{
58+
if (is_bool($value)) {
59+
return $value ? 'true' : 'false';
60+
}
61+
if (is_null($value)) {
62+
return 'null';
63+
}
64+
if ($value instanceof \DateTimeInterface) {
65+
return $value->format('Y-m-d H:i:s');
66+
}
67+
if ($value instanceof \Doctrine\Common\Collections\Collection) {
68+
$count = $value->count();
69+
return sprintf('Collection[%d items]', $count);
70+
}
71+
if (is_object($value)) {
72+
$className = get_class($value);
73+
return sprintf('%s', $className);
74+
}
75+
if (is_array($value)) {
76+
return sprintf('Array[%d items]', count($value));
77+
}
78+
return (string) $value;
79+
}
80+
81+
82+
protected function buildChangeDetails(array $change_set): string
83+
{
84+
$changed_fields = [];
85+
$ignored_fields = $this->getIgnoredFields();
86+
87+
foreach ($change_set as $prop_name => $change_values) {
88+
if (in_array($prop_name, $ignored_fields)) {
89+
continue;
90+
}
91+
92+
$old_value = $change_values[0] ?? null;
93+
$new_value = $change_values[1] ?? null;
94+
95+
$formatted_change = $this->formatFieldChange($prop_name, $old_value, $new_value);
96+
if ($formatted_change !== null) {
97+
$changed_fields[] = $formatted_change;
98+
}
99+
}
100+
101+
if (empty($changed_fields)) {
102+
return 'properties without changes registered';
103+
}
104+
105+
$fields_summary = count($changed_fields) . ' field(s) modified: ';
106+
return $fields_summary . implode(' | ', $changed_fields);
107+
}
108+
109+
protected function formatFieldChange(string $prop_name, $old_value, $new_value): ?string
110+
{
111+
$old_display = $this->formatChangeValue($old_value);
112+
$new_display = $this->formatChangeValue($new_value);
113+
114+
return sprintf("Property \"%s\" has changed from \"%s\" to \"%s\"", $prop_name, $old_display, $new_display);
115+
}
116+
44117
abstract public function format($subject, array $change_set): ?string;
45118
}

app/Audit/ConcreteFormatters/EntityUpdateAuditLogFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(?IChildEntityAuditLogFormatter $child_entity_formatt
4242
$this->child_entity_formatter = $child_entity_formatter;
4343
}
4444

45-
protected function getIgnoredFields()
45+
protected function getIgnoredFields(): array
4646
{
4747
return [
4848
'last_created',
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters\PresentationFormatters;
4+
5+
/**
6+
* Copyright 2025 OpenStack Foundation
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
**/
17+
18+
use App\Audit\AbstractAuditLogFormatter;
19+
use App\Audit\Interfaces\IAuditStrategy;
20+
use models\summit\PresentationActionType;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationActionTypeAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof PresentationActionType) {
35+
return null;
36+
}
37+
38+
try {
39+
$label = $subject->getLabel() ?? 'Unknown Action Type';
40+
$id = $subject->getId() ?? 'unknown';
41+
42+
$summit = $subject->getSummit();
43+
$summit_name = $summit ? ($summit->getName() ?? 'Unknown Summit') : 'Unknown Summit';
44+
45+
switch ($this->event_type) {
46+
case IAuditStrategy::EVENT_ENTITY_CREATION:
47+
return sprintf(
48+
"Presentation Action Type '%s' (%d) created for Summit '%s' by user %s",
49+
$label,
50+
$id,
51+
$summit_name,
52+
$this->getUserInfo()
53+
);
54+
55+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
56+
$change_details = $this->buildChangeDetails($change_set);
57+
return sprintf(
58+
"Presentation Action Type '%s' (%d) for Summit '%s' updated: %s by user %s",
59+
$label,
60+
$id,
61+
$summit_name,
62+
$change_details,
63+
$this->getUserInfo()
64+
);
65+
66+
case IAuditStrategy::EVENT_ENTITY_DELETION:
67+
return sprintf(
68+
"Presentation Action Type '%s' (%d) for Summit '%s' was deleted by user %s",
69+
$label,
70+
$id,
71+
$summit_name,
72+
$this->getUserInfo()
73+
);
74+
}
75+
} catch (\Exception $ex) {
76+
Log::warning("PresentationActionTypeAuditLogFormatter error: " . $ex->getMessage());
77+
}
78+
79+
return null;
80+
}
81+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters\PresentationFormatters;
4+
5+
/**
6+
* Copyright 2025 OpenStack Foundation
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
**/
17+
18+
use App\Audit\AbstractAuditLogFormatter;
19+
use App\Audit\Interfaces\IAuditStrategy;
20+
use models\summit\PresentationLink;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationLinkAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof PresentationLink) {
35+
return null;
36+
}
37+
38+
try {
39+
$title = $subject->getName() ?? 'Unknown Link';
40+
$id = $subject->getId() ?? 'unknown';
41+
$class_name = $subject->getClassName();
42+
43+
$presentation = $subject->getPresentation();
44+
$presentation_title = $presentation ? ($presentation->getTitle() ?? 'Unknown Presentation') : 'Unknown Presentation';
45+
46+
switch ($this->event_type) {
47+
case IAuditStrategy::EVENT_ENTITY_CREATION:
48+
return sprintf(
49+
"Presentation Link '%s' (%d) of type %s created for presentation '%s' by user %s",
50+
$title,
51+
$id,
52+
$class_name,
53+
$presentation_title,
54+
$this->getUserInfo()
55+
);
56+
57+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
58+
$change_details = $this->buildChangeDetails($change_set);
59+
return sprintf(
60+
"Presentation Link '%s' (%d) for presentation '%s' updated: %s by user %s",
61+
$title,
62+
$id,
63+
$presentation_title,
64+
$change_details,
65+
$this->getUserInfo()
66+
);
67+
68+
case IAuditStrategy::EVENT_ENTITY_DELETION:
69+
return sprintf(
70+
"Presentation Link '%s' (%d) of type %s for presentation '%s' was deleted by user %s",
71+
$title,
72+
$id,
73+
$class_name,
74+
$presentation_title,
75+
$this->getUserInfo()
76+
);
77+
}
78+
} catch (\Exception $ex) {
79+
Log::warning("PresentationLinkAuditLogFormatter error: " . $ex->getMessage());
80+
}
81+
82+
return null;
83+
}
84+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters\PresentationFormatters;
4+
5+
/**
6+
* Copyright 2025 OpenStack Foundation
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
**/
17+
18+
use App\Audit\AbstractAuditLogFormatter;
19+
use App\Audit\Interfaces\IAuditStrategy;
20+
use models\summit\PresentationMediaUpload;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationMediaUploadAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof PresentationMediaUpload) {
35+
return null;
36+
}
37+
38+
try {
39+
$title = $subject->getName() ?? 'Unknown Media';
40+
$id = $subject->getId() ?? 'unknown';
41+
$class_name = $subject->getClassName();
42+
43+
$presentation = $subject->getPresentation();
44+
$presentation_title = $presentation ? ($presentation->getTitle() ?? 'Unknown Presentation') : 'Unknown Presentation';
45+
46+
47+
switch ($this->event_type) {
48+
case IAuditStrategy::EVENT_ENTITY_CREATION:
49+
return sprintf(
50+
"Presentation Media Upload '%s' (%d) of type %s created for presentation '%s' by user %s",
51+
$title,
52+
$id,
53+
$class_name,
54+
$presentation_title,
55+
$this->getUserInfo()
56+
);
57+
58+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
59+
$change_details = $this->buildChangeDetails($change_set);
60+
return sprintf(
61+
"Presentation Media Upload '%s' (%d) for presentation '%s' updated: %s by user %s",
62+
$title,
63+
$id,
64+
$presentation_title,
65+
$change_details,
66+
$this->getUserInfo()
67+
);
68+
69+
case IAuditStrategy::EVENT_ENTITY_DELETION:
70+
return sprintf(
71+
"Presentation Media Upload '%s' (%d) of type %s for presentation '%s' was deleted by user %s",
72+
$title,
73+
$id,
74+
$class_name,
75+
$presentation_title,
76+
$this->getUserInfo()
77+
);
78+
}
79+
} catch (\Exception $ex) {
80+
Log::warning("PresentationMediaUploadAuditLogFormatter error: " . $ex->getMessage());
81+
}
82+
83+
return null;
84+
}
85+
}

0 commit comments

Comments
 (0)