Ajaxifisation des commentaires.

- Permet de ne pas perdre ses votes à l'ajout d'un commentaire.
	- On rafraichit aussi à l'envois du commentaire la liste des commentaires.

Fix #3
This commit is contained in:
Antonin 2015-10-22 22:48:30 +02:00
parent 2b38a7ddd6
commit 166927f8af
7 changed files with 158 additions and 15 deletions

88
action/add_comment.php Normal file
View File

@ -0,0 +1,88 @@
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\InputService;
use Framadate\Services\MailService;
use Framadate\Services\NotificationService;
use Framadate\Message;
use Framadate\Utils;
use Framadate\Editable;
include_once __DIR__ . '/../app/inc/init.php';
/* Variables */
/* --------- */
$poll_id = null;
$poll = null;
$message = null;
$result = false;
/* Services */
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp']);
$notificationService = new NotificationService($mailService);
/* PAGE */
/* ---- */
if (!empty($_POST['poll'])) {
$poll_id = filter_input(INPUT_POST, 'poll', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => POLL_REGEX]]);
if (strlen($poll_id) === 16) {
$poll = $pollService->findById($poll_id);
}
}
if (!$poll) {
$message = new Message('error', __('Error', 'This poll doesn\'t exist !'));
} else {
$name = $inputService->filterName($_POST['name']);
$comment = $inputService->filterComment($_POST['comment']);
if ($name == null) {
$message = new Message('danger', __('Error', 'The name is invalid.'));
}
if ($message == null) {
// Add comment
$result = $pollService->addComment($poll_id, $name, $comment);
if ($result) {
$message = new Message('success', __('Comments', 'Comment added'));
$notificationService->sendUpdateNotification($poll, $mailService, $name, NotificationService::ADD_COMMENT);
} else {
$message = new Message('danger', __('Error', 'Comment failed'));
}
}
}
$comments = $pollService->allCommentsByPollId($poll_id);
$smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->assign('comments', $comments);
$comments_html = $smarty->fetch('part/comments_list.tpl');
$response = array('result' => $result, 'message' => $message, 'comments' => $comments_html);
echo json_encode($response);

7
css/jquery-ui.min.css vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -44,4 +44,36 @@ $(document).ready(function () {
$(this).next().removeClass('startunchecked');
});
var form = $('#comment_form');
form.submit(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: form.attr('action'),
data: form.serialize(),
dataType: 'json',
success: function(data)
{
$('#comment').val("");
if (data.result) {
$("#comments_list").replaceWith(data.comments);
var lastComment = $("#comments_list").find("div.comment").last();
lastComment.effect('highlight', {color: 'green'}, 401);
$('html, body').animate({
scrollTop: $("#comments_alerts").offset().top
}, 750);
} else {
var newMessage = $("#genericErrorTemplate").clone();
newMessage.find(".contents").text(data.message.message);
$("#comments_alerts").empty().append(newMessage);
}
}
});
return false;
});
});

6
js/jquery-ui.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,21 +1,11 @@
<hr role="presentation" id="comments" class="hidden-print"/>
<form action="#comments" method="POST">
{* Comment list *}
{* Comment list *}
{include 'part/comments_list.tpl'}
{if $comments|count > 0}
<h3>{__('Comments', 'Comments of polled people')}</h3>
{foreach $comments as $comment}
<div class="comment">
{if $admin && !$expired}
<button type="submit" name="delete_comment" value="{$comment->id|html}" class="btn btn-link" title="{__('Comments', 'Remove the comment')}"><span class="glyphicon glyphicon-remove text-danger"></span><span class="sr-only">{__('Generic', 'Remove')}</span></button>
{/if}
<span class="comment_date">{$comment->date|date_format:$date_format['txt_datetime_short']}</span>
<b>{$comment->name|html}</b>&nbsp;
<span class="comment">{$comment->comment|escape|nl2br}</span>
</div>
{/foreach}
{/if}
<form action="action/add_comment.php" method="POST" id="comment_form">
<input type="hidden" name="poll" value="{$poll_id}"/>
{* Add comment form *}
{if $active && !$expired}

View File

@ -0,0 +1,16 @@
<div id="comments_list">
{if $comments|count > 0}
<h3>{__('Comments', 'Comments of polled people')}</h3>
{foreach $comments as $comment}
<div class="comment">
{if $admin && !$expired}
<button type="submit" name="delete_comment" value="{$comment->id|html}" class="btn btn-link" title="{__('Comments', 'Remove the comment')}"><span class="glyphicon glyphicon-remove text-danger"></span><span class="sr-only">{__('Generic', 'Remove')}</span></button>
{/if}
<span class="comment_date">{$comment->date|date_format:$date_format['txt_datetime_short']}</span>
<b>{$comment->name|html}</b>&nbsp;
<span>{$comment->comment|escape|nl2br}</span>
</div>
{/foreach}
{/if}
<div id="comments_alerts"></div>
</div>

View File

@ -1,9 +1,12 @@
{extends file='page.tpl'}
{block name="header"}
<script src="{"js/jquery-ui.min.js"|resource}" type="text/javascript"></script>
<script src="{"js/Chart.min.js"|resource}" type="text/javascript"></script>
<script src="{"js/Chart.StackedBar.js"|resource}" type="text/javascript"></script>
<script src="{"js/app/studs.js"|resource}" type="text/javascript"></script>
<link rel="stylesheet" href="{'css/jquery-ui.min.css'|resource}">
{/block}
{block name=main}
@ -15,6 +18,7 @@
{/if}
</div>
<div id="nameErrorMessage" class="hidden alert alert-dismissible alert-danger hidden-print" role="alert">{__('Error', 'The name is invalid.')}<button type="button" class="close" data-dismiss="alert" aria-label="{__('Generic', 'CLose')}"><span aria-hidden="true">&times;</span></button></div>
<div id="genericErrorTemplate" class="hidden alert alert-dismissible alert-danger hidden-print" role="alert"><span class="contents"></span><button type="button" class="close" data-dismiss="alert" aria-label="{__('Generic', 'CLose')}"><span aria-hidden="true">&times;</span></button></div>
{* Global informations about the current poll *}