1 | <?php |
||
2 | |||
3 | namespace Pan\Comment; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | |||
8 | |||
9 | // use Anax\Route\Exception\ForbiddenException; |
||
10 | // use Anax\Route\Exception\NotFoundException; |
||
11 | // use Anax\Route\Exception\InternalErrorException; |
||
12 | |||
13 | /** |
||
14 | * A sample controller to show how a controller class can be implemented. |
||
15 | */ |
||
16 | class CommentController implements ContainerInjectableInterface |
||
17 | { |
||
18 | use ContainerInjectableTrait; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * @var $data description |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
24 | */ |
||
25 | private $currentUser; |
||
26 | private $db; |
||
27 | private $userId; |
||
28 | |||
29 | |||
30 | |||
31 | // /** |
||
32 | // * The initialize method is optional and will always be called before the |
||
33 | // * target method/action. This is a convienient method where you could |
||
34 | // * setup internal properties that are commonly used by several methods. |
||
35 | // * |
||
36 | // * @return void |
||
37 | // */ |
||
38 | public function initialize() : void |
||
39 | { |
||
40 | // Get the current user from session |
||
41 | $session = $this->di->get("session"); |
||
42 | // var_dump($_SESSION); |
||
43 | $this->currentUser = $session->get("username"); |
||
44 | // Connect the database |
||
45 | $this->db = $this->di->get("db"); |
||
46 | $this->db->connect(); |
||
47 | if ($this->currentUser !=null) { |
||
48 | $sql = "SELECT id from users where username = ?;"; |
||
49 | $res = $this->db->executeFetchAll($sql, [$this->currentUser]); |
||
50 | $this->userId = $res[0]->id; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | |||
55 | public function createActionPost(int $id) : object |
||
56 | { |
||
57 | $request = $this->di->get("request"); |
||
58 | $response = $this->di->get("response"); |
||
59 | $submit = $request->getPost("submit") ?: null; |
||
60 | if ($this->currentUser) { |
||
61 | if ($submit) { |
||
62 | $comment = $request->getPost("comment") ?: null; |
||
63 | $sql = "INSERT INTO comments (comment, post_id, user_id, answer) VALUES (?, ?, ?, ?);"; |
||
64 | $this->db->execute($sql, [$comment, $id, $this->userId, 0]); |
||
65 | return $response->redirect("post/show/$id"); |
||
66 | } |
||
67 | } |
||
68 | $response = $this->di->get("response"); |
||
69 | return $response->redirect("user/login"); |
||
70 | } |
||
71 | |||
72 | public function uppvoteAction(int $id, int $post_id) : object |
||
73 | { |
||
74 | $page = $this->di->get("page"); |
||
0 ignored issues
–
show
|
|||
75 | if ($this->currentUser) { |
||
76 | $sql = "INSERT INTO comment_votes (score, comment_id, user_id) VALUES (?, ?, ?);"; |
||
77 | $this->db->execute($sql, [1, $id, $this->userId]); |
||
78 | |||
79 | $response = $this->di->get("response"); |
||
80 | return $response->redirect("post/show/$post_id"); |
||
81 | } |
||
82 | $response = $this->di->get("response"); |
||
83 | return $response->redirect("user/login"); |
||
84 | } |
||
85 | |||
86 | public function downvoteAction(int $id, int $post_id) : object |
||
87 | { |
||
88 | $page = $this->di->get("page"); |
||
0 ignored issues
–
show
|
|||
89 | if ($this->currentUser) { |
||
90 | $sql = "INSERT INTO comment_votes (score, comment_id, user_id) VALUES (?, ?, ?);"; |
||
91 | $this->db->execute($sql, [-1, $id, $this->userId]); |
||
92 | |||
93 | $response = $this->di->get("response"); |
||
94 | return $response->redirect("post/show/$post_id"); |
||
95 | } |
||
96 | $response = $this->di->get("response"); |
||
97 | return $response->redirect("user/login"); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Handler to change the status of answer accepted or unaccepted |
||
102 | * |
||
103 | * @return object as a response object |
||
104 | */ |
||
105 | public function acceptAction(int $id, int $post_id) : object |
||
106 | { |
||
107 | $page = $this->di->get("page"); |
||
0 ignored issues
–
show
|
|||
108 | //get the status of this answer |
||
109 | $sql = "select accepted from comments where post_id=? and id=?;"; |
||
110 | $res = $this->db->executeFetchAll($sql, [$post_id, $id]); |
||
111 | |||
112 | if ($res[0]->accepted==0) { |
||
113 | $accepted =1; |
||
114 | } elseif ($res[0]->accepted==1) { |
||
115 | $accepted=0; |
||
116 | } |
||
117 | //change the status of this answer |
||
118 | $sql = "update comments set accepted=? where post_id=? and id=?;"; |
||
119 | $this->db->execute($sql, [$accepted, $post_id, $id]); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
120 | $response = $this->di->get("response"); |
||
121 | return $response->redirect("post/show/$post_id"); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Handler with form to reply a comment. |
||
126 | * |
||
127 | * @param int $id the id to answer. |
||
128 | * |
||
129 | * @return object as a response object |
||
130 | */ |
||
131 | public function replyActionPost(int $id, int $post_id) : object |
||
132 | { |
||
133 | $request = $this->di->get("request"); |
||
134 | $response = $this->di->get("response"); |
||
135 | $submit = $request->getPost("submit") ?: null; |
||
136 | if ($this->currentUser) { |
||
137 | if ($submit) { |
||
138 | $comment = $request->getPost("comment") ?: null; |
||
139 | $sql = "INSERT INTO comments (comment, comment_reply_id, post_id, user_id, answer) VALUES (?, ?, ?, ?, ?);"; |
||
140 | $this->db->execute($sql, [$comment, $id, $post_id, $this->userId, 0]); |
||
141 | return $response->redirect("post/show/$post_id"); |
||
142 | } |
||
143 | } |
||
144 | $response = $this->di->get("response"); |
||
145 | return $response->redirect("user/login"); |
||
146 | } |
||
147 | } |
||
148 |