|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Controllers\API; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Comment as CommentLib; |
| 6 | +use \BNETDocs\Libraries\Common; |
| 7 | +use \BNETDocs\Libraries\Controller; |
| 8 | +use \BNETDocs\Libraries\Exceptions\CommentNotFoundException; |
| 9 | +use \BNETDocs\Libraries\Exceptions\QueryException; |
| 10 | +use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException; |
| 11 | +use \BNETDocs\Libraries\Router; |
| 12 | +use \BNETDocs\Libraries\UserSession; |
| 13 | +use \BNETDocs\Models\API\Comment as APICommentModel; |
| 14 | +use \BNETDocs\Views\API\CommentJSON as APICommentJSONView; |
| 15 | +use \DateTime; |
| 16 | +use \DateTimeZone; |
| 17 | + |
| 18 | +class Comment extends Controller { |
| 19 | + |
| 20 | + public function run(Router &$router) { |
| 21 | + switch ($router->getRequestPathExtension()) { |
| 22 | + case "json": case "": |
| 23 | + $view = new APICommentJSONView(); |
| 24 | + break; |
| 25 | + default: |
| 26 | + throw new UnspecifiedViewException(); |
| 27 | + } |
| 28 | + $model = new APICommentModel(); |
| 29 | + $model->user_session = UserSession::load($router); |
| 30 | + |
| 31 | + switch ($router->getRequestMethod()) { |
| 32 | + case "GET": $code = $this->handleGet($router, $model); break; |
| 33 | + case "OPTIONS": $code = $this->handleOptions($router, $model); break; |
| 34 | + case "PUT": $code = $this->handlePut($router, $model); break; |
| 35 | + default: $code = $this->handleUnknown($router, $model); break; |
| 36 | + } |
| 37 | + |
| 38 | + ob_start(); |
| 39 | + $view->render($model); |
| 40 | + $router->setResponseCode($code); |
| 41 | + $router->setResponseTTL(0); |
| 42 | + $router->setResponseHeader("Content-Type", $view->getMimeType()); |
| 43 | + $router->setResponseContent(ob_get_contents()); |
| 44 | + ob_end_clean(); |
| 45 | + } |
| 46 | + |
| 47 | + protected function handleGet(Router &$router, APICommentModel &$model) { |
| 48 | + $query = $router->getRequestQueryArray(); |
| 49 | + $p_id = (isset($query["parent_id" ]) ? $query["parent_id" ] : null); |
| 50 | + $p_type = (isset($query["parent_type"]) ? $query["parent_type"] : null); |
| 51 | + |
| 52 | + if ($p_id !== null) $p_id = (int) $p_id; |
| 53 | + if ($p_type !== null) $p_type = (int) $p_type; |
| 54 | + |
| 55 | + $comments = CommentLib::getAll($p_type, $p_id); |
| 56 | + $error = ($comments === null ? true : false); |
| 57 | + |
| 58 | + $model->response = [ |
| 59 | + "comments" => $comments, |
| 60 | + "error" => $error, |
| 61 | + "parent_id" => $p_id, |
| 62 | + "parent_type" => $p_type, |
| 63 | + ]; |
| 64 | + |
| 65 | + return ($error ? 400 : 200); |
| 66 | + } |
| 67 | + |
| 68 | + protected function handleOptions(Router &$router, APICommentModel &$model) { |
| 69 | + $router->setResponseHeader("Allow", "GET,OPTIONS,PUT"); |
| 70 | + $model->response = ["error" => false, |
| 71 | + "allow" => ["GET","OPTIONS","PUT"]]; |
| 72 | + return 200; |
| 73 | + } |
| 74 | + |
| 75 | + protected function handlePut(Router &$router, APICommentModel &$model) { |
| 76 | + return 403; |
| 77 | + } |
| 78 | + |
| 79 | + protected function handleUnknown(Router &$router, APICommentModel &$model) { |
| 80 | + $router->setResponseHeader("Allow", "GET,OPTIONS,PUT"); |
| 81 | + $model->response = ["error" => "Method Not Allowed", |
| 82 | + "allow" => ["GET","OPTIONS","PUT"]]; |
| 83 | + return 405; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments