| Total Complexity | 73 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsComments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsComments, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class XoopsComments extends XoopsObject |
||
| 38 | { |
||
| 39 | public $ctable; |
||
| 40 | /** |
||
| 41 | * @var \XoopsMySQLDatabase |
||
| 42 | */ |
||
| 43 | public $db; |
||
| 44 | //PHP 8.2 Dynamic properties deprecated |
||
| 45 | public $comment_id; |
||
| 46 | public $item_id; |
||
| 47 | public $order; |
||
| 48 | public $mode; |
||
| 49 | public $subject; |
||
| 50 | public $comment; |
||
| 51 | public $ip; |
||
| 52 | public $pid; |
||
| 53 | public $date; |
||
| 54 | public $nohtml; |
||
| 55 | public $nosmiley; |
||
| 56 | public $noxcode; |
||
| 57 | public $user_id; |
||
| 58 | public $icon; |
||
| 59 | public $prefix; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $ctable |
||
| 63 | * @param null|array $id |
||
| 64 | */ |
||
| 65 | public function __construct($ctable, $id = null) |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Load Comment by ID |
||
| 96 | * |
||
| 97 | * @param int $id |
||
| 98 | */ |
||
| 99 | public function load($id) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Save Comment |
||
| 117 | * |
||
| 118 | * @return int|false |
||
| 119 | */ |
||
| 120 | public function store() |
||
| 121 | { |
||
| 122 | if (!$this->cleanVars()) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | foreach ($this->cleanVars as $k => $v) { |
||
| 126 | ${$k} = $v; |
||
| 127 | } |
||
| 128 | $isnew = false; |
||
| 129 | if (empty($comment_id)) { |
||
| 130 | $isnew = true; |
||
| 131 | $comment_id = $this->db->genId($this->ctable . '_comment_id_seq'); |
||
| 132 | $sql = sprintf("INSERT INTO %s (comment_id, pid, item_id, date, user_id, ip, subject, comment, nohtml, nosmiley, noxcode, icon) VALUES (%u, %u, %u, %u, %u, '%s', '%s', '%s', %u, %u, %u, '%s')", $this->ctable, $comment_id, $pid, $item_id, time(), $user_id, $ip, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon); |
||
| 133 | } else { |
||
| 134 | $sql = sprintf("UPDATE %s SET subject = '%s', comment = '%s', nohtml = %u, nosmiley = %u, noxcode = %u, icon = '%s' WHERE comment_id = %u", $this->ctable, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon, $comment_id); |
||
| 135 | } |
||
| 136 | if (!$result = $this->db->exec($sql)) { |
||
| 137 | //echo $sql; |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | if (empty($comment_id)) { |
||
| 141 | $comment_id = $this->db->getInsertId(); |
||
| 142 | } |
||
| 143 | if ($isnew != false) { |
||
| 144 | $sql = sprintf('UPDATE %s SET posts = posts+1 WHERE uid = %u', $this->db->prefix('users'), $user_id); |
||
| 145 | if (!$result = $this->db->exec($sql)) { |
||
| 146 | echo 'Could not update user posts.'; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return $comment_id; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Enter description here... |
||
| 155 | * |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | public function delete() |
||
| 159 | { |
||
| 160 | $sql = sprintf('DELETE FROM %s WHERE comment_id = %u', $this->ctable, $this->getVar('comment_id')); |
||
| 161 | if (!$result = $this->db->exec($sql)) { |
||
| 162 | return false; |
||
| 163 | } |
||
| 164 | $sql = sprintf('UPDATE %s SET posts = posts-1 WHERE uid = %u', $this->db->prefix('users'), $this->getVar('user_id')); |
||
| 165 | if (!$result = $this->db->exec($sql)) { |
||
| 166 | echo 'Could not update user posts.'; |
||
| 167 | } |
||
| 168 | $mytree = new XoopsTree($this->ctable, 'comment_id', 'pid'); |
||
| 169 | $arr = $mytree->getAllChild($this->getVar('comment_id'), 'comment_id'); |
||
| 170 | $size = count($arr); |
||
| 171 | if ($size > 0) { |
||
| 172 | for ($i = 0; $i < $size; ++$i) { |
||
| 173 | $sql = sprintf('DELETE FROM %s WHERE comment_bid = %u', $this->ctable, $arr[$i]['comment_id']); |
||
| 174 | if (!$result = $this->db->exec($sql)) { |
||
| 175 | echo 'Could not delete comment.'; |
||
| 176 | } |
||
| 177 | $sql = sprintf('UPDATE %s SET posts = posts-1 WHERE uid = %u', $this->db->prefix('users'), $arr[$i]['user_id']); |
||
| 178 | if (!$result = $this->db->exec($sql)) { |
||
| 179 | echo 'Could not update user posts.'; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return ($size + 1); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get Comments Tree |
||
| 189 | * |
||
| 190 | * @return mixed |
||
| 191 | */ |
||
| 192 | public function getCommentTree() |
||
| 193 | { |
||
| 194 | $mytree = new XoopsTree($this->ctable, 'comment_id', 'pid'); |
||
| 195 | $ret = []; |
||
| 196 | $tarray = $mytree->getChildTreeArray($this->getVar('comment_id'), 'comment_id'); |
||
| 197 | foreach ($tarray as $ele) { |
||
| 198 | $ret[] = new XoopsComments($this->ctable, $ele); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $ret; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get All Comments using criteria match |
||
| 206 | * |
||
| 207 | * @param array $criteria |
||
| 208 | * @param bool $asobject |
||
| 209 | * @param string $orderby |
||
| 210 | * @param int $limit |
||
| 211 | * @param int $start |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function getAllComments($criteria = [], $asobject = true, $orderby = 'comment_id ASC', $limit = 0, $start = 0) |
||
| 215 | { |
||
| 216 | $ret = []; |
||
| 217 | $where_query = ''; |
||
| 218 | if (!empty($criteria) && \is_array($criteria)) { |
||
| 219 | $where_query = ' WHERE'; |
||
| 220 | foreach ($criteria as $c) { |
||
| 221 | $where_query .= " $c AND"; |
||
| 222 | } |
||
| 223 | $where_query = substr($where_query, 0, -4); |
||
| 224 | } |
||
| 225 | if (!$asobject) { |
||
| 226 | $sql = 'SELECT comment_id FROM ' . $this->ctable . "$where_query ORDER BY $orderby"; |
||
| 227 | $result = $this->db->query($sql, $limit, $start); |
||
| 228 | if (!$this->db->isResultSet($result)) { |
||
| 229 | throw new \RuntimeException( |
||
| 230 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), |
||
| 231 | E_USER_ERROR, |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | /** @var array $myrow */ |
||
| 235 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 236 | $ret[] = $myrow['comment_id']; |
||
| 237 | } |
||
| 238 | } else { |
||
| 239 | $sql = 'SELECT * FROM ' . $this->ctable . '' . $where_query . " ORDER BY $orderby"; |
||
| 240 | $result = $this->db->query($sql, $limit, $start); |
||
| 241 | if (!$this->db->isResultSet($result)) { |
||
| 242 | throw new \RuntimeException( |
||
| 243 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), |
||
| 244 | E_USER_ERROR, |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | /** @var array $myrow */ |
||
| 248 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 249 | $ret[] = new XoopsComments($this->ctable, $myrow); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | //echo $sql; |
||
| 254 | return $ret; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Enter printNavBar |
||
| 259 | * |
||
| 260 | * @param int $item_id |
||
| 261 | * @param string $mode |
||
| 262 | * @param int $order |
||
| 263 | */ |
||
| 264 | public function printNavBar($item_id, $mode = 'flat', $order = 1) |
||
| 265 | { |
||
| 266 | global $xoopsConfig, $xoopsUser; |
||
| 267 | echo "<form method='get' action='" . $_SERVER['PHP_SELF'] . "'><table width='100%' border='0' cellspacing='1' cellpadding='2'><tr><td class='bg1' align='center'><select name='mode'><option value='nocomments'"; |
||
| 268 | if ($mode === 'nocomments') { |
||
| 269 | echo " selected"; |
||
| 270 | } |
||
| 271 | echo '>' . _NOCOMMENTS . "</option><option value='flat'"; |
||
| 272 | if ($mode === 'flat') { |
||
| 273 | echo " selected"; |
||
| 274 | } |
||
| 275 | echo '>' . _FLAT . "</option><option value='thread'"; |
||
| 276 | if ($mode === 'thread' || $mode == '') { |
||
| 277 | echo " selected"; |
||
| 278 | } |
||
| 279 | echo '>' . _THREADED . "</option></select><select name='order'><option value='0'"; |
||
| 280 | if ($order != 1) { |
||
| 281 | echo " selected"; |
||
| 282 | } |
||
| 283 | echo '>' . _OLDESTFIRST . "</option><option value='1'"; |
||
| 284 | if ($order == 1) { |
||
| 285 | echo " selected"; |
||
| 286 | } |
||
| 287 | echo '>' . _NEWESTFIRST . "</option></select><input type='hidden' name='item_id' value='" . (int) $item_id . "' /><input type='submit' value='" . _CM_REFRESH . "' />"; |
||
| 288 | if ($xoopsConfig['anonpost'] == 1 || $xoopsUser) { |
||
| 289 | if ($mode !== 'flat' || $mode !== 'nocomments' || $mode !== 'thread') { |
||
| 290 | $mode = 'flat'; |
||
| 291 | } |
||
| 292 | echo " <input type='button' onclick='location=\"newcomment.php?item_id=" . (int) $item_id . '&order=' . (int) $order . '&mode=' . $mode . "\"' value='" . _CM_POSTCOMMENT . "' />"; |
||
| 293 | } |
||
| 294 | echo '</td></tr></table></form>'; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Show Thread |
||
| 299 | * |
||
| 300 | */ |
||
| 301 | public function showThreadHead() |
||
| 302 | { |
||
| 303 | openThread(); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Enter description here... |
||
| 308 | * |
||
| 309 | * @param string $order |
||
| 310 | * @param string $mode |
||
| 311 | * @param int $adminview |
||
| 312 | * @param int $color_num |
||
| 313 | */ |
||
| 314 | public function showThreadPost($order, $mode, $adminview = 0, $color_num = 1) |
||
| 315 | { |
||
| 316 | global $xoopsConfig, $xoopsUser; |
||
| 317 | $edit_image = ''; |
||
| 318 | $reply_image = ''; |
||
| 319 | $delete_image = ''; |
||
| 320 | $post_date = formatTimestamp($this->getVar('date'), 'm'); |
||
| 321 | if ($this->getVar('user_id') != 0) { |
||
| 322 | $poster = new XoopsUser($this->getVar('user_id')); |
||
| 323 | if (!$poster->isActive()) { |
||
| 324 | $poster = 0; |
||
| 325 | } |
||
| 326 | } else { |
||
| 327 | $poster = 0; |
||
| 328 | } |
||
| 329 | if ($this->getVar('icon') != null && $this->getVar('icon') != '') { |
||
| 330 | $subject_image = "<a name='" . $this->getVar('comment_id') . "' id='" . $this->getVar('comment_id') . "'></a><img src='" . XOOPS_URL . '/images/subject/' . $this->getVar('icon') . "' alt='' />"; |
||
| 331 | } else { |
||
| 332 | $subject_image = "<a name='" . $this->getVar('comment_id') . "' id='" . $this->getVar('comment_id') . "'></a><img src='" . XOOPS_URL . "/images/icons/no_posticon.gif' alt='' />"; |
||
| 333 | } |
||
| 334 | if ($adminview) { |
||
| 335 | $ip_image = "<img src='" . XOOPS_URL . "/images/icons/ip.gif' alt='" . $this->getVar('ip') . "' />"; |
||
| 336 | } else { |
||
| 337 | $ip_image = "<img src='" . XOOPS_URL . "/images/icons/ip.gif' alt='' />"; |
||
| 338 | } |
||
| 339 | if ($adminview || ($xoopsUser && $this->getVar('user_id') == $xoopsUser->getVar('uid'))) { |
||
| 340 | $edit_image = "<a href='editcomment.php?comment_id=" . $this->getVar('comment_id') . '&mode=' . $mode . '&order=' . (int) $order . "'><img src='" . XOOPS_URL . "/images/icons/edit.gif' alt='" . _EDIT . "' /></a>"; |
||
| 341 | } |
||
| 342 | if ($xoopsConfig['anonpost'] || $xoopsUser) { |
||
| 343 | $reply_image = "<a href='replycomment.php?comment_id=" . $this->getVar('comment_id') . '&mode=' . $mode . '&order=' . (int) $order . "'><img src='" . XOOPS_URL . "/images/icons/reply.gif' alt='" . _REPLY . "' /></a>"; |
||
| 344 | } |
||
| 345 | if ($adminview) { |
||
| 346 | $delete_image = "<a href='deletecomment.php?comment_id=" . $this->getVar('comment_id') . '&mode=' . $mode . '&order=' . (int) $order . "'><img src='" . XOOPS_URL . "/images/icons/delete.gif' alt='" . _DELETE . "' /></a>"; |
||
| 347 | } |
||
| 348 | |||
| 349 | if ($poster) { |
||
| 350 | $text = $this->getVar('comment'); |
||
| 351 | if ($poster->getVar('attachsig')) { |
||
| 352 | $text .= '<p><br>_________________<br>' . $poster->user_sig() . '</p>'; |
||
| 353 | } |
||
| 354 | $reg_date = _CM_JOINED; |
||
| 355 | $reg_date .= formatTimestamp($poster->getVar('user_regdate'), 's'); |
||
| 356 | $posts = _CM_POSTS; |
||
| 357 | $posts .= $poster->getVar('posts'); |
||
| 358 | $user_from = _CM_FROM; |
||
| 359 | $user_from .= $poster->getVar('user_from'); |
||
| 360 | $rank = $poster->rank(); |
||
| 361 | if ($rank['image'] != '') { |
||
| 362 | $rank['image'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $rank['image'] . "' alt='' />"; |
||
| 363 | } |
||
| 364 | $avatar_image = "<img src='" . XOOPS_UPLOAD_URL . '/' . $poster->getVar('user_avatar') . "' alt='' />"; |
||
| 365 | $online_image = ''; |
||
| 366 | if ($poster->isOnline()) { |
||
| 367 | $online_image = "<span style='color:#ee0000;font-weight:bold;'>" . _CM_ONLINE . '</span>'; |
||
| 368 | } |
||
| 369 | $profile_image = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $poster->getVar('uid') . "'><img src='" . XOOPS_URL . "/images/icons/profile.gif' alt='" . _PROFILE . "' /></a>"; |
||
| 370 | $pm_image = ''; |
||
| 371 | if ($xoopsUser) { |
||
| 372 | $pm_image = "<a href='javascript:openWithSelfMain(\"" . XOOPS_URL . '/pmlite.php?send2=1&to_userid=' . $poster->getVar('uid') . "\",\"pmlite\",565,500);'><img src='" . XOOPS_URL . "/images/icons/pm.gif' alt='" . sprintf(_SENDPMTO, $poster->getVar('uname', 'E')) . "' /></a>"; |
||
| 373 | } |
||
| 374 | $email_image = ''; |
||
| 375 | if ($poster->getVar('user_viewemail')) { |
||
| 376 | $email_image = "<a href='mailto:" . $poster->getVar('email', 'E') . "'><img src='" . XOOPS_URL . "/images/icons/email.gif' alt='" . sprintf(_SENDEMAILTO, $poster->getVar('uname', 'E')) . "' /></a>"; |
||
| 377 | } |
||
| 378 | $posterurl = $poster->getVar('url'); |
||
| 379 | $www_image = ''; |
||
| 380 | if ($posterurl != '') { |
||
| 381 | $www_image = "<a href='$posterurl' rel='external'><img src='" . XOOPS_URL . "/images/icons/www.gif' alt='" . _VISITWEBSITE . "' /></a>"; |
||
| 382 | } |
||
| 383 | $icq_image = ''; |
||
| 384 | if ($poster->getVar('user_icq') != '') { |
||
| 385 | $icq_image = "<a href='http://wwp.icq.com/scripts/search.dll?to=" . $poster->getVar('user_icq', 'E') . "'><img src='" . XOOPS_URL . "/images/icons/icq_add.gif' alt='" . _ADD . "' /></a>"; |
||
| 386 | } |
||
| 387 | $aim_image = ''; |
||
| 388 | if ($poster->getVar('user_aim') != '') { |
||
| 389 | $aim_image = "<a href='aim:goim?screenname=" . $poster->getVar('user_aim', 'E') . '&message=Hi+' . $poster->getVar('user_aim') . "+Are+you+there?'><img src='" . XOOPS_URL . "/images/icons/aim.gif' alt='aim' /></a>"; |
||
| 390 | } |
||
| 391 | $yim_image = ''; |
||
| 392 | if ($poster->getVar('user_yim') != '') { |
||
| 393 | $yim_image = "<a href='http://edit.yahoo.com/config/send_webmesg?.target=" . $poster->getVar('user_yim', 'E') . "&.src=pg'><img src='" . XOOPS_URL . "/images/icons/yim.gif' alt='yim' /></a>"; |
||
| 394 | } |
||
| 395 | $msnm_image = ''; |
||
| 396 | if ($poster->getVar('user_msnm') != '') { |
||
| 397 | $msnm_image = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $poster->getVar('uid') . "'><img src='" . XOOPS_URL . "/images/icons/msnm.gif' alt='msnm' /></a>"; |
||
| 398 | } |
||
| 399 | showThread($color_num, $subject_image, $this->getVar('subject'), $text, $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $poster->getVar('uname'), $rank['title'], $rank['image'], $avatar_image, $reg_date, $posts, $user_from, $online_image, $profile_image, $pm_image, $email_image, $www_image, $icq_image, $aim_image, $yim_image, $msnm_image); |
||
| 400 | } else { |
||
| 401 | showThread($color_num, $subject_image, $this->getVar('subject'), $this->getVar('comment'), $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $xoopsConfig['anonymous']); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Show Thread Footer |
||
| 407 | * |
||
| 408 | */ |
||
| 409 | public function showThreadFoot() |
||
| 410 | { |
||
| 411 | closeThread(); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Show Thread Head |
||
| 416 | * |
||
| 417 | * @param int|string $width |
||
| 418 | */ |
||
| 419 | public function showTreeHead($width = '100%') |
||
| 420 | { |
||
| 421 | echo "<table border='0' class='outer' cellpadding='0' cellspacing='0' align='center' width='$width'><tr class='bg3' align='center'><td colspan='3'>" . _CM_REPLIES . "</td></tr><tr class='bg3' align='left'><td width='60%' class='fg2'>" . _CM_TITLE . "</td><td width='20%' class='fg2'>" . _CM_POSTER . "</td><td class='fg2'>" . _CM_POSTED . '</td></tr>'; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Show Tree Items |
||
| 426 | * |
||
| 427 | * @param string $order |
||
| 428 | * @param string $mode |
||
| 429 | * @param int $color_num |
||
| 430 | */ |
||
| 431 | public function showTreeItem($order, $mode, $color_num) |
||
| 432 | { |
||
| 433 | $bg = 'odd'; |
||
| 434 | if ($color_num == 1) { |
||
| 435 | $bg = 'even'; |
||
| 436 | } |
||
| 437 | $prefix = str_replace('.', ' ', $this->getVar('prefix')); |
||
| 438 | $date = formatTimestamp($this->getVar('date'), 'm'); |
||
| 439 | $icon = 'icons/no_posticon.gif'; |
||
| 440 | if ($this->getVar('icon') != '') { |
||
| 441 | $icon = 'subject/' . $this->getVar('icon', 'E'); |
||
| 442 | } |
||
| 443 | echo "<tr class='$bg' align='left'><td>" . $prefix . "<img src='" . XOOPS_URL . '/images/' . $icon . "'> <a href='" . $_SERVER['PHP_SELF'] . '?item_id=' . $this->getVar('item_id') . '&comment_id=' . $this->getVar('comment_id') . '&mode=' . $mode . '&order=' . $order . '#' . $this->getVar('comment_id') . "'>" . $this->getVar('subject') . "</a></td><td><a href='" . XOOPS_URL . '/userinfo.php?uid=' . $this->getVar('user_id') . "'>" . XoopsUser::getUnameFromId($this->getVar('user_id')) . '</a></td><td>' . $date . '</td></tr>'; |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Show Thread Foot |
||
| 448 | * |
||
| 449 | */ |
||
| 450 | public function showTreeFoot() |
||
| 453 | } |
||
| 454 | } |
||
| 455 |