Add a new form to Update a comment authored by the user
This commit is contained in:
parent
02e4cd9759
commit
69dbba2a3d
|
@ -123,8 +123,121 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
e.preventDefault();
|
/**
|
||||||
return false;
|
* Hide comment reply forms by default (unless visiting via permalink)
|
||||||
});
|
*/
|
||||||
});
|
$(".comment")
|
||||||
}(jQuery));
|
.children('.info')
|
||||||
|
.not(window.document.location.hash)
|
||||||
|
.nextAll(".comment-replies-container")
|
||||||
|
.children(".comment-reply-form-holder")
|
||||||
|
.hide();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hide comment update forms by default
|
||||||
|
*/
|
||||||
|
$(".comment")
|
||||||
|
.children(".comment-update-form-holder")
|
||||||
|
.hide();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle on/off reply form
|
||||||
|
*/
|
||||||
|
$('.comments-holder').on('click', '.comment-reply-link', function(e) {
|
||||||
|
var allForms = $('.comment-reply-form-holder');
|
||||||
|
var formID = '#' + $(this).attr('aria-controls');
|
||||||
|
var form = $(formID).closest('.comment-reply-form-holder');
|
||||||
|
|
||||||
|
$(this).attr('aria-expanded', function (i, attr) {
|
||||||
|
return attr == 'true' ? 'false' : 'true'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Prevent focus
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if(form.is(':visible')) {
|
||||||
|
allForms.slideUp();
|
||||||
|
} else {
|
||||||
|
allForms.not(form).slideUp();
|
||||||
|
form.slideDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle on/off update form
|
||||||
|
*/
|
||||||
|
$('.comments-holder').on('click', '.comment-update-link', function(e) {
|
||||||
|
var allForms = $('.comment-update-form-holder');
|
||||||
|
var formID = '#' + $(this).attr('aria-controls');
|
||||||
|
var form = $(formID).closest('.comment-update-form-holder');
|
||||||
|
|
||||||
|
$(this).attr('aria-expanded', function (i, attr) {
|
||||||
|
return attr == 'true' ? 'false' : 'true'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Prevent focus
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if(form.is(':visible')) {
|
||||||
|
allForms.slideUp();
|
||||||
|
} else {
|
||||||
|
allForms.not(form).slideUp();
|
||||||
|
form.slideDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clicking one of the metalinks performs the operation via ajax
|
||||||
|
* this inclues the spam and approve links
|
||||||
|
*/
|
||||||
|
$('.comments-holder .comments-list').on('click', 'div.comment-moderation-options a', function(e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
var link = $(this);
|
||||||
|
if (link.hasClass('delete')) {
|
||||||
|
var confirmationMsg = ss.i18n._t('CommentsInterface_singlecomment_ss.DELETE_CONFIRMATION');
|
||||||
|
var confirmation = window.confirm(confirmationMsg);
|
||||||
|
if (!confirmation) {
|
||||||
|
e.preventDefault();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var comment = link.parents('.comment:first');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: $(this).attr('href'),
|
||||||
|
cache: false,
|
||||||
|
success: function(html){
|
||||||
|
if(link.hasClass('ham')) {
|
||||||
|
// comment has been marked as not spam
|
||||||
|
comment.html(html);
|
||||||
|
comment.removeClass('spam');
|
||||||
|
}
|
||||||
|
else if(link.hasClass('approve')) {
|
||||||
|
// comment has been approved
|
||||||
|
comment.html(html);
|
||||||
|
comment.removeClass('unmoderated');
|
||||||
|
}
|
||||||
|
else if(link.hasClass('delete')) {
|
||||||
|
comment.fadeOut(1000, function() {
|
||||||
|
comment.remove();
|
||||||
|
|
||||||
|
if($('.comments-holder .comments-list').children().length === 0) {
|
||||||
|
$('.no-comments-yet').show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(link.hasClass('spam')) {
|
||||||
|
comment.html(html).addClass('spam');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
failure: function(html) {
|
||||||
|
var errorMsg = ss.i18n._t('CommentsInterface_singlecomment_ss.AJAX_ERROR');
|
||||||
|
alert(errorMsg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})(jQuery);
|
||||||
|
|
|
@ -22,6 +22,7 @@ en:
|
||||||
ISNTSPAM: 'Not spam'
|
ISNTSPAM: 'Not spam'
|
||||||
ISSPAM: 'Spam it'
|
ISSPAM: 'Spam it'
|
||||||
REMCOM: 'Reject it'
|
REMCOM: 'Reject it'
|
||||||
|
UPDCOM: 'Update it'
|
||||||
REPLYTO: 'Reply to'
|
REPLYTO: 'Reply to'
|
||||||
CommentsInterface_ss:
|
CommentsInterface_ss:
|
||||||
AWAITINGMODERATION: 'Your comment has been submitted and is now awaiting moderation.'
|
AWAITINGMODERATION: 'Your comment has been submitted and is now awaiting moderation.'
|
||||||
|
|
|
@ -11,6 +11,8 @@ es:
|
||||||
YOURNAME_MESSAGE_REQUIRED: 'Por favor, ingrese su nombre'
|
YOURNAME_MESSAGE_REQUIRED: 'Por favor, ingrese su nombre'
|
||||||
CommentsInterface_pendingcomment_ss:
|
CommentsInterface_pendingcomment_ss:
|
||||||
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
|
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
|
||||||
|
CommentsInterface_singlecomment_ss:
|
||||||
|
UPDCOM: 'Actualizarlo'
|
||||||
CommentsInterface_ss:
|
CommentsInterface_ss:
|
||||||
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
|
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
|
||||||
COMMENTLOGINERROR: 'Ud no puede comentar hasta que haya iniciado sesión'
|
COMMENTLOGINERROR: 'Ud no puede comentar hasta que haya iniciado sesión'
|
||||||
|
|
|
@ -20,6 +20,7 @@ use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
use SilverStripe\ORM\PaginatedList;
|
use SilverStripe\ORM\PaginatedList;
|
||||||
use SilverStripe\Security\Security;
|
use SilverStripe\Security\Security;
|
||||||
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
|
||||||
|
use SilverStripe\Forms\HiddenField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @package comments
|
* @package comments
|
||||||
|
@ -37,6 +38,7 @@ class CommentingController extends Controller
|
||||||
'rss',
|
'rss',
|
||||||
'CommentsForm',
|
'CommentsForm',
|
||||||
'reply',
|
'reply',
|
||||||
|
'updateComment',
|
||||||
'doPostComment',
|
'doPostComment',
|
||||||
'doPreviewComment',
|
'doPreviewComment',
|
||||||
];
|
];
|
||||||
|
@ -46,6 +48,7 @@ class CommentingController extends Controller
|
||||||
*/
|
*/
|
||||||
private static $url_handlers = [
|
private static $url_handlers = [
|
||||||
'reply/$ParentCommentID//$ID/$OtherID' => 'reply',
|
'reply/$ParentCommentID//$ID/$OtherID' => 'reply',
|
||||||
|
'updateComment/$ParentCommentID//$ID/$OtherID' => 'updateComment',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -456,6 +459,24 @@ class CommentingController extends Controller
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function UpdateForm($comment)
|
||||||
|
{
|
||||||
|
$form = $this->CommentsForm();
|
||||||
|
$form->setName('UpdateForm_' . $comment->ID);
|
||||||
|
$form->setHTMLID(null);
|
||||||
|
$form->addExtraClass('update-form');
|
||||||
|
|
||||||
|
$form->Fields()->push(HiddenField::create('CommentId', '', $comment->ID));
|
||||||
|
$form->loadDataFrom($comment);
|
||||||
|
|
||||||
|
// Customise action
|
||||||
|
$form->setFormAction($this->Link('updateComment', $comment->ID));
|
||||||
|
|
||||||
|
$this->extend('updateUpdateForm', $form);
|
||||||
|
|
||||||
|
return $form;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request handler for reply form.
|
* Request handler for reply form.
|
||||||
|
@ -478,6 +499,19 @@ class CommentingController extends Controller
|
||||||
return $this->httpError(404);
|
return $this->httpError(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateComment(HTTPRequest $request)
|
||||||
|
{
|
||||||
|
if ($commentID = $request->param('ParentCommentID')) {
|
||||||
|
/** @var Comment $comment */
|
||||||
|
$comment = DataObject::get_by_id(Comment::class, $commentID, true);
|
||||||
|
if ($comment) {
|
||||||
|
return $this->UpdateForm($comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->httpError(404);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post a comment form
|
* Post a comment form
|
||||||
*
|
*
|
||||||
|
|
|
@ -236,7 +236,29 @@ class CommentForm extends Form
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$comment = Comment::create();
|
// Updating comment
|
||||||
|
$existingCommentId = $data["CommentId"] ?? null;
|
||||||
|
|
||||||
|
if ($existingCommentId) {
|
||||||
|
$comment = Comment::get()->byID($existingCommentId);
|
||||||
|
|
||||||
|
if (!$comment) {
|
||||||
|
return $this->getRequestHandler()->httpError(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$comment->canUpdate()) {
|
||||||
|
return Security::permissionFailure(
|
||||||
|
$this->controller,
|
||||||
|
_t(
|
||||||
|
'SilverStripe\\Comments\\Controllers\\CommentingController.PERMISSIONFAILURE',
|
||||||
|
"You're not able to update this comment. Please ensure you are logged in and have an "
|
||||||
|
. 'appropriate permission level.'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$comment = Comment::create();
|
||||||
|
}
|
||||||
$form->saveInto($comment);
|
$form->saveInto($comment);
|
||||||
|
|
||||||
$comment->ParentID = $data['ParentID'];
|
$comment->ParentID = $data['ParentID'];
|
||||||
|
|
|
@ -381,6 +381,15 @@ class Comment extends DataObject
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canUpdate($member = null)
|
||||||
|
{
|
||||||
|
$member = $this->getMember($member);
|
||||||
|
$memberID = $member->ID ?? null;
|
||||||
|
$authorID = $this->Author()->ID;
|
||||||
|
|
||||||
|
return $memberID === $authorID;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the comment can be deleted.
|
* Checks if the comment can be deleted.
|
||||||
*
|
*
|
||||||
|
@ -880,6 +889,27 @@ class Comment extends DataObject
|
||||||
return $controller->ReplyForm($this);
|
return $controller->ReplyForm($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function UpdateForm()
|
||||||
|
{
|
||||||
|
if (!$this->canUpdate()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Check parent is available
|
||||||
|
$parent = $this->Parent();
|
||||||
|
|
||||||
|
if (!$parent || !$parent->exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build update controller
|
||||||
|
$controller = CommentingController::create();
|
||||||
|
$controller->setOwnerRecord($parent);
|
||||||
|
$controller->setParentClass($parent->ClassName);
|
||||||
|
$controller->setOwnerController(Controller::curr());
|
||||||
|
|
||||||
|
return $controller->UpdateForm($this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -14,9 +14,14 @@
|
||||||
<div class="comment-text<% if $Gravatar %> hasGravatar<% end_if %>" id="<% if $isPreview %>comment-preview<% else %>{$Permalink}-text<% end_if %>">
|
<div class="comment-text<% if $Gravatar %> hasGravatar<% end_if %>" id="<% if $isPreview %>comment-preview<% else %>{$Permalink}-text<% end_if %>">
|
||||||
<p>$EscapedComment</p>
|
<p>$EscapedComment</p>
|
||||||
</div>
|
</div>
|
||||||
|
<% if $UpdateForm %>
|
||||||
|
<div class="comment-update-form-holder">
|
||||||
|
$UpdateForm
|
||||||
|
</div>
|
||||||
|
<% end_if %>
|
||||||
|
|
||||||
<% if not $isPreview %>
|
<% if not $isPreview %>
|
||||||
<% if $ApproveLink || $SpamLink || $HamLink || $DeleteLink || $RepliesEnabled %>
|
<% if $ApproveLink || $SpamLink || $HamLink || $DeleteLink || $UpdateForm || $RepliesEnabled %>
|
||||||
<div class="comment-action-links">
|
<div class="comment-action-links">
|
||||||
<div class="comment-moderation-options">
|
<div class="comment-moderation-options">
|
||||||
<% if $ApproveLink %>
|
<% if $ApproveLink %>
|
||||||
|
@ -32,6 +37,11 @@
|
||||||
<a href="$DeleteLink.ATT" class="delete"><%t CommentsInterface_singlecomment_ss.REMCOM "Reject it" %></a>
|
<a href="$DeleteLink.ATT" class="delete"><%t CommentsInterface_singlecomment_ss.REMCOM "Reject it" %></a>
|
||||||
<% end_if %>
|
<% end_if %>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="comment-author-options">
|
||||||
|
<% if $UpdateForm %>
|
||||||
|
<button class="comment-update-link" aria-controls="$UpdateForm.FormName" aria-expanded="false"><%t CommentsInterface_singlecomment_ss.UPDCOM "Update it" %></button>
|
||||||
|
<% end_if %>
|
||||||
|
</div>
|
||||||
<% if $RepliesEnabled && $canPostComment %>
|
<% if $RepliesEnabled && $canPostComment %>
|
||||||
<button class="comment-reply-link" type="button" aria-controls="$ReplyForm.FormName" aria-expanded="false">
|
<button class="comment-reply-link" type="button" aria-controls="$ReplyForm.FormName" aria-expanded="false">
|
||||||
<%t CommentsInterface_singlecomment_ss.REPLYTO "Reply to" %> $AuthorName.XML
|
<%t CommentsInterface_singlecomment_ss.REPLYTO "Reply to" %> $AuthorName.XML
|
||||||
|
|
Loading…
Reference in New Issue