1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\CommentModel; |
6
|
|
|
use App\Models\PostModel; |
7
|
|
|
use App\Models\TagModel; |
8
|
|
|
use Core\Constant; |
9
|
|
|
use Core\Controller; |
10
|
|
|
use Core\Container; |
11
|
|
|
|
12
|
|
|
class Post extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $siteConfig; |
16
|
|
|
protected $pagination; |
17
|
|
|
|
18
|
|
|
private $commentModel; |
19
|
|
|
private $tagModel; |
20
|
|
|
private $postModel; |
21
|
|
|
|
22
|
|
|
public function __construct(Container $container) |
23
|
|
|
{ |
24
|
|
|
$this->loadModules[] = 'SiteConfig'; |
25
|
|
|
$this->loadModules[] = 'pagination'; |
26
|
|
|
parent::__construct($container); |
27
|
|
|
$this->commentModel = new CommentModel($this->container); |
28
|
|
|
$this->tagModel = new TagModel($this->container); |
29
|
|
|
$this->postModel = new PostModel($this->container); |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $slug |
35
|
|
|
* @throws \Exception |
36
|
|
|
* @throws \ReflectionException |
37
|
|
|
* @throws \Twig_Error_Loader |
38
|
|
|
* @throws \Twig_Error_Runtime |
39
|
|
|
* @throws \Twig_Error_Syntax |
40
|
|
|
*/ |
41
|
|
|
public function viewPost(string $slug, string $page = "page-1", int $linesPerPage = Constant::COMMENTS_PER_PAGE) |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
//TODO Implement comment pagination |
45
|
|
|
|
46
|
|
|
$postId = $this->postModel->getPostIdFromSlug($slug); |
47
|
|
|
|
48
|
|
|
$posts = $this->postModel->getSinglePost($postId); |
49
|
|
|
//only admins can view unpublished posts |
50
|
|
|
if (!$posts->published) { |
51
|
|
|
if (!$this->auth->isAdmin()) { |
52
|
|
|
throw new \Exception("File does not exist", "404"); |
53
|
|
|
} |
54
|
|
|
$this->alertBox->setAlert('This post is not yet published', 'warning'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
$totalComments = $this->commentModel->countCommentsOnPost($postId); |
59
|
|
|
$pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage); |
60
|
|
|
|
61
|
|
|
if ($linesPerPage !== Constant::COMMENTS_PER_PAGE) { |
62
|
|
|
$this->data['paginationPostsPerPage'] = $linesPerPage; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->sendSessionVars(); |
67
|
|
|
$this->data['configs'] = $this->siteConfig->getSiteConfig(); |
68
|
|
|
$this->data['post'] = $posts; |
69
|
|
|
$this->data['postTags'] = $this->tagModel->getTagsOnPost($postId); |
70
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
71
|
|
|
$this->data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $pagination["offset"], $linesPerPage); |
72
|
|
|
$this->data['pagination'] = $pagination; |
73
|
|
|
|
74
|
|
|
$this->renderView('post'); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add a comment to the post |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
public function addComment() |
83
|
|
|
{ |
84
|
|
|
$this->onlyPost(); |
85
|
|
|
$this->onlyUser(); |
86
|
|
|
|
87
|
|
|
//get the session userId |
88
|
|
|
$userId = $this->session->get("userId"); |
89
|
|
|
$comment = $this->request->getData("newComment"); |
90
|
|
|
$postId = $this->request->getData("postId"); |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->commentModel->addComment($postId, $userId, $comment); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$refererUrl = $this->request->getReferer(); |
96
|
|
|
$baseUrl = $this->request->getBaseUrl(); |
97
|
|
|
$redirectUrl = $this->removeFromBeginning($refererUrl, $baseUrl); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->alertBox->setAlert("Your post will be published after moderation."); |
100
|
|
|
|
101
|
|
|
$this->response->redirect($redirectUrl); |
102
|
|
|
} |
103
|
|
|
} |