| Total Complexity | 52 |
| Total Lines | 360 |
| 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 |
||
| 31 | class XoopsTree |
||
| 32 | { |
||
| 33 | public $table; //table with parent-child structure |
||
| 34 | public $id; //name of unique id for records in table $table |
||
| 35 | public $pid; // name of parent id used in table $table |
||
| 36 | public $order; //specifies the order of query results |
||
| 37 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
| 38 | /** |
||
| 39 | * @var \XoopsMySQLDatabase |
||
| 40 | */ |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | //constructor of class XoopsTree |
||
| 44 | //sets the names of table, unique id, and parend id |
||
| 45 | /** |
||
| 46 | * @param $table_name |
||
| 47 | * @param $id_name |
||
| 48 | * @param $pid_name |
||
| 49 | */ |
||
| 50 | public function __construct($table_name, $id_name, $pid_name) |
||
| 57 | } |
||
| 58 | |||
| 59 | // returns an array of first child objects for a given id($sel_id) |
||
| 60 | /** |
||
| 61 | * @param $sel_id |
||
| 62 | * @param string $order |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getFirstChild($sel_id, $order = '') |
||
| 87 | } |
||
| 88 | |||
| 89 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
| 90 | /** |
||
| 91 | * @param $sel_id |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function getFirstChildId($sel_id) |
||
| 96 | { |
||
| 97 | $sel_id = (int)$sel_id; |
||
| 98 | $idarray = array(); |
||
| 99 | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 100 | $result = $this->db->query($sql); |
||
| 101 | if (!$this->db->isResultSet($result)) { |
||
| 102 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 103 | } |
||
| 104 | $count = $this->db->getRowsNum($result); |
||
| 105 | if ($count == 0) { |
||
| 106 | return $idarray; |
||
| 107 | } |
||
| 108 | while (false !== (list($id) = $this->db->fetchRow($result))) { |
||
| 109 | $idarray[] = $id; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $idarray; |
||
| 113 | } |
||
| 114 | |||
| 115 | //returns an array of ALL child ids for a given id($sel_id) |
||
| 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()) |
||
| 144 | } |
||
| 145 | |||
| 146 | //returns an array of ALL parent ids for a given id($sel_id) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string|int $sel_id |
||
| 150 | * @param string $order |
||
| 151 | * @param array $idarray |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
||
| 156 | { |
||
| 157 | $sel_id = (int)$sel_id; |
||
| 158 | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
||
| 159 | if ($order != '') { |
||
| 160 | $sql .= " ORDER BY $order"; |
||
| 161 | } |
||
| 162 | $result = $this->db->query($sql); |
||
| 163 | if (!$this->db->isResultSet($result)) { |
||
| 164 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 165 | } |
||
| 166 | list($r_id) = $this->db->fetchRow($result); |
||
| 167 | $r_id = (int)$r_id; |
||
| 168 | if ($r_id === 0) { |
||
| 169 | return $idarray; |
||
| 170 | } |
||
| 171 | $idarray[] = $r_id; |
||
| 172 | $idarray = $this->getAllParentId($r_id, $order, $idarray); |
||
| 173 | |||
| 174 | return $idarray; |
||
| 175 | } |
||
| 176 | |||
| 177 | //generates path from the root id to a given id($sel_id) |
||
| 178 | // the path is delimetered with "/" |
||
| 179 | /** |
||
| 180 | * @param string|int $sel_id |
||
| 181 | * @param string $title |
||
| 182 | * @param string $path |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getPathFromId($sel_id, $title, $path = '') |
||
| 187 | { |
||
| 188 | $sel_id = (int)$sel_id; |
||
| 189 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
| 190 | $result = $this->db->query($sql); |
||
| 191 | if (!$this->db->isResultSet($result)) { |
||
| 192 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 193 | } |
||
| 194 | if ($this->db->getRowsNum($result) == 0) { |
||
| 195 | return $path; |
||
| 196 | } |
||
| 197 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 198 | $myts = \MyTextSanitizer::getInstance(); |
||
| 199 | $parentid = (int)$parentid; |
||
| 200 | $name = $myts->htmlSpecialChars($name); |
||
| 201 | $path = '/' . $name . $path . ''; |
||
| 202 | if ($parentid === 0) { |
||
| 203 | return $path; |
||
| 204 | } |
||
| 205 | $path = $this->getPathFromId($parentid, $title, $path); |
||
| 206 | |||
| 207 | return $path; |
||
| 208 | } |
||
| 209 | |||
| 210 | //makes a nicely ordered selection box |
||
| 211 | //$preset_id is used to specify a preselected item |
||
| 212 | //set $none to 1 to add a option with value 0 |
||
| 213 | /** |
||
| 214 | * @param $title |
||
| 215 | * @param string $order |
||
| 216 | * @param int $preset_id |
||
| 217 | * @param int $none |
||
| 218 | * @param string $sel_name |
||
| 219 | * @param string $onchange |
||
| 220 | */ |
||
| 221 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
||
| 222 | { |
||
| 223 | if ($sel_name == '') { |
||
| 224 | $sel_name = $this->id; |
||
| 225 | } |
||
| 226 | $myts = \MyTextSanitizer::getInstance(); |
||
| 227 | echo "<select name='" . $sel_name . "'"; |
||
| 228 | if ($onchange != '') { |
||
| 229 | echo " onchange='" . $onchange . "'"; |
||
| 230 | } |
||
| 231 | echo ">\n"; |
||
| 232 | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
||
| 233 | if ($order != '') { |
||
| 234 | $sql .= " ORDER BY $order"; |
||
| 235 | } |
||
| 236 | $result = $this->db->query($sql); |
||
| 237 | if (!$this->db->isResultSet($result)) { |
||
| 238 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 239 | } |
||
| 240 | if ($none) { |
||
| 241 | echo "<option value='0'>----</option>\n"; |
||
| 242 | } |
||
| 243 | while (false !== (list($catid, $name) = $this->db->fetchRow($result))) { |
||
| 244 | $sel = ''; |
||
| 245 | if ($catid == $preset_id) { |
||
| 246 | $sel = " selected"; |
||
| 247 | } |
||
| 248 | echo "<option value='$catid'$sel>$name</option>\n"; |
||
| 249 | $sel = ''; |
||
| 250 | $arr = $this->getChildTreeArray($catid, $order); |
||
| 251 | foreach ($arr as $option) { |
||
| 252 | $option['prefix'] = str_replace('.', '--', $option['prefix']); |
||
| 253 | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
||
| 254 | if ($option[$this->id] == $preset_id) { |
||
| 255 | $sel = " selected"; |
||
| 256 | } |
||
| 257 | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
||
| 258 | $sel = ''; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | echo "</select>\n"; |
||
| 262 | } |
||
| 263 | |||
| 264 | //generates nicely formatted linked path from the root id to a given id |
||
| 265 | /** |
||
| 266 | * @param string|int $sel_id |
||
| 267 | * @param string $title |
||
| 268 | * @param string $funcURL |
||
| 269 | * @param string $path |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
| 274 | { |
||
| 275 | $path = !empty($path) ? ' : ' . $path : $path; |
||
| 276 | $sel_id = (int)$sel_id; |
||
| 277 | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
||
| 278 | $result = $this->db->query($sql); |
||
| 279 | if (!$this->db->isResultSet($result)) { |
||
| 280 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 281 | } |
||
| 282 | if ($this->db->getRowsNum($result) == 0) { |
||
| 283 | return $path; |
||
| 284 | } |
||
| 285 | list($parentid, $name) = $this->db->fetchRow($result); |
||
| 286 | $myts = \MyTextSanitizer::getInstance(); |
||
| 287 | $name = $myts->htmlSpecialChars($name); |
||
| 288 | $parentid = (int)$parentid; |
||
| 289 | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
||
| 290 | if ($parentid === 0) { |
||
| 291 | return $path; |
||
| 292 | } |
||
| 293 | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
||
| 294 | |||
| 295 | return $path; |
||
| 296 | } |
||
| 297 | |||
| 298 | //generates id path from the root id to a given id |
||
| 299 | // the path is delimetered with "/" |
||
| 300 | /** |
||
| 301 | * @param string|int $sel_id |
||
| 302 | * @param string $path |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getIdPathFromId($sel_id, $path = '') |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Enter description here... |
||
| 330 | * |
||
| 331 | * @param int|mixed $sel_id |
||
| 332 | * @param string|mixed $order |
||
| 333 | * @param array|mixed $parray |
||
| 334 | * |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
||
| 338 | { |
||
| 339 | $sel_id = (int)$sel_id; |
||
| 340 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
||
| 341 | if ($order != '') { |
||
| 342 | $sql .= " ORDER BY $order"; |
||
| 343 | } |
||
| 344 | $result = $this->db->query($sql); |
||
| 345 | if (!$this->db->isResultSet($result)) { |
||
| 346 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 347 | } |
||
| 348 | $count = $this->db->getRowsNum($result); |
||
| 349 | if ($count == 0) { |
||
| 350 | return $parray; |
||
| 351 | } |
||
| 352 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 353 | $parray[] = $row; |
||
| 354 | $parray = $this->getAllChild($row[$this->id], $order, $parray); |
||
| 355 | } |
||
| 356 | |||
| 357 | return $parray; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Enter description here... |
||
| 362 | * |
||
| 363 | * @param int|mixed $sel_id |
||
| 364 | * @param string|mixed $order |
||
| 365 | * @param array|mixed $parray |
||
| 366 | * @param string|mixed $r_prefix |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
| 391 | } |
||
| 392 | } |
||
| 393 |