/**
  * Overwrite the default comments form so we can tweak a bit
  *
  * @see docs/en/Extending
  */
 public function CommentsForm()
 {
     if (Commenting::has_commenting($this->ownerBaseClass) && Commenting::get_config_value($this->ownerBaseClass, 'include_js')) {
         Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
         Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/lib/jquery.form.js');
         Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/jquery.validate.pack.js');
         Requirements::javascript('comments/javascript/CommentsInterface.js');
     }
     $interface = new SSViewer('DiscussionsCommentsInterface');
     $enabled = !$this->attachedToSiteTree() || $this->owner->ProvideComments ? true : false;
     // do not include the comments on pages which don't have id's such as security pages
     if ($this->owner->ID < 0) {
         return false;
     }
     $controller = new CommentingController();
     $controller->setOwnerRecord($this);
     $controller->setBaseClass($this->ClassName);
     $controller->setOwnerController(Controller::curr());
     $moderatedSubmitted = Session::get('CommentsModerated');
     Session::clear('CommentsModerated');
     // Tweak the comments form a bit, so it is more user friendly
     if ($enabled) {
         $form = $controller->CommentsForm();
         $form->Fields()->removeByName("Comment");
         $comment_field = TextareaField::create("Comment", "")->setCustomValidationMessage(_t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'))->setAttribute('data-message-required', _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'));
         if ($form->Fields()->dataFieldByName("NameView")) {
             $form->Fields()->insertBefore($comment_field, "NameView");
         } else {
             $form->Fields()->insertBefore($comment_field, "Name");
         }
     } else {
         $form = false;
     }
     // a little bit all over the show but to ensure a slightly easier upgrade for users
     // return back the same variables as previously done in comments
     return $interface->process(new ArrayData(array('CommentHolderID' => Commenting::get_config_value($this->ClassName, 'comments_holder_id'), 'PostingRequiresPermission' => Commenting::get_config_value($this->ClassName, 'required_permission'), 'CanPost' => Commenting::can_member_post($this->ClassName), 'RssLink' => "CommentingController/rss", 'RssLinkPage' => "CommentingController/rss/" . $this->ClassName . '/' . $this->ID, 'CommentsEnabled' => $enabled, 'Parent' => $this, 'AddCommentForm' => $form, 'ModeratedSubmitted' => $moderatedSubmitted, 'Comments' => $this->getComments())));
 }
예제 #2
0
 /**
  * Returns the permalink for this {@link Comment}. Inserted into
  * the ID tag of the comment
  *
  * @return string
  */
 public function Permalink()
 {
     $prefix = Commenting::get_config_value($this->BaseClass, 'comment_permalink_prefix');
     return $prefix . $this->ID;
 }
