XOOPS  2.6.0
commentrenderer.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 
28 {
32  private $_tpl;
33 
37  private $_comments = array();
38 
42  private $_useIcons = true;
43 
47  private $_doIconCheck = false;
48 
52  private $_statusText;
53 
61  public function __construct(XoopsTpl $tpl, $use_icons = true, $do_iconcheck = false)
62  {
63  $this->_tpl = $tpl;
64  $this->_useIcons = $use_icons;
65  $this->_doIconCheck = $do_iconcheck;
66  $this->_statusText = array(
67  COMMENTS_PENDING => '<span style="text-decoration: none; font-weight: bold; color: #00ff00;">' . _MD_COMMENTS_PENDING . '</span>',
68  COMMENTS_ACTIVE => '<span style="text-decoration: none; font-weight: bold; color: #ff0000;">' . _MD_COMMENTS_ACTIVE . '</span>',
69  COMMENTS_HIDDEN => '<span style="text-decoration: none; font-weight: bold; color: #0000ff;">' . _MD_COMMENTS_HIDDEN . '</span>'
70  );
71  }
72 
82  static public function getInstance(XoopsTpl $tpl, $use_icons = true, $do_iconcheck = false)
83  {
84  static $instance;
85  if (!isset($instance)) {
86  $class = __CLASS__;
87  $instance = new $class($tpl, $use_icons, $do_iconcheck);
88  }
89  return $instance;
90  }
91 
97  public function setComments(&$comments_arr)
98  {
99  if (isset($this->_comments)) {
100  unset($this->_comments);
101  }
102  $this->_comments =& $comments_arr;
103  }
104 
110  public function renderFlatView($admin_view = false)
111  {
112  foreach ($this->_comments as $i => $comment) {
113  /* @var $comment XoopsComment */
114  if (false != $this->_useIcons) {
115  $title = $this->_getTitleIcon($comment->getVar('icon')) . '&nbsp;' . $comment->getVar('title');
116  } else {
117  $title = $comment->getVar('title');
118  }
119  $poster = $this->_getPosterArray($comment->getVar('uid'));
120  if (false != $admin_view) {
121  $text = $comment->getVar('text') . '<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">' . _MD_COMMENTS_STATUS . ': ' . $this->_statusText[$comment->getVar('status')] . '<br />IP: <span style="font-weight: bold;">' . $comment->getVar('ip') . '</span></div>';
122  } else {
123  // hide comments that are not active
124  if (COMMENTS_ACTIVE != $comment->getVar('status')) {
125  continue;
126  } else {
127  $text = $comment->getVar('text');
128  }
129  }
130  $this->_comments[$i] = $comment;
131  $this->_tpl->append('comments', array(
132  'id' => $comment->getVar('id'),
133  'title' => $title,
134  'text' => $text,
135  'date_posted' => XoopsLocale::formatTimestamp($comment->getVar('created'), 'm'),
136  'date_modified' => XoopsLocale::formatTimestamp($comment->getVar('modified'), 'm'),
137  'poster' => $poster
138  ));
139  }
140  }
141 
150  public function renderThreadView($comment_id = 0, $admin_view = false, $show_nav = true)
151  {
152  // construct comment tree
153  $xot = new XoopsObjectTree($this->_comments, 'id', 'pid', 'rootid');
154  $tree = $xot->getTree();
155 
156  if (false != $this->_useIcons) {
157  $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('icon')) . '&nbsp;' . $tree[$comment_id]['obj']->getVar('title');
158  } else {
159  $title = $tree[$comment_id]['obj']->getVar('title');
160  }
161  if (false != $show_nav && $tree[$comment_id]['obj']->getVar('pid') != 0) {
162  $this->_tpl->assign('lang_top', _MD_COMMENTS_TOP);
163  $this->_tpl->assign('lang_parent', _MD_COMMENTS_PARENT);
164  $this->_tpl->assign('show_threadnav', true);
165  } else {
166  $this->_tpl->assign('show_threadnav', false);
167  }
168  if (false != $admin_view) {
169  // admins can see all
170  $text = $tree[$comment_id]['obj']->getVar('text') . '<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">' . _MD_COMMENTS_STATUS . ': ' . $this->_statusText[$tree[$comment_id]['obj']->getVar('status')] . '<br />IP: <span style="font-weight: bold;">' . $tree[$comment_id]['obj']->getVar('ip') . '</span></div>';
171  } else {
172  // hide comments that are not active
173  if (COMMENTS_ACTIVE != $tree[$comment_id]['obj']->getVar('status')) {
174  // if there are any child comments, display them as root comments
175  if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
176  foreach ($tree[$comment_id]['child'] as $child_id) {
177  $this->renderThreadView($child_id, $admin_view, false);
178  }
179  }
180  return;
181  } else {
182  $text = $tree[$comment_id]['obj']->getVar('text');
183  }
184  }
185  $replies = array();
186  $this->_renderThreadReplies($tree, $comment_id, $replies, '&nbsp;&nbsp;', $admin_view);
187  $show_replies = (count($replies) > 0) ? true : false;
188  $this->_tpl->append('comments', array(
189  'pid' => $tree[$comment_id]['obj']->getVar('pid'),
190  'id' => $tree[$comment_id]['obj']->getVar('id'),
191  'itemid' => $tree[$comment_id]['obj']->getVar('itemid'),
192  'rootid' => $tree[$comment_id]['obj']->getVar('rootid'),
193  'title' => $title,
194  'text' => $text,
195  'date_posted' => XoopsLocale::formatTimestamp($tree[$comment_id]['obj']->getVar('created'), 'm'),
196  'date_modified' => XoopsLocale::formatTimestamp($tree[$comment_id]['obj']->getVar('modified'), 'm'),
197  'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('uid')),
198  'replies' => $replies,
199  'show_replies' => $show_replies
200  ));
201  }
202 
216  private function _renderThreadReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth = 0, $current_prefix = '')
217  {
218  if ($depth > 0) {
219  if (false != $this->_useIcons) {
220  $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('icon')) . '&nbsp;' . $thread[$key]['obj']->getVar('title');
221  } else {
222  $title = $thread[$key]['obj']->getVar('title');
223  }
224  $title = (false != $admin_view) ? $title . ' ' . $this->_statusText[$thread[$key]['obj']->getVar('status')] : $title;
225  $replies[] = array(
226  'id' => $key,
227  'prefix' => $current_prefix,
228  'date_posted' => XoopsLocale::formatTimestamp($thread[$key]['obj']->getVar('created'), 'm'),
229  'title' => $title,
230  'root_id' => $thread[$key]['obj']->getVar('rootid'),
231  'status' => $this->_statusText[$thread[$key]['obj']->getVar('status')],
232  'poster' => $this->_getPosterName($thread[$key]['obj']->getVar('uid'))
233  );
234  $current_prefix .= $prefix;
235  }
236  if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
237  ++$depth;
238  foreach ($thread[$key]['child'] as $childkey) {
239  if (!$admin_view && $thread[$childkey]['obj']->getVar('status') != COMMENTS_ACTIVE) {
240  // skip this comment if it is not active and continue on processing its child comments instead
241  if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
242  foreach ($thread[$childkey]['child'] as $childchildkey) {
243  $this->_renderThreadReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
244  }
245  }
246  } else {
247  $this->_renderThreadReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth, $current_prefix);
248  }
249  }
250  }
251  }
252 
260  public function renderNestView($comment_id = 0, $admin_view = false)
261  {
262  $xot = new XoopsObjectTree($this->_comments, 'id', 'pid', 'rootid');
263  $tree = $xot->getTree();
264  if (false != $this->_useIcons) {
265  $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('icon')) . '&nbsp;' . $tree[$comment_id]['obj']->getVar('title');
266  } else {
267  $title = $tree[$comment_id]['obj']->getVar('title');
268  }
269  if (false != $admin_view) {
270  $text = $tree[$comment_id]['obj']->getVar('text') . '<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">' . _MD_COMMENTS_STATUS . ': ' . $this->_statusText[$tree[$comment_id]['obj']->getVar('status')] . '<br />IP: <span style="font-weight: bold;">' . $tree[$comment_id]['obj']->getVar('ip') . '</span></div>';
271  } else {
272  // skip this comment if it is not active and continue on processing its child comments instead
273  if (COMMENTS_ACTIVE != $tree[$comment_id]['obj']->getVar('status')) {
274  // if there are any child comments, display them as root comments
275  if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
276  foreach ($tree[$comment_id]['child'] as $child_id) {
277  $this->renderNestView($child_id, $admin_view);
278  }
279  }
280  return;
281  } else {
282  $text = $tree[$comment_id]['obj']->getVar('text');
283  }
284  }
285  $replies = array();
286  $this->_renderNestReplies($tree, $comment_id, $replies, 25, $admin_view);
287  $this->_tpl->append('comments', array(
288  'pid' => $tree[$comment_id]['obj']->getVar('pid'),
289  'id' => $tree[$comment_id]['obj']->getVar('id'),
290  'itemid' => $tree[$comment_id]['obj']->getVar('itemid'),
291  'rootid' => $tree[$comment_id]['obj']->getVar('rootid'),
292  'title' => $title,
293  'text' => $text,
294  'date_posted' => XoopsLocale::formatTimestamp($tree[$comment_id]['obj']->getVar('created'), 'm'),
295  'date_modified' => XoopsLocale::formatTimestamp($tree[$comment_id]['obj']->getVar('modified'), 'm'),
296  'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('uid')),
297  'replies' => $replies
298  ));
299  }
300 
313  private function _renderNestReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth = 0)
314  {
315  if ($depth > 0) {
316  if (false != $this->_useIcons) {
317  $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('icon')) . '&nbsp;' . $thread[$key]['obj']->getVar('title');
318  } else {
319  $title = $thread[$key]['obj']->getVar('title');
320  }
321  $text = (false != $admin_view) ? $thread[$key]['obj']->getVar('text') . '<div style="text-align:right; margin-top: 2px; margin-right: 2px;">' . _MD_COMMENTS_STATUS . ': ' . $this->_statusText[$thread[$key]['obj']->getVar('status')] . '<br />IP: <span style="font-weight: bold;">' . $thread[$key]['obj']->getVar('ip') . '</span></div>' : $thread[$key]['obj']->getVar('text');
322  $replies[] = array(
323  'id' => $key,
324  'prefix' => $prefix,
325  'pid' => $thread[$key]['obj']->getVar('pid'),
326  'itemid' => $thread[$key]['obj']->getVar('itemid'),
327  'rootid' => $thread[$key]['obj']->getVar('rootid'),
328  'title' => $title,
329  'text' => $text,
330  'date_posted' => XoopsLocale::formatTimestamp($thread[$key]['obj']->getVar('created'), 'm'),
331  'date_modified' => XoopsLocale::formatTimestamp($thread[$key]['obj']->getVar('modified'), 'm'),
332  'poster' => $this->_getPosterArray($thread[$key]['obj']->getVar('uid'))
333  );
334 
335  $prefix = $prefix + 25;
336  }
337  if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
338  ++$depth;
339  foreach ($thread[$key]['child'] as $childkey) {
340  if (!$admin_view && $thread[$childkey]['obj']->getVar('status') != COMMENTS_ACTIVE) {
341  // skip this comment if it is not active and continue on processing its child comments instead
342  if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
343  foreach ($thread[$childkey]['child'] as $childchildkey) {
344  $this->_renderNestReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
345  }
346  }
347  } else {
348  $this->_renderNestReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth);
349  }
350  }
351  }
352  }
353 
362  private function _getPosterName($poster_id)
363  {
364  $poster['id'] = intval($poster_id);
365  if ($poster['id'] > 0) {
366  $user = Xoops::getInstance()->getHandlerMember()->getUser($poster['id']);
367  if (!is_object($user)) {
368  $poster['id'] = 0;
369  }
370  }
371  $poster['uname'] = XoopsUserUtility::getUnameFromId($poster['id'], false, true);
372  return $poster;
373  }
374 
383  private function _getPosterArray($poster_id)
384  {
385  $poster['id'] = intval($poster_id);
386  if ($poster['id'] > 0) {
388  $member_handler = $xoops->getHandlerMember();
389  $user = $member_handler->getUser($poster['id']);
390  if (is_object($user)) {
391  $poster['uname'] = XoopsUserUtility::getUnameFromId($poster['id'], false, true);
392  $poster_rank = $user->rank();
393  $poster['rank_image'] = ($poster_rank['image'] != '') ? $poster_rank['image'] : 'blank.gif';
394  $poster['rank_title'] = $poster_rank['title'];
395  $response = $xoops->service("Avatar")->getAvatarUrl($user);
396  $avatar = $response->getValue();
397  $avatar = empty($avatar) ? '' : $avatar;
398  $poster['avatar'] = $avatar;
399  $poster['regdate'] = XoopsLocale::formatTimestamp($user->getVar('user_regdate'), 's');
400  $poster['from'] = $user->getVar('user_from');
401  $poster['postnum'] = $user->getVar('posts');
402  $poster['status'] = $user->isOnline() ? _MD_COMMENTS_ONLINE : '';
403  return $poster;
404  } else {
405  $poster['id'] = 0;
406  }
407  }
408 
409  $poster['uname'] = XoopsUserUtility::getUnameFromId($poster['id'], false, true);
410  $poster['rank_title'] = '';
411  $poster['avatar'] = 'blank.gif';
412  $poster['regdate'] = '';
413  $poster['from'] = '';
414  $poster['postnum'] = 0;
415  $poster['status'] = '';
416  return $poster;
417  }
418 
427  private function _getTitleIcon($icon_image)
428  {
429  $icon_image = htmlspecialchars(trim($icon_image));
430  if ($icon_image != '') {
431  if (false != $this->_doIconCheck) {
432  if (!XoopsLoad::fileExists(Xoops::getInstance()->path('images/subject/' . $icon_image))) {
433  return '<img src="' . \XoopsBaseConfig::get('url') . '/images/icons/no_posticon.gif" alt="" />';
434  } else {
435  return '<img src="' . \XoopsBaseConfig::get('url') . '/images/subject/' . $icon_image . '" alt="" />';
436  }
437  } else {
438  return '<img src="' . \XoopsBaseConfig::get('url') . '/images/subject/' . $icon_image . '" alt="" />';
439  }
440  }
441  return '<img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" />';
442  }
443 }
$tpl
Definition: backend.php:39
static formatTimestamp($time, $format= 'l', $timeoffset=null)
Definition: Abstract.php:289
static getUnameFromId($userid, $usereal=false, $linked=false)
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
$i
Definition: dialog.php:68
renderThreadView($comment_id=0, $admin_view=false, $show_nav=true)
static getInstance()
Definition: Xoops.php:160
const _MD_COMMENTS_ACTIVE
Definition: main.php:28
$user
Definition: checklogin.php:47
$text
Definition: qrrender.php:27
const _MD_COMMENTS_PENDING
Definition: main.php:26
const _MD_COMMENTS_TOP
Definition: main.php:33
_renderThreadReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth=0, $current_prefix= '')
renderNestView($comment_id=0, $admin_view=false)
__construct(XoopsTpl $tpl, $use_icons=true, $do_iconcheck=false)
$xoops
Definition: admin.php:25
const _MD_COMMENTS_HIDDEN
Definition: main.php:27
static fileExists($file)
Definition: xoopsload.php:506
static get($name)
const _MD_COMMENTS_PARENT
Definition: main.php:32
$avatar
Definition: userinfo.php:94
renderFlatView($admin_view=false)
static getInstance(XoopsTpl $tpl, $use_icons=true, $do_iconcheck=false)
if($xoops->isUser()&&$isAdmin) $response
Definition: userinfo.php:83
setComments(&$comments_arr)
_renderNestReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth=0)
const _MD_COMMENTS_STATUS
Definition: main.php:29
const _MD_COMMENTS_ONLINE
Definition: main.php:35