diff --git a/public/index.php b/public/index.php index f55cee1..7a9431f 100644 --- a/public/index.php +++ b/public/index.php @@ -10,14 +10,12 @@ $loader->register(); $security = new \Silex\Controller\SecurityController(); $visitor = new \Silex\Controller\VisitorController(); -$user = new \Silex\Controller\UserController(); $admin = new \Silex\Controller\AdminController(); $router = new Router($_SERVER['REQUEST_URI']); -$router->setBasePath("~cofrizot/silex/index.php"); $router->get('/^$/', [$visitor, 'index']); $router->get('/^recent\/(?\d+)$/', [$visitor, 'index']); $router->get('/^news\/(?[A-Za-z0-9-]+)-(?\d+)$/', [$visitor, 'viewPost']); -$router->post('/^comment\/(?\d+)$/', [$user, 'comment']); +$router->post('/^comment\/(?\d+)$/', [$visitor, 'comment']); $router->match('/^login$/', [$security, 'login']); $router->match('/^register$/', [$security, 'register']); $router->match('/^logout$/', [$security, 'logout']); diff --git a/src/Silex/Controller/FrontController.php b/src/Silex/Controller/FrontController.php index ff149c3..747c1ca 100644 --- a/src/Silex/Controller/FrontController.php +++ b/src/Silex/Controller/FrontController.php @@ -23,13 +23,6 @@ class FrontController && ($di->getSecurity()->getCurrentUser() === null || !$di->getSecurity()->getCurrentUser()->isAdmin())) { HttpResponse::redirect($di->getRouter()->url('login')); } - - if($this->route->getController() instanceof UserController - && $di->getSecurity()->getCurrentUser() === null) - { - HttpResponse::redirect($di->getRouter()->url('login')); - } - return $this->route->call($di); } } diff --git a/src/Silex/Controller/UserController.php b/src/Silex/Controller/UserController.php deleted file mode 100644 index d7b855d..0000000 --- a/src/Silex/Controller/UserController.php +++ /dev/null @@ -1,24 +0,0 @@ -getNewsGateway()->getById($newsId); - $di->getCommentGateway()->insert(new Comment(-1, $newsId, new DateTime(), $_POST['content'], $di->getSecurity()->getCurrentUserId())); - HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect())); - } -} diff --git a/src/Silex/Controller/VisitorController.php b/src/Silex/Controller/VisitorController.php index 2164ebb..935cafe 100644 --- a/src/Silex/Controller/VisitorController.php +++ b/src/Silex/Controller/VisitorController.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace Silex\Controller; +use DateTime; use Silex\DI\DI; use Silex\Http\HttpResponse; +use Silex\Model\Comment; use Silex\Util\Pagination; class VisitorController { @@ -23,7 +25,7 @@ class VisitorController { $nbPages = Pagination::getNbPages($total, self::PER_PAGE); $news = $gw->getPaginatedRecentNews($page , self::PER_PAGE); $nbComments = $gwc->getCommentNumber(); - if($user !== null){ + if ($user !== null) { $nbCommentsByUser = $gwc->getCommentNumberFromUser($user->getId()); } else { $nbCommentsByUser = 0; @@ -35,10 +37,25 @@ class VisitorController { { $newsId = intval($params['id']); $news = $di->getNewsGateway()->getById($newsId); - if($news->getSlug() !== $params['slug']){ + if ($news->getSlug() !== $params['slug']) { HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect())); } $comments = $di->getCommentGateway()->getByNewsId($newsId); return new HttpResponse(200, 'newsView', ['news' => $news, 'comments' => $comments]); } -} \ No newline at end of file + + public function comment(DI $di, array $params): void + { + $newsId = intval($params['id']); + $news = $di->getNewsGateway()->getById($newsId); + $comment = new Comment(-1, $newsId, new DateTime(), $_POST['content']); + $author = $di->getSecurity()->getCurrentUser(); + if ($author !== null) { + $comment->setAuthor($author); + } else { + $comment->setAuthorName($_POST['name']); + } + $di->getCommentGateway()->insert($comment); + HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect())); + } +} diff --git a/src/Silex/Gateway/CommentGateway.php b/src/Silex/Gateway/CommentGateway.php index b3ef534..8fac53c 100644 --- a/src/Silex/Gateway/CommentGateway.php +++ b/src/Silex/Gateway/CommentGateway.php @@ -19,8 +19,16 @@ class CommentGateway public function insert(Comment $comment): bool { - $req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id) VALUES (:news_id, :content, :author_id);'); - $req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId()]); + $req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id, author_name) VALUES (:news_id, :content, :author_id, :author_name);'); + $req->bindValue(':news_id', $comment->getNewsId(), PDO::PARAM_INT); + $req->bindValue(':content', $comment->getContent()); + $req->bindValue(':author_id', $comment->getAuthorId(), $comment->getAuthorId() !== null ? PDO::PARAM_INT : PDO::PARAM_NULL); + if ($comment->getAuthorId() === null) { + $req->bindValue(':author_name', $comment->getAuthorName()); + } else { + $req->bindValue(':author_name', null, PDO::PARAM_NULL); + } + $req->execute(); $comment->setId(intval($this->pdo->lastInsertId())); return true; } @@ -41,9 +49,9 @@ class CommentGateway /** * @return Comment[] */ - public function getByNewsId(int $id): array + public function getByNewsId(int $id): array { - $req = $this->pdo->prepare('SELECT c.*, u.login author_login FROM comment c INNER JOIN registered_user u ON u.id_user = c.author_id WHERE c.news_id = :id ORDER BY c.publication_date ASC'); + $req = $this->pdo->prepare('SELECT c.*, COALESCE(c.author_name, u.login) author_name FROM comment c LEFT JOIN registered_user u ON u.id_user = c.author_id WHERE c.news_id = :id ORDER BY c.publication_date ASC'); $req->bindValue(':id', $id, PDO::PARAM_INT); if (!$req->execute()) { return []; @@ -60,8 +68,7 @@ class CommentGateway $req = $this->pdo->prepare('SELECT COUNT(*) FROM comment'); $req->execute(); $data = $req->fetch(); - $nbComment = intval($data[0]); - return $nbComment; + return intval($data[0]); } public function getCommentNumberFromUser(int $id): int @@ -70,8 +77,7 @@ class CommentGateway $req->bindValue(':id', $id, PDO::PARAM_INT); $req->execute(); $data = $req->fetch(); - $nbComment = intval($data[0]); - return $nbComment; + return intval($data[0]); } private function createComment(array $data): Comment @@ -83,9 +89,9 @@ class CommentGateway $data['content'], intval($data['author_id']) ); - if (isset($data['author_login'])) { - $comment->setAuthorLogin($data['author_login']); + if (isset($data['author_name'])) { + $comment->setAuthorName($data['author_name']); } return $comment; } -} \ No newline at end of file +} diff --git a/src/Silex/Model/Comment.php b/src/Silex/Model/Comment.php index fd57121..d64ce6a 100644 --- a/src/Silex/Model/Comment.php +++ b/src/Silex/Model/Comment.php @@ -12,16 +12,15 @@ class Comment private int $newsId; private DateTime $publicationDate; private string $content; - private int $authorId; - private string $authorLogin; + private ?int $authorId = null; + private string $authorName; - public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content, int $authorId) + public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content) { $this->idComment = $idComment; $this->newsId = $newsId; $this->publicationDate = $publicationDate; $this->content = $content; - $this->authorId = $authorId; } public function getId(): int @@ -44,14 +43,14 @@ class Comment return $this->content; } - public function getAuthorId(): int + public function getAuthorId(): ?int { return $this->authorId; } - public function getAuthorLogin(): string + public function getAuthorName(): string { - return $this->authorLogin; + return $this->authorName; } public function setId(int $id): void @@ -59,8 +58,14 @@ class Comment $this->idComment = $id; } - public function setAuthorLogin(string $authorLogin): void + public function setAuthorName(string $authorName): void { - $this->authorLogin = $authorLogin; + $this->authorName = $authorName; } -} \ No newline at end of file + + public function setAuthor(User $author): void + { + $this->authorId = $author->getId(); + $this->authorName = $author->getLogin(); + } +} diff --git a/tables.sql b/tables.sql index 5678201..1996a27 100644 --- a/tables.sql +++ b/tables.sql @@ -20,7 +20,9 @@ CREATE TABLE comment ( news_id INT NOT NULL, publication_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, content TEXT NOT NULL, - author_id INT NOT NULL, + author_id INT NULL, + author_name VARCHAR(32) NULL, + CONSTRAINT either_authors CHECK ((author_id IS NULL) != (author_name IS NULL)), FOREIGN KEY (news_id) REFERENCES news(id_news) ON DELETE CASCADE, FOREIGN KEY (author_id) REFERENCES registered_user(id_user) diff --git a/views/newsView.php b/views/newsView.php index cd52780..8daf54a 100644 --- a/views/newsView.php +++ b/views/newsView.php @@ -1,43 +1,59 @@ -getTitle(); ?> +getTitle(); +?>

News

- getTitle() ?> + getTitle() ?>

- getContent() ?> + getContent() ?>
- +
- From getAuthorLogin() ?> published on getPublicationDate()->format('Y-m-d H:i:s') ?> + From getAuthorName() ?> published on getPublicationDate()->format('Y-m-d H:i:s') ?>
getContent() ?>
- getCurrentUserId() !== null) : ?> -
+ + getCurrentUserId() === null): ?>
- +
- +
- -
-
- -
-
-
+
+ +
+ +
+
+ +
+
+ +
+
+