Permet d'ajouter de nouveaux articles
This commit is contained in:
35
src/Silex/Controller/AdminController.php
Normal file
35
src/Silex/Controller/AdminController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\News;
|
||||
|
||||
class AdminController
|
||||
{
|
||||
public function publish(DI $di): HttpResponse
|
||||
{
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$news = new News(-1, $_POST['title'], $_POST['content'], new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||
$di->getNewsGateway()->insert($news);
|
||||
HttpResponse::redirect($di->getRouter()->url('news/' . $news->getId()));
|
||||
}
|
||||
$news = new News(-1, '', '', new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||
return HttpResponse::found('edit', ['news' => $news]);
|
||||
}
|
||||
|
||||
public function edit(DI $di, array $params): HttpResponse
|
||||
{
|
||||
$news = $di->getNewsGateway()->getById(intval($params['id']));
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$news = new News($news->getId(), $_POST['title'], $_POST['content'], $news->getPublicationDate(), $news->getAuthorId());
|
||||
$di->getNewsGateway()->update($news);
|
||||
HttpResponse::redirect($di->getRouter()->url('news/' . $news->getId()));
|
||||
}
|
||||
return HttpResponse::found('edit', ['news' => $news]);
|
||||
}
|
||||
}
|
28
src/Silex/Controller/FrontController.php
Normal file
28
src/Silex/Controller/FrontController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Controller;
|
||||
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Router\Route;
|
||||
|
||||
class FrontController
|
||||
{
|
||||
private Route $route;
|
||||
|
||||
public function __construct(Route $route)
|
||||
{
|
||||
$this->route = $route;
|
||||
}
|
||||
|
||||
public function run(DI $di): HttpResponse
|
||||
{
|
||||
if ($this->route->getController() instanceof AdminController
|
||||
&& ($di->getSecurity()->getCurrentUser() === null || !$di->getSecurity()->getCurrentUser()->isAdmin())) {
|
||||
HttpResponse::redirect($di->getRouter()->url('login'));
|
||||
}
|
||||
return $this->route->call($di);
|
||||
}
|
||||
}
|
@@ -19,12 +19,31 @@ class NewsGateway
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function insert(News $news): void
|
||||
{
|
||||
$req = $this->pdo->prepare('INSERT INTO news (title, slug, content, author_id) VALUES (:title, :slug, :content, :author_id);');
|
||||
$req->execute(['title' => $news->getTitle(), 'slug' => $news->getSlug(), 'content' => $news->getContent(), 'author_id' => $news->getAuthorId()]);
|
||||
$news->setId(intval($this->pdo->lastInsertId()));
|
||||
}
|
||||
|
||||
public function update(News $news): void
|
||||
{
|
||||
$req = $this->pdo->prepare('UPDATE news SET title = :title, slug = :slug, content = :content, author_id = :author_id WHERE id_news = :id_news;');
|
||||
$req->execute(['title' => $news->getTitle(), 'slug' => $news->getSlug(), 'content' => $news->getContent(), 'author_id' => $news->getAuthorId(), 'id_news' => $news->getId()]);
|
||||
}
|
||||
|
||||
public function delete(News $news): void
|
||||
{
|
||||
$req = $this->pdo->prepare('DELETE FROM news WHERE id = :id_news;');
|
||||
$req->execute(['id_news' => $news->getId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return News[]
|
||||
*/
|
||||
public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array
|
||||
{
|
||||
$req = $this->pdo->prepare('SELECT id_news, title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
|
||||
$req = $this->pdo->prepare('SELECT id_news, title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date, author_id FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
|
||||
$req->bindValue('limit', $limit, PDO::PARAM_INT);
|
||||
$req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT);
|
||||
if (!$req->execute()) {
|
||||
@@ -46,7 +65,7 @@ class NewsGateway
|
||||
return intval($req->fetch()['nb']);
|
||||
}
|
||||
|
||||
public function getById(int $id): News
|
||||
public function getById(int $id): News
|
||||
{
|
||||
$req = $this->pdo->prepare('SELECT * FROM news WHERE id_news=:id;');
|
||||
$req->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
@@ -61,6 +80,6 @@ class NewsGateway
|
||||
|
||||
private function createNews(array $data): News
|
||||
{
|
||||
return new News(intval($data['id_news']), $data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
|
||||
return new News(intval($data['id_news']), $data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']), intval($data['author_id']));
|
||||
}
|
||||
}
|
||||
|
@@ -12,13 +12,15 @@ class News
|
||||
private string $title;
|
||||
private string $content;
|
||||
private DateTime $publicationDate;
|
||||
private int $authorId;
|
||||
|
||||
public function __construct(int $id, string $title, string $content, DateTime $publicationDate)
|
||||
public function __construct(int $id, string $title, string $content, DateTime $publicationDate, int $authorId)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->title = $title;
|
||||
$this->content = $content;
|
||||
$this->publicationDate = $publicationDate;
|
||||
$this->authorId = $authorId;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
@@ -31,6 +33,11 @@ class News
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getSlug(): string
|
||||
{
|
||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->title)));
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
@@ -40,4 +47,14 @@ class News
|
||||
{
|
||||
return $this->publicationDate;
|
||||
}
|
||||
|
||||
public function getAuthorId(): int
|
||||
{
|
||||
return $this->authorId;
|
||||
}
|
||||
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
}
|
||||
|
@@ -40,6 +40,11 @@ class User
|
||||
return $this->role;
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role >= 1;
|
||||
}
|
||||
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id_user = $id;
|
||||
|
@@ -25,6 +25,11 @@ class Route
|
||||
$this->callable = $callable;
|
||||
}
|
||||
|
||||
public function getController(): object
|
||||
{
|
||||
return $this->callable[0];
|
||||
}
|
||||
|
||||
public function matches(string $url): bool
|
||||
{
|
||||
return preg_match($this->path, $url, $this->matches) === 1;
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Silex\Router;
|
||||
|
||||
use Silex\Controller\FrontController;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\DI\DI;
|
||||
|
||||
@@ -61,7 +62,6 @@ class Router
|
||||
} else {
|
||||
return $this->basePath . '/' . $url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function run(DI $di): HttpResponse
|
||||
@@ -79,7 +79,7 @@ class Router
|
||||
}
|
||||
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
|
||||
if ($route->matches($url)) {
|
||||
return $route->call($di);
|
||||
return (new FrontController($route))->run($di);
|
||||
}
|
||||
}
|
||||
throw new RouteNotFoundException('No matching routes');
|
||||
|
@@ -38,6 +38,11 @@ class Security
|
||||
unset($this->session[USER]);
|
||||
}
|
||||
|
||||
public function getCurrentUserId(): ?int
|
||||
{
|
||||
return $this->session[USER] ?? null;
|
||||
}
|
||||
|
||||
public function getCurrentUser(): ?User
|
||||
{
|
||||
if (!empty($this->session[USER]) && $this->user === null) {
|
||||
|
Reference in New Issue
Block a user