Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * ../../ext/pok4/comment_replies/ext.php
- *
- * @package default
- */
- namespace ext\pok4;
- use \PDO;
- use \App\Entity\Emoji;
- if (count(get_included_files()) == 1) exit("Direct access not permitted."); //Don't edit
- //Your Extension Script
- class comment_replies extends \App\Controllers\BaseController {
- /**
- *
- */
- public function __construct() {
- parent::__construct();
- //The Model for this Controller
- $this->model = new \App\Models\Ajax\Vote_Model;
- $this->comment_replies_lang();
- }
- /**
- *
- */
- public function ajax() {
- if(is_ajax()) {
- //get news id by title
- if(isset($_POST['title'])) {
- $news_title= $_POST['title'];
- $pieces = explode("topic_", $news_title);
- $news_title_clear = htmlspecialchars($pieces[1]);
- //catch news id by title
- $go = $this->db->prepare("SELECT id from ".$this->argos_db_prefix."news WHERE seourl=?");
- $go->bindParam(1, $news_title_clear, PDO::PARAM_STR);
- $go->execute();
- $row=$go->fetch(PDO::FETCH_ASSOC);
- echo $row['id'];
- exit();
- }
- //submit new reply to exist comment
- if(isset($_POST['news_text'])) {
- $text = trim($_POST['news_text']);
- $reply_id = (int)$_POST['commentreply_id'];
- $news_id = (int)$_POST['newsid'];
- $com_ava = get_user_ava();
- $com_date = time();
- $com_text = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', 'URL is disabled!', $text);
- $go = $this->db->prepare("INSERT INTO ".$this->argos_db_prefix."replies (author,text,date,avatar,nick_colour,user_id,newsid,comment_reply_id) VALUES(?,?,'$com_date','$com_ava','".$this->user_color."','".$this->user_id."', '$news_id','$reply_id')");
- $go->bindParam(1, $this->username, PDO::PARAM_STR);
- $go->bindParam(2, $com_text, PDO::PARAM_STR);
- $go->execute();
- $go = $this->db->query("UPDATE ".$this->argos_db_prefix."news SET comments=comments+1 WHERE id='$news_id'");
- exit();
- }
- //ajax vote
- if(isset($_GET['for'])) {
- $id = (int)$_POST['id'];
- $type_for = htmlspecialchars(preg_replace('/\s+/', '', $_GET['for'])); //type news/comments/greyfish
- $type_vote = htmlspecialchars(preg_replace('/\s+/', '', $_GET['vote'])); //vote type (up or down)
- $ip = $_SERVER['REMOTE_ADDR'];
- $count = $this->model->check_if_user_voted($id, $type_for, $ip);
- if ($count==0) {
- switch ($type_vote) {
- case 'up': {
- $this->model->up($id, $type_for);
- break;
- }
- case 'down': {
- $this->model->down($id, $type_for);
- break;
- }
- }
- $this->model->insert_user_vote($type_for, $id, $ip);
- echo "<script>alert('".$this->lang['lang_vote_success']."');</script>";
- }
- else {
- echo "<script>alert('".$this->lang['lang_already_voted']."');</script>";
- }
- $row = $this->model->get_votes($id, $type_for);
- echo $row['vote'];
- exit();
- }
- //Print comments to users
- $results=[];//globalize
- $get_comm = $this->db->query("SELECT * FROM ".$this->argos_db_prefix."replies order by id DESC");
- if ($get_comm->rowCount() > 0) {
- while ($row = $get_comm->fetch(PDO::FETCH_ASSOC)) {
- $comment_id = $row['id'];
- $comment_votes = $row['vote'];
- $comment_author = $row['author'];
- \App\Entity\Emoji\Emojione::$imageType = 'png'; // or svg / png is default
- \App\Entity\Emoji\Emojione::$imagePathPNG = 'https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.6/assets/png/'; // defaults to jsdelivr's free CDN
- $comment_text=\App\Entity\Emoji\Emojione::toImage($row['text']);
- $comment_date = date('d.m.y :: h:i', $row['date']);
- $comment_ava = $row['avatar'];
- $comment_nick_color = $row['nick_colour'];
- if (empty($comment_nick_color )) {
- $comment_nick_color = "FF5733"; //default user color
- }
- $comment_userid = $row['user_id'];
- $comment_reply_id = $row['comment_reply_id'];
- $results[] = ['comment_id'=>$comment_id, 'comment_reply_id'=>$comment_reply_id, 'comment_votes'=>$comment_votes, 'comment_author'=>$comment_author, 'comment_text'=>$comment_text, 'comment_date'=>$comment_date, 'comment_ava'=>$comment_ava, 'comment_nick_color'=>$comment_nick_color, 'comment_userid'=>$comment_userid];
- }
- echo json_encode($results, JSON_PRETTY_PRINT); //print array with results
- }
- }
- }
- public function comment_replies_lang() {
- //language definitions
- if (get_current_language() == 'bg') {
- $this->lang= array_merge($this->lang, [
- 'ext_comment_replies_explain1'=>'Формата е настроена за отговор на коментар',
- 'ext_comment_replies_explain2'=>'Моля',
- 'ext_comment_replies_explain3'=>'рефрешнете',
- 'ext_commment_replies_explain4'=>'страницата, за да напишете коментар към новината.',
- 'ext_comments_replies_loading'=>'Зареждаме коментарите...',
- ]);
- }
- if (get_current_language() == 'en') {
- $this->lang= array_merge($this->lang, [
- 'ext_comment_replies_explain1'=>'The form is set up to respond to a comment',
- 'ext_comment_replies_explain2'=>'Please',
- 'ext_comment_replies_explain3'=>'refresh',
- 'ext_commment_replies_explain4'=>'the page to write a comment to the news.',
- 'ext_comments_replies_loading'=>'Loading comments...',
- ]);
- }
- if (get_current_language() == 'ru') {
- $this->lang= array_merge($this->lang, [
- 'ext_comment_replies_explain1'=>'Форма настроена для ответа на комментарий',
- 'ext_comment_replies_explain2'=>'Пожалуйста',
- 'ext_comment_replies_explain3'=>'обновите',
- 'ext_commment_replies_explain4'=>'страницу, чтобы написать комментарий к новости.',
- 'ext_comments_replies_loading'=>'Загрузка комментариев...',
- ]);
- }
- if (get_current_language() == 'es') {
- $this->lang= array_merge($this->lang, [
- 'ext_comment_replies_explain1'=>'El formulario está configurado para responder a un comentario',
- 'ext_comment_replies_explain2'=>'Por favor',
- 'ext_comment_replies_explain3'=>'actualizar',
- 'ext_commment_replies_explain4'=>'página para escribir un comentario a la noticia.',
- 'ext_comments_replies_loading'=>'Cargando comentarios...',
- ]);
- }
- }
- /**
- *
- */
- public function load() {
- $js = '';//globalize
- if (strpos($_SERVER['REQUEST_URI'], 'topic_')) {
- $js = '
- //add overlay when we load the comments (we hide it below, when ajax is success) // we use: https://gasparesganga.com/labs/jquery-loading-overlay/#quick-demo
- $.LoadingOverlay("show", {
- text : "'.$this->lang['ext_comments_replies_loading'].'"
- });
- $(".comments_block,.media-content").append("<div class=\"btn btn-sm btn-default pull-right reply_to\">reply</div><div class=\"clear clearfix\"></div>");
- var news_id = ""; //globalize, holds news id to pass in replies for db
- var comment_reply_id = "";//globalize, holds comment id
- $(".reply_to").click(function(e) {
- //catch reply id
- comment_reply_id = $(this).closest(".comment_aid").attr("data-comment-aid"); //current comment id to reply
- var news_title = location.pathname.substr(1); //to catch news id from this
- //make ajax call to find news id by news_title
- $.ajax({
- url: "/ajax/ext/pok4/comment_replies",
- type: "POST",
- data: {title: news_title},
- success: function(data){
- news_id = data; //news id
- //alert(news_id); //debug
- }
- }); //end ajax call
- //scroll to textarea
- $("html, body").animate({
- scrollTop: $("textarea[name=\"com_text\"]").parent().offset().top - ($(window).height() - $("textarea[name=\"com_text\"]").outerHeight(true)) / 2
- }, 1500);
- //change submit form with different parameters which will hold the replies
- var print_string = "<div class=\"replying_to\">'.$this->lang['ext_comment_replies_explain1'].': #" + comment_reply_id + "<br/>'.$this->lang['ext_comment_replies_explain2'].' <a href=\"#\" onClick=\"window.location.reload();return false;\" style=\"color:red\">'.$this->lang['ext_comment_replies_explain3'].'</a> '.$this->lang['ext_commment_replies_explain4'].'</div>";
- if ($(".replying_to").length < 1 ) {
- $("textarea[name=\"com_text\"]").parent().before(print_string);
- $( "input[name=\"submit_comm\"]" ).addClass( "submit_reply" ); //add different class to submit button to catch ajax call below
- } else {
- $(".replying_to").html( print_string );
- }
- });
- //form change and ajax submit
- $( "body" ).on( "click", ".submit_reply", function(event) {
- var emojionestrip = $("textarea[name=com_text]").emojioneArea();
- var text = emojionestrip[0].emojioneArea.getText();
- //remove submit button for prevent double content
- $(this).hide();
- event.preventDefault(); //prevent real submit
- $.ajax({
- url: "/ajax/ext/pok4/comment_replies",
- type: "POST",
- async: false,
- data: {news_text:text, newsid:news_id, commentreply_id:comment_reply_id},
- success: function(data){
- $("textarea[name=\"com_text\"]").parent().after("<div class=\"alert alert-success\">'.$this->lang['lang_success'].'</div>");
- location.reload();
- }
- }); //end ajax call
- return false;
- });
- //load comments via ajax
- var comment_data_id = 0; //globalize
- $.ajax({
- url: "/ajax/ext/pok4/comment_replies",
- type: "POST",
- contentType: "application/json; charset=utf-8",
- cache: false,
- dataType: "json",
- success: function(result){
- comment_data_id = result[0]["comment_id"];
- if( comment_data_id ) {
- var clone_comment_div = ""; //clone div with comments
- var string_with_comment_id = "";//globalize, hold the specific string for replies
- var reply_id = 0;//globalize, hold the id from replies table for every user
- var avatar_changes= "";//globalize, holds the user avatar
- var author_date_vote = ""; //holds date/vote/author
- var new_string = "";//holds the new html for .name class
- for (var i=0; i < result.length; i++) {
- //console.log(result[i])
- reply_id = result[i]["comment_id"];
- //manipulate the div which we copy
- string_with_comment_id="div[data-comment-aid=" +result[i]["comment_reply_id"]+ "]";
- clone_comment_div = $(string_with_comment_id).clone().removeAttr("data-comment-aid").attr("data-replyid",reply_id).addClass("reply_comment").find("p").html(result[i]["comment_text"]).end();
- $("body, html").find(string_with_comment_id).after(clone_comment_div).end();
- //change avatar for each post author
- avatar_changes = "div[data-replyid=" +reply_id+ "]";
- $("body, html").find(avatar_changes).find(".author-avatar .ava_news, .media-left").html(result[i]["comment_ava"]).end();
- //change date, author up/down vote
- author_date_vote = "div[data-replyid=" +reply_id+ "]";
- new_string = "<a href=\"forum/memberlist.php?mode=viewprofile&u=" +result[i]["comment_userid"]+ "\" style=\"color:#"+ result[i]["comment_nick_color"] +" !important\"> " + result[i]["comment_author"] + "</a> <span style=\"color:red\">::</span> "+ result[i]["comment_date"]+ ":<br/><span class=\"upme vote-btn_r\" data-vote=\"upvote\" data-my=\"" +result[i]["comment_id"]+ "\"></span> <span id=\"bid-" +result[i]["comment_id"]+ "\">" +result[i]["comment_votes"]+ "</span> <span class=\"downme vote-btn_r\" data-vote=\"downvote\" data-my=\"" +result[i]["comment_id"]+ "\"></span>";
- console.log(new_string);
- $("body, html").find(author_date_vote).find(".name").html(new_string).end();
- //remove order button if order by comments ext is enabled (hot fix)
- $(".order_by_c").not(":first").remove();
- //remove reply_to button in replies
- $(".reply_comment .reply_to").hide();
- }
- $.LoadingOverlay("hide"); //hide overlay (we success)
- }
- }
- }).fail(function (data) {
- $.LoadingOverlay("hide"); //hide overlay (we fail)
- }); //end ajax call
- //check if user has voting on replies
- $( "body" ).on( "click", ".vote-btn_r", function(event) {
- var id = $(this).attr("data-my");
- var name = $(this).attr("data-vote");
- var dataString = "id="+ id ;
- var parent = $("#bid-"+id);
- if(name=="upvote")
- {
- $(this).fadeIn(200).html("");
- $.ajax({
- type: "POST",
- url: "/ajax/ext/pok4/comment_replies/?for=replies&vote=up",
- data: dataString,
- cache: false,
- success: function(html)
- {
- parent.html(html);
- }
- });
- }
- else
- {
- $(this).fadeIn(200).html("");
- $.ajax({
- type: "POST",
- url: "/ajax/ext/pok4/comment_replies/?for=replies&vote=down",
- data: dataString,
- cache: false,
- success: function(html)
- {
- parent.html(html);
- }
- });
- }
- return false;
- });
- //end ajax vote
- ';
- }
- $this->dispatcher->emit('core_event_head_append', ['<link rel="stylesheet" href="ext/pok4/comment_replies/css/style.css">']);
- $this->dispatcher->emit('core_event_head_append', ['<script type="text/javascript">head.load("//cdn.jsdelivr.net/npm/gasparesganga-jquery-loading-overlay@2.1.7/dist/loadingoverlay.min.js");</script>']);
- $this->dispatcher->emit('core_event_inside_head_ready_front', [$js]);
- }
- };
- $load_ext = new comment_replies;
- $load_ext->load();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement