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 $sendMail; |
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[] = 'SendMail'; |
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 string $slug |
35
|
|
|
* @param string $page |
36
|
|
|
* @param int $linesPerPage |
37
|
|
|
* @throws \ReflectionException |
38
|
|
|
* @throws \Twig_Error_Loader |
39
|
|
|
* @throws \Twig_Error_Runtime |
40
|
|
|
* @throws \Twig_Error_Syntax |
41
|
|
|
*/ |
42
|
|
|
public function viewPost(string $slug, string $page = "page-1", int $linesPerPage = Constant::COMMENTS_PER_PAGE) |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
$postId = (int)$this->postModel->getPostIdFromSlug($slug); |
46
|
|
|
|
47
|
|
|
$posts = $this->postModel->getSinglePost($postId); |
48
|
|
|
|
49
|
|
|
if($posts === false) |
50
|
|
|
{ |
51
|
|
|
throw new \Exception("Page no longer exists", "404"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
//only admins can view unpublished posts |
55
|
|
|
if (!$posts->published) { |
56
|
|
|
if (!$this->auth->isAdmin()) { |
57
|
|
|
throw new \Exception("File does not exist", "404"); |
58
|
|
|
} |
59
|
|
|
$this->alertBox->setAlert('This post is not yet published', 'warning'); |
60
|
|
|
$this->data["postwarning"] = "This post is unpublished, only admins can see this page"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$totalComments = $this->commentModel->countCommentsOnPost($postId); |
64
|
|
|
$pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage); |
65
|
|
|
|
66
|
|
|
if ($linesPerPage !== Constant::COMMENTS_PER_PAGE) { |
67
|
|
|
$this->data['paginationPostsPerPage'] = $linesPerPage; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->sendSessionVars(); |
71
|
|
|
$this->data['configs'] = $this->siteConfig->getSiteConfig(); |
72
|
|
|
$this->data['post'] = $posts; |
73
|
|
|
$this->data['postTags'] = $this->tagModel->getTagsOnPost($postId); |
74
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
75
|
|
|
$this->data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $pagination["offset"], |
76
|
|
|
$linesPerPage); |
77
|
|
|
$this->data['pagination'] = $pagination; |
78
|
|
|
|
79
|
|
|
$this->renderView('Post'); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add a comment to the post |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function addComment() |
88
|
|
|
{ |
89
|
|
|
$this->onlyPost(); |
90
|
|
|
$this->onlyUser(); |
91
|
|
|
|
92
|
|
|
//get the session userId |
93
|
|
|
$userId = (int)$this->session->get("userId"); |
94
|
|
|
$comment = (string)$this->request->getData("newComment"); |
95
|
|
|
$postId = (int)$this->request->getData("postId"); |
96
|
|
|
|
97
|
|
|
//check if we are admin, Admins do not need moderation |
98
|
|
|
$admin = $this->session->get('user_role_level') >= Constant::ADMIN_LEVEL; |
99
|
|
|
$commentId = $this->commentModel->addComment($postId, $userId, $comment, $admin); |
100
|
|
|
|
101
|
|
|
if (!$admin) //if we are not an admin, send an email to alert and add an alertBox |
102
|
|
|
{ |
103
|
|
|
$siteConfig = $this->siteConfig->getSiteConfig(); |
104
|
|
|
$post = $this->postModel->getSinglePost($postId); |
105
|
|
|
$baseUrl = $this->request->getBaseUrl(); |
106
|
|
|
|
107
|
|
|
$emailMessage = "<h1>New comment on post " . $post->title . "</a></h1>"; |
108
|
|
|
$emailMessage .= "<p>Check it out <a href='" . $baseUrl . "admin/comments/moderate-comment/" . $commentId . "'>here</a> </p>"; |
109
|
|
|
|
110
|
|
|
$this->sendMail->send($siteConfig["admin_email_address"], "New comment added", $emailMessage); |
111
|
|
|
|
112
|
|
|
$this->alertBox->setAlert("Your post will be published after moderation."); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$postSlug = $this->postModel->getPostSlugFromId($postId); |
116
|
|
|
|
117
|
|
|
$this->response->redirect("/post/view-post/" . $postSlug); |
118
|
|
|
} |
119
|
|
|
} |