예제 #3
0
 public function testDefaultTemplateRendersHtmlWithAllowHtml()
 {
     if (!class_exists('HTMLPurifier')) {
         $this->markTestSkipped('HTMLPurifier class not found');
     }
     $origAllowed = Commenting::get_config_value('CommentableItem', 'html_allowed');
     $item = new CommentableItem();
     $item->write();
     // Without HTML allowed
     $comment = new Comment();
     $comment->Comment = '<p>my comment</p>';
     $comment->ParentID = $item->ID;
     $comment->BaseClass = 'CommentableItem';
     $comment->write();
     $html = $item->customise(array('CommentsEnabled' => true))->renderWith('CommentsInterface');
     $this->assertContains('&lt;p&gt;my comment&lt;/p&gt;', $html);
     Commenting::set_config_value('CommentableItem', 'html_allowed', true);
     $html = $item->customise(array('CommentsEnabled' => true))->renderWith('CommentsInterface');
     $this->assertContains('<p>my comment</p>', $html);
     Commenting::set_config_value('CommentableItem', 'html_allowed', $origAllowed);
 }
 /**
  * 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.
  *
  * @todo Cleanup the passing of all this configuration based functionality
  *
  * @see docs/en/Extending
  */
 public function CommentsForm()
 {
     $interface = new SSViewer('CommentsInterface');
     // detect whether we comments are enabled. By default if $CommentsForm is included
     // on a {@link DataObject} then it is enabled, however {@link SiteTree} objects can
     // trigger comments on / off via ProvideComments
     $enabled = !$this->attachedToSiteTree() || $this->owner->ProvideComments ? true : false;
     // do not include the comments on pages which don't have id's such as security pages
     if ($this->owner->ID < 0) {
         return false;
     }
     $controller = new CommentingController();
     $controller->setOwnerRecord($this->owner);
     $controller->setBaseClass($this->ownerBaseClass);
     $controller->setOwnerController(Controller::curr());
     $form = $enabled ? $controller->CommentsForm() : false;
     // a little bit all over the show but to ensure a slightly easier upgrade for users
     // return back the same variables as previously done in comments
     return $interface->process(new ArrayData(array('CommentHolderID' => Commenting::get_config_value($this->ownerBaseClass, 'comments_holder_id'), 'PostingRequiresPermission' => Commenting::get_config_value($this->ownerBaseClass, 'required_permission'), 'CanPost' => Commenting::can_member_post($this->ownerBaseClass), 'RssLink' => "CommentingController/rss", 'RssLinkPage' => "CommentingController/rss/" . $this->ownerBaseClass . '/' . $this->owner->ID, 'CommentsEnabled' => $enabled, 'AddCommentForm' => $form, 'Comments' => $this->Comments())));
 }
 /**
  * Process which creates a {@link Comment} once a user submits a comment from this form.
  *
  * @param array $data 
  * @param Form $form
  */
 public function doPostComment($data, $form)
 {
     $class = isset($data['BaseClass']) ? $data['BaseClass'] : $this->getBaseClass();
     // if no class then we cannot work out what controller or model they
     // are on so throw an error
     if (!$class) {
         user_error("No OwnerClass set on CommentingController.", E_USER_ERROR);
     }
     // cache users data
     Cookie::set("CommentsForm_UserData", Convert::raw2json($data));
     Cookie::set("CommentsForm_Comment", $data['Comment']);
     // extend hook to allow extensions. Also see onAfterPostComment
     $this->extend('onBeforePostComment', $form);
     // If commenting can only be done by logged in users, make sure the user is logged in
     $member = Member::currentUser();
     if (Commenting::can_member_post($class) && $member) {
         $form->Fields()->push(new HiddenField("AuthorID", "Author ID", $member->ID));
     }
     if (!Commenting::can_member_post($class)) {
         echo _t('CommentingController.PERMISSIONFAILURE', "You're not able to post comments to this page. Please ensure you are logged in and have an appropriate permission level.");
         return;
     }
     // is moderation turned on
     $moderated = Commenting::get_config_value($class, 'require_moderation');
     $comment = new Comment();
     $form->saveInto($comment);
     $comment->Moderated = $moderated ? false : true;
     $comment->write();
     // clear the users comment since it passed validation
     Cookie::set('CommentsForm_Comment', false);
     if (Director::is_ajax()) {
         if (!$comment->Moderated) {
             echo $comment->renderWith('CommentInterface_pendingcomment');
         } else {
             echo $comment->renderWith('CommentInterface_singlecomment');
         }
         return true;
     }
     if ($comment->NeedsModeration) {
         $this->sessionMessage($moderationMsg, 'good');
     }
     // build up the return link. Ideally redirect to
     $holder = Commenting::get_config_value($comment->BaseClass, 'comments_holder_id');
     $hash = $comment->NeedsModeration ? $holder : $comment->Permalink();
     $url = isset($data['ReturnURL']) ? $data['ReturnURL'] : false;
     return $url ? $this->redirect($url . '#' . $hash) : $this->redirectBack();
 }