From 0adf17d3631b2237da850db50e7e52b6e68b4635 Mon Sep 17 00:00:00 2001 From: Shea Date: Fri, 1 Jun 2012 16:34:31 +1000 Subject: [PATCH] fixed some api compatibility issues, added a basic comment gridfield to comment admin --- code/CommentAdmin.php | 136 ++++++++++------------ code/controllers/CommentingController.php | 4 +- code/dataobjects/Comment.php | 29 +++++ code/extensions/CommentsExtension.php | 11 +- 4 files changed, 98 insertions(+), 82 deletions(-) diff --git a/code/CommentAdmin.php b/code/CommentAdmin.php index 568cb94..734668e 100644 --- a/code/CommentAdmin.php +++ b/code/CommentAdmin.php @@ -13,6 +13,8 @@ class CommentAdmin extends LeftAndMain { static $menu_title = 'Comments'; + static $template_path = null; // defaults to (project)/templates/email + static $allowed_actions = array( 'approvedmarked', 'deleteall', @@ -32,8 +34,65 @@ class CommentAdmin extends LeftAndMain { public function init() { parent::init(); - Requirements::javascript(CMS_DIR . '/javascript/CommentAdmin_right.js'); - Requirements::css(CMS_DIR . '/css/CommentAdmin.css'); + //Requirements::javascript(CMS_DIR . '/javascript/CommentAdmin_right.js'); + //Requirements::css(CMS_DIR . '/css/CommentAdmin.css'); + } + + public function getEditForm($id = null, $fields = null) { + // TODO Duplicate record fetching (see parent implementation) + if(!$id) $id = $this->currentPageID(); + $form = parent::getEditForm($id); + + // TODO Duplicate record fetching (see parent implementation) + $record = $this->getRecord($id); + if($record && !$record->canView()) return Security::permissionFailure($this); + + $commentList = GridField::create( + 'Comments', + false, + Comment::get(), + $commentListConfig = GridFieldConfig_RecordViewer::create() + //->addComponent(new GridFieldExportButton()) + )->addExtraClass("comments_grid"); + //$commentListConfig->getComponentByType('GridFieldDetailForm')->setValidator(new Comment_Validator()); + + $fields = new FieldList( + $root = new TabSet( + 'Root', + $commentsTab = new Tab('Comments', _t('CommentAdmin.Comments', 'Comments'), + $commentList// , + // new HeaderField(_t('CommentAdmin.IMPORTCOMMENTS', 'Import comments'), 3), + // new LiteralField( + // 'CommentImportFormIframe', + // sprintf( + // '', + // $this->Link('commentimport') + // ) + // ) + ) + ), + // necessary for tree node selection in LeftAndMain.EditForm.js + new HiddenField('ID', false, 0) + ); + + $root->setTemplate('CMSTabSet'); + + $actions = new FieldList(); + + $form = new Form( + $this, + 'EditForm', + $fields, + $actions + ); + $form->addExtraClass('cms-edit-form'); + $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); + if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); + $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); + + $this->extend('updateEditForm', $form); + + return $form; } public function showtable($params) { @@ -58,79 +117,6 @@ class CommentAdmin extends LeftAndMain { return $section; } - public function EditForm() { - $section = $this->Section(); - - if($section == 'approved') { - $filter = "\"IsSpam\" = 0 AND \"NeedsModeration\" = 0"; - $title = "

". _t('CommentAdmin.APPROVEDCOMMENTS', 'Approved Comments')."

"; - } else if($section == 'unmoderated') { - $filter = '"NeedsModeration" = 1'; - $title = "

"._t('CommentAdmin.COMMENTSAWAITINGMODERATION', 'Comments Awaiting Moderation')."

"; - } else { - $filter = '"IsSpam" = 1'; - $title = "

"._t('CommentAdmin.SPAM', 'Spam')."

"; - } - - $filter .= ' AND "ParentID">0'; - - $tableFields = array( - "Name" => _t('CommentAdmin.AUTHOR', 'Author'), - "Comment" => _t('CommentAdmin.COMMENT', 'Comment'), - "Parent.Title" => _t('CommentAdmin.PAGE', 'Page'), - "URL" => _t('CommentAdmin.COMMENTERURL', 'URL'), - "Created" => _t('CommentAdmin.DATEPOSTED', 'Date Posted') - ); - - $popupFields = new FieldSet( - new TextField('Name', _t('CommentAdmin.NAME', 'Name')), - new TextField('URL', _t('CommentAdmin.URL', 'URL')), - new TextareaField('Comment', _t('CommentAdmin.COMMENT', 'Comment')) - ); - - $idField = new HiddenField('ID', '', $section); - $table = new CommentTableField($this, "Comments", "Comment", $section, $tableFields, $popupFields, array($filter), 'Created DESC'); - - $table->setParentClass(false); - $table->setFieldCasting(array( - 'Created' => 'SSDatetime->Full', - 'Comment' => array('HTMLText->LimitCharacters', 150) - )); - - $table->setPageSize(self::get_comments_per_page()); - $table->addSelectOptions(array('all'=>'All', 'none'=>'None')); - $table->Markable = true; - - $fields = new FieldSet( - new LiteralField("Title", $title), - $idField, - $table - ); - - $actions = new FieldSet(); - - if($section == 'unmoderated') { - $actions->push(new FormAction('acceptmarked', _t('CommentAdmin.ACCEPT', 'Accept'))); - } - - if($section == 'approved' || $section == 'unmoderated') { - $actions->push(new FormAction('spammarked', _t('CommentAdmin.SPAMMARKED', 'Mark as spam'))); - } - - if($section == 'spam') { - $actions->push(new FormAction('hammarked', _t('CommentAdmin.MARKASNOTSPAM', 'Mark as not spam'))); - } - - $actions->push(new FormAction('deletemarked', _t('CommentAdmin.DELETE', 'Delete'))); - - if($section == 'spam') { - $actions->push(new FormAction('deleteall', _t('CommentAdmin.DELETEALL', 'Delete All'))); - } - - $form = new Form($this, "EditForm", $fields, $actions); - - return $form; - } function deletemarked() { $numComments = 0; diff --git a/code/controllers/CommentingController.php b/code/controllers/CommentingController.php index 89e453c..db17c86 100644 --- a/code/controllers/CommentingController.php +++ b/code/controllers/CommentingController.php @@ -76,7 +76,7 @@ class CommentingController extends Controller { function CommentsForm() { $member = Member::currentUser(); - $fields = new FieldSet( + $fields = new FieldList( new TextField("Name", _t('CommentInterface.YOURNAME', 'Your name')), new EmailField("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)")), new TextField("URL", _t('CommentingController.WEBSITEURL', "Your website URL")), @@ -87,7 +87,7 @@ class CommentingController extends Controller { ); // save actions - $actions = new FieldSet( + $actions = new FieldList( new FormAction("doPostComment", _t('CommentInterface.POST', 'Post')) ); diff --git a/code/dataobjects/Comment.php b/code/dataobjects/Comment.php index 0641d38..e8717b1 100755 --- a/code/dataobjects/Comment.php +++ b/code/dataobjects/Comment.php @@ -34,6 +34,24 @@ class Comment extends DataObject { static $casting = array( "RSSTitle" => "Varchar", ); + + static $searchable_fields = array( + 'Name', + 'Email', + 'Comment', + 'Created', + 'BaseClass', + ); + + static $summary_fields = array( + 'Name' => 'Submitted By', + 'Email' => 'Email', + 'Comment' => 'Comment', + 'Created' => 'Date Posted', + 'ParentTitle' => 'Parent', + ); + + /** * Migrates the old {@link PageComment} objects to {@link Comment} @@ -107,6 +125,17 @@ class Comment extends DataObject { return DataObject::get_by_id($this->BaseClass, $this->ParentID); } + + + /** + * Returns a string to help identify the parent of the comment + * + * @return string + */ + function getParentTitle(){ + $parent = $this->getParent(); + return ($parent->Title) ? $parent->Title : $parent->ClassName . " #" . $parent->ID; + } /** * This method is called just before this object is diff --git a/code/extensions/CommentsExtension.php b/code/extensions/CommentsExtension.php index bfcbfee..9fc4fd7 100644 --- a/code/extensions/CommentsExtension.php +++ b/code/extensions/CommentsExtension.php @@ -6,7 +6,7 @@ * @package comments */ -class CommentsExtension extends DataObjectDecorator { +class CommentsExtension extends DataExtension { /** * Adds a relationship between this {@link DataObject} and its @@ -15,7 +15,7 @@ class CommentsExtension extends DataObjectDecorator { * * @return array */ - public function extraStatics() { + function extraStatics($class = null, $extension = null) { $fields = array(); $relationships = array( @@ -29,7 +29,7 @@ class CommentsExtension extends DataObjectDecorator { $args = func_get_args(); if($args && ($owner = array_shift($args))) { - if(ClassInfo::is_subclass_of($owner, 'SiteTree') || $owner == "SiteTree") { + if(is_subclass_of($owner, 'SiteTree') || $owner == "SiteTree") { $fields = array( 'db' => array( 'ProvideComments' => 'Boolean' @@ -40,6 +40,7 @@ class CommentsExtension extends DataObjectDecorator { return array_merge($fields, $relationships); } + /** * If this extension is applied to a {@link SiteTree} record then @@ -50,7 +51,7 @@ class CommentsExtension extends DataObjectDecorator { * * @param FieldSet */ - public function updateCMSFields(&$fields) { + public function updateCMSFields(FieldList $fields) { if($this->attachedToSiteTree()) { $fields->addFieldToTab('Root.Behaviour', new CheckboxField('ProvideComments', _t('Comment.ALLOWCOMMENTS', 'Allow Comments')) @@ -127,7 +128,7 @@ class CommentsExtension extends DataObjectDecorator { public function attachedToSiteTree() { $class = $this->ownerBaseClass; - return (ClassInfo::is_subclass_of($class, 'SiteTree')) || ($class == 'SiteTree'); + return (is_subclass_of($class, 'SiteTree')) || ($class == 'SiteTree'); } /**