| Total Complexity | 42 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsTree 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 XoopsTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class XoopsTree |
||
| 34 | { |
||
| 35 | public $table; //table with parent-child structure |
||
| 36 | public $id; //name of unique id for records in table $table |
||
| 37 | public $pid; // name of parent id used in table $table |
||
| 38 | public $order; //specifies the order of query results |
||
| 39 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
| 40 | public $db; |
||
| 41 | //constructor of class XoopsTree |
||
| 42 | //sets the names of table, unique id, and parend id |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $table_name |
||
| 46 | * @param $id_name |
||
| 47 | * @param $pid_name |
||
| 48 | */ |
||
| 49 | public function __construct($table_name, $id_name, $pid_name) |
||
| 50 | { |
||
| 51 | // $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php"); |
||
| 52 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 53 | $this->table = $table_name; |
||
| 54 | $this->id = $id_name; |
||
| 55 | $this->pid = $pid_name; |
||
| 56 | } |
||
| 57 | |||
| 58 | // returns an array of first child objects for a given id($sel_id) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param $sel_id |
||
| 62 | * @param string $order |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getFirstChild($sel_id, $order = '') |
||
| 67 | { |
||
| 68 | $sel_id = (int)$sel_id; |
||
| 69 | $arr = []; |
||
| 70 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' '; |
||
| 71 | if ('' != $order) { |
||
| 72 | $sql .= " ORDER BY $order"; |
||
| 73 | } |
||
| 74 | $result = $this->db->query($sql); |
||
| 75 | if ($result) { |
||
| 76 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 77 | $arr[] = $myrow; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | return $arr; |
||
| 81 | } |
||
| 82 | |||
| 83 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $sel_id |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function getFirstChildId($sel_id) |
||
| 91 | { |
||
| 92 | $sel_id = (int)$sel_id; |
||
| 93 | $idarray = []; |
||
| 94 | $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''); |
||
| 95 | $count = $this->db->getRowsNum($result); |
||
| 96 | if (0 == $count) { |
||
| 97 | return $idarray; |
||
| 98 | } |
||
| 99 | while (list($id) = $this->db->fetchRow($result)) { |
||
| 100 | $idarray[] = $id; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $idarray; |
||
| 104 | } |
||
| 105 | |||
| 106 | //returns an array of ALL child ids for a given id($sel_id) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $sel_id |
||
| 110 | * @param string $order |
||
| 111 | * @param array $idarray |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getAllChildId($sel_id, $order = '', $idarray = []) |
||
| 133 | } |
||
| 134 | |||
| 135 | //returns an array of ALL parent ids for a given id($sel_id) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $sel_id |
||
| 139 | * @param string $order |
||
| 140 | * @param array $idarray |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getAllParentId($sel_id, $order = '', $idarray = []) |
||
| 145 | { |
||
| 146 | $sel_id = (int)$sel_id; |
||
| 147 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
| 148 | if ('' != $order) { |
||
| 149 | $sql .= " ORDER BY $order"; |
||
| 150 | } |
||
| 151 | $result = $this->db->query($sql); |
||
| 152 | [$r_id] = $this->db->fetchRow($result); |
||
| 153 | if (0 == $r_id) { |
||
| 154 | return $idarray; |
||
| 155 | } |
||
| 156 | $idarray[] = $r_id; |
||
| 157 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 158 | |||
| 159 | return $idarray; |
||
| 160 | } |
||
| 161 | |||
| 162 | //generates path from the root id to a given id($sel_id) |
||
| 163 | // the path is delimetered with "/" |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $sel_id |
||
| 167 | * @param $title |
||
| 168 | * @param string $path |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getPathFromId($sel_id, $title, $path = '') |
||
| 173 | { |
||
| 174 | $sel_id = (int)$sel_id; |
||
| 175 | $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
||
| 176 | if (0 == $this->db->getRowsNum($result)) { |
||
| 177 | return $path; |
||
| 178 | } |
||
| 179 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 180 | $myts = \MyTextSanitizer::getInstance(); |
||
|
|
|||
| 181 | $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
||
| 182 | $path = '/' . $name . $path . ''; |
||
| 183 | if (0 == $parentid) { |
||
| 184 | return $path; |
||
| 185 | } |
||
| 186 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 187 | |||
| 188 | return $path; |
||
| 189 | } |
||
| 190 | |||
| 191 | //makes a nicely ordered selection box |
||
| 192 | //$preset_id is used to specify a preselected item |
||
| 193 | //set $none to 1 to add a option with value 0 |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $title |
||
| 197 | * @param string $order |
||
| 198 | * @param int $preset_id |
||
| 199 | * @param int $none |
||
| 200 | * @param string $sel_name |
||
| 201 | * @param string $onchange |
||
| 202 | */ |
||
| 203 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 204 | { |
||
| 205 | if ('' == $sel_name) { |
||
| 206 | $sel_name = $this->id; |
||
| 207 | } |
||
| 208 | $myts = \MyTextSanitizer::getInstance(); |
||
| 209 | echo "<select name='" . $sel_name . "'"; |
||
| 210 | if ('' != $onchange) { |
||
| 211 | echo " onchange='" . $onchange . "'"; |
||
| 212 | } |
||
| 213 | echo ">\n"; |
||
| 214 | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 215 | if ('' != $order) { |
||
| 216 | $sql .= " ORDER BY $order"; |
||
| 217 | } |
||
| 218 | $result = $this->db->query($sql); |
||
| 219 | if ($none) { |
||
| 220 | echo "<option value='0'>----</option>\n"; |
||
| 221 | } |
||
| 222 | while (list($catid, $name) = $this->db->fetchRow($result)) { |
||
| 223 | $sel = ''; |
||
| 224 | if ($catid == $preset_id) { |
||
| 225 | $sel = ' selected'; |
||
| 226 | } |
||
| 227 | echo "<option value='$catid'$sel>$name</option>\n"; |
||
| 228 | $sel = ''; |
||
| 229 | $arr = $this->getChildTreeArray($catid, $order); |
||
| 230 | foreach ($arr as $option) { |
||
| 231 | $option['prefix'] = \str_replace('.', '--', $option['prefix']); |
||
| 232 | $catpath = $option['prefix'] . ' ' . htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5); |
||
| 233 | if ($option[$this->id] == $preset_id) { |
||
| 234 | $sel = ' selected'; |
||
| 235 | } |
||
| 236 | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
||
| 237 | $sel = ''; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | echo "</select>\n"; |
||
| 241 | } |
||
| 242 | |||
| 243 | //generates nicely formatted linked path from the root id to a given id |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param $sel_id |
||
| 247 | * @param $title |
||
| 248 | * @param $funcURL |
||
| 249 | * @param string $path |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 254 | { |
||
| 255 | $path = !empty($path) ? ' : ' . $path : $path; |
||
| 256 | $sel_id = (int)$sel_id; |
||
| 257 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
| 258 | $result = $this->db->query($sql); |
||
| 259 | if (0 == $this->db->getRowsNum($result)) { |
||
| 260 | return $path; |
||
| 261 | } |
||
| 262 | [$parentid, $name] = $this->db->fetchRow($result); |
||
| 263 | $myts = \MyTextSanitizer::getInstance(); |
||
| 264 | $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
||
| 265 | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
||
| 266 | if (0 == $parentid) { |
||
| 267 | return $path; |
||
| 268 | } |
||
| 269 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 270 | |||
| 271 | return $path; |
||
| 272 | } |
||
| 273 | |||
| 274 | //generates id path from the root id to a given id |
||
| 275 | // the path is delimetered with "/" |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param $sel_id |
||
| 279 | * @param string $path |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getIdPathFromId($sel_id, $path = '') |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Enter description here... |
||
| 302 | * |
||
| 303 | * @param int|mixed $sel_id |
||
| 304 | * @param string|mixed $order |
||
| 305 | * @param array|mixed $parray |
||
| 306 | * |
||
| 307 | * @return mixed |
||
| 308 | */ |
||
| 309 | public function getAllChild($sel_id = 0, $order = '', $parray = []) |
||
| 310 | { |
||
| 311 | $sel_id = (int)$sel_id; |
||
| 312 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 313 | if ('' != $order) { |
||
| 314 | $sql .= " ORDER BY $order"; |
||
| 315 | } |
||
| 316 | $result = $this->db->query($sql); |
||
| 317 | $count = $this->db->getRowsNum($result); |
||
| 318 | if (0 == $count) { |
||
| 319 | return $parray; |
||
| 320 | } |
||
| 321 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 322 | $parray[] = $row; |
||
| 323 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 324 | } |
||
| 325 | |||
| 326 | return $parray; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Enter description here... |
||
| 331 | * |
||
| 332 | * @param int|mixed $sel_id |
||
| 333 | * @param string|mixed $order |
||
| 334 | * @param array|mixed $parray |
||
| 335 | * @param string|mixed $r_prefix |
||
| 336 | * |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '') |
||
| 358 | } |
||
| 359 | } |
||
| 360 |