2010-11-30 01:33:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension to {@link DataObject} to enable tracking comments.
|
|
|
|
*
|
|
|
|
* @package comments
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CommentsExtension extends DataObjectDecorator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function extraStatics() {
|
2010-11-30 04:30:10 +01:00
|
|
|
$fields = array();
|
|
|
|
|
|
|
|
$relationships = array(
|
2010-11-30 01:33:19 +01:00
|
|
|
'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
|
|
|
|
// cannot check $this->owner as this in intialised via call_user_func
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
if($args && ($owner = array_shift($args))) {
|
|
|
|
if(ClassInfo::is_subclass_of($owner, 'SiteTree') || $owner == "SiteTree") {
|
|
|
|
$fields = array(
|
|
|
|
'db' => array(
|
|
|
|
'ProvideComments' => 'Boolean'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2010-12-06 11:09:04 +01:00
|
|
|
|
2010-11-30 04:30:10 +01:00
|
|
|
return array_merge($fields, $relationships);
|
2010-11-30 01:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of all the comments attached to this record.
|
|
|
|
*
|
|
|
|
* @todo pagination
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
function Comments() {
|
2010-12-07 01:34:17 +01:00
|
|
|
return DataObject::get(
|
|
|
|
'Comment',
|
|
|
|
"\"ParentID\" = '". $this->owner->ID ."' AND \"BaseClass\" = '". $this->ownerBaseClass ."'",
|
|
|
|
Commenting::get_config_value($this->ownerBaseClass, 'order_comments_by')
|
|
|
|
);
|
2010-11-30 01:33:19 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 11:09:04 +01:00
|
|
|
|
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.
|
|
|
|
*
|
2010-12-06 11:09:04 +01:00
|
|
|
* @todo Cleanup the passing of all this state based functionality
|
|
|
|
*
|
2010-11-30 01:33:19 +01:00
|
|
|
* @see docs/en/Extending
|
|
|
|
*/
|
2010-11-30 04:30:10 +01:00
|
|
|
public function CommentsForm() {
|
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;
|
|
|
|
|
2010-12-06 11:09:04 +01:00
|
|
|
$controller = new CommentingController();
|
|
|
|
|
|
|
|
// tad bit messy but needed to ensure all datas available
|
|
|
|
$controller->setOwnerRecord($this->owner);
|
|
|
|
$controller->setBaseClass($this->ownerBaseClass);
|
|
|
|
$controller->setOwnerController(Controller::curr());
|
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),
|
|
|
|
'CommentsEnabled' => $enabled,
|
|
|
|
'AddCommentForm' => $form,
|
|
|
|
'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() {
|
|
|
|
return ClassInfo::is_subclass_of($this->ownerBaseClass, 'SiteTree');
|
|
|
|
}
|
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 1.0 Please use {@link CommentsExtension->CommentsForm()}
|
|
|
|
*/
|
|
|
|
function PageComments() {
|
2010-11-30 04:30:10 +01:00
|
|
|
user_error('$PageComments is deprecated. Please use $CommentsForm', E_USER_WARNING);
|
2010-11-30 01:33:19 +01:00
|
|
|
|
|
|
|
return $this->CommentsForm();
|
|
|
|
}
|
|
|
|
}
|