2010-11-30 01:33:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension to {@link DataObject} to enable tracking comments.
|
|
|
|
*
|
|
|
|
* @package comments
|
|
|
|
*/
|
|
|
|
|
2012-06-01 08:34:31 +02:00
|
|
|
class CommentsExtension extends DataExtension {
|
2010-11-30 01:33:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a relationship between this {@link DataObject} and its
|
2010-11-30 04:30:10 +01:00
|
|
|
* {@link Comment} objects. If the owner class is a sitetree class
|
|
|
|
* it also enables a checkbox allowing comments to be turned off and off
|
2010-11-30 01:33:19 +01:00
|
|
|
*/
|
2013-04-11 14:23:03 +02:00
|
|
|
public static function get_extra_config($class, $extension, $args = null) {
|
|
|
|
$config = array('has_many' => array('Comments' => 'Comment'));
|
|
|
|
|
2010-11-30 04:30:10 +01:00
|
|
|
// if it is attached to the SiteTree then we need to add ProvideComments
|
2012-10-11 00:01:14 +02:00
|
|
|
if(is_subclass_of($class, 'SiteTree') || $class == 'SiteTree') {
|
2013-04-11 14:23:03 +02:00
|
|
|
$config['db'] = array('ProvideComments' => 'Boolean');
|
2010-11-30 04:30:10 +01:00
|
|
|
}
|
2010-12-06 11:09:04 +01:00
|
|
|
|
2013-04-11 14:23:03 +02:00
|
|
|
return $config;
|
2010-11-30 01:33:19 +01:00
|
|
|
}
|
2012-06-01 08:34:31 +02:00
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
|
2010-12-13 00:33:05 +01:00
|
|
|
/**
|
|
|
|
* If this extension is applied to a {@link SiteTree} record then
|
|
|
|
* append a Provide Comments checkbox to allow authors to trigger
|
|
|
|
* whether or not to display comments
|
|
|
|
*
|
|
|
|
* @todo Allow customization of other {@link Commenting} configuration
|
|
|
|
*
|
|
|
|
* @param FieldSet
|
|
|
|
*/
|
2012-07-21 23:46:51 +02:00
|
|
|
public function updateSettingsFields(FieldList $fields) {
|
2010-12-13 00:33:05 +01:00
|
|
|
if($this->attachedToSiteTree()) {
|
2012-07-21 23:46:51 +02:00
|
|
|
$fields->addFieldToTab('Root.Settings',
|
2010-12-13 00:33:05 +01:00
|
|
|
new CheckboxField('ProvideComments', _t('Comment.ALLOWCOMMENTS', 'Allow Comments'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
/**
|
|
|
|
* Returns a list of all the comments attached to this record.
|
|
|
|
*
|
2012-07-22 03:44:30 +02:00
|
|
|
* @return PaginatedList
|
2010-11-30 01:33:19 +01:00
|
|
|
*/
|
2010-12-13 00:33:05 +01:00
|
|
|
public function Comments() {
|
|
|
|
$order = Commenting::get_config_value($this->ownerBaseClass, 'order_comments_by');
|
2012-08-08 02:51:14 +02:00
|
|
|
|
2012-12-16 05:24:58 +01:00
|
|
|
$list = Comment::get()->filter(array(
|
|
|
|
'ParentID' => $this->owner->ID,
|
|
|
|
'BaseClass' => $this->ownerBaseClass
|
|
|
|
))->sort($order);
|
|
|
|
|
2013-01-17 02:45:47 +01:00
|
|
|
// Filter content for unauthorised users
|
|
|
|
if (!($member = Member::currentUser()) || !Permission::checkMember($member, 'CMS_ACCESS_CommentAdmin')) {
|
|
|
|
|
|
|
|
// Filter unmoderated comments for non-administrators if moderation is enabled
|
|
|
|
if (Commenting::get_config_value($this->ownerBaseClass, 'require_moderation')) {
|
2012-12-16 05:24:58 +01:00
|
|
|
$list = $list->filter('Moderated', 1);
|
2013-01-17 02:45:47 +01:00
|
|
|
} else {
|
|
|
|
// Filter spam comments for non-administrators if auto-moderted
|
|
|
|
$list = $list->filter('IsSpam', 0);
|
2012-08-08 02:51:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 05:24:58 +01:00
|
|
|
$list = new PaginatedList($list);
|
2012-07-22 03:44:30 +02:00
|
|
|
|
|
|
|
$list->setPageLength(Commenting::get_config_value(
|
|
|
|
$this->ownerBaseClass, 'comments_per_page'
|
|
|
|
));
|
|
|
|
|
2012-07-31 10:45:29 +02:00
|
|
|
|
|
|
|
$controller = Controller::curr();
|
2012-07-26 09:40:57 +02:00
|
|
|
$list->setPageStart($controller->request->getVar("commentsstart". $this->owner->ID));
|
2012-07-22 03:44:30 +02:00
|
|
|
$list->setPaginationGetVar("commentsstart". $this->owner->ID);
|
|
|
|
|
|
|
|
return $list;
|
2010-11-30 01:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Comments interface for the front end. Includes the CommentAddForm and the composition
|
|
|
|
* of the comments display.
|
|
|
|
*
|
|
|
|
* To customize the html see templates/CommentInterface.ss or extend this function with
|
|
|
|
* your own extension.
|
|
|
|
*
|
2012-07-22 03:44:30 +02:00
|
|
|
* @todo Cleanup the passing of all this configuration based functionality
|
2010-12-06 11:09:04 +01:00
|
|
|
*
|
2010-11-30 01:33:19 +01:00
|
|
|
* @see docs/en/Extending
|
|
|
|
*/
|
2010-11-30 04:30:10 +01:00
|
|
|
public function CommentsForm() {
|
2013-03-05 15:37:08 +01:00
|
|
|
if(Commenting::has_commenting($this->ownerBaseClass) && Commenting::get_config_value($this->ownerBaseClass, 'include_js')) {
|
2013-02-20 07:15:28 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
2013-02-19 08:01:25 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/lib/jquery.form.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/jquery.validate.pack.js');
|
|
|
|
Requirements::javascript('comments/javascript/CommentsInterface.js');
|
|
|
|
}
|
2013-02-19 07:46:58 +01:00
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
$interface = new SSViewer('CommentsInterface');
|
|
|
|
|
2010-11-30 10:49:21 +01:00
|
|
|
// detect whether we comments are enabled. By default if $CommentsForm is included
|
|
|
|
// on a {@link DataObject} then it is enabled, however {@link SiteTree} objects can
|
|
|
|
// trigger comments on / off via ProvideComments
|
|
|
|
$enabled = (!$this->attachedToSiteTree() || $this->owner->ProvideComments) ? true : false;
|
2010-12-11 06:33:21 +01:00
|
|
|
|
|
|
|
// do not include the comments on pages which don't have id's such as security pages
|
|
|
|
if($this->owner->ID < 0) return false;
|
|
|
|
|
2012-07-22 03:30:33 +02:00
|
|
|
$controller = new CommentingController();
|
2010-12-06 11:09:04 +01:00
|
|
|
$controller->setOwnerRecord($this->owner);
|
|
|
|
$controller->setBaseClass($this->ownerBaseClass);
|
|
|
|
$controller->setOwnerController(Controller::curr());
|
2012-08-08 02:51:14 +02:00
|
|
|
|
|
|
|
$moderatedSubmitted = Session::get('CommentsModerated');
|
|
|
|
Session::clear('CommentsModerated');
|
2010-11-30 10:49:21 +01:00
|
|
|
|
2010-12-06 11:09:04 +01:00
|
|
|
$form = ($enabled) ? $controller->CommentsForm() : false;
|
2010-12-03 03:03:49 +01:00
|
|
|
|
2010-12-06 11:09:04 +01:00
|
|
|
// a little bit all over the show but to ensure a slightly easier upgrade for users
|
|
|
|
// return back the same variables as previously done in comments
|
2010-11-30 01:33:19 +01:00
|
|
|
return $interface->process(new ArrayData(array(
|
2010-12-06 11:09:04 +01:00
|
|
|
'CommentHolderID' => Commenting::get_config_value($this->ownerBaseClass, 'comments_holder_id'),
|
|
|
|
'PostingRequiresPermission' => Commenting::get_config_value($this->ownerBaseClass, 'required_permission'),
|
|
|
|
'CanPost' => Commenting::can_member_post($this->ownerBaseClass),
|
2012-07-22 03:30:33 +02:00
|
|
|
'RssLink' => "CommentingController/rss",
|
|
|
|
'RssLinkPage' => "CommentingController/rss/". $this->ownerBaseClass . '/'.$this->owner->ID,
|
2010-12-06 11:09:04 +01:00
|
|
|
'CommentsEnabled' => $enabled,
|
2012-12-16 00:44:57 +01:00
|
|
|
'Parent' => $this->owner,
|
2010-12-06 11:09:04 +01:00
|
|
|
'AddCommentForm' => $form,
|
2012-08-08 02:51:14 +02:00
|
|
|
'ModeratedSubmitted' => $moderatedSubmitted,
|
2010-12-06 11:09:04 +01:00
|
|
|
'Comments' => $this->Comments()
|
2010-11-30 01:33:19 +01:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2010-11-30 04:30:10 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this extension instance is attached to a {@link SiteTree} object
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function attachedToSiteTree() {
|
2010-12-13 00:33:05 +01:00
|
|
|
$class = $this->ownerBaseClass;
|
|
|
|
|
2012-06-01 08:34:31 +02:00
|
|
|
return (is_subclass_of($class, 'SiteTree')) || ($class == 'SiteTree');
|
2010-11-30 04:30:10 +01:00
|
|
|
}
|
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 1.0 Please use {@link CommentsExtension->CommentsForm()}
|
|
|
|
*/
|
2012-07-22 03:30:33 +02:00
|
|
|
public function PageComments() {
|
2010-12-16 22:21:58 +01:00
|
|
|
// This method is very commonly used, don't throw a warning just yet
|
|
|
|
//user_error('$PageComments is deprecated. Please use $CommentsForm', E_USER_WARNING);
|
2010-11-30 01:33:19 +01:00
|
|
|
|
|
|
|
return $this->CommentsForm();
|
|
|
|
}
|
2012-08-08 02:51:14 +02:00
|
|
|
}
|