function execute()
 {
     $dao = new CommentDAO();
     $res = '';
     $res = '{"records":' . json_encode($dao->readAll()) . '}';
     return $res;
 }
Beispiel #2
0
 public static function getInstance()
 {
     if (!self::$instance) {
         self::$instance = new CommentDAO();
     }
     return self::$instance;
 }
 /**
  * Metodo del controlador Comments cuya funcionalidad es insertar un comentario
  * en un post.
  * Verifica que el usuario ha iniciado sesion y que el post existe
  *
  * @throws Exception Si el usuario no inicio sesion
  * @return void
  */
 public function add()
 {
     if (!isset($this->currentUser)) {
         throw new Exception("Not in session. Adding comments requires login");
     }
     if (isset($_GET["id"])) {
         // reaching via HTTP Post...
         // Se obtiene el post de la BD
         $idPost = $_GET["id"];
         $post = $this->postDAO->findByIdPost($idPost);
         // Si no existe el post lanza una excepcion
         if ($post == NULL) {
             throw new Exception("no such post with id: " . $idPost);
         }
         // Crea el objeto Comment
         $comment = new Comment();
         $comment->setDate(date("Y-m-d H:i:s"));
         $comment->setContent($_POST["content"]);
         $comment->setAuthor($this->currentUser->getEmail());
         $comment->setIdPost($post->getIdPost());
         try {
             // Valida el comentario, si falla lanza una excepcion
             $comment->checkIsValidForCreate();
             // Guarda el comentario en la BD
             $this->commentDAO->save($comment);
             // Redirige al post
             $this->view->redirect("posts", "viewPosts", "id=" . $post->getIdPost());
         } catch (ValidationException $ex) {
             $errors = $ex->getErrors();
             $this->view->setVariable("comment", $comment, true);
             $this->view->setVariable("errors", $errors, true);
             $this->view->redirect("posts", "viewPosts", "id=" . $post->getIdPost());
         }
     } else {
         throw new Exception("No such post id");
     }
 }
Beispiel #4
0
 public function startCommentDAO()
 {
     $this->CommentDAO = CommentDAO::getInstance();
 }