diff --git a/public/index.php b/public/index.php index 17bc652..d93991d 100644 --- a/public/index.php +++ b/public/index.php @@ -10,6 +10,7 @@ $loader->register(); $security = new \Silex\Controller\SecurityController(); $user = new \Silex\Controller\UserController(); +$admin = new \Silex\Controller\AdminController(); $router = new Router($_SERVER['REQUEST_URI']); $router->get('/^$/', [$user, 'index']); $router->get('/^recent\/(?\d+)$/', [$user, 'index']); @@ -18,6 +19,8 @@ $router->get('/^comments\/(?[\w-]+)$/', [$user, 'viewPostComments']); $router->match('/^login$/', [$security, 'login']); $router->match('/^register$/', [$security, 'register']); $router->match('/^logout$/', [$security, 'logout']); +$router->match('/^admin\/publish$/', [$admin, 'publish']); +$router->match('/^admin\/edit\/(?\d+)$/', [$admin, 'edit']); $di = new \Silex\DI\DI($router); $router->run($di)->render($di, __DIR__ . '/../' . VIEW_PATH); diff --git a/src/Silex/Controller/AdminController.php b/src/Silex/Controller/AdminController.php new file mode 100644 index 0000000..315fcdb --- /dev/null +++ b/src/Silex/Controller/AdminController.php @@ -0,0 +1,35 @@ +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]); + } +} diff --git a/src/Silex/Controller/FrontController.php b/src/Silex/Controller/FrontController.php new file mode 100644 index 0000000..747c1ca --- /dev/null +++ b/src/Silex/Controller/FrontController.php @@ -0,0 +1,28 @@ +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); + } +} diff --git a/src/Silex/Gateway/NewsGateway.php b/src/Silex/Gateway/NewsGateway.php index 53f7a13..a2b3f37 100644 --- a/src/Silex/Gateway/NewsGateway.php +++ b/src/Silex/Gateway/NewsGateway.php @@ -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'])); } } diff --git a/src/Silex/Model/News.php b/src/Silex/Model/News.php index afc736a..c4f401b 100644 --- a/src/Silex/Model/News.php +++ b/src/Silex/Model/News.php @@ -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; + } } diff --git a/src/Silex/Model/User.php b/src/Silex/Model/User.php index a5d1d58..4733d3b 100644 --- a/src/Silex/Model/User.php +++ b/src/Silex/Model/User.php @@ -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; diff --git a/src/Silex/Router/Route.php b/src/Silex/Router/Route.php index 2278e61..3574bea 100644 --- a/src/Silex/Router/Route.php +++ b/src/Silex/Router/Route.php @@ -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; diff --git a/src/Silex/Router/Router.php b/src/Silex/Router/Router.php index 602f0ae..23bc022 100644 --- a/src/Silex/Router/Router.php +++ b/src/Silex/Router/Router.php @@ -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'); diff --git a/src/Silex/Security/Security.php b/src/Silex/Security/Security.php index 9d9a635..40f7b21 100644 --- a/src/Silex/Security/Security.php +++ b/src/Silex/Security/Security.php @@ -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) { diff --git a/views/edit.php b/views/edit.php new file mode 100644 index 0000000..0d7d9f9 --- /dev/null +++ b/views/edit.php @@ -0,0 +1,20 @@ +
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+
+ +
+
+
diff --git a/views/layout.php b/views/layout.php index f360250..f0d76b2 100644 --- a/views/layout.php +++ b/views/layout.php @@ -3,7 +3,7 @@ - <?= $viewsArgs['title'] ?? 'Is it a blog?' ?> + <?= $title ?? 'Is it a blog?' ?> diff --git a/views/newsView.php b/views/newsView.php index 3716e3b..712f8cd 100644 --- a/views/newsView.php +++ b/views/newsView.php @@ -1,4 +1,4 @@ - +getTitle(); ?>

News

@@ -8,7 +8,7 @@
- getContent() ?>... + getContent() ?>
\ No newline at end of file