| Total Complexity | 45 |
| Total Lines | 452 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ModelDesignCustomMenu 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 ModelDesignCustomMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ModelDesignCustomMenu extends \Divine\Engine\Core\Model |
||
|
|
|||
| 24 | { |
||
| 25 | public function getcustommenus() |
||
| 26 | { |
||
| 27 | $data = array(); |
||
| 28 | |||
| 29 | $sql = " |
||
| 30 | SELECT * |
||
| 31 | FROM `custommenu` m |
||
| 32 | LEFT JOIN custommenu_description md ON (m.custommenu_id = md.custommenu_id) |
||
| 33 | WHERE md.language_id = '" . (int)$this->config->get('config_language_id') . "' |
||
| 34 | ORDER BY m.sort_order |
||
| 35 | "; |
||
| 36 | |||
| 37 | $query = $this->db->query($sql); |
||
| 38 | |||
| 39 | if ($query->rows) { |
||
| 40 | foreach ($query->rows as $custommenu) { |
||
| 41 | $data[$custommenu['custommenu_id']] = $custommenu; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return $data; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getChildcustommenus() |
||
| 69 | } |
||
| 70 | |||
| 71 | public function add($data, $languages) |
||
| 72 | { |
||
| 73 | $this->db->query(" |
||
| 74 | INSERT INTO custommenu |
||
| 75 | SET sort_order= '1', |
||
| 76 | columns = '1', |
||
| 77 | custommenu_type = '" . $this->db->escape($data['type']) . "', |
||
| 78 | status = '1' |
||
| 79 | "); |
||
| 80 | |||
| 81 | $custommenu_id = $this->db->getLastId(); |
||
| 82 | |||
| 83 | if ($data['type'] == 'custom') { |
||
| 84 | $link = $data['link']; |
||
| 85 | |||
| 86 | $data['custommenu_desc'] = array(); |
||
| 87 | |||
| 88 | foreach ($languages as $language) { |
||
| 89 | $data['custommenu_desc'][] = array('name' => $data['name'], 'language_id' => $language['language_id']); |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $link = (int)$data['id']; |
||
| 93 | |||
| 94 | if ($data['type'] == 'information') { |
||
| 95 | $fields = 'title AS name, '.$data['type'].'_id, language_id'; |
||
| 96 | } else { |
||
| 97 | $fields = 'name, '.$data['type'].'_id, language_id'; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($data['type'] != 'manufacturer') { |
||
| 101 | $query = $this->db->query(" |
||
| 102 | SELECT " . $fields . " F |
||
| 103 | ROM " . $data['type'] . "_description |
||
| 104 | WHERE ". $data['type'] ."_id = '" . (int)$data['id'] . "' |
||
| 105 | "); |
||
| 106 | |||
| 107 | $data['custommenu_desc'] = $query->rows; |
||
| 108 | } else { |
||
| 109 | $fields = 'name, '.$data['type'].'_id '; |
||
| 110 | |||
| 111 | $query = $this->db->query(" |
||
| 112 | SELECT " . $fields . " |
||
| 113 | FROM " . $data['type'] . " |
||
| 114 | WHERE ". $data['type'] ."_id = '" . (int)$data['id'] . "' |
||
| 115 | "); |
||
| 116 | $result = array(); |
||
| 117 | |||
| 118 | foreach ($languages as $language) { |
||
| 119 | $result[] = array('name' => $query->row['name'], 'manufacturer_id' => $query->row['manufacturer_id'], 'language_id' => $language['language_id']); |
||
| 120 | }; |
||
| 121 | |||
| 122 | $data['custommenu_desc'] = $result; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($data['custommenu_desc'] as $desc) { |
||
| 127 | $this->db->query(" |
||
| 128 | INSERT INTO custommenu_description |
||
| 129 | SET custommenu_id = '" . (int)$custommenu_id . "', |
||
| 130 | language_id = '" . (int)$desc['language_id'] . "', |
||
| 131 | name = '" . $this->db->escape($desc['name']) . "', |
||
| 132 | link = '" . $link . "' |
||
| 133 | "); |
||
| 134 | } |
||
| 135 | |||
| 136 | $custommenu = array( |
||
| 137 | 'name' =>$data['custommenu_desc'][0]['name'], |
||
| 138 | 'custommenu_type' =>$data['type'], |
||
| 139 | 'custommenu_id' => $custommenu_id |
||
| 140 | ); |
||
| 141 | |||
| 142 | return $custommenu; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function save($data) |
||
| 146 | { |
||
| 147 | foreach ($data['custommenu-item-typecustommenu'] as $key => $value) { |
||
| 148 | $_custommenu_id = explode('-', $key); |
||
| 149 | } |
||
| 150 | $custommenu_id = $_custommenu_id[1]; |
||
| 151 | |||
| 152 | $this->db->query(" |
||
| 153 | UPDATE `custommenu` |
||
| 154 | SET columns = '" . (int)$data['custommenu_columns'] . "' |
||
| 155 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 156 | "); |
||
| 157 | |||
| 158 | $this->db->query(" |
||
| 159 | DELETE |
||
| 160 | FROM custommenu_description |
||
| 161 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 162 | "); |
||
| 163 | |||
| 164 | foreach ($data['custommenu_name'] as $language_id => $value) { |
||
| 165 | $this->db->query(" |
||
| 166 | INSERT INTO custommenu_description |
||
| 167 | SET custommenu_id = '" . (int)$custommenu_id . "', |
||
| 168 | language_id = '" . (int)$language_id . "', |
||
| 169 | name = '" . $this->db->escape($value) . "', |
||
| 170 | link = '" . $this->db->escape($data['custommenu_link'][$language_id]) . "' |
||
| 171 | "); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | public function saveChild($data) |
||
| 176 | { |
||
| 177 | foreach ($data['custommenu-item-typecustommenu'] as $key => $value) { |
||
| 178 | $_custommenu_id = explode('-', $key); |
||
| 179 | } |
||
| 180 | $custommenu_child_id = $_custommenu_id[1]; |
||
| 181 | |||
| 182 | $query = $this->db->query(" |
||
| 183 | SELECT * |
||
| 184 | FROM custommenu_child |
||
| 185 | WHERE custommenu_child_id = '" . (int)$custommenu_child_id . "' |
||
| 186 | "); |
||
| 187 | |||
| 188 | $custommenu_id = $query->row['custommenu_id']; |
||
| 189 | |||
| 190 | $this->db->query(" |
||
| 191 | DELETE |
||
| 192 | FROM custommenu_child_description |
||
| 193 | WHERE custommenu_child_id = '" . (int)$custommenu_child_id . "' |
||
| 194 | "); |
||
| 195 | |||
| 196 | foreach ($data['custommenu_child_name'] as $language_id => $value) { |
||
| 197 | $this->db->query(" |
||
| 198 | INSERT INTO custommenu_child_description |
||
| 199 | SET custommenu_id = '" . (int)$custommenu_id . "', |
||
| 200 | custommenu_child_id = '" . (int)$custommenu_child_id . "', |
||
| 201 | language_id = '" . (int)$language_id . "', |
||
| 202 | name = '" . $this->db->escape($value) . "', |
||
| 203 | link = '" . $this->db->escape($data['custommenu_child_link'][$language_id]) . "' |
||
| 204 | "); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | public function deletecustommenu($custommenu_id) |
||
| 209 | { |
||
| 210 | $this->db->query(" |
||
| 211 | DELETE |
||
| 212 | FROM `custommenu` |
||
| 213 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 214 | "); |
||
| 215 | $this->db->query(" |
||
| 216 | DELETE |
||
| 217 | FROM `custommenu_description` |
||
| 218 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 219 | "); |
||
| 220 | |||
| 221 | $query = $this->db->query(" |
||
| 222 | SELECT * |
||
| 223 | FROM custommenu_child |
||
| 224 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 225 | "); |
||
| 226 | |||
| 227 | $this->db->query(" |
||
| 228 | DELETE |
||
| 229 | FROM `custommenu_child` |
||
| 230 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 231 | "); |
||
| 232 | $this->db->query(" |
||
| 233 | DELETE |
||
| 234 | FROM `custommenu_child_description` |
||
| 235 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 236 | "); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function deleteChildcustommenu($custommenu_child_id) |
||
| 240 | { |
||
| 241 | $this->db->query(" |
||
| 242 | DELETE |
||
| 243 | FROM `custommenu_child` |
||
| 244 | WHERE custommenu_child_id = '" . (int)$custommenu_child_id . "' |
||
| 245 | "); |
||
| 246 | $this->db->query(" |
||
| 247 | DELETE |
||
| 248 | FROM `custommenu_child_description` |
||
| 249 | WHERE custommenu_child_id = '" . (int)$custommenu_child_id . "' |
||
| 250 | "); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getcustommenuDesc() |
||
| 254 | { |
||
| 255 | $data = array(); |
||
| 256 | |||
| 257 | $link = array(); |
||
| 258 | |||
| 259 | $query = $this->db->query(" |
||
| 260 | SELECT * FROM custommenu_description AS md |
||
| 261 | LEFT JOIN custommenu m ON (m.custommenu_id = md.custommenu_id) |
||
| 262 | "); |
||
| 263 | |||
| 264 | foreach ($query->rows as $result) { |
||
| 265 | // Quick fix for multilanguage |
||
| 266 | // Check if link exists to set for later usage |
||
| 267 | if (!empty($result['link'])) { |
||
| 268 | $link[$result['custommenu_id']] = $result['link']; |
||
| 269 | } |
||
| 270 | |||
| 271 | // Try to get the link from previous set if not already exists |
||
| 272 | if (empty($result['link']) and !empty($link[$result['custommenu_id']])) { |
||
| 273 | $result['link'] = $link[$result['custommenu_id']]; |
||
| 274 | } |
||
| 275 | |||
| 276 | $data[$result['custommenu_id']][$result['language_id']] = $result; |
||
| 277 | } |
||
| 278 | |||
| 279 | return $data; |
||
| 280 | } |
||
| 281 | |||
| 282 | public function getcustommenuChildDesc() |
||
| 283 | { |
||
| 284 | $data = array(); |
||
| 285 | |||
| 286 | $link = array(); |
||
| 287 | |||
| 288 | $query = $this->db->query(" |
||
| 289 | SELECT * |
||
| 290 | FROM custommenu_child_description AS md |
||
| 291 | LEFT JOIN custommenu_child m ON (m.custommenu_child_id = md.custommenu_child_id) |
||
| 292 | "); |
||
| 293 | |||
| 294 | foreach ($query->rows as $result) { |
||
| 295 | // Quick fix for multilanguage |
||
| 296 | // Check if link exists to set for later usage |
||
| 297 | if (!empty($result['link'])) { |
||
| 298 | $link[$result['custommenu_child_id']] = $result['link']; |
||
| 299 | } |
||
| 300 | |||
| 301 | // Try to get the link from previous set if not already exists |
||
| 302 | if (empty($result['link']) and !empty($link[$result['custommenu_child_id']])) { |
||
| 303 | $result['link'] = $link[$result['custommenu_child_id']]; |
||
| 304 | } |
||
| 305 | |||
| 306 | $data[$result['custommenu_child_id']][$result['language_id']] = $result; |
||
| 307 | } |
||
| 308 | |||
| 309 | return $data; |
||
| 310 | } |
||
| 311 | |||
| 312 | public function enablecustommenu($custommenu_id) |
||
| 318 | "); |
||
| 319 | } |
||
| 320 | |||
| 321 | public function enableChildcustommenu($custommenu_child_id) |
||
| 327 | "); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function disablecustommenu($custommenu_id) |
||
| 331 | { |
||
| 332 | $this->db->query(" |
||
| 333 | UPDATE `custommenu` |
||
| 334 | SET status = '0' |
||
| 335 | WHERE custommenu_id = '" . (int)$custommenu_id . "' |
||
| 336 | "); |
||
| 337 | } |
||
| 338 | |||
| 339 | public function disableChildcustommenu($custommenu_child_id) |
||
| 340 | { |
||
| 341 | $this->db->query(" |
||
| 342 | UPDATE `custommenu_child` |
||
| 343 | SET status = '0' |
||
| 344 | WHERE custommenu_child_id = '" . (int)$custommenu_child_id . "' |
||
| 345 | "); |
||
| 346 | } |
||
| 347 | |||
| 348 | public function changecustommenuPosition($data) |
||
| 475 | } |
||
| 476 | } |
||
| 477 | } |
||
| 478 | } |
||
| 479 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.