Exemplo n.º 1
0
 private function requireNews($id)
 {
     if (!($news = \StudipNews::find($id))) {
         $this->notFound("News not found");
     }
     if (!$news->havePermission('view', '', $GLOBALS['user']->id)) {
         $this->error(401);
     }
     return $news;
 }
Exemplo n.º 2
0
 /**
  *
  **/
 public function routes(&$router)
 {
     // Comments
     // Load comments for a news
     $router->get('/news/:news_id/comments', function ($news_id) use($router) {
         $news = \StudipNews::find($news_id);
         if (!$news) {
             $router->halt(404, sprintf('News "%s" not found', $news_id));
         }
         if (!$news->allow_comments) {
             $router->halt(406, sprintf('Comments are disabled for news "%s"', $news_id));
         }
         $comments = NewsComments::loadByNewsId($news_id);
         if ($router->compact()) {
             $router->render(compact('comments'));
             return;
         }
         $users = NewsCommentsRoute::extractUsers($comments, $router);
         $router->render(compact('comments', 'users'));
     });
     // Create comment for a news
     $router->post('/news/:news_id/comments', function ($news_id) use($router) {
         $content = trim(Request::get('content'));
         if (empty($content)) {
             $router->halt(406, 'No comment provided');
         }
         $news = \StudipNews::find($news_id);
         if (!$news) {
             $router->halt(404, sprintf('News "%s" not found', $news_id));
         }
         $comment = new \StudipComments();
         $comment->object_id = $news_id;
         $comment->user_id = $GLOBALS['user']->id;
         $comment->content = $content;
         if (!$comment->store()) {
             $router->halt(500, 'Could not create comment for news "%s"', $news_id);
         }
         $router->render($router->dispatch('get', '/news/:news_id/comments/:comment_id', $news_id, $comment->comment_id), 201);
     });
     // Load comment
     $router->get('/news/:news_id/comments/:comment_id', function ($news_id, $comment_id) use($router) {
         $news = \StudipNews::find($news_id);
         if (!$news) {
             $router->halt(404, 'News "%s" not found', $news_id);
         }
         $comment = NewsComments::load($news_id, $comment_id);
         if (!$comment) {
             $router->halt(404, 'Comment "%s" for news "%s" not found', $comment, $news_id);
         }
         if ($router->compact()) {
             $router->render(compact('comment'));
             return;
         }
         $users = NewsCommentsRoute::extractUsers(array($comments), $router);
         $router->render(compact('comment', 'users'));
     });
     // Remove news comment
     $router->delete('/news/:news_id/comments/:comment_id', function ($news_id, $comment_id) use($router) {
         $news = \StudipNews::find($news_id);
         if (!$news) {
             $router->halt(404, 'News "%s" not found', $news_id);
         }
         $comment = \StudipComments::find($comment_id);
         if (!$comment) {
             $router->halt(404, 'Comment "%s" for news "%s" not found', $comment_id, $news_id);
         }
         if (!$comment->delete()) {
             $router->halt(500, 'Comment "%s" for news "%s" could not be deleted.', $comment_id, $news_id);
         }
         $router->halt(200, 'Deleted comment "%s" for news "%s"', $comment_id, $news_id);
     });
 }
Exemplo n.º 3
0
 static function load($id)
 {
     $result = array();
     foreach ((array) $id as $i) {
         $news = \StudipNews::find($i);
         $news = self::adjust($news);
         $result[] = $news;
     }
     return is_array($id) ? $result : reset($result);
 }