ejessyp /
flowerflow
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Pan\Post; |
||||
| 4 | |||||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||||
| 7 | |||||
| 8 | // use Anax\Route\Exception\ForbiddenException; |
||||
| 9 | // use Anax\Route\Exception\NotFoundException; |
||||
| 10 | // use Anax\Route\Exception\InternalErrorException; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * A sample controller to show how a controller class can be implemented. |
||||
| 14 | */ |
||||
| 15 | class PostController implements ContainerInjectableInterface |
||||
| 16 | { |
||||
| 17 | use ContainerInjectableTrait; |
||||
| 18 | |||||
| 19 | |||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @var $data description |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 23 | */ |
||||
| 24 | private $currentUser; |
||||
| 25 | private $db; |
||||
| 26 | private $userId; |
||||
| 27 | |||||
| 28 | |||||
| 29 | |||||
| 30 | // /** |
||||
| 31 | // * The initialize method is optional and will always be called before the |
||||
| 32 | // * target method/action. This is a convienient method where you could |
||||
| 33 | // * setup internal properties that are commonly used by several methods. |
||||
| 34 | // * |
||||
| 35 | // * @return void |
||||
| 36 | // */ |
||||
| 37 | public function initialize() : void |
||||
| 38 | { |
||||
| 39 | // Get the current user from session |
||||
| 40 | $session = $this->di->get("session"); |
||||
| 41 | // var_dump($session); |
||||
| 42 | $this->currentUser = $session->get("username"); |
||||
| 43 | |||||
| 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 | |||||
| 56 | /** |
||||
| 57 | * Show all items. |
||||
| 58 | * |
||||
| 59 | * @return object as a response object |
||||
| 60 | */ |
||||
| 61 | public function indexActionGet() : object |
||||
| 62 | { |
||||
| 63 | $page = $this->di->get("page"); |
||||
| 64 | |||||
| 65 | // Get settings from GET or use defaults |
||||
| 66 | $request = $this->di->get("request"); |
||||
| 67 | $orderBy = $request->getGet("orderby") ?: "created"; |
||||
| 68 | $order = $request->getGet("order") ?: "asc"; |
||||
| 69 | |||||
| 70 | // $sql = "SELECT * FROM posts ORDER BY $orderBy $order;"; |
||||
| 71 | $sql = "SELECT p.*, sum(v.score) as votes |
||||
| 72 | FROM posts AS p |
||||
| 73 | LEFT JOIN post_votes AS v |
||||
| 74 | ON p.id = v.post_id |
||||
| 75 | group by id |
||||
| 76 | order by $orderBy $order"; |
||||
| 77 | // var_dump($sql); |
||||
| 78 | $posts = $this->db->executeFetchAll($sql); |
||||
| 79 | |||||
| 80 | $page->add("post/view-all", [ |
||||
| 81 | "items" => $posts, |
||||
| 82 | ]); |
||||
| 83 | |||||
| 84 | return $page->render([ |
||||
| 85 | "title" => "All Posts", |
||||
| 86 | ]); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | |||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Handler with form to create a new item. |
||||
| 93 | * |
||||
| 94 | * @return object as a response object |
||||
| 95 | */ |
||||
| 96 | public function createActionGet() : object |
||||
| 97 | { |
||||
| 98 | if ($this->currentUser) { |
||||
| 99 | $page = $this->di->get("page"); |
||||
| 100 | $page->add("post/create"); |
||||
| 101 | |||||
| 102 | return $page->render([ |
||||
| 103 | "title" => "Ask Question", |
||||
| 104 | ]); |
||||
| 105 | } |
||||
| 106 | $response = $this->di->get("response"); |
||||
| 107 | return $response->redirect("user/login"); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | |||||
| 111 | public function createActionPost() : object |
||||
| 112 | { |
||||
| 113 | $request = $this->di->get("request"); |
||||
| 114 | $response = $this->di->get("response"); |
||||
| 115 | $submit = $request->getPost("submit") ?: null; |
||||
| 116 | |||||
| 117 | if ($submit) { |
||||
| 118 | $title = $request->getPost("Title") ?: null; |
||||
| 119 | $content = $request->getPost("Body") ?: null; |
||||
| 120 | $tags = $request->getPost("Tags") ?: null; |
||||
| 121 | |||||
| 122 | $sql = "INSERT INTO posts (title, content, user_id) VALUES (?, ?, ?);"; |
||||
| 123 | $this->db->execute($sql, [$title, $content, $this->userId]); |
||||
| 124 | $lastInsertId = $this->db->lastInsertId(); |
||||
| 125 | // var_dump($lastInsertId); |
||||
| 126 | $tagsArray = explode(",", $tags); |
||||
|
0 ignored issues
–
show
It seems like
$tags can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 127 | foreach ($tagsArray as $value) { |
||||
| 128 | $sql = "INSERT INTO post2tag (post_id, tag_name) VALUES (?, ?);"; |
||||
| 129 | $this->db->execute($sql, [$lastInsertId, trim($value)]); |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | return $response->redirect("post"); |
||||
| 133 | } |
||||
|
0 ignored issues
–
show
The function implicitly returns
null when the if condition on line 117 is false. This is incompatible with the type-hinted return object. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||||
| 134 | } |
||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * Handler with form to update an item. |
||||
| 138 | * |
||||
| 139 | * @param int $id the id to answer. |
||||
| 140 | * |
||||
| 141 | * @return object as a response object |
||||
| 142 | */ |
||||
| 143 | public function answerActionPost() : object |
||||
| 144 | { |
||||
| 145 | $page = $this->di->get("page"); |
||||
|
0 ignored issues
–
show
|
|||||
| 146 | $request = $this->di->get("request"); |
||||
| 147 | $submit = $request->getPost("submit") ?: null; |
||||
| 148 | // one has to login to answer the question |
||||
| 149 | if ($this->currentUser) { |
||||
| 150 | if ($submit) { |
||||
| 151 | $post_id = $request->getPost("post_id") ?: null; |
||||
| 152 | $comment = $request->getPost("answer") ?: null; |
||||
| 153 | |||||
| 154 | $sql = "INSERT INTO comments (comment, user_id, post_id, answer) VALUES (?, ?, ?, ?);"; |
||||
| 155 | $this->db->execute($sql, [$comment, $this->userId, $post_id, 1]); |
||||
| 156 | $response = $this->di->get("response"); |
||||
| 157 | return $response->redirect("post/show/$post_id"); |
||||
| 158 | } |
||||
|
0 ignored issues
–
show
The function implicitly returns
null when the if condition on line 150 is false. This is incompatible with the type-hinted return object. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||||
| 159 | } else { |
||||
| 160 | $response = $this->di->get("response"); |
||||
| 161 | return $response->redirect("user/login"); |
||||
| 162 | } |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | /** |
||||
| 166 | * Handler to view an item. |
||||
| 167 | * |
||||
| 168 | * @param int $id the id to view. |
||||
| 169 | * |
||||
| 170 | * @return object as a response object |
||||
| 171 | */ |
||||
| 172 | public function showAction(int $id) : object |
||||
| 173 | { |
||||
| 174 | $page = $this->di->get("page"); |
||||
| 175 | $postid = $id; |
||||
| 176 | $request = $this->di->get("request"); |
||||
| 177 | $orderBy = $request->getGet("orderby") ?: "created"; |
||||
| 178 | $order = $request->getGet("order") ?: "asc"; |
||||
| 179 | // Get the post $id , votes and answers and tags and username |
||||
| 180 | $sql = "SELECT * from posts where id=?;"; |
||||
| 181 | |||||
| 182 | $posts = $this->db->executeFetchAll($sql, [$postid]); |
||||
| 183 | $sql = "SELECT username FROM users WHERE id=?;"; |
||||
| 184 | $username = $this->db->executeFetchAll($sql, [$posts[0]->user_id]); |
||||
| 185 | $sql = "select * from post2tag where post_id=?;"; |
||||
| 186 | $posttags = $this->db->executeFetchAll($sql, [$postid]); |
||||
| 187 | $sql = "SELECT sum(score) as postscore from post_votes where post_id=?;"; |
||||
| 188 | $postscore = $this->db->executeFetchAll($sql, [$postid]); |
||||
| 189 | // Get the comments for the post |
||||
| 190 | |||||
| 191 | $sql = "SELECT * from comments WHERE post_id=? and answer=0 and ISNULL(comment_reply_id);"; |
||||
| 192 | $comments0 = $this->db->executeFetchAll($sql, [$postid]); |
||||
| 193 | //Get the answers for the post $id |
||||
| 194 | // $sql = "SELECT * from comments WHERE post_id=? and answer=1 order by accepted desc, $orderBy $order;"; |
||||
| 195 | $sql ="SELECT c.*, sum(v.score) votes |
||||
| 196 | FROM comments AS c |
||||
| 197 | LEFT JOIN comment_votes AS v |
||||
| 198 | ON c.id = v.comment_id |
||||
| 199 | WHERE post_id=? and answer=1 |
||||
| 200 | group by id |
||||
| 201 | order by accepted desc, $orderBy $order"; |
||||
| 202 | $answers = $this->db->executeFetchAll($sql, [$postid]); |
||||
| 203 | // var_dump($sql); |
||||
| 204 | // var_dump($answers); |
||||
| 205 | // check if the current user is the owner of the question, if yes, show the accepted answer button otherwise not |
||||
| 206 | // var_dump($this->currentUser,$posts[0]->username ); |
||||
| 207 | $isOwner=true; |
||||
| 208 | if ($this->userId != $posts[0]->user_id ) { |
||||
| 209 | $isOwner = false; |
||||
| 210 | } |
||||
| 211 | $page->add("post/show", |
||||
| 212 | ["post" => $posts[0], |
||||
| 213 | "postscore" => $postscore[0]->postscore?:0, |
||||
| 214 | "posttags" => $posttags, |
||||
| 215 | "answers" => $answers, |
||||
| 216 | "comments0" => $comments0, |
||||
| 217 | "isOwner" => $isOwner, |
||||
| 218 | "post_owner" => $username[0]->username, |
||||
| 219 | ]); |
||||
| 220 | |||||
| 221 | return $page->render([ |
||||
| 222 | "title" => "Show a Post", |
||||
| 223 | ]); |
||||
| 224 | } |
||||
| 225 | |||||
| 226 | public function uppvoteAction(int $id) : object |
||||
| 227 | { |
||||
| 228 | $page = $this->di->get("page"); |
||||
|
0 ignored issues
–
show
|
|||||
| 229 | if ($this->currentUser){ |
||||
| 230 | $sql = "INSERT INTO post_votes (score, post_id, user_id) VALUES (?, ?, ?);"; |
||||
| 231 | $this->db->execute($sql, [1, $id, $this->userId]); |
||||
| 232 | $response = $this->di->get("response"); |
||||
| 233 | return $response->redirect("post/show/$id"); |
||||
| 234 | } |
||||
| 235 | $response = $this->di->get("response"); |
||||
| 236 | return $response->redirect("user/login"); |
||||
| 237 | } |
||||
| 238 | |||||
| 239 | public function downvoteAction(int $id) : object |
||||
| 240 | { |
||||
| 241 | $page = $this->di->get("page"); |
||||
|
0 ignored issues
–
show
|
|||||
| 242 | if ($this->currentUser) { |
||||
| 243 | $sql = "INSERT INTO post_votes (score, post_id, user_id) VALUES (?, ?, ?);"; |
||||
| 244 | $this->db->execute($sql, [-1, $id, $this->userId]); |
||||
| 245 | |||||
| 246 | $response = $this->di->get("response"); |
||||
| 247 | return $response->redirect("post/show/$id"); |
||||
| 248 | } |
||||
| 249 | $response = $this->di->get("response"); |
||||
| 250 | return $response->redirect("user/login"); |
||||
| 251 | } |
||||
| 252 | } |
||||
| 253 |