| Total Complexity | 73 |
| Total Lines | 463 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Tree |
||
| 33 | { |
||
| 34 | public $table; |
||
| 35 | public $id; |
||
| 36 | public $pid; |
||
| 37 | public $order; |
||
| 38 | public $title; |
||
| 39 | /** @var \XoopsMySQLDatabase $db */ |
||
| 40 | public $db; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $table_name |
||
| 44 | * @param $id_name |
||
| 45 | * @param $pid_name |
||
| 46 | */ |
||
| 47 | public function __construct($table_name, $id_name, $pid_name) |
||
| 48 | { |
||
| 49 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 50 | $this->table = $table_name; |
||
| 51 | $this->id = $id_name; |
||
| 52 | $this->pid = $pid_name; |
||
| 53 | $this->order = ''; |
||
| 54 | $this->title = ''; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param int $sel_id |
||
| 59 | * @param string $order |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function getFirstChild($sel_id, $order = ''): array |
||
| 64 | { |
||
| 65 | $arr = []; |
||
| 66 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' '; |
||
| 67 | |||
| 68 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 69 | if (\is_array($categories) && $categories !== []) { |
||
| 70 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 71 | } |
||
| 72 | |||
| 73 | if ('' != $order) { |
||
| 74 | $sql .= " ORDER BY $order"; |
||
| 75 | } |
||
| 76 | |||
| 77 | $result = $this->db->query($sql); |
||
| 78 | $count = $this->db->getRowsNum($result); |
||
|
|
|||
| 79 | if (0 == $count) { |
||
| 80 | return $arr; |
||
| 81 | } |
||
| 82 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 83 | $arr[] = $myrow; |
||
| 84 | } |
||
| 85 | |||
| 86 | return $arr; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param $sel_id |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getFirstChildId($sel_id): array |
||
| 95 | { |
||
| 96 | $idarray = []; |
||
| 97 | $sel_id = (int)$sel_id; |
||
| 98 | $result = $this->db->query('SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id); |
||
| 99 | |||
| 100 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 101 | if (\is_array($categories) && $categories !== []) { |
||
| 102 | $result .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 103 | } |
||
| 104 | |||
| 105 | $count = $this->db->getRowsNum($result); |
||
| 106 | if (0 == $count) { |
||
| 107 | return $idarray; |
||
| 108 | } |
||
| 109 | while (false !== (list($id) = $this->db->fetchRow($result))) { |
||
| 110 | $idarray[] = $id; |
||
| 111 | } |
||
| 112 | |||
| 113 | return $idarray; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param $sel_id |
||
| 118 | * @param string $order |
||
| 119 | * @param array $idarray |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getAllChildId($sel_id, $order = '', $idarray = []): array |
||
| 124 | { |
||
| 125 | $sel_id = (int)$sel_id; |
||
| 126 | $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 127 | |||
| 128 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 129 | if (\is_array($categories) && $categories !== []) { |
||
| 130 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ('' != $order) { |
||
| 134 | $sql .= " ORDER BY {$order}"; |
||
| 135 | } |
||
| 136 | $result = $this->db->query($sql); |
||
| 137 | $count = $this->db->getRowsNum($result); |
||
| 138 | if (0 == $count) { |
||
| 139 | return $idarray; |
||
| 140 | } |
||
| 141 | while (false !== (list($r_id) = $this->db->fetchRow($result))) { |
||
| 142 | $idarray[] = $r_id; |
||
| 143 | $idarray = $this->getAllChildId($r_id, $order, $idarray); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $idarray; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $sel_id |
||
| 151 | * @param string $order |
||
| 152 | * @param array $idarray |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getAllParentId($sel_id, $order = '', $idarray = []): array |
||
| 157 | { |
||
| 158 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id; |
||
| 159 | |||
| 160 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 161 | if (\is_array($categories) && $categories !== []) { |
||
| 162 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ('' != $order) { |
||
| 166 | $sql .= " ORDER BY {$order}"; |
||
| 167 | } |
||
| 168 | $result = $this->db->query($sql); |
||
| 169 | [$r_id] = $this->db->fetchRow($result); |
||
| 170 | if (0 == $r_id) { |
||
| 171 | return $idarray; |
||
| 172 | } |
||
| 173 | $idarray[] = $r_id; |
||
| 174 | |||
| 175 | return $this->getAllParentId($r_id, $order, $idarray); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param $sel_id |
||
| 180 | * @param $title |
||
| 181 | * @param string $path |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getPathFromId($sel_id, $title, $path = ''): string |
||
| 186 | { |
||
| 187 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id . ''; |
||
| 188 | // $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . "'"); |
||
| 189 | |||
| 190 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 191 | if (\is_array($categories) && $categories !== []) { |
||
| 192 | // $result .= ' AND cid IN (' . implode(',', $categories) . ') '; |
||
| 193 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 194 | } |
||
| 195 | |||
| 196 | $result = $this->db->query($sql); |
||
| 197 | |||
| 198 | if (0 == $this->db->getRowsNum($result)) { |
||
| 199 | return $path; |
||
| 200 | } |
||
| 201 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 202 | \MyTextSanitizer::getInstance(); |
||
| 203 | $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5); |
||
| 204 | $path = '/' . $name . $path . ''; |
||
| 205 | if (0 == $parentid) { |
||
| 206 | return $path; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $this->getPathFromId($parentid, $title, $path); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $title |
||
| 214 | * @param string $order |
||
| 215 | * @param int $preset_id |
||
| 216 | * @param int $none |
||
| 217 | * @param string $sel_name |
||
| 218 | * @param string $onchange |
||
| 219 | */ |
||
| 220 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
| 221 | { |
||
| 222 | if ('' == $sel_name) { |
||
| 223 | $sel_name = $this->id; |
||
| 224 | } |
||
| 225 | $myts = \MyTextSanitizer::getInstance(); |
||
| 226 | echo '<select name="' . $sel_name . '"'; |
||
| 227 | if ('' !== $onchange) { |
||
| 228 | echo ' onchange="' . $onchange . '"'; |
||
| 229 | } |
||
| 230 | echo '>'; |
||
| 231 | |||
| 232 | $sql = 'SELECT SQL_CACHE cid, title FROM ' . $this->table . ' WHERE pid=0'; |
||
| 233 | $categories = Utility::getMyItemIds('adslight_submit'); |
||
| 234 | |||
| 235 | if (\is_array($categories) && $categories !== []) { |
||
| 236 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 237 | } |
||
| 238 | |||
| 239 | if ('' != $order) { |
||
| 240 | $sql .= " ORDER BY $order"; |
||
| 241 | } |
||
| 242 | |||
| 243 | $result = $this->db->query($sql); |
||
| 244 | if (0 !== $none) { |
||
| 245 | echo '<option value="0">----</option>'; |
||
| 246 | } |
||
| 247 | while (false !== (list($catid, $name) = $this->db->fetchRow($result))) { |
||
| 248 | $sel = ''; |
||
| 249 | if ($catid == $preset_id) { |
||
| 250 | $sel = ' selected'; |
||
| 251 | } |
||
| 252 | echo "<option value=\"{$catid}\"{$sel}>{$name}</option>"; |
||
| 253 | $sel = ''; |
||
| 254 | $arr = $this->getChildTreeArray($catid, $order); |
||
| 255 | foreach ($arr as $option) { |
||
| 256 | $option['prefix'] = \str_replace('.', '--', $option['prefix']); |
||
| 257 | $catpath = $option['prefix'] . ' ' . $myts->displayTarea($option[$title]); |
||
| 258 | if ($option['cid'] == $preset_id) { |
||
| 259 | $sel = ' selected'; |
||
| 260 | } |
||
| 261 | echo "<option value=\"{$option['cid']}\"{$sel}>{$catpath}</option>"; |
||
| 262 | $sel = ''; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | echo '</select>'; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param $sel_id |
||
| 270 | * @param $title |
||
| 271 | * @param $funcURL |
||
| 272 | * @param string $path |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = ''): string |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $sel_id |
||
| 300 | * @param string $path |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function getIdPathFromId($sel_id, $path = ''): string |
||
| 305 | { |
||
| 306 | $sel_id = (int)$sel_id; |
||
| 307 | $result = $this->db->query('SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id); |
||
| 308 | if (0 == $this->db->getRowsNum($result)) { |
||
| 309 | return $path; |
||
| 310 | } |
||
| 311 | [$parentid] = $this->db->fetchRow($result); |
||
| 312 | $path = "/{$sel_id}{$path}"; |
||
| 313 | if (0 == $parentid) { |
||
| 314 | return $path; |
||
| 315 | } |
||
| 316 | |||
| 317 | return $this->getIdPathFromId($parentid, $path); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param int $sel_id |
||
| 322 | * @param string $order |
||
| 323 | * @param array $parray |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | public function getAllChild($sel_id = 0, $order = '', $parray = []): array |
||
| 328 | { |
||
| 329 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 330 | |||
| 331 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 332 | if (\is_array($categories) && $categories !== []) { |
||
| 333 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 334 | } |
||
| 335 | |||
| 336 | if ('' != $order) { |
||
| 337 | $sql .= " ORDER BY {$order}"; |
||
| 338 | } |
||
| 339 | |||
| 340 | $result = $this->db->query($sql); |
||
| 341 | $count = $this->db->getRowsNum($result); |
||
| 342 | if (0 == $count) { |
||
| 343 | return $parray; |
||
| 344 | } |
||
| 345 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 346 | $parray[] = $row; |
||
| 347 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 348 | } |
||
| 349 | |||
| 350 | return $parray; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param int $sel_id |
||
| 355 | * @param string $order |
||
| 356 | * @param array $parray |
||
| 357 | * @param string $r_prefix |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
| 362 | { |
||
| 363 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id; |
||
| 364 | |||
| 365 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 366 | if (\is_array($categories) && $categories !== []) { |
||
| 367 | $sql .= ' AND cid IN (' . \implode(',', $categories) . ') '; |
||
| 368 | } |
||
| 369 | |||
| 370 | if ('' != $order) { |
||
| 371 | $sql .= " ORDER BY {$order}"; |
||
| 372 | } |
||
| 373 | $result = $this->db->query($sql); |
||
| 374 | $count = $this->db->getRowsNum($result); |
||
| 375 | if (0 == $count) { |
||
| 376 | return $parray; |
||
| 377 | } |
||
| 378 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 379 | $row['prefix'] = $r_prefix . '.'; |
||
| 380 | $parray[] = $row; |
||
| 381 | $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 382 | } |
||
| 383 | |||
| 384 | return $parray; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param $title |
||
| 389 | * @param string $order |
||
| 390 | * @param int $preset_id |
||
| 391 | * @param int $none |
||
| 392 | * @param string $sel_name |
||
| 393 | * @param string $onchange |
||
| 394 | */ |
||
| 395 | public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = ''): void |
||
| 396 | { |
||
| 397 | global $myts, $xoopsDB; |
||
| 398 | $pathIcon16 = Admin::iconUrl('', 16); |
||
| 399 | // require_once XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php'; |
||
| 400 | |||
| 401 | if ('' == $sel_name) { |
||
| 402 | $sel_name = $this->id; |
||
| 403 | } |
||
| 404 | |||
| 405 | $sql = 'SELECT ' . $this->id . ', ' . $title . ', cat_order FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 406 | if ('' != $order) { |
||
| 407 | $sql .= " ORDER BY {$order}"; |
||
| 408 | } |
||
| 409 | $result = $xoopsDB->query($sql); |
||
| 410 | while (false !== (list($catid, $name, $cat_order) = $xoopsDB->fetchRow($result))) { |
||
| 411 | echo '<table class="width100 bnone outer"><tr> |
||
| 412 | <th class="left">'; |
||
| 413 | if ('cat_order' === $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 414 | echo "({$cat_order})"; |
||
| 415 | } |
||
| 416 | echo " {$name} </th> |
||
| 417 | <th class=\"center width10\"><a href=\"category.php?op=AdsNewCat&cid={$catid}\"><img src=\"{$pathIcon16}/add.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . \_AM_ADSLIGHT_ADDSUBCAT . '" title="' . \_AM_ADSLIGHT_ADDSUBCAT . "\"></a></th> |
||
| 418 | <th class=\"center width10\"><a href=\"category.php?op=AdsModCat&cid={$catid}\"><img src=\"{$pathIcon16}/edit.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . \_AM_ADSLIGHT_MODIFSUBCAT . '" title="' . \_AM_ADSLIGHT_MODIFSUBCAT . "\"></a></th> |
||
| 419 | <th class=\"center width10\"><a href=\"category.php?op=AdsDelCat&cid={$catid}\"><img src=\"{$pathIcon16}/delete.png\" border=\"0\" width=\"18\" height=\"18\" alt=\"" . \_AM_ADSLIGHT_DELSUBCAT . '" title="' . \_AM_ADSLIGHT_DELSUBCAT . '"></a></th> |
||
| 420 | </tr>'; |
||
| 421 | |||
| 422 | $arr = $this->getChildTreeMapArray($catid, $order); |
||
| 423 | $class = 'odd'; |
||
| 424 | foreach ($arr as $option) { |
||
| 425 | echo "<tr class=\"{$class}\"><td>"; |
||
| 426 | |||
| 427 | $option['prefix'] = \str_replace('.', ' - ', $option['prefix']); |
||
| 428 | $catpath = $option['prefix'] . ' ' . \htmlspecialchars($option[$title], \ENT_QUOTES | \ENT_HTML5); |
||
| 429 | $cat_orderS = $option['cat_order']; |
||
| 430 | if ('cat_order' === $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) { |
||
| 431 | echo "({$cat_orderS})"; |
||
| 432 | } |
||
| 433 | echo '' . $catpath . '</a></td> |
||
| 434 | <td align="center"><a href="category.php?op=AdsNewCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/add.png' . '" border=0 width=18 height=18 alt="' . \_AM_ADSLIGHT_ADDSUBCAT . '"title="' . \_AM_ADSLIGHT_ADDSUBCAT . '"></a></td> |
||
| 435 | <td align="center"><a href="category.php?op=AdsModCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/edit.png' . '" border=0 width=18 height=18 alt="' . \_AM_ADSLIGHT_MODIFSUBCAT . '" title ="' . \_AM_ADSLIGHT_MODIFSUBCAT . '"></a></td> |
||
| 436 | <td align="center"><a href="category.php?op=AdsDelCat&cid=' . $option[$this->id] . '"><img src="' . $pathIcon16 . '/delete.png' . '" border=0 width=18 height=18 alt="' . \_AM_ADSLIGHT_DELSUBCAT . '" title="' . \_AM_ADSLIGHT_DELSUBCAT . '"></a></td>'; |
||
| 437 | |||
| 438 | $class = ('even' === $class) ? 'odd' : 'even'; |
||
| 439 | } |
||
| 440 | echo '</td></tr></table><br>'; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param int $sel_id |
||
| 446 | * @param string $order |
||
| 447 | * @param array $parray |
||
| 448 | * @param string $r_prefix |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = [], $r_prefix = ''): array |
||
| 453 | { |
||
| 454 | global $xoopsDB; |
||
| 455 | $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' '; |
||
| 456 | |||
| 457 | $categories = Utility::getMyItemIds('adslight_view'); |
||
| 458 | if (\is_array($categories) && $categories !== []) { |
||
| 459 | $sql .= ' AND ' . $this->pid . ' IN (' . \implode(',', $categories) . ') '; |
||
| 460 | } |
||
| 461 | |||
| 462 | if ('' != $order) { |
||
| 463 | $sql .= " ORDER BY {$order}"; |
||
| 464 | } |
||
| 465 | $result = $xoopsDB->query($sql); |
||
| 466 | $count = $xoopsDB->getRowsNum($result); |
||
| 467 | if (0 == $count) { |
||
| 468 | return $parray; |
||
| 469 | } |
||
| 470 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 471 | $row['prefix'] = $r_prefix . '.'; |
||
| 472 | $parray[] = $row; |
||
| 473 | $parray = $this->getChildTreeMapArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 474 | } |
||
| 475 | |||
| 476 | return $parray; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | public function getCategoryList(): array |
||
| 495 | } |
||
| 496 | } |
||
| 497 |