Version: current

      1 <?php
      2 /**
      3  * Comment administration system within the CMS
      4  * @package cms
      5  * @subpackage comments
      6  */
      7 class CommentAdmin extends LeftAndMain {
      8
      9     static $url_segment = 'comments';
     10
     11     static $url_rule = '/$Action';
     12
     13     static $menu_title = 'Comments';
     14
     15     static $allowed_actions = array(
     16         'approvedmarked',
     17         'deleteall',
     18         'deletemarked',
     19         'hammarked',
     20         'showtable',
     21         'spammarked',
     22         'EditForm',
     23         'unmoderated'
     24     );
     25
     26         function canView($member = null){
     27             if(!$member && $member !== FALSE) $member = Member::currentUser();
     28
     29         if(!parent::canView($member)) return false;
     30
     31         return true;
     32         }
     33
     34     /**
     35      * @var int The number of comments per page for the {@link CommentTable} in this admin.
     36      */
     37     static $comments_per_page = '20';
     38
     39     public function init() {
     40         parent::init();
     41
     42         Requirements::javascript('comments/javascript/CommentAdmin_right.js');
     43         Requirements::css('comments/css/CommentAdmin.css');
     44     }
     45
     46     public function showtable($params) {
     47         return $this->getLastFormIn($this->renderWith('CommentAdmin_right'));
     48     }
     49
     50     public function Section() {
     51         $url = rtrim($_SERVER['REQUEST_URI'], '/');
     52         if(strrpos($url, '&')) {
     53             $url = substr($url, 0, strrpos($url, '&'));
     54         }
     55         $section = substr($url, strrpos($url, '/') + 1);
     56
     57         if($section != 'approved' && $section != 'unmoderated' && $section != 'spam') {
     58             $section = Session::get('CommentsSection');
     59         }
     60
     61         if($section != 'approved' && $section != 'unmoderated' && $section != 'spam') {
     62             $section = 'approved';
     63         }
     64
     65         return $section;
     66     }
     67
     68     public function EditForm() {
     69         $section = $this->Section();
     70
     71         if($section == 'approved') {
     72             $filter = "\"IsSpam\" = 0 AND \"NeedsModeration\" = 0";
     73             $title = "<h2>". _t('CommentAdmin.APPROVEDCOMMENTS', 'Approved Comments')."</h2>";
     74         } else if($section == 'unmoderated') {
     75             $filter = '"NeedsModeration" = 1';
     76             $title = "<h2>"._t('CommentAdmin.COMMENTSAWAITINGMODERATION', 'Comments Awaiting Moderation')."</h2>";
     77         } else {
     78             $filter = '"IsSpam" = 1';
     79             $title = "<h2>"._t('CommentAdmin.SPAM', 'Spam')."</h2>";
     80         }
     81
     82         $filter .= ' AND "ParentID">0';
     83
     84         $tableFields = array(
     85             "Name" => _t('CommentAdmin.AUTHOR', 'Author'),
     86             "Comment" => _t('CommentAdmin.COMMENT', 'Comment'),
     87             "Parent.Title" => _t('CommentAdmin.PAGE', 'Page'),
     88             "CommenterURL" => _t('CommentAdmin.COMMENTERURL', 'URL'),
     89             "Created" => _t('CommentAdmin.DATEPOSTED', 'Date Posted')
     90         );
     91
     92         $idField = new HiddenField('ID', '', $section);
     93         $table = new CommentTableField($this, "Comments", "PageComment", $section, $tableFields, null, array($filter), 'Created DESC');
     94
     95         $table->setParentClass(false);
     96         $table->setFieldCasting(array(
     97             'Created' => 'SS_Datetime->Full',
     98             'Comment' => array('HTMLText->LimitCharacters', 150)
     99         ));
    100
    101         $table->setPageSize(self::get_comments_per_page());
    102 //      $table->addSelectOptions(array('all'=>'All', 'none'=>'None'));
    103         $table->Markable = true;
    104
    105         $fields = new FieldSet(
    106             new LiteralField("Title", $title),
    107             $idField,
    108             $table
    109         );
    110
    111         $actions = new FieldSet();
    112
    113         if($section == 'unmoderated') {
    114             $actions->push(new FormAction('acceptmarked', _t('CommentAdmin.ACCEPT', 'Accept')));
    115         }
    116
    117         if($section == 'approved' || $section == 'unmoderated') {
    118             $actions->push(new FormAction('spammarked', _t('CommentAdmin.SPAMMARKED', 'Mark as spam')));
    119         }
    120
    121         if($section == 'spam') {
    122             $actions->push(new FormAction('hammarked', _t('CommentAdmin.MARKASNOTSPAM', 'Mark as not spam')));
    123         }
    124
    125         $actions->push(new FormAction('deletemarked', _t('CommentAdmin.DELETE', 'Delete')));
    126
    127         if($section == 'spam') {
    128             $actions->push(new FormAction('deleteall', _t('CommentAdmin.DELETEALL', 'Delete All')));
    129         }
    130
    131         $form = new Form($this, "EditForm", $fields, $actions);
    132
    133         return $form;
    134     }
    135
    136     function deletemarked() {
    137             $numComments = 0;
    138             $folderID = 0;
    139             $deleteList = '';
    140
    141             if($_REQUEST['Comments']) {
    142                 foreach($_REQUEST['Comments'] as $commentid) {
    143                     $comment = DataObject::get_by_id('PageComment', $commentid);
    144                     if($comment) {
    145                         $comment->delete();
    146                         $numComments++;
    147                     }
    148                 }
    149             } else {
    150                 user_error("No comments in $commentList could be found!", E_USER_ERROR);
    151             }
    152
    153             echo <<<JS
    154                 $deleteList
    155                 $('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value);
    156                 statusMessage("Deleted $numComments comments.");
    157 JS;
    158     }
    159
    160     function deleteall() {
    161         $numComments = 0;
    162         $spam = DataObject::get('PageComment', '"PageComment"."IsSpam" = 1');
    163
    164         if($spam) {
    165             $numComments = $spam->Count();
    166
    167             foreach($spam as $comment) {
    168                 $comment->delete();
    169             }
    170         }
    171
    172         $msg = sprintf(_t('CommentAdmin.DELETED', 'Deleted %s comments.'), $numComments);
    173         echo <<<JS
    174                 $('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value);
    175                 statusMessage("$msg");
    176 JS;
    177
    178     }
    179
    180     function spammarked() {
    181             $numComments = 0;
    182             $folderID = 0;
    183             $deleteList = '';
    184
    185             if($_REQUEST['Comments']) {
    186                 foreach($_REQUEST['Comments'] as $commentid) {
    187                     $comment = DataObject::get_by_id('PageComment', $commentid);
    188                     if($comment) {
    189                         $comment->IsSpam = true;
    190                         $comment->NeedsModeration = false;
    191                         $comment->write();
    192
    193                         if(SSAkismet::isEnabled()) {
    194                             try {
    195                                 $akismet = new SSAkismet();
    196                                 $akismet->setCommentAuthor($comment->getField('Name'));
    197                                 $akismet->setCommentContent($comment->getField('Comment'));
    198
    199                                 $akismet->submitSpam();
    200                             } catch (Exception $e) {
    201                                 // Akismet didn't work, most likely the service is down.
    202                             }
    203                         }
    204                         $numComments++;
    205                     }
    206                 }
    207             } else {
    208                 user_error("No comments in $commentList could be found!", E_USER_ERROR);
    209             }
    210
    211             $msg = sprintf(_t('CommentAdmin.MARKEDSPAM', 'Marked %s comments as spam.'), $numComments);
    212             echo <<<JS
    213                 $deleteList
    214                 $('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value);
    215                 statusMessage("$msg");
    216 JS;
    217     }
    218
    219     function hammarked() {
    220             $numComments = 0;
    221             $folderID = 0;
    222             $deleteList = '';
    223
    224             if($_REQUEST['Comments']) {
    225                 foreach($_REQUEST['Comments'] as $commentid) {
    226                     $comment = DataObject::get_by_id('PageComment', $commentid);
    227                     if($comment) {
    228                         $comment->IsSpam = false;
    229                         $comment->NeedsModeration = false;
    230                         $comment->write();
    231
    232                         if(SSAkismet::isEnabled()) {
    233                             try {
    234                                 $akismet = new SSAkismet();
    235                                 $akismet->setCommentAuthor($comment->getField('Name'));
    236                                 $akismet->setCommentContent($comment->getField('Comment'));
    237
    238                                 $akismet->submitSpam();
    239                             } catch (Exception $e) {
    240                                 // Akismet didn't work, most likely the service is down.
    241                             }
    242                         }
    243
    244                         $numComments++;
    245                     }
    246                 }
    247             } else {
    248                 user_error("No comments in $commentList could be found!", E_USER_ERROR);
    249             }
    250
    251             $msg = sprintf(_t('CommentAdmin.MARKEDNOTSPAM', 'Marked %s comments as not spam.'), $numComments);
    252             echo <<<JS
    253                 $deleteList
    254                 $('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value);
    255                 statusMessage("$msg");
    256 JS;
    257     }
    258
    259     function acceptmarked() {
    260             $numComments = 0;
    261             $folderID = 0;
    262             $deleteList = '';
    263
    264             if($_REQUEST['Comments']) {
    265                 foreach($_REQUEST['Comments'] as $commentid) {
    266                     $comment = DataObject::get_by_id('PageComment', $commentid);
    267                     if($comment) {
    268                         $comment->IsSpam = false;
    269                         $comment->NeedsModeration = false;
    270                         $comment->write();
    271                         $numComments++;
    272                     }
    273                 }
    274             } else {
    275                 user_error("No comments in $commentList could be found!", E_USER_ERROR);
    276             }
    277
    278             $msg = sprintf(_t('CommentAdmin.APPROVED', 'Accepted %s comments.'), $numComments);
    279             echo <<<JS
    280                 $deleteList
    281                 $('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value);
    282                 statusMessage("Accepted $numComments comments.");
    283 JS;
    284     }
    285
    286     /**
    287      * Return the number of moderated comments
    288      */
    289     function NumModerated() {
    290         return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=0 AND \"NeedsModeration\"=0")->value();
    291     }
    292
    293     /**
    294      * Return the number of unmoderated comments
    295      */
    296     function NumUnmoderated() {
    297         return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=0 AND \"NeedsModeration\"=1")->value();
    298     }
    299
    300     /**
    301      * Return the number of comments marked as spam
    302      */
    303     function NumSpam() {
    304         return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=1")->value();
    305     }
    306
    307     /**
    308      * @param $num int
    309      */
    310     function set_comments_per_page($num){
    311         self::$comments_per_page = $num;
    312     }
    313
    314     /**
    315      * @return int
    316      */
    317     function get_comments_per_page(){
    318         return self::$comments_per_page;
    319     }
    320 }
    321
    322 ?>
    323

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -