diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..c57d55f --- /dev/null +++ b/TODO.md @@ -0,0 +1,14 @@ +# TODO + + * Permissions and configuration needs to be set on a per type of owner class rather than global + so we can support a blog with public comments and an ecommerce store with comments that you + have to login to post for instance + + Commenting::enable_comments('SiteTree', array( + 'requires_permission' => FOO, + 'requires_login' => false + )); + + * Merge simon_w's jQuery work for page comments back in. + + * Tests \ No newline at end of file diff --git a/code/controllers/CommentInterface.php b/code/controllers/CommentInterface.php index 633aa00..657e7cc 100755 --- a/code/controllers/CommentInterface.php +++ b/code/controllers/CommentInterface.php @@ -18,52 +18,7 @@ class CommentInterface extends RequestHandler { protected $controller, $methodName, $page; - /** - * If this is true, you must be logged in to post a comment - * (and therefore, you don't need to specify a 'Your name' field unless - * your name is blank) - * - * @var bool - */ - private static $comments_require_login = false; - - /** - * If this is a valid permission code, you must be logged in - * and have the appropriate permission code on your account before you can - * post a comment. - * - * @var string - */ - private static $comments_require_permission = ""; - - /** - * If this is true it will include the javascript for AJAX - * commenting. If it is set to false then it will not load - * the files required and it will fall back - * - * @var bool - */ - private static $use_ajax_commenting = true; - - /** - * If this is true then we should show the existing comments on - * the page even when we have disabled the comment form. - * - * If this is false the form + existing comments will be hidden - * - * @var bool - */ - private static $show_comments_when_disabled = true; - - /** - * Define how you want to order page comments by. By default order by newest - * to oldest. - * - * @var String - used as $orderby in DB query - * @since 2.4 - */ - static $order_comments_by = "\"Created\" DESC"; - + /** * Create a new page comment interface * @param controller The controller that the interface is used on @@ -81,78 +36,8 @@ class CommentInterface extends RequestHandler { return Controller::join_links($this->controller->Link(), $this->methodName); } - /** - * See {@link CommentInterface::$comments_require_login} - * - * @param boolean state The new state of this static field - */ - static function set_comments_require_login($state) { - self::$comments_require_login = (boolean) $state; - } - - /** - * See {@link CommentInterface::$comments_require_permission} - * - * @param string permission The permission to check against. - */ - static function set_comments_require_permission($permission) { - self::$comments_require_permission = $permission; - } - - /** - * See {@link CommentInterface::$show_comments_when_disabled} - * - * @param bool - show / hide the existing comments when disabled - */ - static function set_show_comments_when_disabled($state) { - self::$show_comments_when_disabled = $state; - } - - /** - * See {@link CommentInterface::$order_comments_by} - * - * @param String - */ - static function set_order_comments_by($order) { - self::$order_comments_by = $order; - } - - /** - * See {@link CommentInterface::$use_ajax_commenting} - * - * @param bool - */ - static function set_use_ajax_commenting($state) { - self::$use_ajax_commenting = $state; - } - - /** - * @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 - * security by using - * - * @link CommentInterface::set_comments_require_login() and - * @link {CommentInterface::set_comments_require_permission()}. - */ - public static function canPost() { - $member = Member::currentUser(); - - if(self::$comments_require_permission && $member && Permission::check(self::$comments_require_permission)) { - // Comments require a certain permission, and the user has the correct permission - return true; - - } elseif(self::$comments_require_login && $member && !self::$comments_require_permission) { - // Comments only require that a member is logged in - return true; - - } elseif(!self::$comments_require_permission && !self::$comments_require_login) { - // Comments don't require anything - anyone can add a comment - return true; - } - - return false; - } - + + /** * if this page comment form requires users to have a * valid permission code in order to post (used to customize the error diff --git a/code/dataobjects/Comment.php b/code/dataobjects/Comment.php index 1f67b38..82174d6 100755 --- a/code/dataobjects/Comment.php +++ b/code/dataobjects/Comment.php @@ -39,6 +39,7 @@ class Comment extends DataObject { /** * Return a link to this comment + * * @return string link to this comment. */ function Link() { @@ -165,9 +166,24 @@ class Comment extends DataObject { * @return Boolean */ function canCreate($member = null) { - return true; + $member = Member::currentUser(); + + if(self::$comments_require_permission && $member && Permission::check(self::$comments_require_permission)) { + // Comments require a certain permission, and the user has the correct permission + return true; + + } elseif(self::$comments_require_login && $member && !self::$comments_require_permission) { + // Comments only require that a member is logged in + return true; + + } elseif(!self::$comments_require_permission && !self::$comments_require_login) { + // Comments don't require anything - anyone can add a comment + return true; + } + + return false; } - + /** * Checks for association with a page, * and {@link SiteTree->ProvidePermission} flag being set to TRUE. diff --git a/code/extensions/CommentsExtension.php b/code/extensions/CommentsExtension.php index d43d4db..ee939fd 100644 --- a/code/extensions/CommentsExtension.php +++ b/code/extensions/CommentsExtension.php @@ -87,7 +87,15 @@ class CommentsExtension extends DataObjectDecorator { 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; + + // if comments are turned off then return $interface->process(new ArrayData(array( + 'CommentsEnabled' => $enabled, 'AddCommentForm' => $this->AddCommentForm(), 'Comments' => $this->Comments() ))); @@ -100,17 +108,12 @@ class CommentsExtension extends DataObjectDecorator { * @return Form|bool */ public function AddCommentForm() { - - // 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 - if($this->attachedToSiteTree() && !$this->owner->ProvideComments) return false; - + $form = new CommentForm(Controller::curr(), 'CommentsForm'); // hook to allow further extensions to alter the comments form $this->extend('alterAddCommentForm', $form); - + return $form; } diff --git a/code/forms/CommentForm.php b/code/forms/CommentForm.php index 871a8ff..faa98f3 100644 --- a/code/forms/CommentForm.php +++ b/code/forms/CommentForm.php @@ -7,7 +7,7 @@ */ class CommentForm extends Form { - + /** * Returns a create comment form * @@ -28,7 +28,7 @@ class CommentForm extends Form { } $fields->push(new TextField("URL", _t('CommentForm.COMMENTERURL', "Your website URL"))); - $fields->push(new EmailField("Email", _t('CommentForm', "Your email address (will not be published)"))) + $fields->push(new EmailField("Email", _t('CommentForm', "Your email address (will not be published)"))); $fields->push(new TextareaField("Comment", _t('CommentInterface.YOURCOMMENT', "Comments"))); $actions = new FieldSet( @@ -126,12 +126,11 @@ class CommentForm extends Form { // we need to link to the comment holder rather than the individual comment $url = ($comment->NeedsModeration) ? $page->Link() . '#Comments_holder' : $page->Link() . '#Comment_' . $comment->ID; - return Director::redirect($url); + return $this->controller->redirect($url); } } - return Director::redirectBack(); + return $this->controller->redirectBack(); } } - } } diff --git a/docs/en/Configuration.md b/docs/en/Configuration.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/en/Upgrading.md b/docs/en/Upgrading.md new file mode 100644 index 0000000..e69de29 diff --git a/templates/CommentsInterface.ss b/templates/CommentsInterface.ss index 483acc2..758ecd6 100755 --- a/templates/CommentsInterface.ss +++ b/templates/CommentsInterface.ss @@ -1,65 +1,70 @@ -
+<% if CommentsEnabled %> +
-

