/** * Action to adds a comment to a post * * This method should only be called via HTTP POST. * * The user of the comment is taken from the {@link BaseController::currentUser} * property. * The expected HTTP parameters are: * <ul> * <li>id: Id of the post (via HTTP POST)</li> * <li>content: Content of the comment (via HTTP POST)</li> * </ul> * * The views are: * <ul> * <li>posts/view?id=post: If comment was successfully added of, * or if it was not validated (via redirect). Includes these view variables:</li> * <ul> * <li>errors (flash): Array including per-field validation errors</li> * <li>comment (flash): The current Comment instance, empty or being added</li> * </ul> * </ul> * * @return void */ public function add() { if (!isset($this->currentUser)) { throw new Exception("Not in session. Adding posts requires login"); } if (isset($_POST["id"])) { // reaching via HTTP Post... // Get the Post object from the database $postid = $_POST["id"]; $post = $this->postmapper->findById($postid); // Does the post exist? if ($post == NULL) { throw new Exception("no such post with id: " . $postid); } // Create and populate the Comment object $comment = new Comment(); $comment->setContent($_POST["content"]); $comment->setAuthor($this->currentUser); $comment->setPost($post); try { // validate Comment object $comment->checkIsValidForCreate(); // if it fails, ValidationException // save the Comment object into the database $this->commentmapper->save($comment); // POST-REDIRECT-GET // Everything OK, we will redirect the user to the list of posts // We want to see a message after redirection, so we establish // a "flash" message (which is simply a Session variable) to be // get in the view after redirection. $this->view->setFlash("Comment \"" . $post->getTitle() . "\" successfully added."); // perform the redirection. More or less: // header("Location: index.php?controller=posts&action=view&id=$postid") // die(); $this->view->redirect("posts", "view", "id=" . $post->getId()); } catch (ValidationException $ex) { $errors = $ex->getErrors(); // Go back to the form to show errors. // However, the form is not in a single page (comments/add) // It is in the View Post page. // We will save errors as a "flash" variable (third parameter true) // and redirect the user to the referring page // (the View post page) $this->view->setVariable("comment", $comment, true); $this->view->setVariable("errors", $errors, true); $this->view->redirect("posts", "view", "id=" . $post->getId()); } } else { throw new Exception("No such post id"); } }
/** * Action to delete a post * * This action should only be called via HTTP POST * * The expected HTTP parameters are: * <ul> * <li>id: Id of the post (via HTTP POST)</li> * </ul> * * The views are: * <ul> * <li>posts/index: If post was successfully deleted (via redirect)</li> * </ul> * @throws Exception if no id was provided * @throws Exception if no user is in session * @throws Exception if there is not any post with the provided id * @throws Exception if the author of the post to be deleted is not the current user * @return void */ public function delete() { if (!isset($_POST["id"])) { throw new Exception("id is mandatory"); } if (!isset($this->currentUser)) { throw new Exception("Not in session. Editing posts requires login"); } // Get the Post object from the database $postid = $_REQUEST["id"]; $post = $this->postMapper->findById($postid); // Does the post exist? if ($post == NULL) { throw new Exception("no such post with id: " . $postid); } // Check if the Post author is the currentUser (in Session) if ($post->getAuthor() != $this->currentUser) { throw new Exception("Post author is not the logged user"); } // Delete the Post object from the database $this->postMapper->delete($post); // POST-REDIRECT-GET // Everything OK, we will redirect the user to the list of posts // We want to see a message after redirection, so we establish // a "flash" message (which is simply a Session variable) to be // get in the view after redirection. $this->view->setFlash("Post \"" . $post->getTitle() . "\" successfully deleted."); // perform the redirection. More or less: // header("Location: index.php?controller=posts&action=index") // die(); $this->view->redirect("posts", "index"); }
public function mostrarPincho() { $pincho1 = $this->pinchoMapper->findById($_SESSION["currentcod1"]); $pincho2 = $this->pinchoMapper->findById($_SESSION["currentcod2"]); $pincho3 = $this->pinchoMapper->findById($_SESSION["currentcod3"]); if ($pincho1 && $pincho2 && $pincho3) { $this->view->setVariable("pincho1", $pincho1); $this->view->setVariable("pincho2", $pincho2); $this->view->setVariable("pincho3", $pincho3); } $this->view->render("pinchos", "votarPinchoPopular"); }