Add a new form to Update a comment authored by the user

This commit is contained in:
Juan José González 2023-01-14 21:22:26 +00:00 committed by Juan Jose Gonzalez
parent 02e4cd9759
commit 69dbba2a3d
7 changed files with 219 additions and 7 deletions

View File

@ -123,8 +123,121 @@
}
});
e.preventDefault();
return false;
});
});
}(jQuery));
/**
* Hide comment reply forms by default (unless visiting via permalink)
*/
$(".comment")
.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);

View File

@ -22,6 +22,7 @@ en:
ISNTSPAM: 'Not spam'
ISSPAM: 'Spam it'
REMCOM: 'Reject it'
UPDCOM: 'Update it'
REPLYTO: 'Reply to'
CommentsInterface_ss:
AWAITINGMODERATION: 'Your comment has been submitted and is now awaiting moderation.'

View File

@ -11,6 +11,8 @@ es:
YOURNAME_MESSAGE_REQUIRED: 'Por favor, ingrese su nombre'
CommentsInterface_pendingcomment_ss:
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
CommentsInterface_singlecomment_ss:
UPDCOM: 'Actualizarlo'
CommentsInterface_ss:
AWAITINGMODERATION: 'Se envió su comentario y ahora está esperando la moderación.'
COMMENTLOGINERROR: 'Ud no puede comentar hasta que haya iniciado sesión'

View File

@ -20,6 +20,7 @@ use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Security\Security;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Forms\HiddenField;
/**
* @package comments
@ -37,6 +38,7 @@ class CommentingController extends Controller
'rss',
'CommentsForm',
'reply',
'updateComment',
'doPostComment',
'doPreviewComment',
];
@ -46,6 +48,7 @@ class CommentingController extends Controller
*/
private static $url_handlers = [
'reply/$ParentCommentID//$ID/$OtherID' => 'reply',
'updateComment/$ParentCommentID//$ID/$OtherID' => 'updateComment',
];
/**
@ -456,6 +459,24 @@ class CommentingController extends Controller
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.
@ -478,6 +499,19 @@ class CommentingController extends Controller
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
*

View File

@ -236,7 +236,29 @@ class CommentForm extends Form
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);
$comment->ParentID = $data['ParentID'];

View File

@ -381,6 +381,15 @@ class Comment extends DataObject
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.
*
@ -880,6 +889,27 @@ class Comment extends DataObject
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
*/

View File

@ -14,9 +14,14 @@
<div class="comment-text<% if $Gravatar %> hasGravatar<% end_if %>" id="<% if $isPreview %>comment-preview<% else %>{$Permalink}-text<% end_if %>">
<p>$EscapedComment</p>
</div>
<% if $UpdateForm %>
<div class="comment-update-form-holder">
$UpdateForm
</div>
<% end_if %>
<% if not $isPreview %>
<% if $ApproveLink || $SpamLink || $HamLink || $DeleteLink || $RepliesEnabled %>
<% if $ApproveLink || $SpamLink || $HamLink || $DeleteLink || $UpdateForm || $RepliesEnabled %>
<div class="comment-action-links">
<div class="comment-moderation-options">
<% if $ApproveLink %>
@ -32,6 +37,11 @@
<a href="$DeleteLink.ATT" class="delete"><%t CommentsInterface_singlecomment_ss.REMCOM "Reject it" %></a>
<% end_if %>
</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 %>
<button class="comment-reply-link" type="button" aria-controls="$ReplyForm.FormName" aria-expanded="false">
<%t CommentsInterface_singlecomment_ss.REPLYTO "Reply to" %> $AuthorName.XML