Skip to content

Commit ce02c70

Browse files
committed
refactor: introduced news model and tests
1 parent 8847918 commit ce02c70

File tree

7 files changed

+336
-66
lines changed

7 files changed

+336
-66
lines changed

phpmyfaq/admin/news.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
use phpMyFAQ\Component\Alert;
2121
use phpMyFAQ\Date;
2222
use phpMyFAQ\Entity\CommentType;
23+
use phpMyFAQ\Entity\NewsMessage;
2324
use phpMyFAQ\Filter;
2425
use phpMyFAQ\Helper\LanguageHelper;
2526
use phpMyFAQ\Language;
2627
use phpMyFAQ\News;
27-
use phpMyFAQ\Session\Token;use phpMyFAQ\Strings;
28+
use phpMyFAQ\Session\Token;
29+
use phpMyFAQ\Strings;
2830
use phpMyFAQ\Translation;
2931

3032
if (!defined('IS_VALID_PHPMYFAQ')) {
@@ -216,7 +218,7 @@
216218
</thead>
217219
<tbody>
218220
<?php
219-
$newsHeader = $news->getNewsHeader();
221+
$newsHeader = $news->getHeader();
220222
$date = new Date($faqConfig);
221223
if (is_countable($newsHeader) ? count($newsHeader) : 0) {
222224
foreach ($newsHeader as $newsItem) {
@@ -251,7 +253,7 @@
251253
<?php
252254
} elseif ('edit-news' == $action && $user->perm->hasPermission($user->getUserId(), 'editnews')) {
253255
$id = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);
254-
$newsData = $news->getNewsEntry($id, true);
256+
$newsData = $news->get($id, true);
255257
?>
256258
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
257259
<h1 class="h2">
@@ -473,9 +475,23 @@ class="form-control">
473475
$newsLang = Filter::filterInput(INPUT_POST, 'langTo', FILTER_SANITIZE_SPECIAL_CHARS);
474476
$target = Filter::filterInput(INPUT_POST, 'target', FILTER_SANITIZE_SPECIAL_CHARS);
475477

476-
$newsData = ['lang' => $newsLang, 'header' => $header, 'content' => html_entity_decode((string) $content), 'authorName' => $author, 'authorEmail' => $email, 'active' => (is_null($active)) ? 'n' : 'y', 'comment' => (is_null($comment)) ? 'n' : 'y', 'dateStart' => (empty($dateStart)) ? '00000000000000' : str_replace('-', '', (string) $dateStart) . '000000', 'dateEnd' => (empty($dateEnd)) ? '99991231235959' : str_replace('-', '', (string) $dateEnd) . '235959', 'link' => $link, 'linkTitle' => $linkTitle, 'date' => date('YmdHis'), 'target' => (is_null($target)) ? '' : $target];
477-
478-
if ($news->addNewsEntry($newsData)) {
478+
$newsMessage = new NewsMessage();
479+
$newsMessage
480+
->setLanguage($newsLang)
481+
->setHeader($header)
482+
->setMessage(html_entity_decode((string) $content))
483+
->setAuthor($author)
484+
->setEmail($email)
485+
->setActive(!is_null($active))
486+
->setComment(!is_null($comment))
487+
->setDateStart(new DateTime($dateStart))
488+
->setDateEnd(new DateTime($dateEnd))
489+
->setLink($link ?? '')
490+
->setLinkTitle($linkTitle ?? '')
491+
->setLinkTarget($target ?? '')
492+
->setCreated(new DateTime());
493+
494+
if ($news->create($newsMessage)) {
479495
echo Alert::success('ad_news_updatesuc');
480496
} else {
481497
echo Alert::danger('ad_news_insertfail', $faqConfig->getDb()->error());
@@ -497,6 +513,7 @@ class="form-control">
497513
<div class="row">
498514
<div class="col-12">
499515
<?php
516+
$newsId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
500517
$dateStart = Filter::filterInput(INPUT_POST, 'dateStart', FILTER_SANITIZE_SPECIAL_CHARS);
501518
$dateEnd = Filter::filterInput(INPUT_POST, 'dateEnd', FILTER_SANITIZE_SPECIAL_CHARS);
502519
$header = Filter::filterInput(INPUT_POST, 'newsheader', FILTER_SANITIZE_SPECIAL_CHARS);
@@ -510,24 +527,24 @@ class="form-control">
510527
$newsLang = Filter::filterInput(INPUT_POST, 'langTo', FILTER_SANITIZE_SPECIAL_CHARS);
511528
$target = Filter::filterInput(INPUT_POST, 'target', FILTER_SANITIZE_SPECIAL_CHARS);
512529

513-
$newsData = [
514-
'lang' => $newsLang,
515-
'header' => $header,
516-
'content' => html_entity_decode((string) $content),
517-
'authorName' => $author,
518-
'authorEmail' => $email,
519-
'active' => (is_null($active)) ? 'n' : 'y',
520-
'comment' => (is_null($comment)) ? 'n' : 'y',
521-
'dateStart' => (empty($dateStart)) ? '00000000000000' : str_replace('-', '', (string) $dateStart) . '000000',
522-
'dateEnd' => (empty($dateEnd)) ? '99991231235959' : str_replace('-', '', (string) $dateEnd) . '235959',
523-
'link' => $link,
524-
'linkTitle' => $linkTitle,
525-
'date' => date('YmdHis'),
526-
'target' => (is_null($target)) ? '' : $target,
527-
];
528-
529-
$newsId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
530-
if ($news->updateNewsEntry((int) $newsId, $newsData)) {
530+
$newsMessage = new NewsMessage();
531+
$newsMessage
532+
->setId($newsId)
533+
->setLanguage($newsLang)
534+
->setHeader($header)
535+
->setMessage(html_entity_decode((string) $content))
536+
->setAuthor($author)
537+
->setEmail($email)
538+
->setActive(!is_null($active))
539+
->setComment(!is_null($comment))
540+
->setDateStart(new DateTime($dateStart))
541+
->setDateEnd(new DateTime($dateEnd))
542+
->setLink($link ?? '')
543+
->setLinkTitle($linkTitle ?? '')
544+
->setLinkTarget($target ?? '')
545+
->setCreated(new DateTime());
546+
547+
if ($news->update($newsMessage)) {
531548
echo Alert::success('ad_news_updatesuc');
532549
} else {
533550
echo Alert::danger('ad_news_updatefail', $faqConfig->getDb()->error());
@@ -539,7 +556,8 @@ class="form-control">
539556
<?php
540557
} elseif ('delete-news' == $action && $user->perm->hasPermission($user->getUserId(), 'delnews')) {
541558
?>
542-
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
559+
<div
560+
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
543561
<h1 class="h2">
544562
<i aria-hidden="true" class="bi bi-pencil"></i>
545563
<?= Translation::get('ad_news_data') ?>
@@ -560,7 +578,7 @@ class="form-control">
560578
<form action="?action=delete-news" method="post" accept-charset="utf-8">
561579
<input type="hidden" name="id" value="<?= $deleteId ?>">
562580
<input type="hidden" name="really" value="yes">
563-
<?= Token::getInstance()->getTokenInput('delete-news') ?>
581+
<?= Token::getInstance()->getTokenInput('delete-news') ?>
564582
<button class="btn btn-warning" type="submit" name="submit">
565583
<?= Translation::get('ad_news_yesdelete') ?>
566584
</button>
@@ -574,7 +592,7 @@ class="form-control">
574592
} else {
575593
if (Token::getInstance()->verifyToken('delete-news', $csrfToken)) {
576594
$deleteId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
577-
$news->deleteNews((int)$deleteId);
595+
$news->delete((int)$deleteId);
578596
echo Alert::success('ad_news_delsuc');
579597
printf('<div class="row">&rarr; <a href="?action=news">%s</a></p>', Translation::get('msgNews'));
580598
}

phpmyfaq/news.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
$newsMainHeader = $faqConfig->getTitle() . Translation::get('msgNews');
5353

5454
// Get all data from the news record
55-
$news = $oNews->getNewsEntry($newsId);
55+
$news = $oNews->get($newsId);
5656

5757
$newsContent = $news['content'];
5858
$newsHeader = $news['header'];

phpmyfaq/src/phpMyFAQ/Controller/Frontend/CommentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function create(Request $request): JsonResponse
134134
$notification->sendFaqCommentNotification($faq, $commentEntity);
135135
} else {
136136
$news = new News($this->configuration);
137-
$newsData = $news->getNewsEntry($id);
137+
$newsData = $news->get($id);
138138
$notification->sendNewsCommentNotification($newsData, $commentEntity);
139139
}
140140

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
/**
4+
* The News message model class
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ\Entity
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2024 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2024-03-24
16+
*/
17+
18+
namespace phpMyFAQ\Entity;
19+
20+
use DateTime;
21+
22+
class NewsMessage
23+
{
24+
private int $id;
25+
26+
private string $language;
27+
28+
private string $header;
29+
30+
private string $message;
31+
32+
private DateTime $created;
33+
34+
private string $author;
35+
36+
private string $email;
37+
38+
private bool $active;
39+
40+
private bool $comment;
41+
42+
private ?DateTime $dateStart;
43+
44+
private ?DateTime $dateEnd;
45+
46+
private ?string $link;
47+
48+
private ?string $linkTitle;
49+
50+
private ?string $linkTarget;
51+
52+
public function getId(): int
53+
{
54+
return $this->id;
55+
}
56+
public function setId(int $id): NewsMessage
57+
{
58+
$this->id = $id;
59+
return $this;
60+
}
61+
public function getLanguage(): string
62+
{
63+
return $this->language;
64+
}
65+
public function setLanguage(string $language): NewsMessage
66+
{
67+
$this->language = $language;
68+
return $this;
69+
}
70+
public function getHeader(): string
71+
{
72+
return $this->header;
73+
}
74+
public function setHeader(string $header): NewsMessage
75+
{
76+
$this->header = $header;
77+
return $this;
78+
}
79+
public function getMessage(): string
80+
{
81+
return $this->message;
82+
}
83+
public function setMessage(string $message): NewsMessage
84+
{
85+
$this->message = $message;
86+
return $this;
87+
}
88+
public function getCreated(): DateTime
89+
{
90+
return $this->created;
91+
}
92+
public function setCreated(DateTime $created): NewsMessage
93+
{
94+
$this->created = $created;
95+
return $this;
96+
}
97+
public function getAuthor(): string
98+
{
99+
return $this->author;
100+
}
101+
public function setAuthor(string $author): NewsMessage
102+
{
103+
$this->author = $author;
104+
return $this;
105+
}
106+
public function getEmail(): string
107+
{
108+
return $this->email;
109+
}
110+
public function setEmail(string $email): NewsMessage
111+
{
112+
$this->email = $email;
113+
return $this;
114+
}
115+
public function isActive(): bool
116+
{
117+
return $this->active;
118+
}
119+
public function setActive(bool $active): NewsMessage
120+
{
121+
$this->active = $active;
122+
return $this;
123+
}
124+
public function isComment(): bool
125+
{
126+
return $this->comment;
127+
}
128+
public function setComment(bool $comment): NewsMessage
129+
{
130+
$this->comment = $comment;
131+
return $this;
132+
}
133+
public function getDateStart(): ?DateTime
134+
{
135+
return $this->dateStart ?? null;
136+
}
137+
public function setDateStart(DateTime $dateStart): NewsMessage
138+
{
139+
$this->dateStart = $dateStart;
140+
return $this;
141+
}
142+
public function getDateEnd(): ?DateTime
143+
{
144+
return $this->dateEnd ?? null;
145+
}
146+
public function setDateEnd(DateTime $dateEnd): NewsMessage
147+
{
148+
$this->dateEnd = $dateEnd;
149+
return $this;
150+
}
151+
public function getLink(): string
152+
{
153+
return $this->link ?? '';
154+
}
155+
public function setLink(string $link): NewsMessage
156+
{
157+
$this->link = $link;
158+
return $this;
159+
}
160+
public function getLinkTitle(): string
161+
{
162+
return $this->linkTitle ?? '';
163+
}
164+
public function setLinkTitle(string $linkTitle): NewsMessage
165+
{
166+
$this->linkTitle = $linkTitle;
167+
return $this;
168+
}
169+
public function getLinkTarget(): string
170+
{
171+
return $this->linkTarget ?? '';
172+
}
173+
public function setLinkTarget(string $linkTarget): NewsMessage
174+
{
175+
$this->linkTarget = $linkTarget;
176+
return $this;
177+
}
178+
}

0 commit comments

Comments
 (0)