APICHANGE: moved comments_show_per_page to Commenting default configuration. Instead of setting it via the function. BUGFIX: updated CommentForm to use Commenting configuration API

This commit is contained in:
Will Rossiter 2010-12-03 15:03:49 +13:00
parent e28fdbff35
commit e7d8471a43
4 changed files with 40 additions and 77 deletions

View File

@ -27,7 +27,8 @@ class Commenting {
'required_permission' => '', // required permission to comment (or array of permissions)
'use_ajax_commenting' => true, // use ajax to post comments.
'show_comments_when_disabled' => false, // when comments are disabled should we show older comments (if available)
'order_comments_by' => "\"Created\" DESC"
'order_comments_by' => "\"Created\" DESC",
'comments_per_page' => 10
);
/**

View File

@ -36,32 +36,8 @@ class CommentInterface extends RequestHandler {
return Controller::join_links($this->controller->Link(), $this->methodName);
}
/**
* if this page comment form requires users to have a
* valid permission code in order to post (used to customize the error
* message).
*
* @return bool
*/
function PostingRequiresPermission() {
return self::$comments_require_permission;
}
function Page() {
return $this->page;
}
function PostCommentForm() {
// Load the data from Session
$form->loadDataFrom(array(
"Name" => Cookie::get("CommentInterface_Name"),
"Comment" => Cookie::get("CommentInterface_Comment"),
"CommenterURL" => Cookie::get("CommentInterface_CommenterURL")
));
return $form;
}

View File

@ -41,29 +41,6 @@ class CommentsExtension extends DataObjectDecorator {
return array_merge($fields, $relationships);
}
/**
* @var int Number of comments to show per page
*/
private static $comments_per_page = 10;
/**
* Set the number of comments displayed per page
*
* @param int Number of comments to show per page
*/
public static function set_comments_per_page($num) {
self::$comments_per_page = (int)$num;
}
/**
* Returns the number of comments per page
*
* @return int
*/
public static function comments_per_page() {
return self::$comments_per_page;
}
/**
* Returns a list of all the comments attached to this record.
*
@ -93,30 +70,16 @@ class CommentsExtension extends DataObjectDecorator {
// trigger comments on / off via ProvideComments
$enabled = (!$this->attachedToSiteTree() || $this->owner->ProvideComments) ? true : false;
$form = ($enabled) ? new CommentForm(Controller::curr(), 'CommentsForm', $this->owner) : false;
// if comments are turned off then
return $interface->process(new ArrayData(array(
'CommentsEnabled' => $enabled,
'AddCommentForm' => $this->AddCommentForm(),
'AddCommentForm' => $form,
'Comments' => $this->Comments()
)));
}
/**
* Add Comment Form.
*
* @see CommentForm
* @return Form|bool
*/
public function AddCommentForm() {
$form = new CommentForm(Controller::curr(), 'CommentsForm');
// hook to allow further extensions to alter the comments form
$this->extend('alterAddCommentForm', $form);
return $form;
}
/**
* Returns whether this extension instance is attached to a {@link SiteTree} object
*
@ -126,8 +89,6 @@ class CommentsExtension extends DataObjectDecorator {
return ClassInfo::is_subclass_of($this->ownerBaseClass, 'SiteTree');
}
/**
* @deprecated 1.0 Please use {@link CommentsExtension->CommentsForm()}
*/

View File

@ -13,11 +13,15 @@ class CommentForm extends Form {
*
* @return Form
*/
function __construct($controller, $name) {
function __construct($controller, $name, $class) {
$member = Member::currentUser();
if((self::$comments_require_login || self::$comments_require_permission) && $member && $member->FirstName) {
$class = (is_object($class)) ? $class->ClassName : $class;
$fields = new FieldSet();
if((Commenting::get_config_value($class, 'require_login')
|| Commenting::get_config_value($class, 'required_permission'))
&& ($member && $member->FirstName)) {
// note this was a ReadonlyField - which displayed the name in a span as well as the hidden field but
// it was not saving correctly. Have changed it to a hidden field. It passes the data correctly but I
// believe the id of the form field is wrong.
@ -38,7 +42,28 @@ class CommentForm extends Form {
// Set it so the user gets redirected back down to the form upon form fail
$this->setRedirectToFormOnValidationError(true);
$required = new RequiredFields();
// Required fields for server side
$required = new RequiredFields(array(
'Name',
'Email',
'Comment'
));
// load any data from the cookies
if($data = Cookie::get('CommentsForm_UserData')) {
$data = unserialize($data);
$form->loadDataFrom(array(
"Name" => isset($data['Name']) ? $data['Name'] : '',
"URL" => isset($data['URL']) ? $data['URL'] : '',
"Email" => isset($data['Email']) ? $data['Email'] : '',
"Comment" => Cookie::get('CommentsForm_Comment')
));
}
// hook to allow further extensions to alter the comments form
$this->extend('alterCommentForm', $form);
parent::__construct($controller, $name, $fields, $actions, $required);
}
@ -52,10 +77,9 @@ class CommentForm extends Form {
function doPostComment($data, $form) {
// cache users data
Cookie::set("CommentInterface_Name", $data['Name']);
Cookie::set("CommentInterface_CommenterURL", $data['CommenterURL']);
Cookie::set("CommentInterface_Comment", $data['Comment']);
Cookie::set("CommentsForm_UserData", serialize($data));
Cookie::set("CommentsForm_Comment", $data['Comment']);
// @todo turn this into an extension
if(SSAkismet::isEnabled()) {
try {
@ -104,10 +128,11 @@ class CommentForm extends Form {
$comment->NeedsModeration = Comment::moderationEnabled();
$comment->write();
Cookie::set("CommentInterface_Comment", '');
$moderationMsg = _t('CommentInterface_Form.AWAITINGMODERATION', "Your comment has been submitted and is now awaiting moderation.");
// clear the users comment since it passed validation
Cookie::set('CommentsForm_Comment', false);
if(Director::is_ajax()) {
if($comment->NeedsModeration){
echo $moderationMsg;