<% _t('POSTCOM','Post your comment') %>

- <% if PostCommentForm %> - <% if CanPostComment %> - $PostCommentForm - <% else %> -

<% _t('COMMENTLOGINERROR', 'You cannot post comments until you have logged in') %><% if PostingRequiresPermission %>,<% _t('COMMENTPERMISSIONERROR', 'and that you have an appropriate permission level') %><% end_if %>. - <% _t('COMMENTPOSTLOGIN', 'Login Here') %>. -

- <% end_if %> - <% else %> -

<% _t('COMMENTSDISABLED', 'Posting comments has been disabled') %>.

- <% end_if %> - -

<% _t('COMMENTS','Comments') %>

- -
- <% if Comments %> -
    - <% control Comments %> -
  • - <% include PageCommentInterface_singlecomment %> -
  • - <% end_control %> -
- - <% if Comments.MoreThanOnePage %> -
-

- <% if Comments.PrevLink %> - « <% _t('PREV','previous') %> - <% end_if %> - - <% if Comments.Pages %> - <% control Comments.Pages %> - <% if CurrentBool %> - $PageNum - <% else %> - $PageNum - <% end_if %> - <% end_control %> - <% end_if %> - - <% if Comments.NextLink %> - <% _t('NEXT','next') %> » - <% end_if %> -

-
+

<% _t('POSTCOM','Post your comment') %>

+ + <% if AddCommentForm %> + <% if canPost %> + $AddCommentForm + <% else %> +

<% _t('COMMENTLOGINERROR', 'You cannot post comments until you have logged in') %><% if PostingRequiresPermission %>,<% _t('COMMENTPERMISSIONERROR', 'and that you have an appropriate permission level') %><% end_if %>. + <% _t('COMMENTPOSTLOGIN', 'Login Here') %>. +

<% end_if %> <% else %> -

<% _t('NOCOMMENTSYET','No one has commented on this page yet.') %>

+

<% _t('COMMENTSDISABLED', 'Posting comments has been disabled') %>.

<% end_if %> -
- <% if DeleteAllLink %> -

- <% _t('PageCommentInterface.DELETEALLCOMMENTS','Delete all comments on this page') %> -

- <% end_if %> -

- <% _t('RSSFEEDCOMMENTS', 'RSS feed for comments on this page') %> | - <% _t('RSSFEEDALLCOMMENTS', 'RSS feed for all comments') %> -

-
+ +

<% _t('COMMENTS','Comments') %>

+ +
+ <% if Comments %> + + + <% if Comments.MoreThanOnePage %> +
+

+ <% if Comments.PrevLink %> + « <% _t('PREV','previous') %> + <% end_if %> + + <% if Comments.Pages %> + <% control Comments.Pages %> + <% if CurrentBool %> + $PageNum + <% else %> + $PageNum + <% end_if %> + <% end_control %> + <% end_if %> + + <% if Comments.NextLink %> + <% _t('NEXT','next') %> » + <% end_if %> +

+
+ <% end_if %> + <% else %> +

<% _t('NOCOMMENTSYET','No one has commented on this page yet.') %>

+ <% end_if %> +
+ + <% if DeleteAllLink %> +

+ <% _t('PageCommentInterface.DELETEALLCOMMENTS','Delete all comments on this page') %> +

+ <% end_if %> + +

+ <% _t('RSSFEEDCOMMENTS', 'RSS feed for comments on this page') %> | + <% _t('RSSFEEDALLCOMMENTS', 'RSS feed for all comments') %> +

+
+<% end_if %> diff --git a/templates/PageCommentInterface_singlecomment.ss b/templates/CommentsInterface_singlecomment.ss similarity index 100% rename from templates/PageCommentInterface_singlecomment.ss rename to templates/CommentsInterface_singlecomment.ss