2010-12-06 11:08:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package comments
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CommentingController extends Controller {
|
|
|
|
|
2013-04-11 14:23:03 +02:00
|
|
|
private static $allowed_actions = array(
|
2010-12-11 06:01:19 +01:00
|
|
|
'delete',
|
2012-07-22 03:30:33 +02:00
|
|
|
'spam',
|
|
|
|
'ham',
|
|
|
|
'approve',
|
|
|
|
'rss',
|
2010-12-11 06:01:19 +01:00
|
|
|
'CommentsForm',
|
2013-03-04 11:37:18 +01:00
|
|
|
'doPostComment',
|
|
|
|
'doPreviewComment'
|
2010-12-11 06:01:19 +01:00
|
|
|
);
|
2012-07-31 10:45:29 +02:00
|
|
|
|
2015-04-07 05:56:00 +02:00
|
|
|
/**
|
|
|
|
* Base class this commenting form is for
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
private $baseClass = "";
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The record this commenting form is for
|
|
|
|
*
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
|
|
|
private $ownerRecord = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parent controller record
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
private $ownerController = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup url to return to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-03-30 07:57:29 +02:00
|
|
|
protected $fallbackReturnURL = null;
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the base class to use
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function setBaseClass($class) {
|
|
|
|
$this->baseClass = $class;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the base class used
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function getBaseClass() {
|
|
|
|
return $this->baseClass;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the record this controller is working on
|
|
|
|
*
|
|
|
|
* @param DataObject $record
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function setOwnerRecord($record) {
|
|
|
|
$this->ownerRecord = $record;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the record
|
|
|
|
*
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function getOwnerRecord() {
|
|
|
|
return $this->ownerRecord;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the parent controller
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function setOwnerController($controller) {
|
|
|
|
$this->ownerController = $controller;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the parent controller
|
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
*/
|
2010-12-06 11:08:38 +01:00
|
|
|
public function getOwnerController() {
|
|
|
|
return $this->ownerController;
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the commenting option for the current state
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed Result if the setting is available, or null otherwise
|
|
|
|
*/
|
|
|
|
public function getOption($key) {
|
|
|
|
// If possible use the current record
|
|
|
|
if($record = $this->getOwnerRecord()) {
|
|
|
|
return $record->getCommentsOption($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise a singleton of that record
|
|
|
|
if($class = $this->getBaseClass()) {
|
|
|
|
return singleton($class)->getCommentsOption($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise just use the default options
|
|
|
|
return singleton('CommentsExtension')->getCommentsOption($key);
|
|
|
|
}
|
2010-12-06 11:08:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Workaround for generating the link to this controller
|
2012-07-22 03:30:33 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2010-12-06 11:08:38 +01:00
|
|
|
*/
|
2015-04-07 05:56:00 +02:00
|
|
|
public function Link($action = '', $id = '', $other = '') {
|
|
|
|
return Controller::join_links(Director::baseURL(), __CLASS__ , $action, $id, $other);
|
2010-12-06 11:08:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-31 10:45:29 +02:00
|
|
|
* Outputs the RSS feed of comments
|
|
|
|
*
|
2015-04-07 05:56:00 +02:00
|
|
|
* @return HTMLText
|
2012-07-31 10:45:29 +02:00
|
|
|
*/
|
|
|
|
public function rss() {
|
|
|
|
return $this->getFeed($this->request)->outputToBrowser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an RSSFeed of comments for a given set of comments or all
|
2012-07-22 03:30:33 +02:00
|
|
|
* comments on the website.
|
|
|
|
*
|
|
|
|
* To maintain backwards compatibility with 2.4 this supports mapping
|
|
|
|
* of PageComment/rss?pageid= as well as the new RSS format for comments
|
|
|
|
* of CommentingController/rss/{classname}/{id}
|
2010-12-11 06:01:19 +01:00
|
|
|
*
|
2012-07-31 10:45:29 +02:00
|
|
|
* @param SS_HTTPRequest
|
|
|
|
*
|
|
|
|
* @return RSSFeed
|
2010-12-11 06:01:19 +01:00
|
|
|
*/
|
2012-07-31 10:45:29 +02:00
|
|
|
public function getFeed(SS_HTTPRequest $request) {
|
2012-07-22 03:30:33 +02:00
|
|
|
$link = $this->Link('rss');
|
2012-07-31 10:45:29 +02:00
|
|
|
$class = $request->param('ID');
|
|
|
|
$id = $request->param('OtherID');
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2015-04-07 05:56:00 +02:00
|
|
|
// Support old pageid param
|
|
|
|
if(!$id && !$class && ($id = $request->getVar('pageid'))) {
|
|
|
|
$class = 'SiteTree';
|
|
|
|
}
|
|
|
|
|
2012-12-16 05:24:58 +01:00
|
|
|
$comments = Comment::get()->filter(array(
|
|
|
|
'Moderated' => 1,
|
|
|
|
'IsSpam' => 0,
|
|
|
|
));
|
|
|
|
|
2015-04-07 05:56:00 +02:00
|
|
|
// Check if class filter
|
|
|
|
if($class) {
|
|
|
|
if(!is_subclass_of($class, 'DataObject') || !$class::has_extension('CommentsExtension')) {
|
2012-07-22 03:30:33 +02:00
|
|
|
return $this->httpError(404);
|
|
|
|
}
|
2015-04-07 05:56:00 +02:00
|
|
|
$this->setBaseClass($class);
|
|
|
|
$comments = $comments->filter('BaseClass', $class);
|
|
|
|
$link = Controller::join_links($link, $class);
|
|
|
|
|
|
|
|
// Check if id filter
|
|
|
|
if($id) {
|
|
|
|
$comments = $comments->filter('ParentID', $id);
|
|
|
|
$link = Controller::join_links($link, $id);
|
|
|
|
$this->setOwnerRecord(DataObject::get_by_id($class, $id));
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = _t('CommentingController.RSSTITLE', "Comments RSS Feed");
|
|
|
|
|
2012-07-31 10:45:29 +02:00
|
|
|
$comments = new PaginatedList($comments, $request);
|
2015-04-07 05:56:00 +02:00
|
|
|
$comments->setPageLength($this->getOption('comments_per_page'));
|
2012-07-31 10:45:29 +02:00
|
|
|
|
2013-03-05 10:01:42 +01:00
|
|
|
return new RSSFeed(
|
|
|
|
$comments,
|
|
|
|
$link,
|
|
|
|
$title,
|
|
|
|
$link,
|
|
|
|
'Title', 'EscapedComment', 'AuthorName'
|
|
|
|
);
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a given {@link Comment} via the URL.
|
|
|
|
*/
|
2012-07-31 10:45:29 +02:00
|
|
|
public function delete() {
|
2015-03-27 05:40:00 +01:00
|
|
|
$comment = $this->getComment();
|
|
|
|
if(!$comment) return $this->httpError(404);
|
2015-03-30 07:57:29 +02:00
|
|
|
if(!$comment->canDelete()) {
|
|
|
|
return Security::permissionFailure($this, 'You do not have permission to delete this comment');
|
|
|
|
}
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2015-03-27 05:40:00 +01:00
|
|
|
$comment->delete();
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2015-03-27 05:40:00 +01:00
|
|
|
return $this->request->isAjax()
|
|
|
|
? true
|
|
|
|
: $this->redirectBack();
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a given {@link Comment} as spam. Removes the comment from display
|
|
|
|
*/
|
|
|
|
public function spam() {
|
|
|
|
$comment = $this->getComment();
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment) return $this->httpError(404);
|
2015-03-30 07:57:29 +02:00
|
|
|
if(!$comment->canEdit()) {
|
|
|
|
return Security::permissionFailure($this, 'You do not have permission to edit this comment');
|
|
|
|
}
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
|
|
|
|
|
|
|
|
$comment->IsSpam = true;
|
|
|
|
$comment->Moderated = true;
|
|
|
|
$comment->write();
|
|
|
|
|
|
|
|
return $this->request->isAjax()
|
|
|
|
? $comment->renderWith('CommentsInterface_singlecomment')
|
|
|
|
: $this->redirectBack();
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a given {@link Comment} as ham (not spam).
|
|
|
|
*/
|
2012-07-31 10:45:29 +02:00
|
|
|
public function ham() {
|
2012-07-22 03:30:33 +02:00
|
|
|
$comment = $this->getComment();
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment) return $this->httpError(404);
|
2015-03-30 07:57:29 +02:00
|
|
|
if(!$comment->canEdit()) {
|
|
|
|
return Security::permissionFailure($this, 'You do not have permission to edit this comment');
|
|
|
|
}
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
|
|
|
|
|
|
|
|
$comment->IsSpam = false;
|
|
|
|
$comment->Moderated = true;
|
|
|
|
$comment->write();
|
|
|
|
|
|
|
|
return $this->request->isAjax()
|
|
|
|
? $comment->renderWith('CommentsInterface_singlecomment')
|
|
|
|
: $this->redirectBack();
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a given {@link Comment} as approved.
|
|
|
|
*/
|
2012-07-31 10:45:29 +02:00
|
|
|
public function approve() {
|
2012-07-22 03:30:33 +02:00
|
|
|
$comment = $this->getComment();
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment) return $this->httpError(404);
|
2015-03-30 07:57:29 +02:00
|
|
|
if(!$comment->canEdit()) {
|
|
|
|
return Security::permissionFailure($this, 'You do not have permission to approve this comment');
|
|
|
|
}
|
2015-03-27 05:40:00 +01:00
|
|
|
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2015-03-27 05:40:00 +01:00
|
|
|
$comment->IsSpam = false;
|
|
|
|
$comment->Moderated = true;
|
|
|
|
$comment->write();
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2015-03-27 05:40:00 +01:00
|
|
|
return $this->request->isAjax()
|
|
|
|
? $comment->renderWith('CommentsInterface_singlecomment')
|
|
|
|
: $this->redirectBack();
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-31 10:45:29 +02:00
|
|
|
* Returns the comment referenced in the URL (by ID). Permission checking
|
|
|
|
* should be done in the callee.
|
2012-07-22 03:30:33 +02:00
|
|
|
*
|
|
|
|
* @return Comment|false
|
|
|
|
*/
|
|
|
|
public function getComment() {
|
2010-12-11 06:01:19 +01:00
|
|
|
$id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : false;
|
|
|
|
|
|
|
|
if($id) {
|
|
|
|
$comment = DataObject::get_by_id('Comment', $id);
|
|
|
|
|
2012-07-22 03:30:33 +02:00
|
|
|
if($comment) {
|
2015-03-30 07:57:29 +02:00
|
|
|
$this->fallbackReturnURL = $comment->Link();
|
2012-07-22 03:30:33 +02:00
|
|
|
return $comment;
|
2010-12-11 06:01:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-22 03:30:33 +02:00
|
|
|
return false;
|
2010-12-11 06:01:19 +01:00
|
|
|
}
|
2012-07-22 03:30:33 +02:00
|
|
|
|
2010-12-11 06:01:19 +01:00
|
|
|
/**
|
|
|
|
* Post a comment form
|
|
|
|
*
|
2010-12-06 11:08:38 +01:00
|
|
|
* @return Form
|
|
|
|
*/
|
2012-07-22 03:30:33 +02:00
|
|
|
public function CommentsForm() {
|
2015-04-07 05:56:00 +02:00
|
|
|
$usePreview = $this->getOption('use_preview');
|
2013-02-20 07:15:28 +01:00
|
|
|
|
2013-03-05 10:01:42 +01:00
|
|
|
$fields = new FieldList(
|
|
|
|
$dataFields = new CompositeField(
|
2013-03-05 15:37:08 +01:00
|
|
|
TextField::create("Name", _t('CommentInterface.YOURNAME', 'Your name'))
|
|
|
|
->setCustomValidationMessage(_t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'))
|
|
|
|
->setAttribute('data-message-required', _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name')),
|
2013-03-05 10:01:42 +01:00
|
|
|
|
2013-03-05 15:37:08 +01:00
|
|
|
EmailField::create("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)"))
|
|
|
|
->setCustomValidationMessage(_t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'))
|
|
|
|
->setAttribute('data-message-required', _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'))
|
|
|
|
->setAttribute('data-message-email', _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address')),
|
2013-03-05 10:01:42 +01:00
|
|
|
|
2013-03-05 15:37:08 +01:00
|
|
|
TextField::create("URL", _t('CommentingController.WEBSITEURL', "Your website URL"))
|
|
|
|
->setAttribute('data-message-url', _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL')),
|
2013-03-05 10:01:42 +01:00
|
|
|
|
2013-03-05 15:37:08 +01:00
|
|
|
TextareaField::create("Comment", _t('CommentingController.COMMENTS', "Comments"))
|
|
|
|
->setCustomValidationMessage(_t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'))
|
2013-03-05 10:01:42 +01:00
|
|
|
->setAttribute('data-message-required', _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'))
|
|
|
|
),
|
2013-02-20 07:15:28 +01:00
|
|
|
HiddenField::create("ParentID"),
|
|
|
|
HiddenField::create("ReturnURL"),
|
|
|
|
HiddenField::create("BaseClass")
|
2010-12-06 11:08:38 +01:00
|
|
|
);
|
|
|
|
|
2013-03-04 11:37:18 +01:00
|
|
|
// Preview formatted comment. Makes most sense when shortcodes or
|
|
|
|
// limited HTML is allowed. Populated by JS/Ajax.
|
|
|
|
if($usePreview) {
|
|
|
|
$fields->insertAfter(
|
|
|
|
ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview'))
|
|
|
|
->setAttribute('style', 'display: none'), // enable through JS
|
|
|
|
'Comment'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-05 10:01:42 +01:00
|
|
|
$dataFields->addExtraClass('data-fields');
|
2013-03-04 11:37:18 +01:00
|
|
|
|
2010-12-06 11:08:38 +01:00
|
|
|
// save actions
|
2012-06-01 08:34:31 +02:00
|
|
|
$actions = new FieldList(
|
2010-12-06 11:08:38 +01:00
|
|
|
new FormAction("doPostComment", _t('CommentInterface.POST', 'Post'))
|
|
|
|
);
|
2013-03-04 11:37:18 +01:00
|
|
|
if($usePreview) {
|
|
|
|
$actions->push(
|
|
|
|
FormAction::create('doPreviewComment', _t('CommentInterface.PREVIEW', 'Preview'))
|
|
|
|
->addExtraClass('action-minor')
|
|
|
|
->setAttribute('style', 'display: none') // enable through JS
|
|
|
|
);
|
|
|
|
}
|
2010-12-06 11:08:38 +01:00
|
|
|
|
|
|
|
// required fields for server side
|
|
|
|
$required = new RequiredFields(array(
|
|
|
|
'Name',
|
|
|
|
'Email',
|
|
|
|
'Comment'
|
|
|
|
));
|
|
|
|
|
|
|
|
// create the comment form
|
|
|
|
$form = new Form($this, 'CommentsForm', $fields, $actions, $required);
|
|
|
|
|
|
|
|
// if the record exists load the extra required data
|
|
|
|
if($record = $this->getOwnerRecord()) {
|
2015-04-08 07:55:19 +02:00
|
|
|
|
|
|
|
// Load member data
|
|
|
|
$member = Member::currentUser();
|
|
|
|
if(($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) {
|
|
|
|
$fields = $form->Fields();
|
|
|
|
|
|
|
|
$fields->removeByName('Name');
|
|
|
|
$fields->removeByName('Email');
|
|
|
|
$fields->insertBefore(new ReadonlyField("NameView", _t('CommentInterface.YOURNAME', 'Your name'), $member->getName()), 'URL');
|
|
|
|
$fields->push(new HiddenField("Name", "", $member->getName()));
|
|
|
|
$fields->push(new HiddenField("Email", "", $member->Email));
|
|
|
|
}
|
|
|
|
|
2010-12-06 11:08:38 +01:00
|
|
|
// we do not want to read a new URL when the form has already been submitted
|
|
|
|
// which in here, it hasn't been.
|
|
|
|
$form->loadDataFrom(array(
|
|
|
|
'ParentID' => $record->ID,
|
2015-04-07 05:56:00 +02:00
|
|
|
'ReturnURL' => $this->request->getURL(),
|
2010-12-06 11:08:38 +01:00
|
|
|
'BaseClass' => $this->getBaseClass()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set it so the user gets redirected back down to the form upon form fail
|
|
|
|
$form->setRedirectToFormOnValidationError(true);
|
|
|
|
|
|
|
|
// load any data from the cookies
|
|
|
|
if($data = Cookie::get('CommentsForm_UserData')) {
|
2011-09-15 16:37:10 +02:00
|
|
|
$data = Convert::json2array($data);
|
2013-06-04 22:12:16 +02:00
|
|
|
|
2010-12-06 11:08:38 +01:00
|
|
|
$form->loadDataFrom(array(
|
|
|
|
"Name" => isset($data['Name']) ? $data['Name'] : '',
|
|
|
|
"URL" => isset($data['URL']) ? $data['URL'] : '',
|
2013-06-04 22:12:16 +02:00
|
|
|
"Email" => isset($data['Email']) ? $data['Email'] : ''
|
|
|
|
));
|
|
|
|
// allow previous value to fill if comment not stored in cookie (i.e. validation error)
|
|
|
|
$prevComment = Cookie::get('CommentsForm_Comment');
|
|
|
|
if($prevComment && $prevComment != ''){
|
2015-04-07 05:56:00 +02:00
|
|
|
$form->loadDataFrom(array("Comment" => $prevComment));
|
2013-06-04 22:12:16 +02:00
|
|
|
}
|
2010-12-06 11:08:38 +01:00
|
|
|
}
|
2012-07-22 03:30:33 +02:00
|
|
|
|
|
|
|
if($member) {
|
2013-06-04 22:12:16 +02:00
|
|
|
$form->loadDataFrom($member);
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
2010-12-06 11:08:38 +01:00
|
|
|
|
|
|
|
// hook to allow further extensions to alter the comments form
|
|
|
|
$this->extend('alterCommentForm', $form);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process which creates a {@link Comment} once a user submits a comment from this form.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
*/
|
2012-07-22 03:30:33 +02:00
|
|
|
public function doPostComment($data, $form) {
|
2015-04-07 05:56:00 +02:00
|
|
|
// Load class and parent from data
|
|
|
|
if(isset($data['BaseClass'])) {
|
|
|
|
$this->setBaseClass($data['BaseClass']);
|
|
|
|
}
|
|
|
|
if(isset($data['ParentID']) && ($class = $this->getBaseClass())) {
|
|
|
|
$this->setOwnerRecord($class::get()->byID($data['ParentID']));
|
|
|
|
}
|
|
|
|
if(!$this->getOwnerRecord()) return $this->httpError(404);
|
2010-12-06 11:08:38 +01:00
|
|
|
|
|
|
|
// cache users data
|
2011-09-15 16:37:10 +02:00
|
|
|
Cookie::set("CommentsForm_UserData", Convert::raw2json($data));
|
2010-12-06 11:08:38 +01:00
|
|
|
Cookie::set("CommentsForm_Comment", $data['Comment']);
|
|
|
|
|
|
|
|
// extend hook to allow extensions. Also see onAfterPostComment
|
|
|
|
$this->extend('onBeforePostComment', $form);
|
|
|
|
|
|
|
|
// If commenting can only be done by logged in users, make sure the user is logged in
|
2015-04-07 05:56:00 +02:00
|
|
|
if(!$this->getOwnerRecord()->canPostComment()) {
|
2015-04-01 06:12:47 +02:00
|
|
|
return Security::permissionFailure(
|
|
|
|
$this,
|
|
|
|
_t(
|
|
|
|
'CommentingController.PERMISSIONFAILURE',
|
|
|
|
"You're not able to post comments to this page. Please ensure you are logged in and have an "
|
|
|
|
. "appropriate permission level."
|
|
|
|
)
|
|
|
|
);
|
2010-12-06 11:08:38 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:12:47 +02:00
|
|
|
if($member = Member::currentUser()) {
|
|
|
|
$form->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID));
|
|
|
|
}
|
|
|
|
|
2015-04-08 07:55:19 +02:00
|
|
|
// What kind of moderation is required?
|
|
|
|
switch($this->getOwnerRecord()->ModerationRequired) {
|
|
|
|
case 'Required':
|
|
|
|
$requireModeration = true;
|
|
|
|
break;
|
|
|
|
case 'NonMembersOnly':
|
|
|
|
$requireModeration = empty($member);
|
|
|
|
break;
|
|
|
|
case 'None':
|
|
|
|
default:
|
|
|
|
$requireModeration = false;
|
|
|
|
break;
|
2013-06-04 22:12:16 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 02:51:14 +02:00
|
|
|
// we want to show a notification if comments are moderated
|
2015-04-01 06:12:47 +02:00
|
|
|
if ($requireModeration) {
|
2012-08-08 02:51:14 +02:00
|
|
|
Session::set('CommentsModerated', 1);
|
|
|
|
}
|
|
|
|
|
2010-12-06 11:08:38 +01:00
|
|
|
$comment = new Comment();
|
|
|
|
$form->saveInto($comment);
|
|
|
|
|
2015-04-07 05:56:00 +02:00
|
|
|
$comment->AllowHtml = $this->getOption('html_allowed');
|
2015-04-01 06:12:47 +02:00
|
|
|
$comment->Moderated = !$requireModeration;
|
2013-03-04 11:37:18 +01:00
|
|
|
|
|
|
|
// Save into DB, or call pre-save hooks to give accurate preview
|
2015-04-07 05:56:00 +02:00
|
|
|
$usePreview = $this->getOption('use_preview');
|
|
|
|
$isPreview = $usePreview && !empty($data['IsPreview']);
|
2013-03-04 11:37:18 +01:00
|
|
|
if($isPreview) {
|
2015-04-07 05:56:00 +02:00
|
|
|
$comment->extend('onBeforeWrite');
|
2013-03-04 11:37:18 +01:00
|
|
|
} else {
|
2015-04-07 05:56:00 +02:00
|
|
|
$comment->write();
|
2012-11-27 13:56:21 +01:00
|
|
|
|
2015-04-01 06:12:47 +02:00
|
|
|
// extend hook to allow extensions. Also see onBeforePostComment
|
|
|
|
$this->extend('onAfterPostComment', $comment);
|
2013-03-05 10:01:42 +01:00
|
|
|
}
|
2010-12-06 11:08:38 +01:00
|
|
|
|
|
|
|
// clear the users comment since it passed validation
|
|
|
|
Cookie::set('CommentsForm_Comment', false);
|
2012-07-31 10:45:29 +02:00
|
|
|
|
2015-04-01 06:12:47 +02:00
|
|
|
// Find parent link
|
|
|
|
if(!empty($data['ReturnURL'])) {
|
|
|
|
$url = $data['ReturnURL'];
|
|
|
|
} elseif($parent = $comment->getParent()) {
|
|
|
|
$url = $parent->Link();
|
|
|
|
} else {
|
|
|
|
return $this->redirectBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a redirect page exists, attempt to link to the correct anchor
|
|
|
|
if(!$comment->Moderated) {
|
|
|
|
// Display the "awaiting moderation" text
|
2015-04-07 05:56:00 +02:00
|
|
|
$holder = $this->getOption('comments_holder_id');
|
2015-04-01 06:12:47 +02:00
|
|
|
$hash = "{$holder}_PostCommentForm_error";
|
|
|
|
} elseif($comment->IsSpam) {
|
|
|
|
// Link to the form with the error message contained
|
|
|
|
$hash = $form->FormName();
|
|
|
|
} else {
|
|
|
|
// Link to the moderated, non-spam comment
|
|
|
|
$hash = $comment->Permalink();
|
|
|
|
}
|
2010-12-07 01:34:17 +01:00
|
|
|
|
2015-04-01 06:12:47 +02:00
|
|
|
return $this->redirect(Controller::join_links($url, "#{$hash}"));
|
2010-12-06 11:08:38 +01:00
|
|
|
}
|
2013-03-04 11:37:18 +01:00
|
|
|
|
|
|
|
public function doPreviewComment($data, $form) {
|
|
|
|
$data['IsPreview'] = 1;
|
2013-03-05 10:01:42 +01:00
|
|
|
|
2013-03-04 11:37:18 +01:00
|
|
|
return $this->doPostComment($data, $form);
|
|
|
|
}
|
2015-03-30 07:57:29 +02:00
|
|
|
|
|
|
|
public function redirectBack() {
|
|
|
|
// Don't cache the redirect back ever
|
|
|
|
HTTP::set_cache_age(0);
|
|
|
|
|
|
|
|
$url = null;
|
|
|
|
|
|
|
|
// In edge-cases, this will be called outside of a handleRequest() context; in that case,
|
|
|
|
// redirect to the homepage - don't break into the global state at this stage because we'll
|
|
|
|
// be calling from a test context or something else where the global state is inappropraite
|
|
|
|
if($this->request) {
|
|
|
|
if($this->request->requestVar('BackURL')) {
|
|
|
|
$url = $this->request->requestVar('BackURL');
|
|
|
|
} else if($this->request->isAjax() && $this->request->getHeader('X-Backurl')) {
|
|
|
|
$url = $this->request->getHeader('X-Backurl');
|
|
|
|
} else if($this->request->getHeader('Referer')) {
|
|
|
|
$url = $this->request->getHeader('Referer');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$url) $url = $this->fallbackReturnURL;
|
|
|
|
if(!$url) $url = Director::baseURL();
|
|
|
|
|
|
|
|
// absolute redirection URLs not located on this site may cause phishing
|
|
|
|
if(Director::is_site_url($url)) {
|
|
|
|
return $this->redirect($url);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-08-08 02:51:14 +02:00
|
|
|
}
|