1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21:
22: 23: 24: 25: 26: 27:
28: class XoopsCommentRenderer
29: {
30: 31: 32: 33: 34:
35: public $_tpl;
36: public $_comments;
37: public $_useIcons = true;
38: public $_doIconCheck = false;
39: public $_memberHandler;
40: public $_statusText;
41: 42: 43:
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: public function __construct(XoopsTpl $tpl, $use_icons = true, $do_iconcheck = false)
54: {
55: $this->_tpl = $tpl;
56: $this->_useIcons = (bool)$use_icons;
57: $this->_doIconCheck = (bool)$do_iconcheck;
58:
59: $this->_memberHandler = xoops_getHandler('member');
60: $this->_statusText = array(
61: XOOPS_COMMENT_PENDING => '<span style="text-decoration: none; font-weight: bold; color: #00ff00;">' . _CM_PENDING . '</span>',
62: XOOPS_COMMENT_ACTIVE => '<span style="text-decoration: none; font-weight: bold; color: #ff0000;">' . _CM_ACTIVE . '</span>',
63: XOOPS_COMMENT_HIDDEN => '<span style="text-decoration: none; font-weight: bold; color: #0000ff;">' . _CM_HIDDEN . '</span>');
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73:
74: public static function instance(XoopsTpl $tpl, $use_icons = true, $do_iconcheck = false)
75: {
76: static $instance;
77: if (!isset($instance)) {
78: $instance = new XoopsCommentRenderer($tpl, $use_icons, $do_iconcheck);
79: }
80:
81: return $instance;
82: }
83:
84: 85: 86: 87: 88:
89: public function setComments(&$comments_arr)
90: {
91: if (isset($this->_comments)) {
92: unset($this->_comments);
93: }
94: $this->_comments =& $comments_arr;
95: }
96:
97: 98: 99: 100: 101:
102: public function renderFlatView($admin_view = false)
103: {
104: $count = count($this->_comments);
105: for ($i = 0; $i < $count; ++$i) {
106: if (false !== $this->_useIcons) {
107: $title = $this->_getTitleIcon($this->_comments[$i]->getVar('com_icon')) . ' ' . $this->_comments[$i]->getVar('com_title');
108: } else {
109: $title = $this->_comments[$i]->getVar('com_title');
110: }
111:
112: $poster = $this->_getPosterArray($this->_comments[$i]->getVar('com_uid'), $this->_comments[$i]->getVar('com_user'), $this->_comments[$i]->getVar('com_url'));
113: if (false !== (bool)$admin_view) {
114: $com_email = $this->_comments[$i]->getVar('com_email');
115: $text = $this->_comments[$i]->getVar('com_text');
116: $text .= '<div style="text-align:right; margin-top: 2px; margin-bottom: 0; margin-right: 2px;">';
117: $text .= _CM_STATUS . ': ' . $this->_statusText[$this->_comments[$i]->getVar('com_status')] . '<br>';
118: $text .= 'IP: <span style="font-weight: bold;">' . $this->_comments[$i]->getVar('com_ip') . '</span>';
119: if (!empty($com_email)) {
120: $text .= '<br>' . _CM_EMAIL . ' :<span style="font-weight: bold;"><a href="mailto:' . $com_email . '" title="' . $com_email . '">' . $com_email . '</a></span>';
121: }
122: $text .= '</div>';
123: } else {
124:
125: if (XOOPS_COMMENT_ACTIVE != $this->_comments[$i]->getVar('com_status')) {
126: continue;
127: } else {
128: $text = $this->_comments[$i]->getVar('com_text');
129: }
130: }
131:
132: $this->_tpl->append('comments', array(
133: 'id' => $this->_comments[$i]->getVar('com_id'),
134: 'title' => $title,
135: 'text' => $text,
136: 'date_posted' => formatTimestamp($this->_comments[$i]->getVar('com_created'), 'm'),
137: 'date_modified' => formatTimestamp($this->_comments[$i]->getVar('com_modified'), 'm'),
138: 'poster' => $poster));
139: }
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150: 151:
152: public function renderThreadView($comment_id = 0, $admin_view = false, $show_nav = true)
153: {
154: include_once $GLOBALS['xoops']->path('class/tree.php');
155:
156: $xot = new XoopsObjectTree($this->_comments, 'com_id', 'com_pid', 'com_rootid');
157: $tree =& $xot->getTree();
158:
159: if (false !== $this->_useIcons) {
160: $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('com_icon')) . ' ' . $tree[$comment_id]['obj']->getVar('com_title');
161: } else {
162: $title = $tree[$comment_id]['obj']->getVar('com_title');
163: }
164: if (false !== (bool)$show_nav && $tree[$comment_id]['obj']->getVar('com_pid') != 0) {
165: $this->_tpl->assign('lang_top', _CM_TOP);
166: $this->_tpl->assign('lang_parent', _CM_PARENT);
167: $this->_tpl->assign('show_threadnav', true);
168: } else {
169: $this->_tpl->assign('show_threadnav', false);
170: }
171: $admin_view = (bool)$admin_view;
172: if (false !== $admin_view) {
173:
174: $com_email = $tree[$comment_id]['obj']->getVar('com_email');
175: $text = $tree[$comment_id]['obj']->getVar('com_text');
176: $text .= '<div style="text-align:right; margin-top: 2px; margin-bottom: 0; margin-right: 2px;">';
177: $text .= _CM_STATUS . ': ' . $this->_statusText[$tree[$comment_id]['obj']->getVar('com_status')] . '<br>';
178: $text .= 'IP: <span style="font-weight: bold;">' . $tree[$comment_id]['obj']->getVar('com_ip') . '</span>';
179: if (!empty($com_email)) {
180: $text .= '<br>' . _CM_EMAIL . ' :<span style="font-weight: bold;"><a href="mailto:' . $com_email . '" title="' . $com_email . '">' . $com_email . '</a></span>';
181: }
182: $text .= '</div>';
183: } else {
184:
185: if (XOOPS_COMMENT_ACTIVE != $tree[$comment_id]['obj']->getVar('com_status')) {
186:
187: if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
188: foreach ($tree[$comment_id]['child'] as $child_id) {
189: $this->renderThreadView($child_id, $admin_view, false);
190: }
191: }
192:
193: return null;
194: } else {
195: $text = $tree[$comment_id]['obj']->getVar('com_text');
196: }
197: }
198: $replies = array();
199: $this->_renderThreadReplies($tree, $comment_id, $replies, ' ', $admin_view);
200: $show_replies = (count($replies) > 0);
201:
202: $this->_tpl->append('comments', array(
203: 'pid' => $tree[$comment_id]['obj']->getVar('com_pid'),
204: 'id' => $tree[$comment_id]['obj']->getVar('com_id'),
205: 'itemid' => $tree[$comment_id]['obj']->getVar('com_itemid'),
206: 'rootid' => $tree[$comment_id]['obj']->getVar('com_rootid'),
207: 'title' => $title,
208: 'text' => $text,
209: 'date_posted' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_created'), 'm'),
210: 'date_modified' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_modified'), 'm'),
211: 'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('com_uid'), $tree[$comment_id]['obj']->getVar('com_user'), $tree[$comment_id]['obj']->getVar('com_url')),
212: 'replies' => $replies,
213: 'show_replies' => $show_replies));
214:
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228:
229: public function _renderThreadReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth = 0, $current_prefix = '')
230: {
231: $admin_view = (bool)$admin_view;
232: if ($depth > 0) {
233: if (false !== $this->_useIcons) {
234: $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('com_icon')) . ' ' . $thread[$key]['obj']->getVar('com_title');
235: } else {
236: $title = $thread[$key]['obj']->getVar('com_title');
237: }
238: $title = (false !== $admin_view) ? $title . ' ' . $this->_statusText[$thread[$key]['obj']->getVar('com_status')] : $title;
239:
240: $replies[] = array(
241: 'id' => $key,
242: 'prefix' => $current_prefix,
243: 'date_posted' => formatTimestamp($thread[$key]['obj']->getVar('com_created'), 'm'),
244: 'title' => $title,
245: 'root_id' => $thread[$key]['obj']->getVar('com_rootid'),
246: 'status' => $this->_statusText[$thread[$key]['obj']->getVar('com_status')],
247: 'poster' => $this->_getPosterName($thread[$key]['obj']->getVar('com_uid'), $thread[$key]['obj']->getVar('com_user'), $thread[$key]['obj']->getVar('com_url')));
248:
249: $current_prefix .= $prefix;
250: }
251: if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
252: ++$depth;
253: foreach ($thread[$key]['child'] as $childkey) {
254: if (!$admin_view && $thread[$childkey]['obj']->getVar('com_status') != XOOPS_COMMENT_ACTIVE) {
255:
256: if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
257: foreach ($thread[$childkey]['child'] as $childchildkey) {
258: $this->_renderThreadReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
259: }
260: }
261: } else {
262: $this->_renderThreadReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth, $current_prefix);
263: }
264: }
265: }
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275: 276:
277: public function renderNestView($comment_id = 0, $admin_view = false)
278: {
279: include_once $GLOBALS['xoops']->path('class/tree.php');
280: $xot = new XoopsObjectTree($this->_comments, 'com_id', 'com_pid', 'com_rootid');
281: $tree =& $xot->getTree();
282: if (false !== $this->_useIcons) {
283: $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('com_icon')) . ' ' . $tree[$comment_id]['obj']->getVar('com_title');
284: } else {
285: $title = $tree[$comment_id]['obj']->getVar('com_title');
286: }
287: $admin_view = (bool)$admin_view;
288: if (false !== $admin_view) {
289: $com_email = $tree[$comment_id]['obj']->getVar('com_email');
290: $text = $tree[$comment_id]['obj']->getVar('com_text');
291: $text .= '<div style="text-align:right; margin-top: 2px; margin-bottom: 0; margin-right: 2px;">';
292: $text .= _CM_STATUS . ': ' . $this->_statusText[$tree[$comment_id]['obj']->getVar('com_status')] . '<br>';
293: $text .= 'IP: <span style="font-weight: bold;">' . $tree[$comment_id]['obj']->getVar('com_ip') . '</span>';
294: if (!empty($com_email)) {
295: $text .= '<br>' . _CM_EMAIL . ' :<span style="font-weight: bold;"><a href="mailto:' . $com_email . '" title="' . $com_email . '">' . $com_email . '</a></span>';
296: }
297: $text .= '</div>';
298: } else {
299:
300: if (XOOPS_COMMENT_ACTIVE != $tree[$comment_id]['obj']->getVar('com_status')) {
301:
302: if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
303: foreach ($tree[$comment_id]['child'] as $child_id) {
304: $this->renderNestView($child_id, $admin_view);
305: }
306: }
307:
308: return null;
309: } else {
310: $text = $tree[$comment_id]['obj']->getVar('com_text');
311: }
312: }
313: $replies = array();
314: $this->_renderNestReplies($tree, $comment_id, $replies, 25, $admin_view);
315:
316: $this->_tpl->append('comments', array(
317: 'pid' => $tree[$comment_id]['obj']->getVar('com_pid'),
318: 'id' => $tree[$comment_id]['obj']->getVar('com_id'),
319: 'itemid' => $tree[$comment_id]['obj']->getVar('com_itemid'),
320: 'rootid' => $tree[$comment_id]['obj']->getVar('com_rootid'),
321: 'title' => $title,
322: 'text' => $text,
323: 'date_posted' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_created'), 'm'),
324: 'date_modified' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_modified'), 'm'),
325: 'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('com_uid'), $tree[$comment_id]['obj']->getVar('com_user'), $tree[$comment_id]['obj']->getVar('com_url')),
326: 'replies' => $replies));
327:
328: }
329:
330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340:
341: public function _renderNestReplies(&$thread, $key, &$replies, $prefix, $admin_view, $depth = 0)
342: {
343: if ($depth > 0) {
344: if (false !== $this->_useIcons) {
345: $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('com_icon')) . ' ' . $thread[$key]['obj']->getVar('com_title');
346: } else {
347: $title = $thread[$key]['obj']->getVar('com_title');
348: }
349: $admin_view = (bool)$admin_view;
350: $text = (false !== $admin_view) ? $thread[$key]['obj']->getVar('com_text') . '<div style="text-align:right; margin-top: 2px; margin-right: 2px;">' . _CM_STATUS . ': ' . $this->_statusText[$thread[$key]['obj']->getVar('com_status')] . '<br>IP: <span style="font-weight: bold;">' . $thread[$key]['obj']->getVar('com_ip') . '</span><br>' . _CM_EMAIL . ' :<span style="font-weight: bold;">' . $thread[$key]['obj']->getVar('com_email') . '</span></div>' : $thread[$key]['obj']->getVar('com_text');
351:
352: $replies[] = array(
353: 'id' => $key,
354: 'prefix' => $prefix,
355: 'pid' => $thread[$key]['obj']->getVar('com_pid'),
356: 'itemid' => $thread[$key]['obj']->getVar('com_itemid'),
357: 'rootid' => $thread[$key]['obj']->getVar('com_rootid'),
358: 'title' => $title,
359: 'text' => $text,
360: 'date_posted' => formatTimestamp($thread[$key]['obj']->getVar('com_created'), 'm'),
361: 'date_modified' => formatTimestamp($thread[$key]['obj']->getVar('com_modified'), 'm'),
362: 'poster' => $this->_getPosterArray($thread[$key]['obj']->getVar('com_uid'), $thread[$key]['obj']->getVar('com_user'), $thread[$key]['obj']->getVar('com_url')));
363:
364: $prefix += 25;
365: }
366: if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
367: ++$depth;
368: foreach ($thread[$key]['child'] as $childkey) {
369: if (!$admin_view && $thread[$childkey]['obj']->getVar('com_status') != XOOPS_COMMENT_ACTIVE) {
370:
371: if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
372: foreach ($thread[$childkey]['child'] as $childchildkey) {
373: $this->_renderNestReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
374: }
375: }
376: } else {
377: $this->_renderNestReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth);
378: }
379: }
380: }
381: }
382:
383: 384: 385: 386: 387: 388: 389: 390: 391:
392:
393: public function _getPosterName($poster_id, $poster_user, $poster_website)
394: {
395: $poster['id'] = (int)$poster_id;
396: if ($poster['id'] > 0) {
397: $com_poster = $this->_memberHandler->getUser($poster_id);
398: if (is_object($com_poster)) {
399: $poster['uname'] = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $poster['id'] . '">' . $com_poster->getVar('uname') . '</a>';
400: }
401: } elseif ($poster['id'] == 0 && $poster_user != '') {
402: $poster['id'] = 0;
403: if (!empty($poster_website)) {
404: $poster['uname'] = '<a href="' . $poster_website . '">' . $poster_user . '</a>';
405: } else {
406: $poster['uname'] = $poster_user;
407: }
408: } else {
409: $poster['id'] = 0;
410: $poster['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
411: }
412:
413: return $poster;
414: }
415:
416:
417: 418: 419: 420: 421: 422: 423: 424: 425:
426:
427: public function _getPosterArray($poster_id, $poster_user, $poster_website)
428: {
429: $poster['id'] = (int)$poster_id;
430: if ($poster['id'] > 0) {
431:
432: $com_poster = $this->_memberHandler->getUser($poster['id']);
433: if (is_object($com_poster)) {
434: $poster['uname'] = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $poster['id'] . '">' . $com_poster->getVar('uname') . '</a>';
435: $poster_rank = $com_poster->rank();
436: $poster['rank_image'] = ($poster_rank['image'] != '') ? $poster_rank['image'] : 'blank.gif';
437: $poster['rank_title'] = $poster_rank['title'];
438: $poster['avatar'] = $com_poster->getVar('user_avatar');
439: $poster['regdate'] = formatTimestamp($com_poster->getVar('user_regdate'), 's');
440: $poster['from'] = $com_poster->getVar('user_from');
441: $poster['postnum'] = $com_poster->getVar('posts');
442: $poster['status'] = $com_poster->isOnline() ? _CM_ONLINE : '';
443: }
444: } elseif ($poster['id'] == 0 && $poster_user != '') {
445: if (!empty($poster_website)) {
446: $poster['uname'] = '<a href="' . $poster_website . '">' . $poster_user . '</a>';
447: } else {
448: $poster['uname'] = $poster_user;
449: }
450: $poster['id'] = 0;
451: $poster['rank_title'] = '';
452: $poster['avatar'] = 'blank.gif';
453: $poster['regdate'] = '';
454: $poster['from'] = '';
455: $poster['postnum'] = 0;
456: $poster['status'] = '';
457: } else {
458: $poster['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
459: $poster['id'] = 0;
460: $poster['rank_title'] = '';
461: $poster['avatar'] = 'blank.gif';
462: $poster['regdate'] = '';
463: $poster['from'] = '';
464: $poster['postnum'] = 0;
465: $poster['status'] = '';
466: }
467:
468: return $poster;
469: }
470:
471:
472: 473: 474: 475: 476: 477: 478:
479: public function _getTitleIcon($icon_image)
480: {
481: $icon_image = htmlspecialchars(trim($icon_image));
482: if ($icon_image != '') {
483: if (false !== $this->_doIconCheck) {
484: if (!file_exists($GLOBALS['xoops']->path('images/subject/' . $icon_image))) {
485: return '<img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" />';
486: } else {
487: return '<img src="' . XOOPS_URL . '/images/subject/' . $icon_image . '" alt="" />';
488: }
489: } else {
490: return '<img src="' . XOOPS_URL . '/images/subject/' . $icon_image . '" alt="" />';
491: }
492: }
493:
494: return '<img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" />';
495: }
496: }
497: