Skip to content

Commit bd8f443

Browse files
committed
fix archiv
1 parent 967d79b commit bd8f443

File tree

4 files changed

+148
-8
lines changed

4 files changed

+148
-8
lines changed

src/Module/Communication/View/ShowArchiveSearchResult/ShowUserArchiveSearchResult.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,24 @@ public function handle(GameControllerInterface $game): void
6969
if ($post->getPlotId() && isset($plots[$post->getPlotId()])) {
7070
$plot = $plots[$post->getPlotId()];
7171
}
72-
$knPostings[] = $this->knArchiveFactory->createKnArchiveItem($post, $user, $plot);
72+
$archiveItem = $this->knArchiveFactory->createKnArchiveItem($post, $user, $plot);
73+
74+
$knPostings[] = [
75+
'id' => $archiveItem->getId(),
76+
'username' => $archiveItem->getUsername(),
77+
'title' => $archiveItem->getTitle(),
78+
'text' => $this->sanitizeHtml($archiveItem->getText()),
79+
'date' => $archiveItem->getDate(),
80+
'editDate' => $archiveItem->getEditDate(),
81+
'formerId' => $archiveItem->getFormerId(),
82+
'refs' => $archiveItem->getRefs(),
83+
'ratingBar' => $archiveItem->getRatingBar(),
84+
'commentCount' => $archiveItem->getCommentCount(),
85+
'divClass' => $archiveItem->getDivClass(),
86+
'rpgPlot' => $archiveItem->getRpgPlot(),
87+
'formattedDate' => $this->formatDateForVersion($archiveItem->getDate(), $version),
88+
'formattedEditDate' => $archiveItem->getEditDate() > 0 ? $this->formatDateForVersion($archiveItem->getEditDate(), $version) : null,
89+
];
7390
}
7491

7592
$game->setTemplateVar('KN_POSTINGS', $knPostings);
@@ -78,4 +95,57 @@ public function handle(GameControllerInterface $game): void
7895
$game->setTemplateVar('SHOW_ARCHIVE_VIEW', 'SHOW_KN_ARCHIVE');
7996
$game->addExecuteJS("initTranslations();", GameEnum::JS_EXECUTION_AFTER_RENDER);
8097
}
98+
99+
private function formatDateForVersion(int $timestamp, string $version): string
100+
{
101+
$yearsToAdd = $this->getYearsForVersion($version);
102+
103+
$date = new \DateTime('@' . $timestamp);
104+
$date->modify('+' . $yearsToAdd . ' years');
105+
106+
return $date->format('d.m.Y H:i');
107+
}
108+
109+
private function getYearsForVersion(string $version): int
110+
{
111+
$cleanVersion = ltrim($version, 'v');
112+
113+
return match (true) {
114+
str_contains($cleanVersion, 'alpha') || str_starts_with($cleanVersion, '3') => 370,
115+
str_starts_with($cleanVersion, '26') => 374,
116+
str_starts_with($cleanVersion, '15') => 375,
117+
default => 373
118+
};
119+
}
120+
121+
private function sanitizeHtml(string $html): string
122+
{
123+
if (empty(trim($html))) {
124+
return $html;
125+
}
126+
127+
$dom = new \DOMDocument('1.0', 'UTF-8');
128+
129+
libxml_use_internal_errors(true);
130+
131+
$dom->loadHTML(
132+
'<?xml encoding="UTF-8">' .
133+
'<div>' . $html . '</div>',
134+
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
135+
);
136+
137+
libxml_clear_errors();
138+
139+
$body = $dom->getElementsByTagName('div')->item(0);
140+
if ($body === null) {
141+
return $html;
142+
}
143+
144+
$result = '';
145+
foreach ($body->childNodes as $node) {
146+
$result .= $dom->saveHTML($node);
147+
}
148+
149+
return $result;
150+
}
81151
}

