2007-07-19 12:40:05 +02:00
< ? php
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage comments
*/
2007-07-19 12:40:05 +02:00
/**
* Represents an interface for viewing and adding page comments
* Create one , passing the page discussed to the constructor . It can then be
* inserted into a template .
2008-02-25 03:10:37 +01:00
* @ package cms
* @ subpackage comments
2007-07-19 12:40:05 +02:00
*/
class PageCommentInterface extends ViewableData {
protected $controller , $methodName , $page ;
/**
* Create a new page comment interface
* @ param controller The controller that the U
* @ param methodName The method to return this PageCommentInterface object
* @ param page The page that we ' re commenting on
*/
function __construct ( $controller , $methodName , $page ) {
$this -> controller = $controller ;
$this -> methodName = $methodName ;
$this -> page = $page ;
}
function forTemplate () {
return $this -> renderWith ( 'PageCommentInterface' );
}
function PostCommentForm () {
Requirements :: javascript ( 'jsparty/behaviour.js' );
Requirements :: javascript ( 'jsparty/prototype.js' );
Requirements :: javascript ( 'jsparty/scriptaculous/effects.js' );
Requirements :: javascript ( 'cms/javascript/PageCommentInterface.js' );
2007-07-30 00:56:16 +02:00
$fields = new FieldSet (
2007-07-19 12:40:05 +02:00
new HiddenField ( " ParentID " , " ParentID " , $this -> page -> ID ),
2008-02-25 03:10:37 +01:00
new TextField ( " Name " , _t ( 'PageCommentInterface.YOURNAME' , 'Your name' )));
2007-08-14 06:39:29 +02:00
2007-07-30 00:56:16 +02:00
if ( MathSpamProtection :: isEnabled ()){
2008-02-25 03:10:37 +01:00
$fields -> push ( new TextField ( " Math " , sprintf ( _t ( 'PageCommentInterface.SPAMQUESTION' , " Spam protection question: %s " ), MathSpamProtection :: getMathQuestion ())));
2007-07-30 00:56:16 +02:00
}
2008-02-25 03:10:37 +01:00
$fields -> push ( new TextareaField ( " Comment " , _t ( 'PageCommentInterface.YOURCOMMENT' , " Comments " )));
2007-07-30 00:56:16 +02:00
$form = new PageCommentInterface_Form ( $this -> controller , $this -> methodName . " .PostCommentForm " , $fields , new FieldSet (
2008-02-25 03:10:37 +01:00
new FormAction ( " postcomment " , _t ( 'PageCommentInterface.POST' , 'Post' ))
2007-07-19 12:40:05 +02:00
));
$form -> loadDataFrom ( array (
" Name " => Cookie :: get ( " PageCommentInterface_Name " ),
));
return $form ;
}
function Comments () {
// Comment limits
if ( isset ( $_GET [ 'commentStart' ])) {
$limit = ( int ) $_GET [ 'commentStart' ] . " , " . PageComment :: $comments_per_page ;
} else {
$limit = " 0, " . PageComment :: $comments_per_page ;
}
2007-08-10 06:33:02 +02:00
$spamfilter = isset ( $_GET [ 'showspam' ]) ? '' : 'AND IsSpam=0' ;
$unmoderatedfilter = Permission :: check ( 'ADMIN' ) ? '' : 'AND NeedsModeration = 0' ;
$comments = DataObject :: get ( " PageComment " , " ParentID = ' " . Convert :: raw2sql ( $this -> page -> ID ) . " ' $spamfilter $unmoderatedfilter " , " Created DESC " , " " , $limit );
2007-07-19 12:40:05 +02:00
if ( is_null ( $comments )) {
return ;
}
// This allows us to use the normal 'start' GET variables as well (In the weird circumstance where you have paginated comments AND something else paginated)
$comments -> setPaginationGetVar ( 'commentStart' );
return $comments ;
}
2007-08-07 06:59:19 +02:00
function CommentRssLink () {
return Director :: absoluteBaseURL () . " PageComment/rss?pageid= " . $this -> page -> ID ;
}
2007-07-19 12:40:05 +02:00
}
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage comments
*/
2007-07-19 12:40:05 +02:00
class PageCommentInterface_Form extends Form {
function postcomment ( $data ) {
// Spam filtering
if ( SSAkismet :: isEnabled ()) {
try {
$akismet = new SSAkismet ();
$akismet -> setCommentAuthor ( $data [ 'Name' ]);
$akismet -> setCommentContent ( $data [ 'Comment' ]);
if ( $akismet -> isCommentSpam ()) {
if ( SSAkismet :: getSaveSpam ()) {
$comment = Object :: create ( 'PageComment' );
$this -> saveInto ( $comment );
$comment -> setField ( " IsSpam " , true );
$comment -> write ();
}
2008-02-25 03:10:37 +01:00
echo " <b> " . _t ( 'PageCommentInterface_Form.SPAMDETECTED' , 'Spam detected!!' ) . " </b><br /><br /> " ;
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 /> " ;
2007-07-19 12:40:05 +02:00
echo $data [ 'Comment' ];
return ;
}
} catch ( Exception $e ) {
// Akismet didn't work, continue without spam check
}
}
2007-07-30 00:56:16 +02:00
//check if spam question was right.
2007-08-14 06:39:29 +02:00
if ( MathSpamProtection :: isEnabled ()){
2007-07-30 00:56:16 +02:00
if ( ! MathSpamProtection :: correctAnswer ( $data [ 'Math' ])){
2007-08-14 06:39:29 +02:00
if ( ! Director :: is_ajax ()) {
2007-08-08 06:34:12 +02:00
Director :: redirectBack ();
}
2007-08-14 06:39:29 +02:00
return " spamprotectionfalied " ; //used by javascript for checking if the spam question was wrong
2007-07-30 00:56:16 +02:00
}
}
2007-07-19 12:40:05 +02:00
Cookie :: set ( " PageCommentInterface_Name " , $data [ 'Name' ]);
$comment = Object :: create ( 'PageComment' );
$this -> saveInto ( $comment );
$comment -> IsSpam = false ;
2007-08-10 03:29:09 +02:00
$comment -> NeedsModeration = PageComment :: moderationEnabled ();
$comment -> write ();
2007-08-08 01:18:30 +02:00
2007-07-19 12:40:05 +02:00
if ( Director :: is_ajax ()) {
2007-08-14 06:39:29 +02:00
if ( $comment -> NeedsModeration ){
2008-02-25 03:10:37 +01:00
echo _t ( 'PageCommentInterface_Form.AWAITINGMODERATION' , " Your comment has been submitted and is now awating moderation. " );
2007-08-14 06:39:29 +02:00
} else {
echo $comment -> renderWith ( 'PageCommentInterface_singlecomment' );
}
2007-07-19 12:40:05 +02:00
} else {
Director :: redirectBack ();
}
}
}
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage comments
*/
2007-08-14 06:39:29 +02:00
class PageCommentInterface_Controller extends ContentController {
function __construct () {
parent :: __construct ( null );
}
function newspamquestion () {
2007-08-17 05:09:46 +02:00
if ( Director :: is_ajax ()) {
2008-02-25 03:10:37 +01:00
echo Convert :: raw2xml ( sprintf ( _t ( 'PageCommentInterface_Controller.SPAMQUESTION' , " Spam protection question: %s " ), MathSpamProtection :: getMathQuestion ()));
2007-08-14 06:39:29 +02:00
}
}
}
2008-02-25 03:10:37 +01:00
?>