XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
commentrenderer.php
1<?php
13
14
16{
17
21 public $_tpl;
22 public $_comments = null;
23 public $_useIcons = true;
24 public $_doIconCheck = false;
25 public $_memberHandler;
26 public $_statusText;
28
36 public function __construct(&$tpl, $use_icons = true, $do_iconcheck = false)
37 {
38 $this->_tpl =& $tpl;
39 $this->_useIcons = $use_icons;
40 $this->_doIconCheck = $do_iconcheck;
41 $this->_memberHandler =& xoops_gethandler('member');
42 $this->_statusText = [XOOPS_COMMENT_PENDING => '<span style="text-decoration: none; font-weight: bold; color: #00ff00;">' . _CM_PENDING . '</span>', XOOPS_COMMENT_ACTIVE => '<span style="text-decoration: none; font-weight: bold; color: #ff0000;">' . _CM_ACTIVE . '</span>', XOOPS_COMMENT_HIDDEN => '<span style="text-decoration: none; font-weight: bold; color: #0000ff;">' . _CM_HIDDEN . '</span>'];
43 }
44
53 public function &instance(&$tpl, $use_icons = true, $do_iconcheck = false)
54 {
55 static $instance;
56 if (!isset($instance)) {
57 $instance = new XoopsCommentRenderer($tpl, $use_icons, $do_iconcheck);
58 }
59 return $instance;
60 }
61
67 public function setComments(&$comments_arr)
68 {
69 if (isset($this->_comments)) {
70 unset($this->_comments);
71 }
72 $this->_comments =& $comments_arr;
73 }
74
80 public function renderFlatView($admin_view = false)
81 {
82 foreach ($this->_comments as $iValue) {
83 if (false !== $this->_useIcons) {
84 $title = $this->_getTitleIcon($iValue->getVar('com_icon')).'&nbsp;'. $iValue->getVar('com_title');
85 } else {
86 $title = $iValue->getVar('com_title');
87 }
88 $poster = $this->_getPosterArray($iValue->getVar('com_uid'));
89 if (false !== $admin_view) {
90 $text = $iValue->getVar('com_text')
91 .'<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">'
92 ._CM_STATUS.': '.$this->_statusText[$iValue->getVar('com_status')].'<br>IP: <span style="font-weight: bold;">'
93 . $iValue->getVar('com_ip').'</span></div>';
94 } else if (XOOPS_COMMENT_ACTIVE !== $iValue->getVar('com_status')) {
95 continue;
96 } else {
97 $text = $iValue->getVar('com_text');
98 }
99 $this->_tpl->append('comments',
100 [
101 'id' => $iValue->getVar('com_id'),
102 'title' => $title,
103 'text' => $text,
104 'date_posted' => formatTimestamp($iValue->getVar('com_created'), 'm'),
105 'date_modified' => formatTimestamp($iValue->getVar('com_modified'), 'm'),
106 'poster' => $poster
107 ]
108 );
109 }
110 }
111
121 public function renderThreadView(int $comment_id = 0, bool $admin_view, bool $show_nav = true)
122 {
123 include_once XOOPS_ROOT_PATH.'/class/tree.php';
124 // construct comment tree
125 $xot = new XoopsObjectTree($this->_comments, 'com_id', 'com_pid', 'com_rootid');
126 $tree =& $xot->getTree();
127
128 if (false !== $this->_useIcons) {
129 $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('com_icon')).'&nbsp;'.$tree[$comment_id]['obj']->getVar('com_title');
130 } else {
131 $title = $tree[$comment_id]['obj']->getVar('com_title');
132 }
133 if (false !== $show_nav && 0 !== $tree[$comment_id]['obj']->getVar('com_pid')) {
134 $this->_tpl->assign('lang_top', _CM_TOP);
135 $this->_tpl->assign('lang_parent', _CM_PARENT);
136 $this->_tpl->assign('show_threadnav', true);
137 } else {
138 $this->_tpl->assign('show_threadnav', false);
139 }
140 if (false !== $admin_view) {
141 // admins can see all
142 $text = $tree[$comment_id]['obj']->getVar('com_text')
143 .'<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">'
144 ._CM_STATUS.': '
145 .$this->_statusText[$tree[$comment_id]['obj']->getVar('com_status')]
146 .'<br>IP: <span style="font-weight: bold;">'
147 .$tree[$comment_id]['obj']->getVar('com_ip').'</span></div>';
148 } else if (XOOPS_COMMENT_ACTIVE !== $tree[$comment_id]['obj']->getVar('com_status')) {
149 // if there are any child comments, display them as root comments
150 if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
151 foreach ($tree[$comment_id]['child'] as $child_id) {
152 $this->renderThreadView($child_id, $admin_view, false);
153 }
154 }
155 return;
156 } else {
157 $text = $tree[$comment_id]['obj']->getVar('com_text');
158 }
159 $replies = [];
160 $this->_renderThreadReplies($tree, $comment_id, $replies, '&nbsp;&nbsp;', $admin_view);
161 $show_replies = count($replies) > 0;
162 $this->_tpl->append('comments',
163 [
164 'pid' => $tree[$comment_id]['obj']->getVar('com_pid'),
165 'id' => $tree[$comment_id]['obj']->getVar('com_id'),
166 'itemid' => $tree[$comment_id]['obj']->getVar('com_itemid'),
167 'rootid' => $tree[$comment_id]['obj']->getVar('com_rootid'),
168 'title' => $title, 'text' => $text,
169 'date_posted' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_created'), 'm'),
170 'date_modified' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_modified'), 'm'),
171 'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('com_uid')),
172 'replies' => $replies, 'show_replies' => $show_replies
173 ]
174 );
175 }
176
190 public function _renderThreadReplies(array &$thread, int $key, array &$replies, string $prefix, bool $admin_view, int $depth = 0, string $current_prefix = '')
191 {
192 if ($depth > 0) {
193 if (false !== $this->_useIcons) {
194 $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('com_icon')).'&nbsp;'.$thread[$key]['obj']->getVar('com_title');
195 } else {
196 $title = $thread[$key]['obj']->getVar('com_title');
197 }
198 $title = (false !== $admin_view) ? $title.' '.$this->_statusText[$thread[$key]['obj']->getVar('com_status')] : $title;
199 $replies[] = [
200 'id' => $key,
201 'prefix' => $current_prefix,
202 'date_posted' => formatTimestamp($thread[$key]['obj']->getVar('com_created'), 'm'),
203 'title' => $title,
204 'root_id' => $thread[$key]['obj']->getVar('com_rootid'),
205 'status' => $this->_statusText[$thread[$key]['obj']->getVar('com_status')],
206 'poster' => $this->_getPosterName($thread[$key]['obj']->getVar('com_uid'))
207 ];
208 $current_prefix .= $prefix;
209 }
210 if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
211 $depth++;
212 foreach ($thread[$key]['child'] as $childkey) {
213 if (!$admin_view && XOOPS_COMMENT_ACTIVE !== $thread[$childkey]['obj']->getVar('com_status')) {
214 // skip this comment if it is not active and continue on processing its child comments instead
215 if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
216 foreach ($thread[$childkey]['child'] as $childchildkey) {
217 $this->_renderThreadReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
218 }
219 }
220 } else {
221 $this->_renderThreadReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth, $current_prefix);
222 }
223 }
224 }
225 }
226
235 public function renderNestView(int $comment_id = 0, bool $admin_view)
236 {
237 include_once XOOPS_ROOT_PATH.'/class/tree.php';
238 $xot = new XoopsObjectTree($this->_comments, 'com_id', 'com_pid', 'com_rootid');
239 $tree =& $xot->getTree();
240 if (false !== $this->_useIcons) {
241 $title = $this->_getTitleIcon($tree[$comment_id]['obj']->getVar('com_icon')).'&nbsp;'.$tree[$comment_id]['obj']->getVar('com_title');
242 } else {
243 $title = $tree[$comment_id]['obj']->getVar('com_title');
244 }
245 if (false !== $admin_view) {
246 $text = $tree[$comment_id]['obj']->getVar('com_text')
247 .'<div style="text-align:right; margin-top: 2px; margin-bottom: 0px; margin-right: 2px;">'
248 ._CM_STATUS.': '.$this->_statusText[$tree[$comment_id]['obj']->getVar('com_status')]
249 .'<br>IP: <span style="font-weight: bold;">'
250 .$tree[$comment_id]['obj']->getVar('com_ip').'</span></div>';
251 } else if (XOOPS_COMMENT_ACTIVE !== $tree[$comment_id]['obj']->getVar('com_status')) {
252 // if there are any child comments, display them as root comments
253 if (isset($tree[$comment_id]['child']) && !empty($tree[$comment_id]['child'])) {
254 foreach ($tree[$comment_id]['child'] as $child_id) {
255 $this->renderNestView($child_id, $admin_view);
256 }
257 }
258 return;
259 } else {
260 $text = $tree[$comment_id]['obj']->getVar('com_text');
261 }
262 $replies = [];
263 $this->_renderNestReplies($tree, $comment_id, $replies, 25, $admin_view);
264 $this->_tpl->append('comments', [
265 'pid' => $tree[$comment_id]['obj']->getVar('com_pid'),
266 'id' => $tree[$comment_id]['obj']->getVar('com_id'),
267 'itemid' => $tree[$comment_id]['obj']->getVar('com_itemid'),
268 'rootid' => $tree[$comment_id]['obj']->getVar('com_rootid'),
269 'title' => $title, 'text' => $text,
270 'date_posted' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_created'), 'm'),
271 'date_modified' => formatTimestamp($tree[$comment_id]['obj']->getVar('com_modified'), 'm'),
272 'poster' => $this->_getPosterArray($tree[$comment_id]['obj']->getVar('com_uid')),
273 'replies' => $replies
274 ]
275 );
276 }
277
290 public function _renderNestReplies(array &$thread, int $key, array &$replies, string $prefix, bool $admin_view, int $depth = 0)
291 {
292 if ($depth > 0) {
293 if (false !== $this->_useIcons) {
294 $title = $this->_getTitleIcon($thread[$key]['obj']->getVar('com_icon')).'&nbsp;'.$thread[$key]['obj']->getVar('com_title');
295 } else {
296 $title = $thread[$key]['obj']->getVar('com_title');
297 }
298 $text = (false !== $admin_view) ? $thread[$key]['obj']->getVar('com_text')
299 .'<div style="text-align:right; margin-top: 2px; margin-right: 2px;">'
300 ._CM_STATUS.': '.$this->_statusText[$thread[$key]['obj']->getVar('com_status')]
301 .'<br>IP: <span style="font-weight: bold;">'
302 .$thread[$key]['obj']->getVar('com_ip')
303 .'</span></div>' : $thread[$key]['obj']->getVar('com_text');
304 $replies[] = [
305 'id' => $key,
306 'prefix' => $prefix,
307 'pid' => $thread[$key]['obj']->getVar('com_pid'),
308 'itemid' => $thread[$key]['obj']->getVar('com_itemid'),
309 'rootid' => $thread[$key]['obj']->getVar('com_rootid'),
310 'title' => $title,
311 'text' => $text,
312 'date_posted' => formatTimestamp($thread[$key]['obj']->getVar('com_created'), 'm'),
313 'date_modified' => formatTimestamp($thread[$key]['obj']->getVar('com_modified'), 'm'),
314 'poster' => $this->_getPosterArray($thread[$key]['obj']->getVar('com_uid'))
315 ];
316
317 $prefix += 25;
318 }
319 if (isset($thread[$key]['child']) && !empty($thread[$key]['child'])) {
320 $depth++;
321 foreach ($thread[$key]['child'] as $childkey) {
322 if (!$admin_view && XOOPS_COMMENT_ACTIVE !== $thread[$childkey]['obj']->getVar('com_status')) {
323 // skip this comment if it is not active and continue on processing its child comments instead
324 if (isset($thread[$childkey]['child']) && !empty($thread[$childkey]['child'])) {
325 foreach ($thread[$childkey]['child'] as $childchildkey) {
326 $this->_renderNestReplies($thread, $childchildkey, $replies, $prefix, $admin_view, $depth);
327 }
328 }
329 } else {
330 $this->_renderNestReplies($thread, $childkey, $replies, $prefix, $admin_view, $depth);
331 }
332 }
333 }
334 }
335
336
345
346 public function _getPosterName($poster_id)
347 {
348 $poster['id'] = (int)$poster_id;
349 if ($poster['id'] > 0) {
350 $com_poster =& $this->_memberHandler->getUser($poster_id);
351 if (is_object($com_poster)) {
352 $poster['uname'] = '<a href="'.XOOPS_URL.'/userinfo.php?uid='.$poster['id'].'">'.$com_poster->getVar('uname').'</a>';
353 return $poster;
354 }
355 }
356 $poster['id'] = 0; // to cope with deleted user accounts
357 $poster['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
358 return $poster;
359 }
360
369 public function _getPosterArray($poster_id)
370 {
371 $poster['id'] = (int)$poster_id;
372 if ($poster['id'] > 0) {
373 $com_poster =& $this->_memberHandler->getUser($poster['id']);
374 if (is_object($com_poster)) {
375 $poster['uname'] = '<a href="'.XOOPS_URL.'/userinfo.php?uid='.$poster['id'].'">'.$com_poster->getVar('uname').'</a>';
376 $poster_rank = $com_poster->rank();
377 $poster['rank_image'] = ('' !== $poster_rank['image']) ? $poster_rank['image'] : 'blank.gif';
378 $poster['rank_title'] = $poster_rank['title'];
379 $poster['avatar'] = $com_poster->getVar('user_avatar');
380 $poster['regdate'] = formatTimestamp($com_poster->getVar('user_regdate'), 's');
381 $poster['from'] = $com_poster->getVar('user_from');
382 $poster['postnum'] = $com_poster->getVar('posts');
383 $poster['status'] = $com_poster->isOnline() ? _CM_ONLINE : '';
384 return $poster;
385 }
386 }
387 $poster['id'] = 0; // to cope with deleted user accounts
388 $poster['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
389 $poster['rank_title'] = '';
390 $poster['avatar'] = 'blank.gif';
391 $poster['regdate'] = '';
392 $poster['from'] = '';
393 $poster['postnum'] = 0;
394 $poster['status'] = '';
395 return $poster;
396 }
397
406 public function _getTitleIcon($icon_image)
407 {
408 $icon_image = trim($icon_image);
409 if ('' !== $icon_image) {
410 $icon_image = htmlspecialchars($icon_image);
411 if ((false !== $this->_doIconCheck) && !file_exists(XOOPS_URL . '/images/subject/' . $icon_image)) {
412 return '<img src="'.XOOPS_URL.'/images/icons/no_posticon.svg" alt="" />';
413 }
414
415 return '<img src="'.XOOPS_URL.'/images/subject/'.$icon_image.'" alt="" />';
416 }
417 return '<img src="'.XOOPS_URL.'/images/icons/no_posticon.svg" alt="" />';
418 }
419}
& instance(&$tpl, $use_icons=true, $do_iconcheck=false)
renderFlatView($admin_view=false)
setComments(&$comments_arr)
renderThreadView(int $comment_id=0, bool $admin_view, bool $show_nav=true)
_renderThreadReplies(array &$thread, int $key, array &$replies, string $prefix, bool $admin_view, int $depth=0, string $current_prefix='')
__construct(&$tpl, $use_icons=true, $do_iconcheck=false)
_renderNestReplies(array &$thread, int $key, array &$replies, string $prefix, bool $admin_view, int $depth=0)
renderNestView(int $comment_id=0, bool $admin_view)