Skip to content

Commit

Permalink
refactor: introduced news model and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 24, 2024
1 parent 8847918 commit ce02c70
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 66 deletions.
72 changes: 45 additions & 27 deletions phpmyfaq/admin/news.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
use phpMyFAQ\Component\Alert;
use phpMyFAQ\Date;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Entity\NewsMessage;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\LanguageHelper;
use phpMyFAQ\Language;
use phpMyFAQ\News;
use phpMyFAQ\Session\Token;use phpMyFAQ\Strings;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Strings;
use phpMyFAQ\Translation;

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

$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];

if ($news->addNewsEntry($newsData)) {
$newsMessage = new NewsMessage();
$newsMessage
->setLanguage($newsLang)
->setHeader($header)
->setMessage(html_entity_decode((string) $content))
->setAuthor($author)
->setEmail($email)
->setActive(!is_null($active))
->setComment(!is_null($comment))
->setDateStart(new DateTime($dateStart))
->setDateEnd(new DateTime($dateEnd))
->setLink($link ?? '')
->setLinkTitle($linkTitle ?? '')
->setLinkTarget($target ?? '')
->setCreated(new DateTime());

if ($news->create($newsMessage)) {
echo Alert::success('ad_news_updatesuc');
} else {
echo Alert::danger('ad_news_insertfail', $faqConfig->getDb()->error());
Expand All @@ -497,6 +513,7 @@ class="form-control">
<div class="row">
<div class="col-12">
<?php
$newsId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$dateStart = Filter::filterInput(INPUT_POST, 'dateStart', FILTER_SANITIZE_SPECIAL_CHARS);
$dateEnd = Filter::filterInput(INPUT_POST, 'dateEnd', FILTER_SANITIZE_SPECIAL_CHARS);
$header = Filter::filterInput(INPUT_POST, 'newsheader', FILTER_SANITIZE_SPECIAL_CHARS);
Expand All @@ -510,24 +527,24 @@ class="form-control">
$newsLang = Filter::filterInput(INPUT_POST, 'langTo', FILTER_SANITIZE_SPECIAL_CHARS);
$target = Filter::filterInput(INPUT_POST, 'target', FILTER_SANITIZE_SPECIAL_CHARS);

$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,
];

$newsId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($news->updateNewsEntry((int) $newsId, $newsData)) {
$newsMessage = new NewsMessage();
$newsMessage
->setId($newsId)
->setLanguage($newsLang)
->setHeader($header)
->setMessage(html_entity_decode((string) $content))
->setAuthor($author)
->setEmail($email)
->setActive(!is_null($active))
->setComment(!is_null($comment))
->setDateStart(new DateTime($dateStart))
->setDateEnd(new DateTime($dateEnd))
->setLink($link ?? '')
->setLinkTitle($linkTitle ?? '')
->setLinkTarget($target ?? '')
->setCreated(new DateTime());

if ($news->update($newsMessage)) {
echo Alert::success('ad_news_updatesuc');
} else {
echo Alert::danger('ad_news_updatefail', $faqConfig->getDb()->error());
Expand All @@ -539,7 +556,8 @@ class="form-control">
<?php
} elseif ('delete-news' == $action && $user->perm->hasPermission($user->getUserId(), 'delnews')) {
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<div
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">
<i aria-hidden="true" class="bi bi-pencil"></i>
<?= Translation::get('ad_news_data') ?>
Expand All @@ -560,7 +578,7 @@ class="form-control">
<form action="?action=delete-news" method="post" accept-charset="utf-8">
<input type="hidden" name="id" value="<?= $deleteId ?>">
<input type="hidden" name="really" value="yes">
<?= Token::getInstance()->getTokenInput('delete-news') ?>
<?= Token::getInstance()->getTokenInput('delete-news') ?>
<button class="btn btn-warning" type="submit" name="submit">
<?= Translation::get('ad_news_yesdelete') ?>
</button>
Expand All @@ -574,7 +592,7 @@ class="form-control">
} else {
if (Token::getInstance()->verifyToken('delete-news', $csrfToken)) {
$deleteId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$news->deleteNews((int)$deleteId);
$news->delete((int)$deleteId);
echo Alert::success('ad_news_delsuc');
printf('<div class="row">&rarr; <a href="?action=news">%s</a></p>', Translation::get('msgNews'));
}
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/news.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
$newsMainHeader = $faqConfig->getTitle() . Translation::get('msgNews');

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

$newsContent = $news['content'];
$newsHeader = $news['header'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function create(Request $request): JsonResponse
$notification->sendFaqCommentNotification($faq, $commentEntity);
} else {
$news = new News($this->configuration);
$newsData = $news->getNewsEntry($id);
$newsData = $news->get($id);
$notification->sendNewsCommentNotification($newsData, $commentEntity);
}

Expand Down
178 changes: 178 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Entity/NewsMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

/**
* The News message model class
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ\Entity
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-03-24
*/

namespace phpMyFAQ\Entity;

use DateTime;

class NewsMessage
{
private int $id;

private string $language;

private string $header;

private string $message;

private DateTime $created;

private string $author;

private string $email;

private bool $active;

private bool $comment;

private ?DateTime $dateStart;

private ?DateTime $dateEnd;

private ?string $link;

private ?string $linkTitle;

private ?string $linkTarget;

public function getId(): int
{
return $this->id;
}
public function setId(int $id): NewsMessage
{
$this->id = $id;
return $this;
}
public function getLanguage(): string
{
return $this->language;
}
public function setLanguage(string $language): NewsMessage
{
$this->language = $language;
return $this;
}
public function getHeader(): string
{
return $this->header;
}
public function setHeader(string $header): NewsMessage
{
$this->header = $header;
return $this;
}
public function getMessage(): string
{
return $this->message;
}
public function setMessage(string $message): NewsMessage
{
$this->message = $message;
return $this;
}
public function getCreated(): DateTime
{
return $this->created;
}
public function setCreated(DateTime $created): NewsMessage
{
$this->created = $created;
return $this;
}
public function getAuthor(): string
{
return $this->author;
}
public function setAuthor(string $author): NewsMessage
{
$this->author = $author;
return $this;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): NewsMessage
{
$this->email = $email;
return $this;
}
public function isActive(): bool
{
return $this->active;
}
public function setActive(bool $active): NewsMessage
{
$this->active = $active;
return $this;
}
public function isComment(): bool
{
return $this->comment;
}
public function setComment(bool $comment): NewsMessage
{
$this->comment = $comment;
return $this;
}
public function getDateStart(): ?DateTime
{
return $this->dateStart ?? null;
}
public function setDateStart(DateTime $dateStart): NewsMessage
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?DateTime
{
return $this->dateEnd ?? null;
}
public function setDateEnd(DateTime $dateEnd): NewsMessage
{
$this->dateEnd = $dateEnd;
return $this;
}
public function getLink(): string
{
return $this->link ?? '';
}
public function setLink(string $link): NewsMessage
{
$this->link = $link;
return $this;
}
public function getLinkTitle(): string
{
return $this->linkTitle ?? '';
}
public function setLinkTitle(string $linkTitle): NewsMessage
{
$this->linkTitle = $linkTitle;
return $this;
}
public function getLinkTarget(): string
{
return $this->linkTarget ?? '';
}
public function setLinkTarget(string $linkTarget): NewsMessage
{
$this->linkTarget = $linkTarget;
return $this;
}
}

0 comments on commit ce02c70

Please sign in to comment.