Valide le contenu des news
This commit is contained in:
@@ -8,28 +8,45 @@ use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\News;
|
||||
use Silex\Validation\NewsValidation;
|
||||
|
||||
class AdminController
|
||||
{
|
||||
public function publish(DI $di): HttpResponse
|
||||
{
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$errors = [];
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && NewsValidation::isValidNews($_POST, $errors)) {
|
||||
$news = new News(-1, $_POST['title'], $_POST['content'], new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||
$di->getNewsGateway()->insert($news);
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
$news = new News(-1, '', '', new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||
return HttpResponse::found('edit', ['news' => $news]);
|
||||
return HttpResponse::found('edit', ['news' => $news, 'errors' => $errors]);
|
||||
}
|
||||
|
||||
public function edit(DI $di, array $params): HttpResponse
|
||||
{
|
||||
$news = $di->getNewsGateway()->getById(intval($params['id']));
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if ($news === null) {
|
||||
return new HttpResponse(404, 'errors', ['errors' => ['Unknown news']]);
|
||||
}
|
||||
$errors = [];
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && NewsValidation::isValidNews($_POST, $errors)) {
|
||||
$news = new News($news->getId(), $_POST['title'], $_POST['content'], $news->getPublicationDate(), $news->getAuthorId());
|
||||
$di->getNewsGateway()->update($news);
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
return HttpResponse::found('edit', ['news' => $news]);
|
||||
return HttpResponse::found('edit', ['news' => $news, 'errors' => $errors]);
|
||||
}
|
||||
|
||||
public function delete(DI $di, array $params): HttpResponse
|
||||
{
|
||||
$news = $di->getNewsGateway()->getById(intval($params['id']));
|
||||
if ($news === null) {
|
||||
return new HttpResponse(404, 'errors', ['errors' => ['Unknown news']]);
|
||||
}
|
||||
$di->getNewsGateway()->delete($news);
|
||||
HttpResponse::redirect($di->getRouter()->url(''));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
25
src/Silex/Validation/NewsValidation.php
Normal file
25
src/Silex/Validation/NewsValidation.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Silex\Validation;
|
||||
|
||||
final class NewsValidation
|
||||
{
|
||||
public static function isValidNews(array &$post, array &$errors): bool
|
||||
{
|
||||
if (empty($post['title'])) {
|
||||
$errors[] = 'Empty title';
|
||||
}
|
||||
if (empty($post['content'])) {
|
||||
$errors[] = 'Empty message';
|
||||
}
|
||||
if (!empty($errors)) {
|
||||
return false;
|
||||
}
|
||||
if (strlen($post['title']) > 60) {
|
||||
$errors[] = 'Title too long';
|
||||
}
|
||||
$post['title'] = htmlspecialchars($post['title']);
|
||||
$post['content'] = htmlspecialchars($post['content']);
|
||||
return empty($errors);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user