XOOPS  2.6.0
helper.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
13 
22 {
28  public function init()
29  {
30  $this->setDirname('comments');
31  $this->loadLanguage('main');
32  define('COMMENTS_APPROVENONE', 0);
33  define('COMMENTS_APPROVEALL', 1);
34  define('COMMENTS_APPROVEUSER', 2);
35  define('COMMENTS_APPROVEADMIN', 3);
36  define('COMMENTS_PENDING', 1);
37  define('COMMENTS_ACTIVE', 2);
38  define('COMMENTS_HIDDEN', 3);
39  define('COMMENTS_OLD1ST', 0);
40  define('COMMENTS_NEW1ST', 1);
41  }
42 
46  public static function getInstance()
47  {
48  return parent::getInstance();
49  }
50 
54  public function getHandlerComment()
55  {
56  return $this->getHandler('comment');
57  }
58 
64  public function getUserConfig($config)
65  {
66  static $configs = array();
67  static $fetched = false;
68  /* @var $helper Userconfigs */
69  if (!$fetched && $this->xoops()->isUser() && $helper = $this->xoops()->getModuleHelper('userconfigs')) {
70  $config_handler = $helper->getHandlerConfig();
71  $configs = $config_handler->getConfigsByUser($this->xoops()->user->getVar('uid'), $this->getModule()->getVar('mid'));
72  }
73  $fetched = true;
74  return isset($configs[$config]) ? $configs[$config] : $this->getConfig($config);
75  }
76 
80  public function displayCommentForm(CommentsComment $obj)
81  {
82  $this->getForm($obj, 'comment')->display();
83  }
84 
85  public function displayNew()
86  {
88  /* @var $obj CommentsComment */
89  $obj = $this->getHandlerComment()->create();
90 
91  $itemid = Request::getInt('com_itemid');
92  $modid = Request::getInt('com_modid');
93 
94  if (empty($modid)) {
96  }
97 
98  $module = $xoops->getModuleById($modid);
99  if (!is_object($module)) {
101  }
102 
103  if ((!$xoops->isAdminSide && COMMENTS_APPROVENONE == $xoops->getModuleConfig('com_rule', $module->getVar('dirname'))) || (!$xoops->isUser() && !$xoops->getModuleConfig('com_anonpost', $module->getVar('dirname'))) || !$xoops->isModule()) {
105  }
106  /* @var $plugin CommentsPluginInterface */
107  if (($plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'comments')) && $itemid > 0 && $modid > 0) {
108  $xoops->header();
109  $title = '';
110  $text = '';
111  $uid = 0;
112  $timestamp = 0;
113  if (is_array($itemInfo = $plugin->itemInfo($itemid))) {
114  $title = isset($itemInfo['title']) ? $itemInfo['title'] : $title;
115  $text = isset($itemInfo['text']) ? $itemInfo['text'] : $text;
116  $uid = isset($itemInfo['uid']) ? $itemInfo['uid'] : $uid;
117  $timestamp = isset($itemInfo['timestamp']) ? $itemInfo['timestamp'] : $timestamp;
118  }
119 
120  echo $this->renderHeader($title, $text, $uid, $timestamp);
121 
122  if (!preg_match("/^" . XoopsLocale::C_RE . "/i", $title)) {
123  $title = XoopsLocale::C_RE . " " . XoopsLocale::substr($title, 0, 56);
124  }
125 
126  $obj->setVar('itemid', $itemid);
127  $obj->setVar('title', $title);
128  $obj->setVar('modid', $modid);
129 
130  $this->displayCommentForm($obj);
131  $xoops->footer();
132  }
134  }
135 
136  public function displayPost()
137  {
139  if (Request::getMethod()!=='POST') {
141  }
142  $id = Request::getInt('com_id');
143  $modid = Request::getInt('com_modid');
144  if (empty($modid)) {
146  }
147 
148  /* @var $comment CommentsComment */
149  $comment = $this->getHandlerComment()->get($id);
150  if (!is_object($comment)) {
152  }
153 
154  if (!$comment->isNew()) {
155  $modid = $comment->getVar('modid');
156  } else {
157  $comment->setVar('modid', $modid);
158  }
159 
160  $module = $xoops->getModuleById($modid);
161  if (!is_object($module)) {
163  }
164 
165  $moddir = $module->getVar('dirname');
166 
167  if ($xoops->isAdminSide) {
168  if (empty($id)) {
170  }
171  $redirect_page = $this->url('admin/main.php?com_modid=' . $modid . '&amp;com_itemid');
172  } else {
173  if (COMMENTS_APPROVENONE == $xoops->getModuleConfig('com_rule', $module->getVar('dirname'))) {
175  }
176  $redirect_page = '';
177  }
178 
179  /* @var $plugin CommentsPluginInterface */
180  if ($plugin = \Xoops\Module\Plugin::getPlugin($moddir, 'comments')) {
181  if (!$xoops->isAdminSide) {
182  $redirect_page = $xoops->url('modules/' . $moddir . '/' . $plugin->pageName() . '?');
183  if (is_array($extraParams = $plugin->extraParams())) {
184  $extra_params = '';
185  foreach ($extraParams as $extra_param) {
186  $extra_params .= isset($_POST[$extra_param]) ? $extra_param . '=' . htmlspecialchars($_POST[$extra_param]) . '&amp;' : $extra_param . '=amp;';
187  }
188  $redirect_page .= $extra_params;
189  }
190  $redirect_page .= $plugin->itemName();
191  }
192  $comment_url = $redirect_page;
193 
194  $op = Request::getBool('com_dopost') ? 'post' : '';
195  $op = Request::getBool('com_dopreview') ? 'preview' : $op;
196  $op = Request::getBool('com_dodelete') ? 'delete' : $op;
197 
198  if ($op == 'preview' || $op == 'post') {
199  if (!$xoops->security()->check()) {
200  $op = '';
201  }
202  }
203  if ($op == 'post' && !$xoops->isUser()) {
204  $xoopsCaptcha = XoopsCaptcha::getInstance();
205  if (!$xoopsCaptcha->verify()) {
206  $captcha_message = $xoopsCaptcha->getMessage();
207  $op = 'preview';
208  }
209  }
210 
211  $title = XoopsLocale::trim(Request::getString('com_title'));
212  $text = XoopsLocale::trim(Request::getString('com_text'));
213  $mode = XoopsLocale::trim(Request::getString('com_mode', 'flat'));
214  $order = XoopsLocale::trim(Request::getString('com_order', COMMENTS_OLD1ST));
215  $itemid = Request::getInt('com_itemid');
216  $pid = Request::getInt('com_pid');
217  $rootid = Request::getInt('com_rootid');
218  $status = Request::getInt('com_status');
219  $dosmiley = Request::getBool('com_dosmiley');
220  $doxcode = Request::getBool('com_doxcode');
221  $dobr = Request::getBool('com_dobr');
222  $dohtml = Request::getBool('com_html');
223  $doimage = Request::getBool('com_doimage');
224  $icon = XoopsLocale::trim(Request::getString('com_icon'));
225 
226  $comment->setVar('title', $title);
227  $comment->setVar('text', $text);
228  $comment->setVar('itemid', $itemid);
229  $comment->setVar('pid', $pid);
230  $comment->setVar('rootid', $rootid);
231  $comment->setVar('status', $status);
232  $comment->setVar('dosmiley', $dosmiley);
233  $comment->setVar('doxcode', $doxcode);
234  $comment->setVar('dobr', $dobr);
235  $comment->setVar('dohtml', $dohtml);
236  $comment->setVar('doimage', $doimage);
237  $comment->setVar('icon', $icon);
238 
239  switch ($op) {
240  case "delete":
241  $this->displayDelete();
242  break;
243 
244  case "preview":
245  $comment->setVar('doimage', 1);
246  if ($comment->getVar('dohtml') != 0) {
247  if ($xoops->isUser()) {
248  if (!$xoops->user->isAdmin($comment->getVar('modid'))) {
249  $comment->setVar('dohtml', 0);
250  }
251  } else {
252  $comment->setVar('dohtml', 0);
253  }
254  }
255 
256  $xoops->header();
257  if (!$xoops->isAdminSide && !empty($captcha_message)) {
258  echo $xoops->alert('error', $captcha_message);
259  }
260  echo $this->renderHeader($comment->getVar('title', 'p'), $comment->getVar('text', 'p'), false, time());
261  $this->displayCommentForm($comment);
262  $xoops->footer();
263  break;
264 
265  case "post":
266  $comment->setVar('doimage', 1);
268  $add_userpost = false;
269  $call_approvefunc = false;
270  $call_updatefunc = false;
271  // RMV-NOTIFY - this can be set to 'comment' or 'comment_submit'
272  $notify_event = false;
273  if (!empty($id)) {
274  $accesserror = false;
275 
276  if ($xoops->isUser()) {
277  if ($xoops->user->isAdmin($comment->getVar('modid'))) {
278  if (!empty($status) && $status != COMMENTS_PENDING) {
279  $old_status = $comment->getVar('status');
280  $comment->setVar('status', $status);
281  // if changing status from pending state, increment user post
282  if (COMMENTS_PENDING == $old_status) {
283  $add_userpost = true;
284  if (COMMENTS_ACTIVE == $status) {
285  $call_updatefunc = true;
286  $call_approvefunc = true;
287  // RMV-NOTIFY
288  $notify_event = 'comment';
289  }
290  } else {
291  if (COMMENTS_HIDDEN == $old_status && COMMENTS_ACTIVE == $status) {
292  $call_updatefunc = true;
293  // Comments can not be directly posted hidden,
294  // no need to send notification here
295  } else {
296  if (COMMENTS_ACTIVE == $old_status && COMMENTS_HIDDEN == $status) {
297  $call_updatefunc = true;
298  }
299  }
300  }
301  }
302  } else {
303  $comment->setVar('dohtml', 0);
304  if ($comment->getVar('uid') != $xoops->user->getVar('uid')) {
305  $accesserror = true;
306  }
307  }
308  } else {
309  $comment->setVar('dohtml', 0);
310  $accesserror = true;
311  }
312  if (false != $accesserror) {
313  $xoops->redirect($redirect_page . '=' . $comment->getVar('itemid') . '&amp;com_id=' . $comment->getVar('id') . '&amp;com_mode=' . $mode . '&amp;com_order=' . $order, 1, XoopsLocale::E_NO_ACCESS_PERMISSION);
314  }
315  } else {
316  $comment->setVar('created', time());
317  $comment->setVar('ip', $xoops->getEnv('REMOTE_ADDR'));
318  if ($xoops->isUser()) {
319  if ($xoops->user->isAdmin($comment->getVar('modid'))) {
320  $comment->setVar('status', COMMENTS_ACTIVE);
321  $add_userpost = true;
322  $call_approvefunc = true;
323  $call_updatefunc = true;
324  // RMV-NOTIFY
325  $notify_event = 'comment';
326  } else {
327  $comment->setVar('dohtml', 0);
328  switch ($xoops->getModuleConfig('com_rule')) {
329  case COMMENTS_APPROVEALL:
330  case COMMENTS_APPROVEUSER:
331  $comment->setVar('status', COMMENTS_ACTIVE);
332  $add_userpost = true;
333  $call_approvefunc = true;
334  $call_updatefunc = true;
335  // RMV-NOTIFY
336  $notify_event = 'comment';
337  break;
338  case COMMENTS_APPROVEADMIN:
339  default:
340  $comment->setVar('status', COMMENTS_PENDING);
341  $notify_event = 'comment_submit';
342  break;
343  }
344  }
345  if ($xoops->getModuleConfig('com_anonpost', $module->getVar('dirname')) && $comment->getVar('noname')) {
346  $comment->setVar('uid', 0);
347  } else {
348  $comment->setVar('uid', $xoops->user->getVar('uid'));
349  }
350  } else {
351  $comment->setVar('dohtml', 0);
352  $comment->setVar('uid', 0);
353  if ($xoops->getModuleConfig('com_anonpost', $module->getVar('dirname')) != 1) {
354  $xoops->redirect($redirect_page . '=' . $comment->getVar('itemid') . '&amp;com_id=' . $comment->getVar('id') . '&amp;com_mode=' . $mode . '&amp;com_order=' . $order, 1, XoopsLocale::E_NO_ACCESS_PERMISSION);
355  }
356  }
357  if ($comment->getVar('uid') == 0) {
358  switch ($xoops->getModuleConfig('com_rule')) {
359  case COMMENTS_APPROVEALL:
360  $comment->setVar('status', COMMENTS_ACTIVE);
361  $add_userpost = true;
362  $call_approvefunc = true;
363  $call_updatefunc = true;
364  // RMV-NOTIFY
365  $notify_event = 'comment';
366  break;
367  case COMMENTS_APPROVEADMIN:
368  case COMMENTS_APPROVEUSER:
369  default:
370  $comment->setVar('status', COMMENTS_PENDING);
371  // RMV-NOTIFY
372  $notify_event = 'comment_submit';
373  break;
374  }
375  }
376  }
377  if ($comment->getVar('title') == '') {
378  $comment->setVar('title', XoopsLocale::NO_TITLE);
379  }
380  $comment->setVar('modified', time());
381  if (isset($extra_params)) {
382  $comment->setVar('exparams', $extra_params);
383  }
384 
385  if (false != $comment_handler->insert($comment)) {
386  $newcid = $comment->getVar('id');
387  // set own id as root id if this is a top comment
388  if ($comment->getVar('rootid') == 0) {
389  $comment->setVar('rootid', $newcid);
390  if (!$comment_handler->updateByField($comment, 'rootid', $comment->getVar('rootid'))) {
391  $comment_handler->delete($comment);
392  $xoops->header();
393  echo $xoops->alert('error', $comment->getHtmlErrors());
394  $xoops->footer();
395  }
396  }
397  // call custom approve function if any
398  if (false != $call_approvefunc) {
399  $plugin->approve($comment);
400  }
401 
402  if (false != $call_updatefunc) {
403  $criteria = new CriteriaCompo(new Criteria('modid', $comment->getVar('modid')));
404  $criteria->add(new Criteria('itemid', $comment->getVar('itemid')));
405  $criteria->add(new Criteria('status', COMMENTS_ACTIVE));
406  $comment_count = $comment_handler->getCount($criteria);
407  $plugin->update($comment->getVar('itemid'), $comment_count);
408  }
409 
410  // increment user post if needed
411  $uid = $comment->getVar('uid');
412  if ($uid > 0 && false != $add_userpost) {
413  $member_handler = $xoops->getHandlerMember();
414  $poster = $member_handler->getUser($uid);
415  if ($poster instanceof XoopsUser) {
416  $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') + 1);
417  }
418  }
419 
420  // RMV-NOTIFY
421  // trigger notification event if necessary
422  if ($notify_event && $xoops->isActiveModule('notifications')) {
423  $notifications = Notifications::getInstance();
424  $not_modid = $comment->getVar('modid');
425  $not_catinfo = $notifications->getCommentsCategory($module->getVar('dirname'));
426  $not_category = $not_catinfo['name'];
427  $not_itemid = $comment->getVar('itemid');
428  $not_event = $notify_event;
429  // Build an ABSOLUTE URL to view the comment. Make sure we
430  // point to a viewable page (i.e. not the system administration
431  // module).
432  $comment_tags = array();
433  $comment_tags['X_COMMENT_URL'] = $comment_url . '=' . $comment->getVar('itemid') . '&amp;com_id=' . $comment->getVar('id') . '&amp;com_rootid=' . $comment->getVar('rootid') . '&amp;com_mode=' . $mode . '&amp;com_order=' . $order . '#comment' . $comment->getVar('id');
434 
435  if ($xoops->isActiveModule('notifications')) {
436  Notifications::getInstance()->getHandlerNotification()->triggerEvent($not_category, $not_itemid, $not_event, $comment_tags, false, $not_modid);
437  }
438  }
439  if (!isset($comment_post_results)) {
440  // if the comment is active, redirect to posted comment
441  if ($comment->getVar('status') == COMMENTS_ACTIVE) {
442  $xoops->redirect($redirect_page . '=' . $comment->getVar('itemid') . '&amp;com_id=' . $comment->getVar('id') . '&amp;com_rootid=' . $comment->getVar('rootid') . '&amp;com_mode=' . $mode . '&amp;com_order=' . $order . '#comment' . $comment->getVar('id'), 1, _MD_COMMENTS_THANKSPOST);
443  } else {
444  // not active, so redirect to top comment page
445  $xoops->redirect($redirect_page . '=' . $comment->getVar('itemid') . '&amp;com_mode=' . $mode . '&amp;com_order=' . $order . '#comment' . $comment->getVar('id'), 1, _MD_COMMENTS_THANKSPOST);
446  }
447  }
448  } else {
449  if (!isset($purge_comment_post_results)) {
450  $xoops->header();
451  echo $xoops->alert('error', $comment->getHtmlErrors());
452  $xoops->footer();
453  } else {
454  $comment_post_results = $comment->getErrors();
455  }
456  }
457  break;
458  default:
459  $xoops->redirect(\XoopsBaseConfig::get('url') . '/', 1, implode('<br />', $xoops->security()->getErrors()));
460  break;
461  }
462  }
463  }
464 
465  public function displayReply()
466  {
468 
469  $modid = Request::getInt('com_modid', 0);
470 
471  if (empty($modid)) {
473  }
474 
475  $module = $xoops->getModuleById($modid);
476  if (!is_object($module)) {
478  }
479 
480  if ((!$xoops->isAdminSide && COMMENTS_APPROVENONE == $xoops->getModuleConfig('com_rule', $module->getVar('dirname'))) || (!$xoops->isUser() && !$xoops->getModuleConfig('com_anonpost', $module->getVar('dirname'))) || !$xoops->isModule()) {
482  }
483 
484  //Original comment
485  $comment = $this->getHandlerComment()->get(Request::getInt('com_id', 0));
486 
487  /* @var $reply CommentsComment */
488  $reply = $this->getHandlerComment()->create();
489 
490  $title = $comment->getVar('title', 'e');
491  if (!preg_match("/^" . XoopsLocale::C_RE . "/i", $title)) {
492  $title = XoopsLocale::C_RE . " " . XoopsLocale::substr($title, 0, 56);
493  }
494  $reply->setVar('title', $title);
495  $reply->setVar('modid', $comment->getVar('modid'));
496  $reply->setVar('pid', $comment->getVar('id'));
497  $reply->setVar('rootid', $comment->getVar('rootid'));
498  $reply->setVar('itemid', $comment->getVar('itemid'));
499 
500  $xoops->header();
501  echo $this->renderHeader($comment->getVar('title'), $comment->getVar('text'), $comment->getVar('uid'), $comment->getVar('created'));
502  $this->displayCommentForm($reply);
503  $xoops->footer();
504  }
505 
506  public function renderHeader($title, $text, $uid, $timestamp)
507  {
508  $ret = '<table cellpadding="4" cellspacing="1" width="98%" class="outer">
509  <tr><td class="head">' . $title . '</td></tr><tr><td><br />';
510  if ($uid) {
511  $ret .= _MD_COMMENTS_POSTER . ': <strong>' . XoopsUser::getUnameFromId($uid) . '</strong>&nbsp;&nbsp;';
512  }
513  $ret .= _MD_COMMENTS_POSTED . ': <strong>' . XoopsLocale::formatTimestamp($timestamp) . '</strong><br /><br />' . $text . '<br /></td></tr>';
514  $ret .= '</table>';
515  return $ret;
516  }
517 
518  public function renderView()
519  {
521  /* @var $plugin CommentsPluginInterface */
522  if ($xoops->isModule() && $plugin = \Xoops\Module\Plugin::getPlugin($xoops->module->getVar('dirname'), 'comments')) {
523  if (COMMENTS_APPROVENONE != $xoops->getModuleConfig('com_rule')) {
524  $xoops->tpl()->assign('xoops_iscommentadmin', $this->isUserAdmin());
525 
526  $itemid = (trim($plugin->itemName()) != '' && isset($_GET[$plugin->itemName()])) ? intval($_GET[$plugin->itemName()]) : 0;
527  if ($itemid > 0) {
528  $modid = $xoops->module->getVar('mid');
529  $mode = Request::getString('com_mode', $this->getUserConfig('com_mode'));
530  $xoops->tpl()->assign('comment_mode', $mode);
531 
532  $order = Request::getInt('com_order', $this->getUserConfig('com_order'));
533  if ($order != COMMENTS_OLD1ST) {
534  $xoops->tpl()->assign(array(
535  'comment_order' => COMMENTS_NEW1ST,
536  'order_other' => COMMENTS_OLD1ST
537  ));
538  $dborder = 'DESC';
539  } else {
540  $xoops->tpl()->assign(array(
541  'comment_order' => COMMENTS_OLD1ST,
542  'order_other' => COMMENTS_NEW1ST
543  ));
544  $dborder = 'ASC';
545  }
546  // admins can view all comments and IPs, others can only view approved(active) comments
547  if ($xoops->isUser() && $xoops->user->isAdmin($xoops->module->getVar('mid'))) {
548  $admin_view = true;
549  } else {
550  $admin_view = false;
551  }
552 
553  $id = Request::getInt('com_id', 0);
554  $rootid = Request::getInt('com_rootid', 0);
555 
557  if ($mode == 'flat') {
558  $comments = $comment_handler->getByItemId($xoops->module->getVar('mid'), $itemid, $dborder);
559  $renderer = CommentsCommentRenderer::getInstance($xoops->tpl());
560  $renderer->setComments($comments);
561  $renderer->renderFlatView($admin_view);
562  } elseif ($mode == 'thread') {
563  // RMV-FIX... added extraParam stuff here
564  $comment_url = $plugin->pageName() . '?';
565  if (is_array($extraParams = $plugin->extraParams())) {
566  $extra_params = '';
567  foreach ($extraParams as $extra_param) {
568  // This page is included in the module hosting page -- param could be from anywhere
569 
570  if (isset($_POST[$extra_param])) {
571  $extra_params .= $extra_param . '=' . $_POST[$extra_param] . '&amp;';
572  } else {
573  if (isset($_GET[$extra_param])) {
574  $extra_params .= $extra_param . '=' . $_GET[$extra_param] . '&amp;';
575  } else {
576  $extra_params .= $extra_param . '=&amp;';
577  }
578  }
579  }
580  $comment_url .= $extra_params;
581  }
582  $xoops->tpl()->assign('comment_url', $comment_url . $plugin->itemName() . '=' . $itemid . '&amp;com_mode=thread&amp;com_order=' . $order);
583  if (!empty($id) && !empty($rootid) && ($id != $rootid)) {
584  // Show specific thread tree
585  $comments = $comment_handler->getThread($rootid, $id);
586  if (false != $comments) {
587  $renderer = CommentsCommentRenderer::getInstance($xoops->tpl());
588  $renderer->setComments($comments);
589  $renderer->renderThreadView($id, $admin_view);
590  }
591  } else {
592  // Show all threads
593  $top_comments = $comment_handler->getTopComments($xoops->module->getVar('mid'), $itemid, $dborder);
594  $c_count = count($top_comments);
595  if ($c_count > 0) {
596  for ($i = 0; $i < $c_count; ++$i) {
597  $comments = $comment_handler->getThread($top_comments[$i]->getVar('rootid'), $top_comments[$i]->getVar('id'));
598  if (false != $comments) {
599  $renderer = CommentsCommentRenderer::getInstance($xoops->tpl());
600  $renderer->setComments($comments);
601  $renderer->renderThreadView($top_comments[$i]->getVar('id'), $admin_view);
602  }
603  unset($comments);
604  }
605  }
606  }
607  } else {
608  // Show all threads
609  $top_comments = $comment_handler->getTopComments($xoops->module->getVar('mid'), $itemid, $dborder);
610  $c_count = count($top_comments);
611  if ($c_count > 0) {
612  for ($i = 0; $i < $c_count; ++$i) {
613  $comments = $comment_handler->getThread($top_comments[$i]->getVar('rootid'), $top_comments[$i]->getVar('id'));
614  $renderer = CommentsCommentRenderer::getInstance($xoops->tpl());
615  $renderer->setComments($comments);
616  $renderer->renderNestView($top_comments[$i]->getVar('id'), $admin_view);
617  }
618  }
619  }
620  // assign comment nav bar
621  $xoops->tpl()->assign('page_name', $plugin->pageName());
622  $xoops->tpl()->assign('order', $order);
623  $xoops->tpl()->assign('COMMENTS_OLD1ST', COMMENTS_OLD1ST);
624  $xoops->tpl()->assign('COMMENTS_NEW1ST', COMMENTS_NEW1ST);
625  $xoops->tpl()->assign('itemid', $itemid);
626  $xoops->tpl()->assign('item_name', $plugin->itemName());
627  unset($postcomment_link);
628  if ($xoops->getModuleConfig('com_anonpost') || $xoops->isUser()) {
629  $postcomment_link = $this->url('comment_new.php?com_modid=' . $modid . '&amp;com_itemid=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode);
630  $xoops->tpl()->assign('anon_canpost', true);
631  }
632  $link_extra = '';
633  if (is_array($extraParams = $plugin->extraParams())) {
634  foreach ($extraParams as $extra_param) {
635  if (isset($_POST[$extra_param])) {
636  $extra_param_val = $_POST[$extra_param];
637  } else {
638  if (isset($_GET[$extra_param])) {
639  $extra_param_val = $_GET[$extra_param];
640  }
641  }
642  if (isset($extra_param_val)) {
643  $link_extra .= '&amp;' . $extra_param . '=' . $extra_param_val;
644  $hidden_value = htmlspecialchars($extra_param_val, ENT_QUOTES);
645  $xoops->tpl()->assign('extra_param', $extra_param);
646  $xoops->tpl()->assign('hidden_value', $hidden_value);
647  }
648  }
649  }
650  if (isset($postcomment_link)) {
651  $xoops->tpl()->assign('postcomment_link', $postcomment_link);
652  $xoops->tpl()->assign('link_extra', $link_extra);
653  }
654  $xoops->tpl()->assign(array(
655  'comments_editlink' => $this->url('comment_edit.php?com_modid=' . $modid . '&amp;com_itemid=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode . '' . $link_extra),
656  'comments_deletelink' => $this->url('comment_delete.php?com_modid=' . $modid . '&amp;com_itemid=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode . '' . $link_extra),
657  'comments_replylink' => $this->url('comment_reply.php?com_modid=' . $modid . '&amp;com_itemid=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode . '' . $link_extra)
658  ));
659 
660  // assign some lang variables
661  $xoops->tpl()->assign(array(
662  'comments_lang_from' => _MD_COMMENTS_FROM,
663  'comments_lang_joined' => _MD_COMMENTS_JOINED,
664  'comments_lang_posts' => _MD_COMMENTS_POSTS,
665  'comments_lang_poster' => _MD_COMMENTS_POSTER,
666  'comments_lang_thread' => _MD_COMMENTS_THREAD,
667  'comments_lang_edit' => XoopsLocale::A_EDIT,
668  'comments_lang_delete' => XoopsLocale::A_DELETE,
669  'comments_lang_reply' => XoopsLocale::A_REPLY,
670  'comments_lang_subject' => _MD_COMMENTS_REPLIES,
671  'comments_lang_posted' => _MD_COMMENTS_POSTED,
672  'comments_lang_updated' => _MD_COMMENTS_UPDATED,
673  'comments_lang_notice' => _MD_COMMENTS_NOTICE
674  ));
675  }
676  }
677  }
678  }
679 
680  public function displayEdit()
681  {
683 
684  /* @var $comment CommentsComment */
685  $comment = $this->getHandlerComment()->get(Request::getInt('com_id'));
686  if (!is_object($comment)) {
688  }
689  $module = $xoops->getModuleById($comment->getVar('modid'));
690  if (!is_object($module)) {
692  }
693 
694  if ((!$xoops->isAdminSide && COMMENTS_APPROVENONE == $xoops->getModuleConfig('com_rule', $module->getVar('dirname'))) || (!$xoops->isUser() && !$xoops->getModuleConfig('com_anonpost', $module->getVar('dirname'))) || !$xoops->isModule()) {
696  }
697 
698  /* @var $plugin CommentsPluginInterface */
699  if ($plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'comments')) {
700  $xoops->header();
701  $this->displayCommentForm($comment);
702  $xoops->footer();
703  }
705  }
706 
707  public function displayDelete()
708  {
710  $op = Request::getCmd('op', 'delete', 'POST');
711  $mode = Request::getString('com_mode', 'flat');
712  $order = Request::getString('com_order', COMMENTS_OLD1ST);
713  $id = Request::getInt('com_id');
714 
715  /* @var $comment CommentsComment */
716  /* @var $comment_handler CommentsCommentHandler */
718  $comment = $comment_handler->get($id);
719  if (!is_object($comment)) {
721  }
722  $module = $xoops->getModuleById($comment->getVar('modid'));
723  if (!is_object($module)) {
725  }
726 
727  if ((!$xoops->isAdminSide && COMMENTS_APPROVENONE == $xoops->getModuleConfig('com_rule', $module->getVar('dirname'))) || (!$xoops->isUser() && !$xoops->getModuleConfig('com_anonpost', $module->getVar('dirname'))) || !$xoops->isModule()) {
729  }
730 
731  $modid = $module->getVar('mid');
732  /* @var $plugin CommentsPluginInterface */
733  if ($plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'comments')) {
734  if ($xoops->isAdminSide) {
735  $redirect_page = $this->url('admin/main.php?com_modid=' . $modid . '&amp;com_itemid');
736  } else {
737  $redirect_page = $xoops->url('modules/' . $module->getVar('dirname') . '/' . $plugin->pageName() . '?');
738  $comment_confirm_extra = array();
739  if (is_array($extraParams = $plugin->extraParams())) {
740  foreach ($extraParams as $extra_param) {
741  if (isset($_GET[$extra_param])) {
742  $redirect_page .= $extra_param . '=' . $_GET[$extra_param] . '&amp;';
743  // for the confirmation page
744  $comment_confirm_extra[$extra_param] = $_GET[$extra_param];
745  }
746  }
747  }
748  $redirect_page .= $plugin->itemName();
749  }
750 
751  $accesserror = false;
752  if (!$xoops->isUser()) {
753  $accesserror = true;
754  } else {
755  if (!$xoops->user->isAdmin($modid)) {
756  $accesserror = true;
757  }
758  }
759 
760  if (false != $accesserror) {
761  $ref = $xoops->getEnv('HTTP_REFERER');
762  if ($ref != '') {
763  $xoops->redirect($ref, 2, XoopsLocale::E_NO_ACCESS_PERMISSION);
764  } else {
765  $xoops->redirect($redirect_page . '?' . $plugin->itemName() . '=' . intval($id), 2, XoopsLocale::E_NO_ACCESS_PERMISSION);
766  }
767  }
768 
769  switch ($op) {
770  case 'delete_one':
771  if (!$comment_handler->delete($comment)) {
772  $xoops->header();
773  echo $xoops->alert('error', _MD_COMMENTS_COMDELETENG . ' (ID: ' . $comment->getVar('id') . ')');
774  $xoops->footer();
775  }
776 
777  $itemid = $comment->getVar('itemid');
778 
779  $criteria = new CriteriaCompo(new Criteria('modid', $modid));
780  $criteria->add(new Criteria('itemid', $itemid));
781  $criteria->add(new Criteria('status', COMMENTS_ACTIVE));
782  $comment_count = $comment_handler->getCount($criteria);
783  $plugin->update($itemid, $comment_count);
784 
785  // update user posts if its not an anonymous post
786  if ($comment->getVar('uid') != 0) {
787  $member_handler = $xoops->getHandlerMember();
788  $poster = $member_handler->getUser($comment->getVar('uid'));
789  if (is_object($poster)) {
790  $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') - 1);
791  }
792  }
793 
794  // get all comments posted later within the same thread
795  $thread_comments = $comment_handler->getThread($comment->getVar('rootid'), $id);
796 
797  $xot = new XoopsObjectTree($thread_comments, 'id', 'pid', 'rootid');
798  $child_comments = $xot->getFirstChild($id);
799  // now set new parent ID for direct child comments
800  $new_pid = $comment->getVar('pid');
801  $errs = array();
802  foreach (array_keys($child_comments) as $i) {
803  $child_comments[$i]->setVar('pid', $new_pid);
804  // if the deleted comment is a root comment, need to change root id to own id
805  if (false != $comment->isRoot()) {
806  $new_rootid = $child_comments[$i]->getVar('id');
807  $child_comments[$i]->setVar('rootid', $child_comments[$i]->getVar('id'));
808  if (!$comment_handler->insert($child_comments[$i])) {
809  $errs[] = 'Could not change comment parent ID from <strong>' . $id . '</strong> to <strong>' . $new_pid . '</strong>. (ID: ' . $new_rootid . ')';
810  } else {
811  // need to change root id for all its child comments as well
812  $c_child_comments = $xot->getAllChild($new_rootid);
813  $cc_count = count($c_child_comments);
814  foreach (array_keys($c_child_comments) as $j) {
815  $c_child_comments[$j]->setVar('rootid', $new_rootid);
816  if (!$comment_handler->insert($c_child_comments[$j])) {
817  $errs[] = 'Could not change comment root ID from <strong>' . $id . '</strong> to <strong>' . $new_rootid . '</strong>.';
818  }
819  }
820  }
821  } else {
822  if (!$comment_handler->insert($child_comments[$i])) {
823  $errs[] = 'Could not change comment parent ID from <strong>' . $id . '</strong> to <strong>' . $new_pid . '</strong>.';
824  }
825  }
826  }
827  if (count($errs) > 0) {
828  $xoops->header();
829  echo $xoops->alert('error', $errs);
830  $xoops->footer();
831  exit();
832  }
833  $xoops->redirect($redirect_page . '=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode, 1, _MD_COMMENTS_COMDELETED);
834  break;
835 
836  case 'delete_all':
837  $rootid = $comment->getVar('rootid');
838 
839  // get all comments posted later within the same thread
840  $thread_comments = $comment_handler->getThread($rootid, $id);
841 
842  // construct a comment tree
843  $xot = new XoopsObjectTree($thread_comments, 'id', 'pid', 'rootid');
844  $child_comments = $xot->getAllChild($id);
845  // add itself here
846  $child_comments[$id] = $comment;
847  $msgs = array();
848  $deleted_num = array();
849  $member_handler = $xoops->getHandlerMember();
850  foreach (array_keys($child_comments) as $i) {
851  if (!$comment_handler->delete($child_comments[$i])) {
852  $msgs[] = _MD_COMMENTS_COMDELETENG . ' (ID: ' . $child_comments[$i]->getVar('id') . ')';
853  } else {
854  $msgs[] = _MD_COMMENTS_COMDELETED . ' (ID: ' . $child_comments[$i]->getVar('id') . ')';
855  // store poster ID and deleted post number into array for later use
856  $poster_id = $child_comments[$i]->getVar('uid');
857  if ($poster_id > 0) {
858  $deleted_num[$poster_id] = !isset($deleted_num[$poster_id]) ? 1 : ($deleted_num[$poster_id] + 1);
859  }
860  }
861  }
862  foreach ($deleted_num as $user_id => $post_num) {
863  // update user posts
864  $poster = $member_handler->getUser($user_id);
865  if (is_object($poster)) {
866  $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') - $post_num);
867  }
868  }
869 
870  $itemid = $comment->getVar('itemid');
871 
872  $criteria = new CriteriaCompo(new Criteria('modid', $modid));
873  $criteria->add(new Criteria('itemid', $itemid));
874  $criteria->add(new Criteria('status', COMMENTS_ACTIVE));
875  $comment_count = $comment_handler->getCount($criteria);
876  $plugin->update($itemid, $comment_count);
877 
878  $xoops->header();
879  echo $xoops->alert('info', $msgs);
880  echo '<br /><a href="' . $redirect_page . '=' . $itemid . '&amp;com_order=' . $order . '&amp;com_mode=' . $mode . '">' . XoopsLocale::GO_BACK . '</a>';
881  $xoops->footer();
882  break;
883 
884  case 'delete':
885  default:
886  $xoops->header();
887  $comment_confirm = array(
888  'com_id' => $id,
889  'com_mode' => $mode,
890  'com_order' => $order,
891  'op' => array(
892  _MD_COMMENTS_DELETEONE => 'delete_one',
893  _MD_COMMENTS_DELETEALL => 'delete_all'
894  )
895  );
896  if (!empty($comment_confirm_extra) && is_array($comment_confirm_extra)) {
897  $comment_confirm = $comment_confirm + $comment_confirm_extra;
898  }
899  echo $xoops->confirm($comment_confirm, 'comment_delete.php', _MD_COMMENTS_DELETESELECT);
900  $xoops->footer();
901  break;
902  }
903  }
904  }
905 
910  {
912  $config_handler = $xoops->getHandlerConfig();
913  $configs = $this->getPluginableConfigs();
914 
915  $order = count($xoops->getModuleConfigs($module->getVar('dirname')));
916  foreach ($configs as $config) {
917  $confobj = $config_handler->createConfig();
918  $confobj->setVar('conf_modid', $module->getVar('mid'));
919  $confobj->setVar('conf_catid', 0);
920  $confobj->setVar('conf_name', $config['name']);
921  $confobj->setVar('conf_title', $config['title'], true);
922  $confobj->setVar('conf_desc', $config['description'], true);
923  $confobj->setVar('conf_formtype', $config['formtype']);
924  $confobj->setVar('conf_valuetype', $config['valuetype']);
925  $confobj->setConfValueForInput($config['default'], true);
926  $confobj->setVar('conf_order', $order);
927  if (isset($config['options']) && is_array($config['options'])) {
928  foreach ($config['options'] as $key => $value) {
929  $confop = $config_handler->createConfigOption();
930  $confop->setVar('confop_name', $key, true);
931  $confop->setVar('confop_value', $value, true);
932  $confobj->setConfOptions($confop);
933  unset($confop);
934  }
935  }
936  ++$order;
937  $config_handler->insertConfig($confobj);
938  }
939  }
940 
945  {
947  $this->getHandlerComment()->deleteByModule($module->getVar('mid'));
948 
949 
950  $configNames = array('com_rule', 'com_anonpost');
951  $config_handler = $xoops->getHandlerConfig();
952 
953  //Delete all configs
954  $criteria = new CriteriaCompo();
955  $criteria->add(new Criteria('conf_modid', $module->getVar('mid')));
956  $criteria->add(new Criteria('conf_name', "('" . implode("','", $configNames) . "')", 'IN'));
957  $configs = $config_handler->getConfigs($criteria);
958  /* @var $config XoopsConfigItem */
959  foreach ($configs as $config) {
960  $config_handler->deleteConfig($config);
961  }
962  }
963 
967  public function getPluginableConfigs()
968  {
969  $configs = array();
970  array_push($configs, array(
971  'name' => 'com_rule',
972  'title' => '_MD_COMMENTS_COMRULES',
973  'description' => '',
974  'formtype' => 'select',
975  'valuetype' => 'int',
976  'default' => 1,
977  'options' => array(
978  '_MD_COMMENTS_COMNOCOM' => COMMENTS_APPROVENONE,
979  '_MD_COMMENTS_COMAPPROVEALL' => COMMENTS_APPROVEALL,
980  '_MD_COMMENTS_COMAPPROVEUSER' => COMMENTS_APPROVEUSER,
981  '_MD_COMMENTS_COMAPPROVEADMIN' => COMMENTS_APPROVEADMIN
982  )
983  ));
984  array_push($configs, array(
985  'name' => 'com_anonpost',
986  'title' => '_MD_COMMENTS_COMANONPOST',
987  'description' => '',
988  'formtype' => 'yesno',
989  'valuetype' => 'int',
990  'default' => 0
991  ));
992  return $configs;
993  }
994 }
$reply
Definition: pmlite.php:27
static formatTimestamp($time, $format= 'l', $timeoffset=null)
Definition: Abstract.php:289
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
static getInstance()
init()
Definition: helper.php:28
$i
Definition: dialog.php:68
const _MD_COMMENTS_JOINED
Definition: main.php:40
static getInstance()
Definition: Xoops.php:160
getHandlerComment()
Definition: helper.php:54
displayCommentForm(CommentsComment $obj)
Definition: helper.php:80
static getInstance()
Definition: helper.php:47
static getInstance()
Definition: helper.php:46
$text
Definition: qrrender.php:27
$pid
Definition: admin_menu.php:37
const NO_TITLE
Definition: en_US.php:867
$comment_handler
Definition: main.php:64
renderHeader($title, $text, $uid, $timestamp)
Definition: helper.php:506
$modid
Definition: groupperm.php:27
const _MD_COMMENTS_THREAD
Definition: main.php:38
const _MD_COMMENTS_FROM
Definition: main.php:42
getVar($key, $format= 's')
exit
Definition: browse.php:104
const _MD_COMMENTS_POSTER
Definition: main.php:39
const _MD_COMMENTS_UPDATED
Definition: main.php:37
renderView()
Definition: helper.php:518
$xoops
Definition: admin.php:25
$id
Definition: admin_menu.php:36
const _MD_COMMENTS_POSTS
Definition: main.php:41
deleteModuleRelations(XoopsModule $module)
Definition: helper.php:944
$status
const _MD_COMMENTS_DELETESELECT
Definition: main.php:45
const _MD_COMMENTS_COMDELETENG
Definition: main.php:44
static getUnameFromId($userid, $usereal=0)
Definition: user.php:124
if($_SERVER['REQUEST_METHOD']== 'POST') $config_handler
static get($name)
const _MD_COMMENTS_COMDELETED
Definition: main.php:43
$module
Definition: main.php:52
if(!$xoops->isUser()) $uid
Definition: index.php:31
$configs
Definition: config.php:27
getPluginableConfigs()
Definition: helper.php:967
$comments
Definition: main.php:48
const A_REPLY
Definition: en_US.php:119
const _MD_COMMENTS_REPLIES
Definition: main.php:31
const E_NO_ACCESS_PERMISSION
Definition: en_US.php:351
static trim($text)
Definition: Abstract.php:272
displayNew()
Definition: helper.php:85
$helper
return $config
displayDelete()
Definition: helper.php:707
const _MD_COMMENTS_POSTED
Definition: main.php:36
$criteria
static getInstance(XoopsTpl $tpl, $use_icons=true, $do_iconcheck=false)
const A_DELETE
Definition: en_US.php:89
getUserConfig($config)
Definition: helper.php:64
displayPost()
Definition: helper.php:136
const _MD_COMMENTS_THANKSPOST
Definition: main.php:48
displayReply()
Definition: helper.php:465
const _MD_COMMENTS_DELETEONE
Definition: main.php:46
displayEdit()
Definition: helper.php:680
$op
const A_EDIT
Definition: en_US.php:93
insertModuleRelations(XoopsModule $module)
Definition: helper.php:909
const _MD_COMMENTS_NOTICE
Definition: main.php:49
$j
Definition: help.php:169
$user_id
Definition: update.php:57
const GO_BACK
Definition: en_US.php:452
static substr($str, $start, $length, $trimmarker= '...')
Definition: Abstract.php:194
const _MD_COMMENTS_DELETEALL
Definition: main.php:47