src/Module/Communication/View/ShowKnArchive/ShowKnArchive.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,24 @@ public function handle(GameControllerInterface $game): void
111111
if ($post->getPlotId() && isset($plots[$post->getPlotId()])) {
112112
$plot = $plots[$post->getPlotId()];
113113
}
114-
$posts[] = $this->knArchiveFactory->createKnArchiveItem($post, $game->getUser(), $plot);
114+
$archiveItem = $this->knArchiveFactory->createKnArchiveItem($post, $game->getUser(), $plot);
115+
116+
$posts[] = [
117+
'id' => $archiveItem->getId(),
118+
'username' => $archiveItem->getUsername(),
119+
'title' => $archiveItem->getTitle(),
120+
'text' => $this->sanitizeHtml($archiveItem->getText()),
121+
'date' => $archiveItem->getDate(),
122+
'editDate' => $archiveItem->getEditDate(),
123+
'formerId' => $archiveItem->getFormerId(),
124+
'refs' => $archiveItem->getRefs(),
125+
'ratingBar' => $archiveItem->getRatingBar(),
126+
'commentCount' => $archiveItem->getCommentCount(),
127+
'divClass' => $archiveItem->getDivClass(),
128+
'rpgPlot' => $archiveItem->getRpgPlot(),
129+
'formattedDate' => $this->formatDateForVersion($archiveItem->getDate(), $version),
130+
'formattedEditDate' => $archiveItem->getEditDate() > 0 ? $this->formatDateForVersion($archiveItem->getEditDate(), $version) : null,
131+
];
115132
}
116133

117134
$game->setTemplateVar('KN_POSTINGS', $posts);
@@ -138,4 +155,57 @@ private function formatVersion(string $version): string
138155

139156
return $version;
140157
}
158+
159+
private function formatDateForVersion(int $timestamp, string $version): string
160+
{
161+
$yearsToAdd = $this->getYearsForVersion($version);
162+
163+
$date = new \DateTime('@' . $timestamp);
164+
$date->modify('+' . $yearsToAdd . ' years');
165+
166+
return $date->format('d.m.Y H:i');
167+
}
168+
169+
private function getYearsForVersion(string $version): int
170+
{
171+
$cleanVersion = ltrim($version, 'v');
172+
173+
return match (true) {
174+
str_contains($cleanVersion, 'alpha') || str_starts_with($cleanVersion, '3') => 370,
175+
str_starts_with($cleanVersion, '26') => 374,
176+
str_starts_with($cleanVersion, '15') => 375,
177+
default => 373
178+
};
179+
}
180+
181+
private function sanitizeHtml(string $html): string
182+
{
183+
if (empty(trim($html))) {
184+
return $html;
185+
}
186+
187+
$dom = new \DOMDocument('1.0', 'UTF-8');
188+
189+
libxml_use_internal_errors(true);
190+
191+
$dom->loadHTML(
192+
'<?xml encoding="UTF-8">' .
193+
'<div>' . $html . '</div>',
194+
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
195+
);
196+
197+
libxml_clear_errors();
198+
199+
$body = $dom->getElementsByTagName('div')->item(0);
200+
if ($body === null) {
201+
return $html;
202+
}
203+
204+
$result = '';
205+
foreach ($body->childNodes as $node) {
206+
$result .= $dom->saveHTML($node);
207+
}
208+
209+
return $result;
210+
}
141211
}

src/html/communication/knArchiv.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<div class="knGridUser">
5858
<div class="segment">
5959
<div style="text-align: center; width: 100%">
60-
{{ post.date|stuDateTime }}
60+
{{ post.formattedDate }}
6161
{{ post.date|date('d.m.Y H:i') }}
6262
</div>
6363
</div>
@@ -89,9 +89,9 @@
8989
{% endif %}
9090
</div>
9191
<div class="knGridRating">
92-
{% if post.editDate and post.editDate > 0 %}
92+
{% if post.formattedEditDate %}
9393
<div class="segment">
94-
<span class="knedit">Bearbeitet: {{ post.editDate|stuDateTime }}</span>
94+
<span class="knedit">Bearbeitet: {{ post.formattedEditDate }}</span>
9595
</div>
9696
{% endif %}
9797
<div class="segment" id="kn_rating_{{ postId }}">

tests/inttest/html/__snapshots__/AllViewControllerTest--COMMUNICATION_VIEWS-SHOW_KN_ARCHIVE.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<div class="knGridUser">
5353
<div class="segment">
5454
<div style="text-align: center; width: 100%">
55-
21.07.2394 11:30
55+
18.09.2396 19:13
5656
18.09.2023 19:13
5757
</div>
5858
</div>
@@ -70,7 +70,7 @@
7070
</div>
7171
<div class="knGridRating">
7272
<div class="segment">
73-
<span class="knedit">Bearbeitet: 21.07.2394 11:30</span>
73+
<span class="knedit">Bearbeitet: 18.09.2396 19:15</span>
7474
</div>
7575
<div class="segment" id="kn_rating_2">
7676
<div style="display: inline-block">
@@ -109,7 +109,7 @@
109109
<div class="knGridUser">
110110
<div class="segment">
111111
<div style="text-align: center; width: 100%">
112-
21.07.2394 11:30
112+
18.09.2396 19:11
113113
18.09.2023 19:11
114114
</div>
115115
</div>

0 commit comments

Comments
 (0)