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