public function testDelete() { $commentMapper = new CommentMapper(); $commentMapper->setId(50); $commentMapper->deleteComment(); $this->assertEquals(new Comment(), $commentMapper->selectComment()); }
public function save() { $commentmapper = new CommentMapper($this->db); $comment = new Comment(); $comment->user_id = $this->user_id; $comment->file_id = $this->file_id; $comment->comment = $this->comment; $commentmapper->save($comment); }
public function findById($id) { $row = $this->query('SELECT comment_id, comment, item_id FROM comment WHERE comment_id = ' . (int) $id)->fetch(); if (!$row) { return null; } $comment = new Comment(); CommentMapper::map($comment, $row); return $comment; }
/** * 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"); } }
Rest::sendResponse(204); } break; case 'delete': $commentMapper = new \CommentMapper(); if ($commentMapper->deleteComment()) { Rest::sendResponse(200); } break; case 'put': try { $comment_ = new Comment(); $data_comment_ = $http->getRequestVars(); $commentObject = initObject($data_comment_, $comment_, true); if (!emptyObject($commentObject)) { $commentMapper = new \CommentMapper(); if ($commentMapper->updateComment($commentObject)) { Rest::sendResponse(200); } } else { throw new InvalidArgumentException('Need arguments to UPDATE data !'); } } catch (InvalidArgumentException $e) { print $e->getComment(); exit; } break; default: Rest::sendResponse(501); break; }
<?php $merchdao = new MerchandiseDao(); $dao = new CommentDao(); $item_id = Utils::getUrlParam('item_id'); $comment = new Comment(); $item = $merchdao->findById($item_id); if (array_key_exists('save', $_POST)) { $data = array('comment' => filter_var($_POST['comment']['comment'], FILTER_SANITIZE_STRING), 'username' => $_SESSION['username'], 'user_id' => $_SESSION['user_id'], 'item_id' => filter_var($_GET['item_id'], FILTER_SANITIZE_NUMBER_INT)); CommentMapper::map($comment, $data); $dao->save($comment); $comment->setComment(''); } $comment_list = $dao->find();
error_reporting('E_ALL'); $db = $app->db; $foo = new Foo(); $foo->token = $id; $mapper = new FooMapper($db); $files = $mapper->selectToken($foo); if (empty($files)) { $app->notFound(); } foreach ($files as $value) { $file_id = $value['id']; //$userFile_id = $value['user_id']; } $commentObj = new Comment(); $commentObj->file_id = $file_id; $commentMapper = new CommentMapper($db); $comments = $commentMapper->select($commentObj); ini_set('display_errors', 'Off'); $checkrights = new Checkrights(); $checkrights->Render($files, $app, $comments); if (!empty($_POST['comment'])) { $logged = new Logged(); $cookie = $app->getCookie('username'); $user_id = $logged->getLogged($db, $cookie); $postcomment = new postComment($file_id, $user_id, $db); $regExp = new RegExp(); $comment = $regExp->match(trim($_POST['comment'])); $postcomment->post($comment); $app->redirect("{$id}"); } if (isset($_POST['public'])) {
$options = array('indent' => ' ', 'addDecl' => false, XML_SERIALIZER_OPTION_RETURN_RESULT => true, "defaultTagName" => "comment"); $serializer = new XML_Serializer($options); Rest::sendResponse(200, $serializer->serialize($commentsArray), 'application/xml'); } } } else { Rest::sendResponse(204); } break; case 'post': try { $comment = new Comment(); $data_comment = $http->getRequestVars(); $commentObject = initObject($data_comment, $comment, true); if (!emptyObject($commentObject)) { $commentMapper = new CommentMapper(); if ($commentMapper->insertComment($commentObject)) { Rest::sendResponse(200); } } else { throw new InvalidArgumentException('Need arguments to POST data !'); } } catch (InvalidArgumentException $e) { $e->getComment(); exit; } break; default: Rest::sendResponse(501); break; }
<?php require_once "lib/User.php"; require_once "lib/Post.php"; require_once "lib/Comment.php"; require_once "lib/PDO.php"; require_once "lib/UsersMapper.php"; require_once "lib/PostsMapper.php"; require_once "lib/CommentMapper.php"; $usersMapper = new UsersMapper($DBH); $postsMapper = new PostsMapper($DBH); $commentMapper = new CommentMapper($DBH); $comments = []; if (isset($_COOKIE['userscookie']['code'])) { $code = $_COOKIE['userscookie']['code']; $user = $usersMapper->getUserbyCode($code); $userposts = $postsMapper->getAllPostsByUser($user->getID()); $logined = 1; } else { $logined = 0; } if (isset($_GET['postid'])) { $post_id = $_GET['postid']; $comments = $commentMapper->getAllComments($post_id); $post = $postsMapper->getPost($post_id); if (isset($_POST['submitcom'])) { $comment = new Comment(); $comment->setFields($_POST); $comment->setPostID($post_id); $commentMapper->addComment($comment); }