APICHANGE: Renamed PageCommentInterface to CommentsInterface. Implemented new CommentForm function to display templates

This commit is contained in:
Will Rossiter 2010-11-30 13:33:19 +13:00
parent c34c228ee2
commit c81f3cacfa
7 changed files with 136 additions and 66 deletions

View File

@ -3,10 +3,11 @@
* Represents an interface for viewing and adding page comments * Represents an interface for viewing and adding page comments
* Create one, passing the page discussed to the constructor. It can then be * Create one, passing the page discussed to the constructor. It can then be
* inserted into a template. * inserted into a template.
*
* @package cms * @package cms
* @subpackage comments * @subpackage comments
*/ */
class PageCommentInterface extends RequestHandler { class CommentInterface extends RequestHandler {
static $url_handlers = array( static $url_handlers = array(
'$Item!' => '$Item', '$Item!' => '$Item',
); );
@ -66,7 +67,7 @@ class PageCommentInterface extends RequestHandler {
/** /**
* Create a new page comment interface * Create a new page comment interface
* @param controller The controller that the interface is used on * @param controller The controller that the interface is used on
* @param methodName The method to return this PageCommentInterface object * @param methodName The method to return this CommentInterface object
* @param page The page that we're commenting on * @param page The page that we're commenting on
*/ */
function __construct($controller, $methodName, $page) { function __construct($controller, $methodName, $page) {
@ -81,7 +82,7 @@ class PageCommentInterface extends RequestHandler {
} }
/** /**
* See {@link PageCommentInterface::$comments_require_login} * See {@link CommentInterface::$comments_require_login}
* *
* @param boolean state The new state of this static field * @param boolean state The new state of this static field
*/ */
@ -90,7 +91,7 @@ class PageCommentInterface extends RequestHandler {
} }
/** /**
* See {@link PageCommentInterface::$comments_require_permission} * See {@link CommentInterface::$comments_require_permission}
* *
* @param string permission The permission to check against. * @param string permission The permission to check against.
*/ */
@ -99,7 +100,7 @@ class PageCommentInterface extends RequestHandler {
} }
/** /**
* See {@link PageCommentInterface::$show_comments_when_disabled} * See {@link CommentInterface::$show_comments_when_disabled}
* *
* @param bool - show / hide the existing comments when disabled * @param bool - show / hide the existing comments when disabled
*/ */
@ -108,7 +109,7 @@ class PageCommentInterface extends RequestHandler {
} }
/** /**
* See {@link PageCommentInterface::$order_comments_by} * See {@link CommentInterface::$order_comments_by}
* *
* @param String * @param String
*/ */
@ -117,7 +118,7 @@ class PageCommentInterface extends RequestHandler {
} }
/** /**
* See {@link PageCommentInterface::$use_ajax_commenting} * See {@link CommentInterface::$use_ajax_commenting}
* *
* @param bool * @param bool
*/ */
@ -126,15 +127,15 @@ class PageCommentInterface extends RequestHandler {
} }
function forTemplate() { function forTemplate() {
return $this->renderWith('PageCommentInterface'); return $this->renderWith('CommentInterface');
} }
/** /**
* @return boolean true if the currently logged in user can post a comment, * @return boolean true if the currently logged in user can post a comment,
* false if they can't. Users can post comments by default, enforce * false if they can't. Users can post comments by default, enforce
* security by using * security by using
* @link PageCommentInterface::set_comments_require_login() and * @link CommentInterface::set_comments_require_login() and
* @link {PageCommentInterface::set_comments_require_permission()}. * @link {CommentInterface::set_comments_require_permission()}.
*/ */
static function CanPostComment() { static function CanPostComment() {
$member = Member::currentUser(); $member = Member::currentUser();
@ -178,23 +179,23 @@ class PageCommentInterface extends RequestHandler {
// note this was a ReadonlyField - which displayed the name in a span as well as the hidden field but // 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 // 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. // believe the id of the form field is wrong.
$fields->push(new ReadonlyField("NameView", _t('PageCommentInterface.YOURNAME', 'Your name'), $member->getName())); $fields->push(new ReadonlyField("NameView", _t('CommentInterface.YOURNAME', 'Your name'), $member->getName()));
$fields->push(new HiddenField("Name", "", $member->getName())); $fields->push(new HiddenField("Name", "", $member->getName()));
} else { } else {
$fields->push(new TextField("Name", _t('PageCommentInterface.YOURNAME', 'Your name'))); $fields->push(new TextField("Name", _t('CommentInterface.YOURNAME', 'Your name')));
} }
// optional commenter URL // optional commenter URL
$fields->push(new TextField("CommenterURL", _t('PageCommentInterface.COMMENTERURL', "Your website URL"))); $fields->push(new TextField("CommenterURL", _t('CommentInterface.COMMENTERURL', "Your website URL")));
if(MathSpamProtection::isEnabled()){ if(MathSpamProtection::isEnabled()){
$fields->push(new TextField("Math", sprintf(_t('PageCommentInterface.SPAMQUESTION', "Spam protection question: %s"), MathSpamProtection::getMathQuestion()))); $fields->push(new TextField("Math", sprintf(_t('CommentInterface.SPAMQUESTION', "Spam protection question: %s"), MathSpamProtection::getMathQuestion())));
} }
$fields->push(new TextareaField("Comment", _t('PageCommentInterface.YOURCOMMENT', "Comments"))); $fields->push(new TextareaField("Comment", _t('CommentInterface.YOURCOMMENT', "Comments")));
$form = new PageCommentInterface_Form($this, "PostCommentForm", $fields, new FieldSet( $form = new CommentInterface_Form($this, "PostCommentForm", $fields, new FieldSet(
new FormAction("postcomment", _t('PageCommentInterface.POST', 'Post')) new FormAction("postcomment", _t('CommentInterface.POST', 'Post'))
)); ));
// Set it so the user gets redirected back down to the form upon form fail // Set it so the user gets redirected back down to the form upon form fail
@ -211,14 +212,14 @@ class PageCommentInterface extends RequestHandler {
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js'); Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js'); Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js');
Requirements::javascript(THIRDPARTY_DIR . '/scriptaculous/effects.js'); Requirements::javascript(THIRDPARTY_DIR . '/scriptaculous/effects.js');
Requirements::javascript(CMS_DIR . '/javascript/PageCommentInterface.js'); Requirements::javascript(CMS_DIR . '/javascript/CommentInterface.js');
} }
// Load the data from Session // Load the data from Session
$form->loadDataFrom(array( $form->loadDataFrom(array(
"Name" => Cookie::get("PageCommentInterface_Name"), "Name" => Cookie::get("CommentInterface_Name"),
"Comment" => Cookie::get("PageCommentInterface_Comment"), "Comment" => Cookie::get("CommentInterface_Comment"),
"CommenterURL" => Cookie::get("PageCommentInterface_CommenterURL") "CommenterURL" => Cookie::get("CommentInterface_CommenterURL")
)); ));
return $form; return $form;
@ -228,12 +229,12 @@ class PageCommentInterface extends RequestHandler {
// Comment limits // Comment limits
$limit = array(); $limit = array();
$limit['start'] = isset($_GET['commentStart']) ? (int)$_GET['commentStart'] : 0; $limit['start'] = isset($_GET['commentStart']) ? (int)$_GET['commentStart'] : 0;
$limit['limit'] = PageComment::$comments_per_page; $limit['limit'] = Comment::$comments_per_page;
$spamfilter = isset($_GET['showspam']) ? '' : "AND \"IsSpam\" = 0"; $spamfilter = isset($_GET['showspam']) ? '' : "AND \"IsSpam\" = 0";
$unmoderatedfilter = Permission::check('CMS_ACCESS_CommentAdmin') ? '' : "AND \"NeedsModeration\" = 0"; $unmoderatedfilter = Permission::check('CMS_ACCESS_CommentAdmin') ? '' : "AND \"NeedsModeration\" = 0";
$order = self::$order_comments_by; $order = self::$order_comments_by;
$comments = DataObject::get("PageComment", "\"ParentID\" = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", $order, "", $limit); $comments = DataObject::get("Comment", "\"ParentID\" = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", $order, "", $limit);
if(is_null($comments)) { if(is_null($comments)) {
return; return;
@ -246,16 +247,16 @@ class PageCommentInterface extends RequestHandler {
} }
function CommentRssLink() { function CommentRssLink() {
return Director::absoluteBaseURL() . "PageComment/rss?pageid=" . $this->page->ID; return Director::absoluteBaseURL() . "Comment/rss?pageid=" . $this->page->ID;
} }
/** /**
* A link to PageComment_Controller.deleteallcomments() which deletes all * A link to Comment_Controller.deleteallcomments() which deletes all
* comments on a page referenced by the url param pageid * comments on a page referenced by the url param pageid
*/ */
function DeleteAllLink() { function DeleteAllLink() {
if(Permission::check('CMS_ACCESS_CommentAdmin')) { if(Permission::check('CMS_ACCESS_CommentAdmin')) {
return Director::absoluteBaseURL() . "PageComment/deleteallcomments?pageid=" . $this->page->ID; return Director::absoluteBaseURL() . "Comment/deleteallcomments?pageid=" . $this->page->ID;
} }
} }
@ -265,12 +266,12 @@ class PageCommentInterface extends RequestHandler {
* @package cms * @package cms
* @subpackage comments * @subpackage comments
*/ */
class PageCommentInterface_Form extends Form { class CommentInterface_Form extends Form {
function postcomment($data) { function postcomment($data) {
// Spam filtering // Spam filtering
Cookie::set("PageCommentInterface_Name", $data['Name']); Cookie::set("CommentInterface_Name", $data['Name']);
Cookie::set("PageCommentInterface_CommenterURL", $data['CommenterURL']); Cookie::set("CommentInterface_CommenterURL", $data['CommenterURL']);
Cookie::set("PageCommentInterface_Comment", $data['Comment']); Cookie::set("CommentInterface_Comment", $data['Comment']);
if(SSAkismet::isEnabled()) { if(SSAkismet::isEnabled()) {
try { try {
@ -281,14 +282,14 @@ class PageCommentInterface_Form extends Form {
if($akismet->isCommentSpam()) { if($akismet->isCommentSpam()) {
if(SSAkismet::getSaveSpam()) { if(SSAkismet::getSaveSpam()) {
$comment = Object::create('PageComment'); $comment = Object::create('Comment');
$this->saveInto($comment); $this->saveInto($comment);
$comment->setField("IsSpam", true); $comment->setField("IsSpam", true);
$comment->write(); $comment->write();
} }
echo "<b>"._t('PageCommentInterface_Form.SPAMDETECTED', 'Spam detected!!') . "</b><br /><br />"; echo "<b>"._t('CommentInterface_Form.SPAMDETECTED', 'Spam detected!!') . "</b><br /><br />";
printf("If you believe this was in error, please email %s.", ereg_replace("@", " _(at)_", Email::getAdminEmail())); printf("If you believe this was in error, please email %s.", ereg_replace("@", " _(at)_", Email::getAdminEmail()));
echo "<br /><br />"._t('PageCommentInterface_Form.MSGYOUPOSTED', 'The message you posted was:'). "<br /><br />"; echo "<br /><br />"._t('CommentInterface_Form.MSGYOUPOSTED', 'The message you posted was:'). "<br /><br />";
echo $data['Comment']; echo $data['Comment'];
return; return;
@ -310,14 +311,14 @@ class PageCommentInterface_Form extends Form {
// If commenting can only be done by logged in users, make sure the user is logged in // If commenting can only be done by logged in users, make sure the user is logged in
$member = Member::currentUser(); $member = Member::currentUser();
if(PageCommentInterface::CanPostComment() && $member) { if(CommentInterface::CanPostComment() && $member) {
$this->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID)); $this->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID));
} elseif(!PageCommentInterface::CanPostComment()) { } elseif(!CommentInterface::CanPostComment()) {
echo "You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level."; echo "You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level.";
return; return;
} }
$comment = Object::create('PageComment'); $comment = Object::create('Comment');
$this->saveInto($comment); $this->saveInto($comment);
// Store the Session ID if needed for Spamprotection // Store the Session ID if needed for Spamprotection
@ -326,18 +327,18 @@ class PageCommentInterface_Form extends Form {
Session::clear('mollom_user_session_id'); Session::clear('mollom_user_session_id');
} }
$comment->IsSpam = false; $comment->IsSpam = false;
$comment->NeedsModeration = PageComment::moderationEnabled(); $comment->NeedsModeration = Comment::moderationEnabled();
$comment->write(); $comment->write();
Cookie::set("PageCommentInterface_Comment", ''); Cookie::set("CommentInterface_Comment", '');
$moderationMsg = _t('PageCommentInterface_Form.AWAITINGMODERATION', "Your comment has been submitted and is now awaiting moderation."); $moderationMsg = _t('CommentInterface_Form.AWAITINGMODERATION', "Your comment has been submitted and is now awaiting moderation.");
if(Director::is_ajax()) { if(Director::is_ajax()) {
if($comment->NeedsModeration){ if($comment->NeedsModeration){
echo $moderationMsg; echo $moderationMsg;
} else{ } else{
echo $comment->renderWith('PageCommentInterface_singlecomment'); echo $comment->renderWith('CommentInterface_singlecomment');
} }
} else { } else {
if($comment->NeedsModeration){ if($comment->NeedsModeration){
@ -349,7 +350,7 @@ class PageCommentInterface_Form extends Form {
if($page) { if($page) {
// if it needs moderation then it won't appear in the list. Therefore // if it needs moderation then it won't appear in the list. Therefore
// we need to link to the comment holder rather than the individual comment // we need to link to the comment holder rather than the individual comment
$url = ($comment->NeedsModeration) ? $page->Link() . '#PageComments_holder' : $page->Link() . '#PageComment_' . $comment->ID; $url = ($comment->NeedsModeration) ? $page->Link() . '#Comments_holder' : $page->Link() . '#Comment_' . $comment->ID;
return Director::redirect($url); return Director::redirect($url);
} }
@ -364,14 +365,14 @@ class PageCommentInterface_Form extends Form {
* @package cms * @package cms
* @subpackage comments * @subpackage comments
*/ */
class PageCommentInterface_Controller extends ContentController { class CommentInterface_Controller extends ContentController {
function __construct() { function __construct() {
parent::__construct(null); parent::__construct(null);
} }
function newspamquestion() { function newspamquestion() {
if(Director::is_ajax()) { if(Director::is_ajax()) {
echo Convert::raw2xml(sprintf(_t('PageCommentInterface_Controller.SPAMQUESTION', "Spam protection question: %s"),MathSpamProtection::getMathQuestion())); echo Convert::raw2xml(sprintf(_t('CommentInterface_Controller.SPAMQUESTION', "Spam protection question: %s"),MathSpamProtection::getMathQuestion()));
} }
} }
} }

View File

@ -10,9 +10,10 @@ class Comment extends DataObject {
static $db = array( static $db = array(
"Name" => "Varchar(200)", "Name" => "Varchar(200)",
"Comment" => "Text", "Comment" => "Text",
"Email" => "Varchar(200)" "Email" => "Varchar(200)",
"URL" => "Varchar(255)", "URL" => "Varchar(255)",
"SessionID" => "Varchar(255)", "SessionID" => "Varchar(255)",
"ParentClass" => "Varchar(200)"
); );
static $has_one = array( static $has_one = array(

View File

@ -1,16 +0,0 @@
<?php
/**
* @package comments
*/
class CommentExtension extends DataObjectDecorator {
function Comments() {
return DataObject::get('Comment', "\"RecordClassID\" = '". $this->owner->ID ."' AND \"RecordClass\" = '". $this->ownerBaseClass ."'");
}
function CommentsForm() {
die();
}
}

View File

@ -0,0 +1,84 @@
<?php
/**
* Extension to {@link DataObject} to enable tracking comments.
*
* @package comments
*/
class CommentsExtension extends DataObjectDecorator {
/**
* Adds a relationship between this {@link DataObject} and its
* {@link Comment} objects
*
* @return array
*/
function extraStatics() {
return array(
'has_many' => array(
'Comments' => 'Comment'
)
);
}
/**
* @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.
*
* @todo pagination
*
* @return DataObjectSet
*/
function Comments() {
return DataObject::get('Comment', "\"ParentID\" = '". $this->owner->ID ."' AND \"ParentClass\" = '". $this->ownerBaseClass ."'");
}
/**
* 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.
*
* @see docs/en/Extending
*/
function CommentsForm() {
$interface = new SSViewer('CommentsInterface');
return $interface->process(new ArrayData(array(
'Comments' => $this->Comments()
)));
}
/**
* @deprecated 1.0 Please use {@link CommentsExtension->CommentsForm()}
*/
function PageComments() {
user_error('$PageComments is deprecated. Please use $CommentsForm');
return $this->CommentsForm();
}
}

View File

0
docs/en/Installing.md Normal file
View File