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