Advertisement
Guest User

Untitled

a guest
Mar 8th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.81 KB | None | 0 0
  1. <?php
  2. /**
  3. * ../../ext/pok4/comment_replies/ext.php
  4. *
  5. * @package default
  6. */
  7. namespace ext\pok4;
  8. use \PDO;
  9. use \App\Entity\Emoji;
  10. if (count(get_included_files()) == 1) exit("Direct access not permitted."); //Don't edit
  11.  
  12. //Your Extension Script
  13. class comment_replies extends \App\Controllers\BaseController {
  14.  
  15.  
  16.  
  17. /**
  18. *
  19. */
  20. public function __construct() {
  21. parent::__construct();
  22.  
  23. //The Model for this Controller
  24. $this->model = new \App\Models\Ajax\Vote_Model;
  25.  
  26. $this->comment_replies_lang();
  27. }
  28.  
  29.  
  30. /**
  31. *
  32. */
  33.  
  34. public function ajax() {
  35. if(is_ajax()) {
  36.  
  37. //get news id by title
  38. if(isset($_POST['title'])) {
  39. $news_title= $_POST['title'];
  40. $pieces = explode("topic_", $news_title);
  41. $news_title_clear = htmlspecialchars($pieces[1]);
  42. //catch news id by title
  43. $go = $this->db->prepare("SELECT id from ".$this->argos_db_prefix."news WHERE seourl=?");
  44. $go->bindParam(1, $news_title_clear, PDO::PARAM_STR);
  45. $go->execute();
  46.  
  47. $row=$go->fetch(PDO::FETCH_ASSOC);
  48. echo $row['id'];
  49. exit();
  50. }
  51.  
  52. //submit new reply to exist comment
  53. if(isset($_POST['news_text'])) {
  54. $text = trim($_POST['news_text']);
  55. $reply_id = (int)$_POST['commentreply_id'];
  56. $news_id = (int)$_POST['newsid'];
  57. $com_ava = get_user_ava();
  58. $com_date = time();
  59. $com_text = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', 'URL is disabled!', $text);
  60.  
  61. $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')");
  62. $go->bindParam(1, $this->username, PDO::PARAM_STR);
  63. $go->bindParam(2, $com_text, PDO::PARAM_STR);
  64. $go->execute();
  65. $go = $this->db->query("UPDATE ".$this->argos_db_prefix."news SET comments=comments+1 WHERE id='$news_id'");
  66. exit();
  67. }
  68.  
  69. //ajax vote
  70. if(isset($_GET['for'])) {
  71. $id = (int)$_POST['id'];
  72. $type_for = htmlspecialchars(preg_replace('/\s+/', '', $_GET['for'])); //type news/comments/greyfish
  73. $type_vote = htmlspecialchars(preg_replace('/\s+/', '', $_GET['vote'])); //vote type (up or down)
  74. $ip = $_SERVER['REMOTE_ADDR'];
  75.  
  76. $count = $this->model->check_if_user_voted($id, $type_for, $ip);
  77.  
  78. if ($count==0) {
  79. switch ($type_vote) {
  80. case 'up': {
  81. $this->model->up($id, $type_for);
  82. break;
  83. }
  84. case 'down': {
  85. $this->model->down($id, $type_for);
  86. break;
  87. }
  88. }
  89.  
  90.  
  91. $this->model->insert_user_vote($type_for, $id, $ip);
  92. echo "<script>alert('".$this->lang['lang_vote_success']."');</script>";
  93.  
  94. }
  95. else {
  96. echo "<script>alert('".$this->lang['lang_already_voted']."');</script>";
  97. }
  98.  
  99. $row = $this->model->get_votes($id, $type_for);
  100. echo $row['vote'];
  101.  
  102. exit();
  103.  
  104. }
  105. //Print comments to users
  106. $results=[];//globalize
  107. $get_comm = $this->db->query("SELECT * FROM ".$this->argos_db_prefix."replies order by id DESC");
  108. if ($get_comm->rowCount() > 0) {
  109. while ($row = $get_comm->fetch(PDO::FETCH_ASSOC)) {
  110. $comment_id = $row['id'];
  111. $comment_votes = $row['vote'];
  112. $comment_author = $row['author'];
  113. \App\Entity\Emoji\Emojione::$imageType = 'png'; // or svg / png is default
  114. \App\Entity\Emoji\Emojione::$imagePathPNG = 'https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.6/assets/png/'; // defaults to jsdelivr's free CDN
  115. $comment_text=\App\Entity\Emoji\Emojione::toImage($row['text']);
  116. $comment_date = date('d.m.y :: h:i', $row['date']);
  117. $comment_ava = $row['avatar'];
  118. $comment_nick_color = $row['nick_colour'];
  119. if (empty($comment_nick_color )) {
  120. $comment_nick_color = "FF5733"; //default user color
  121. }
  122. $comment_userid = $row['user_id'];
  123. $comment_reply_id = $row['comment_reply_id'];
  124.  
  125. $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];
  126. }
  127. echo json_encode($results, JSON_PRETTY_PRINT); //print array with results
  128. }
  129.  
  130.  
  131. }
  132. }
  133.  
  134. public function comment_replies_lang() {
  135. //language definitions
  136.  
  137. if (get_current_language() == 'bg') {
  138. $this->lang= array_merge($this->lang, [
  139. 'ext_comment_replies_explain1'=>'Формата е настроена за отговор на коментар',
  140. 'ext_comment_replies_explain2'=>'Моля',
  141. 'ext_comment_replies_explain3'=>'рефрешнете',
  142. 'ext_commment_replies_explain4'=>'страницата, за да напишете коментар към новината.',
  143. 'ext_comments_replies_loading'=>'Зареждаме коментарите...',
  144. ]);
  145. }
  146. if (get_current_language() == 'en') {
  147. $this->lang= array_merge($this->lang, [
  148. 'ext_comment_replies_explain1'=>'The form is set up to respond to a comment',
  149. 'ext_comment_replies_explain2'=>'Please',
  150. 'ext_comment_replies_explain3'=>'refresh',
  151. 'ext_commment_replies_explain4'=>'the page to write a comment to the news.',
  152. 'ext_comments_replies_loading'=>'Loading comments...',
  153. ]);
  154. }
  155. if (get_current_language() == 'ru') {
  156. $this->lang= array_merge($this->lang, [
  157. 'ext_comment_replies_explain1'=>'Форма настроена для ответа на комментарий',
  158. 'ext_comment_replies_explain2'=>'Пожалуйста',
  159. 'ext_comment_replies_explain3'=>'обновите',
  160. 'ext_commment_replies_explain4'=>'страницу, чтобы написать комментарий к новости.',
  161. 'ext_comments_replies_loading'=>'Загрузка комментариев...',
  162.  
  163. ]);
  164. }
  165. if (get_current_language() == 'es') {
  166. $this->lang= array_merge($this->lang, [
  167. 'ext_comment_replies_explain1'=>'El formulario está configurado para responder a un comentario',
  168. 'ext_comment_replies_explain2'=>'Por favor',
  169. 'ext_comment_replies_explain3'=>'actualizar',
  170. 'ext_commment_replies_explain4'=>'página para escribir un comentario a la noticia.',
  171. 'ext_comments_replies_loading'=>'Cargando comentarios...',
  172. ]);
  173. }
  174. }
  175.  
  176. /**
  177. *
  178. */
  179. public function load() {
  180.  
  181.  
  182. $js = '';//globalize
  183. if (strpos($_SERVER['REQUEST_URI'], 'topic_')) {
  184. $js = '
  185. //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
  186. $.LoadingOverlay("show", {
  187. text : "'.$this->lang['ext_comments_replies_loading'].'"
  188. });
  189.  
  190. $(".comments_block,.media-content").append("<div class=\"btn btn-sm btn-default pull-right reply_to\">reply</div><div class=\"clear clearfix\"></div>");
  191.  
  192. var news_id = ""; //globalize, holds news id to pass in replies for db
  193. var comment_reply_id = "";//globalize, holds comment id
  194. $(".reply_to").click(function(e) {
  195. //catch reply id
  196. comment_reply_id = $(this).closest(".comment_aid").attr("data-comment-aid"); //current comment id to reply
  197. var news_title = location.pathname.substr(1); //to catch news id from this
  198.  
  199. //make ajax call to find news id by news_title
  200. $.ajax({
  201. url: "/ajax/ext/pok4/comment_replies",
  202. type: "POST",
  203. data: {title: news_title},
  204. success: function(data){
  205. news_id = data; //news id
  206. //alert(news_id); //debug
  207. }
  208. }); //end ajax call
  209.  
  210. //scroll to textarea
  211. $("html, body").animate({
  212. scrollTop: $("textarea[name=\"com_text\"]").parent().offset().top - ($(window).height() - $("textarea[name=\"com_text\"]").outerHeight(true)) / 2
  213. }, 1500);
  214.  
  215. //change submit form with different parameters which will hold the replies
  216. 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>";
  217.  
  218. if ($(".replying_to").length < 1 ) {
  219. $("textarea[name=\"com_text\"]").parent().before(print_string);
  220. $( "input[name=\"submit_comm\"]" ).addClass( "submit_reply" ); //add different class to submit button to catch ajax call below
  221. } else {
  222. $(".replying_to").html( print_string );
  223. }
  224.  
  225.  
  226.  
  227. });
  228.  
  229. //form change and ajax submit
  230. $( "body" ).on( "click", ".submit_reply", function(event) {
  231. var emojionestrip = $("textarea[name=com_text]").emojioneArea();
  232. var text = emojionestrip[0].emojioneArea.getText();
  233.  
  234. //remove submit button for prevent double content
  235. $(this).hide();
  236.  
  237. event.preventDefault(); //prevent real submit
  238.  
  239. $.ajax({
  240. url: "/ajax/ext/pok4/comment_replies",
  241. type: "POST",
  242. async: false,
  243. data: {news_text:text, newsid:news_id, commentreply_id:comment_reply_id},
  244. success: function(data){
  245. $("textarea[name=\"com_text\"]").parent().after("<div class=\"alert alert-success\">'.$this->lang['lang_success'].'</div>");
  246. location.reload();
  247. }
  248. }); //end ajax call
  249.  
  250. return false;
  251.  
  252. });
  253.  
  254. //load comments via ajax
  255. var comment_data_id = 0; //globalize
  256. $.ajax({
  257. url: "/ajax/ext/pok4/comment_replies",
  258. type: "POST",
  259. contentType: "application/json; charset=utf-8",
  260. cache: false,
  261. dataType: "json",
  262. success: function(result){
  263. comment_data_id = result[0]["comment_id"];
  264.  
  265. if( comment_data_id ) {
  266.  
  267. var clone_comment_div = ""; //clone div with comments
  268. var string_with_comment_id = "";//globalize, hold the specific string for replies
  269. var reply_id = 0;//globalize, hold the id from replies table for every user
  270. var avatar_changes= "";//globalize, holds the user avatar
  271. var author_date_vote = ""; //holds date/vote/author
  272. var new_string = "";//holds the new html for .name class
  273. for (var i=0; i < result.length; i++) {
  274. //console.log(result[i])
  275. reply_id = result[i]["comment_id"];
  276.  
  277. //manipulate the div which we copy
  278. string_with_comment_id="div[data-comment-aid=" +result[i]["comment_reply_id"]+ "]";
  279. 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();
  280. $("body, html").find(string_with_comment_id).after(clone_comment_div).end();
  281.  
  282. //change avatar for each post author
  283. avatar_changes = "div[data-replyid=" +reply_id+ "]";
  284. $("body, html").find(avatar_changes).find(".author-avatar .ava_news, .media-left").html(result[i]["comment_ava"]).end();
  285.  
  286. //change date, author up/down vote
  287. author_date_vote = "div[data-replyid=" +reply_id+ "]";
  288. new_string = "<a href=\"forum/memberlist.php?mode=viewprofile&u=" +result[i]["comment_userid"]+ "\" style=\"color:#"+ result[i]["comment_nick_color"] +" !important\">&nbsp;" + 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>";
  289. console.log(new_string);
  290. $("body, html").find(author_date_vote).find(".name").html(new_string).end();
  291.  
  292. //remove order button if order by comments ext is enabled (hot fix)
  293. $(".order_by_c").not(":first").remove();
  294.  
  295. //remove reply_to button in replies
  296. $(".reply_comment .reply_to").hide();
  297. }
  298. $.LoadingOverlay("hide"); //hide overlay (we success)
  299. }
  300. }
  301. }).fail(function (data) {
  302. $.LoadingOverlay("hide"); //hide overlay (we fail)
  303. }); //end ajax call
  304.  
  305. //check if user has voting on replies
  306. $( "body" ).on( "click", ".vote-btn_r", function(event) {
  307. var id = $(this).attr("data-my");
  308. var name = $(this).attr("data-vote");
  309. var dataString = "id="+ id ;
  310. var parent = $("#bid-"+id);
  311.  
  312. if(name=="upvote")
  313. {
  314.  
  315. $(this).fadeIn(200).html("");
  316. $.ajax({
  317. type: "POST",
  318. url: "/ajax/ext/pok4/comment_replies/?for=replies&vote=up",
  319. data: dataString,
  320. cache: false,
  321. success: function(html)
  322. {
  323. parent.html(html);
  324.  
  325. }
  326. });
  327.  
  328. }
  329. else
  330. {
  331. $(this).fadeIn(200).html("");
  332. $.ajax({
  333. type: "POST",
  334. url: "/ajax/ext/pok4/comment_replies/?for=replies&vote=down",
  335. data: dataString,
  336. cache: false,
  337. success: function(html)
  338. {
  339. parent.html(html);
  340. }
  341. });
  342. }
  343.  
  344. return false;
  345. });
  346. //end ajax vote
  347. ';
  348. }
  349. $this->dispatcher->emit('core_event_head_append', ['<link rel="stylesheet" href="ext/pok4/comment_replies/css/style.css">']);
  350. $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>']);
  351. $this->dispatcher->emit('core_event_inside_head_ready_front', [$js]);
  352. }
  353.  
  354.  
  355. };
  356.  
  357.  
  358. $load_ext = new comment_replies;
  359. $load_ext->load();
  360.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement