Permet d'ajouter de nouveaux articles

main
Clément FRÉVILLE 2 years ago
parent 8118cfe188
commit d0343eb5a9

@ -10,6 +10,7 @@ $loader->register();
$security = new \Silex\Controller\SecurityController(); $security = new \Silex\Controller\SecurityController();
$user = new \Silex\Controller\UserController(); $user = new \Silex\Controller\UserController();
$admin = new \Silex\Controller\AdminController();
$router = new Router($_SERVER['REQUEST_URI']); $router = new Router($_SERVER['REQUEST_URI']);
$router->get('/^$/', [$user, 'index']); $router->get('/^$/', [$user, 'index']);
$router->get('/^recent\/(?<page>\d+)$/', [$user, 'index']); $router->get('/^recent\/(?<page>\d+)$/', [$user, 'index']);
@ -18,6 +19,8 @@ $router->get('/^comments\/(?<id>[\w-]+)$/', [$user, 'viewPostComments']);
$router->match('/^login$/', [$security, 'login']); $router->match('/^login$/', [$security, 'login']);
$router->match('/^register$/', [$security, 'register']); $router->match('/^register$/', [$security, 'register']);
$router->match('/^logout$/', [$security, 'logout']); $router->match('/^logout$/', [$security, 'logout']);
$router->match('/^admin\/publish$/', [$admin, 'publish']);
$router->match('/^admin\/edit\/(?<id>\d+)$/', [$admin, 'edit']);
$di = new \Silex\DI\DI($router); $di = new \Silex\DI\DI($router);
$router->run($di)->render($di, __DIR__ . '/../' . VIEW_PATH); $router->run($di)->render($di, __DIR__ . '/../' . VIEW_PATH);

@ -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]);
}
}

@ -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; $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[] * @return News[]
*/ */
public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array 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('limit', $limit, PDO::PARAM_INT);
$req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT); $req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT);
if (!$req->execute()) { if (!$req->execute()) {
@ -61,6 +80,6 @@ class NewsGateway
private function createNews(array $data): News 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 $title;
private string $content; private string $content;
private DateTime $publicationDate; 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->id = $id;
$this->title = $title; $this->title = $title;
$this->content = $content; $this->content = $content;
$this->publicationDate = $publicationDate; $this->publicationDate = $publicationDate;
$this->authorId = $authorId;
} }
public function getId(): int public function getId(): int
@ -31,6 +33,11 @@ class News
return $this->title; return $this->title;
} }
public function getSlug(): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->title)));
}
public function getContent(): string public function getContent(): string
{ {
return $this->content; return $this->content;
@ -40,4 +47,14 @@ class News
{ {
return $this->publicationDate; 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; return $this->role;
} }
public function isAdmin(): bool
{
return $this->role >= 1;
}
public function setId(int $id) public function setId(int $id)
{ {
$this->id_user = $id; $this->id_user = $id;

@ -25,6 +25,11 @@ class Route
$this->callable = $callable; $this->callable = $callable;
} }
public function getController(): object
{
return $this->callable[0];
}
public function matches(string $url): bool public function matches(string $url): bool
{ {
return preg_match($this->path, $url, $this->matches) === 1; return preg_match($this->path, $url, $this->matches) === 1;

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Silex\Router; namespace Silex\Router;
use Silex\Controller\FrontController;
use Silex\Http\HttpResponse; use Silex\Http\HttpResponse;
use Silex\DI\DI; use Silex\DI\DI;
@ -61,7 +62,6 @@ class Router
} else { } else {
return $this->basePath . '/' . $url; return $this->basePath . '/' . $url;
} }
} }
public function run(DI $di): HttpResponse public function run(DI $di): HttpResponse
@ -79,7 +79,7 @@ class Router
} }
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
if ($route->matches($url)) { if ($route->matches($url)) {
return $route->call($di); return (new FrontController($route))->run($di);
} }
} }
throw new RouteNotFoundException('No matching routes'); throw new RouteNotFoundException('No matching routes');

@ -38,6 +38,11 @@ class Security
unset($this->session[USER]); unset($this->session[USER]);
} }
public function getCurrentUserId(): ?int
{
return $this->session[USER] ?? null;
}
public function getCurrentUser(): ?User public function getCurrentUser(): ?User
{ {
if (!empty($this->session[USER]) && $this->user === null) { if (!empty($this->session[USER]) && $this->user === null) {

@ -0,0 +1,20 @@
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
<div class="field">
<label class="label" for="title">Title</label>
<div class="control">
<input class="input" type="text" id="title" name="title" value="<?= $params['news']->getTitle() ?>">
</div>
</div>
<div class="field">
<label class="label" for="content">Content</label>
<div class="control">
<textarea class="textarea" id="content" name="content" rows="10"><?= $params['news']->getContent() ?></textarea>
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-link">Submit</button>
</div>
</div>
</form>

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= $viewsArgs['title'] ?? 'Is it a blog?' ?></title> <title><?= $title ?? 'Is it a blog?' ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
</head> </head>
<body> <body>

@ -1,4 +1,4 @@
<?php $params['title'] = 'NewsView'; ?> <?php $title = $params['news']->getTitle(); ?>
<h1>News</h1> <h1>News</h1>
<div class="card"> <div class="card">
<header class="card-header"> <header class="card-header">
@ -8,7 +8,7 @@
</header> </header>
<div class="card-content"> <div class="card-content">
<div class="content"> <div class="content">
<?= $params['news']->getContent() ?>... <?= $params['news']->getContent() ?>
</div> </div>
</div> </div>
</div> </div>
Loading…
Cancel
Save