'/create/:thread_id', 'edit' => '/edit/:comment_id', 'quickedit' => '/quickedit/:comment_id', 'delete' => '/delete/:comment_id', ); } function index(&$reg, &$req, &$resp) { } function _genericCommentStuff(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // Perms check. //-------------------------------------------- if (!$user->canDo('view', 'comments')) { $info->display('permsRequired'); } //-------------------------------------------- // Get our comment. //-------------------------------------------- $finder = &$this->getFinder('comments'); $comment = &$finder->find($req->get('comment_id')); if($comment === NULL) { $info->text("The specified comment doesn't exist!"); } $resp->assignRef('comment', $comment); } //-------------------------------------------- // Some simple error checking that is common in creating // normal and quick comments. //-------------------------------------------- function _checkCreateComment(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); if (!$user->canDo('post', 'comments')) { $info->display('permsRequired'); } if ($user->get('karma') < 0.5) { $info->display('karmaRequired'); } } //-------------------------------------------- // Some simple error checking that is common in editing // normal and quick comments. //-------------------------------------------- function _checkEditComment(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); if($req->get('comment_id') == 0) { $info->text("The comment that you are trying to edit doesn't exist!"); } if (!$user->canDo('edit', 'comments')) { $info->display('permsRequired'); } } //-------------------------------------------- // Add/Save a comment to the database. //-------------------------------------------- function &_saveComment(&$reg, &$req, &$resp, $edit = FALSE) { $user = &$reg->get('user'); $ret = array(); $info = &new InformationDisplay($reg, $req, $resp); if(!$edit) { //-------------------------------------------- // We're creating this comment. //-------------------------------------------- $this->_checkCreateComment($reg, $req, $resp); //-------------------------------------------- $finder = &$this->getFinder('threads'); $thread = &$finder->find($req->get('thread_id')); //-------------------------------------------- // Set the database info. //-------------------------------------------- $comment = &$thread->createComment(); $comment->set('body', K4BBParserFilter::filter($req->get('body')) ); $comment->setUser($user); $comment->setThread($thread); $comment->setUserKarma(); $comment->setLastComment(); $ret['thread'] = &$thread; } else { //-------------------------------------------- // We're updating this comment. //-------------------------------------------- $this->_checkEditComment($reg, $req, $resp); $this->_genericCommentStuff($reg, $req, $resp); //-------------------------------------------- // Format the body text. //-------------------------------------------- $comment = &$resp->getRef('comment'); $comment->set('body', K4BBParserFilter::filter($req->get('body'))); $comment->setUser($user); } $ret['comment'] = &$comment; //-------------------------------------------- // Return the comment! //-------------------------------------------- return $ret; } //-------------------------------------------- // Save a comment, either by creating or updating // it. //-------------------------------------------- function _save(&$reg, &$req, &$resp, $edit = FALSE) { $ret = &$this->_saveComment($reg, $req, $resp, $edit); if ($ret['comment']->save()) { // TODO:{[deal with pagination[_save[CommentController} $this->redirect('/thread/'. $ret['comment']->get('thread_id') .'/'. k4_us($ret['thread']->get('name'))); } else { //-------------------------------------------- // Whoops! An error occured, let's go bacl and show them. //-------------------------------------------- $resp->assign('comment', $req->getArray(FA_REQUEST_POST)); $resp->assign('error_msg', TRUE); // Show the error message foreach ($thread->getInvalid() as $invalid) { $resp->setError($invalid); } $func = $edit ? 'edit' : 'create'; $this->$func($reg, $req, $resp); } } //-------------------------------------------- // Create a comment the normal way.. through the form and not // by using the quick comment feature. //-------------------------------------------- function create(&$reg, &$req, &$resp) { $this->_checkCreateComment($reg, $req, $resp); $threads = &$this->getFinder('threads'); $thread = &$threads->find($req->get('thread_id')); $resp->assignRef('thread', $thread); $resp->assign('content', 'comment_create_form.html'); $this->setPage("Post a Comment"); $this->setLayout("forum_base.html"); } //-------------------------------------------- // Add a comment to the database the normal way and redirect // the user back to the thread instead of displaying the // comment template. //-------------------------------------------- function post(&$reg, &$req, &$resp) { $this->_save($reg, $req, $resp, FALSE); } //-------------------------------------------- // deal with quick comment adding. //-------------------------------------------- function quickpost(&$reg, &$req, &$resp) { $this->checkXHR(); $ret = &$this->_saveComment($reg, $req, $resp, FALSE); $ret['comment']->save(); //-------------------------------------------- // We're done, pass the new comment to the template //-------------------------------------------- $finder = &$this->getFinder('comments'); $comment = &$finder->find($ret['comment']->getId()); if($ret['thread']) { $row_class = $ret['thread']->get('num_comments') % 2 == 0 ? 'row2' : 'row1'; $resp->assign('row_class', $row_class); } $resp->assign('comment', $comment); $this->setLayout("comment_view.html"); } //-------------------------------------------- // Show the form to edit a comment. //-------------------------------------------- function edit(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // Standard error checking. //-------------------------------------------- $this->_checkEditComment($reg, $req, $resp); $this->_genericCommentStuff($reg, $req, $resp); //-------------------------------------------- // Finish off with template stuff. //-------------------------------------------- $resp->assign('content', 'comment_edit_form.html'); $this->setPage("Edit a Comment"); $this->setLayout("forum_base.html"); } //-------------------------------------------- // Show the form to quick edit a comment. //-------------------------------------------- function quickedit(&$reg, &$req, &$resp) { $this->checkXHR(); $user = &$reg->get('user'); //-------------------------------------------- // Standard error checking. //-------------------------------------------- $this->_checkEditComment($reg, $req, $resp); //-------------------------------------------- // Get our comment. //-------------------------------------------- $finder = &$this->getFinder('comments'); $comment = &$finder->find($req->get('comment_id')); if($comment === NULL) { $this->informationPage("The comment that you are trying to edit doesn't exist!"); } //-------------------------------------------- // Finish off with template stuff. //-------------------------------------------- $resp->assignRef('comment', $comment); $this->setLayout("comment_quickedit_form.html"); } //-------------------------------------------- // Update a comment the normal way. //-------------------------------------------- function update(&$reg, &$req, &$resp) { $this->_save($reg, $req, $resp, TRUE); } //-------------------------------------------- // Quick update the comment. //-------------------------------------------- function quickupdate(&$reg, &$req, &$resp) { $this->checkXHR(); $ret = &$this->_saveComment($reg, $req, $resp, TRUE); $ret['comment']->save(); //-------------------------------------------- // We're done, pass the new comment to the template //-------------------------------------------- $finder = &$this->getFinder('comments'); $comment = &$finder->find($ret['comment']->getId()); $resp->assign('comment', $comment); $this->setLayout("comment_view_body.html"); } //-------------------------------------------- // Cancel quick editing. //-------------------------------------------- function canceledit(&$reg, &$req, &$resp) { $this->checkXHR(); $finder = &$this->getFinder('comments'); $comment = &$finder->find($req->get('comment_id')); $resp->assign('comment', $comment); $this->setLayout("comment_view_body.html"); } //-------------------------------------------- // Hard delete a comment. //-------------------------------------------- function delete(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // Get our comment and do some checking on it. //-------------------------------------------- $this->_genericCommentStuff($reg, $req, $resp); $comment = &$resp->getRef('comment'); //-------------------------------------------- // Is this user allowed to delete this comment? //-------------------------------------------- if(!($user->getId() == $comment->get('user_id') && $user->canDo('delete', 'comments')) && !$user->canDo('moderate', 'comments')) { $info->display('permsRequired'); } //-------------------------------------------- // Delete the comment. //-------------------------------------------- $comment->hardDelete(); //-------------------------------------------- // We're done! //-------------------------------------------- $info->text("Successfully deleted the comment.", $this->popReferer(), 3); } } ?>