From 391f7c2148a5a19f38a51eec50728cd31ba434a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Mon, 1 Feb 2016 11:43:03 +0100 Subject: [PATCH] Fixed: bug when you use "Quick Reply" twice (in forum). --- e107_plugins/forum/forum_class.php | 232 +++++++---------------------- e107_plugins/forum/js/forum.js | 131 ++++++++++++++++ 2 files changed, 183 insertions(+), 180 deletions(-) create mode 100644 e107_plugins/forum/js/forum.js diff --git a/e107_plugins/forum/forum_class.php b/e107_plugins/forum/forum_class.php index 56ac7bf28..b85950b9d 100644 --- a/e107_plugins/forum/forum_class.php +++ b/e107_plugins/forum/forum_class.php @@ -15,133 +15,7 @@ /* Forum Header File */ if (!defined('e107_INIT')) { exit; } -$jscode = <<check(false) || !$this->checkPerm($_POST['post'], 'post')) { - //$ret['status'] = 'ok'; - // $ret['msg'] = "Token Error"; - - // echo json_encode($ret); - + // Invalid token. exit; } if(varset($_POST['action']) == 'quickreply' && vartrue($_POST['text'])) - { - - $postInfo = array(); - $postInfo['post_ip'] = e107::getIPHandler()->getIP(FALSE); - - if (USER) - { - $postInfo['post_user'] = USERID; + { - } - else - { - $postInfo['post_user_anon'] = $_POST['anonname']; - } - - $postInfo['post_entry'] = $_POST['text']; - $postInfo['post_forum'] = intval($_POST['post']); - $postInfo['post_datestamp'] = time(); - $postInfo['post_thread'] = intval($_POST['thread']); - - $postInfo['post_id'] = $this->postAdd($postInfo); // save it. - - $postInfo['user_name'] = USERNAME; - $postInfo['user_email'] = USEREMAIL; - $postInfo['user_image'] = USERIMAGE; - $postInfo['user_signature'] = USERSIGNATURE; + $postInfo = array(); + $postInfo['post_ip'] = e107::getIPHandler()->getIP(false); - if($_POST['insert'] == 1) - { - $tmpl = e107::getTemplate('forum','forum_viewtopic','replies'); - $sc = e107::getScBatch('view', 'forum'); - $sc->setScVar('postInfo', $postInfo); - $ret['html'] = $tp->parseTemplate($tmpl, true, $sc) . "\n"; - } - else - { - $ret['html'] = false; - } - - $ret['status'] = 'ok'; - $ret['msg'] = "Your post has been added"; + if(USER) + { + $postInfo['post_user'] = USERID; + } + else + { + $postInfo['post_user_anon'] = $_POST['anonname']; + } - //echo $ret; - echo json_encode($ret); - } + $postInfo['post_entry'] = $_POST['text']; + $postInfo['post_forum'] = intval($_POST['post']); + $postInfo['post_datestamp'] = time(); + $postInfo['post_thread'] = intval($_POST['thread']); + + $postInfo['post_id'] = $this->postAdd($postInfo); // save it. + + $postInfo['user_name'] = USERNAME; + $postInfo['user_email'] = USEREMAIL; + $postInfo['user_image'] = USERIMAGE; + $postInfo['user_signature'] = USERSIGNATURE; + + if($_POST['insert'] == 1) + { + $tmpl = e107::getTemplate('forum', 'forum_viewtopic', 'replies'); + $sc = e107::getScBatch('view', 'forum'); + $sc->setScVar('postInfo', $postInfo); + $ret['html'] = $tp->parseTemplate($tmpl, true, $sc) . "\n"; + } + else + { + $ret['html'] = false; + } + + $ret['status'] = 'ok'; + $ret['msg'] = "Your post has been added"; // TODO lan. + } e107::getSession()->reset(); - exit; + if(varset($ret, false)) + { + $ret['e_token'] = e107::getSession()->getFormToken(); + } + + echo $tp->toJSON($ret); + exit; } diff --git a/e107_plugins/forum/js/forum.js b/e107_plugins/forum/js/forum.js new file mode 100644 index 000000000..2a0a26887 --- /dev/null +++ b/e107_plugins/forum/js/forum.js @@ -0,0 +1,131 @@ +/** + * @file + * Forum JavaScript behaviors integration. + */ + +var e107 = e107 || {'settings': {}, 'behaviors': {}}; + +(function ($) +{ + + /** + * Behavior to bind click events on action buttons/links. + * + * @type {{attach: e107.behaviors.forumActions.attach}} + * @see "e107_web/js/core/all.jquery.js" file for more information. + */ + e107.behaviors.forumActions = { + attach: function (context, settings) + { + $('a[data-forum-action], input[data-forum-action]', context).once('data-forum-action').each(function () + { + $(this).on('click', function (e) + { + e.preventDefault(); + + var $this = $(this); + var action = $this.attr('data-forum-action'); + var thread = $this.attr('data-forum-thread'); + var post = $this.attr('data-forum-post'); + var text = $('#forum-quickreply-text').val(); + var insert = $this.attr('data-forum-insert'); + var token = $this.attr('data-token'); + var script = $this.attr("src"); + + $.ajax({ + type: "POST", + url: script, + data: {thread: thread, action: action, post: post, text: text, insert: insert, e_token: token}, + success: function (data) + { + try + { + var d = $.parseJSON(data); + } catch(e) + { + // Not JSON. + return; + } + + // Update e_token value on quick-reply form for the next Ajax request. + if(d.e_token) + { + $this.attr('data-token', d.e_token); + } + + // Show pup-up message. + if(d.msg) + { + var alertType = 'info'; + + if(d.status == 'ok') + { + alertType = 'success'; + } + + if(d.status == 'error') + { + alertType = 'danger'; + } + + if(jQuery().notify) + { + $('#uiAlert').notify({ + type: alertType, + message: {text: d.msg}, + fadeOut: {enabled: true, delay: 3000} + }).show(); + } + else + { + alert(d.msg); + location.reload(); + return; + } + } + + if(action == 'stick' || action == 'unstick' || action == 'lock' || action == 'unlock') + { + location.reload(); + return; + } + + if(action == 'track') + { + if(d.html != false) + { + $('#' + insert).html(d.html); + // Attach all registered behaviors to the new content. + e107.attachBehaviors(); + } + } + + if(action == 'quickreply' && d.status == 'ok') + { + if(d.html != false) + { + $(d.html).appendTo('#forum-viewtopic').hide().slideDown(1000); + // Attach all registered behaviors to the new content. + e107.attachBehaviors(); + } + + $('#forum-quickreply-text').val(''); + return; + } + + if(d.hide) + { + var t = '#thread-' + thread; + var p = '#post-' + post; + + $(t).hide('slow'); + $(p).hide('slow').slideUp(800); + } + } + }); + }); + }); + } + }; + +})(jQuery);