Total Complexity | 53 |
Total Lines | 469 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ACP_Structure 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 ACP_Structure, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ACP_Structure { |
||
27 | |||
28 | # vars |
||
29 | private $engine; |
||
30 | private $unit; |
||
31 | |||
32 | private $sid = 0; |
||
33 | |||
34 | |||
35 | |||
36 | /** |
||
37 | * Lets mortal kombat begin |
||
38 | */ |
||
39 | public function __construct() { |
||
46 | } |
||
47 | |||
48 | |||
49 | /** |
||
50 | * initialisation action |
||
51 | */ |
||
52 | private function init() { |
||
53 | |||
54 | global $roocms, $config, $db, $users, $tpl, $smarty, $get, $post; |
||
55 | |||
56 | # read site tree |
||
57 | $smarty->assign('tree', $this->engine->sitetree); |
||
58 | |||
59 | # Check enabled page type |
||
60 | $content_types = []; |
||
61 | foreach($this->engine->content_types AS $key=>$value) { |
||
62 | $content_types[$key] = $value['title']; |
||
63 | } |
||
64 | $smarty->assign('content_types', $content_types); |
||
65 | |||
66 | |||
67 | # default thumb size |
||
68 | $default_thumb_size = array('width' => $config->gd_thumb_image_width, |
||
69 | 'height' => $config->gd_thumb_image_height); |
||
70 | $smarty->assign("default_thumb_size", $default_thumb_size); |
||
71 | |||
72 | |||
73 | # Check id |
||
74 | if(isset($get->_id) && $db->check_id($get->_id, STRUCTURE_TABLE)) { |
||
75 | $this->sid = $get->_id; |
||
76 | } |
||
77 | |||
78 | |||
79 | # action |
||
80 | switch($roocms->part) { |
||
81 | # create |
||
82 | case 'create': |
||
83 | if(isset($post->create_unit)) { |
||
84 | $this->create_unit(); |
||
85 | } |
||
86 | else { |
||
87 | # list groups |
||
88 | $groups = $users->get_usergroups(); |
||
89 | |||
90 | # tpl |
||
91 | $smarty->assign("groups", $groups); |
||
92 | $content = $tpl->load_template("structure_create", true); |
||
93 | } |
||
94 | break; |
||
95 | |||
96 | # edit and update |
||
97 | case 'edit': |
||
98 | if(isset($post->update_unit)) { |
||
99 | $this->update_unit($this->sid); |
||
100 | } |
||
101 | |||
102 | $content = $this->edit_unit($this->sid); |
||
103 | break; |
||
104 | |||
105 | # delete |
||
106 | case 'delete': |
||
107 | $this->delete_unit($this->sid); |
||
108 | break; |
||
109 | |||
110 | default: |
||
111 | $content = $tpl->load_template("structure_tree", true); |
||
112 | break; |
||
113 | } |
||
114 | |||
115 | # tpl |
||
116 | $smarty->assign('content', $content); |
||
|
|||
117 | $tpl->load_template("structure"); |
||
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Create new structure unit |
||
123 | */ |
||
124 | private function create_unit() { |
||
125 | |||
126 | global $db, $logger, $post; |
||
127 | |||
128 | # check unit parametrs |
||
129 | $this->check_unit_parametrs(); |
||
130 | |||
131 | |||
132 | if(!isset($_SESSION['error'])) { |
||
133 | |||
134 | # check parent type |
||
135 | $q = $db->query("SELECT page_type FROM ".STRUCTURE_TABLE." WHERE id='".$post->parent_id."'"); |
||
136 | $d = $db->fetch_assoc($q); |
||
137 | |||
138 | # not add another page type to feed |
||
139 | if($d['page_type'] == "feed" && $post->page_type != "feed") { |
||
140 | $logger->error("Вы не можете установить для ленты в качестве дочерней страницы другой структурный элемент, кроме ленты."); |
||
141 | goback(); |
||
142 | } |
||
143 | |||
144 | # insert structure int data |
||
145 | $db->query("INSERT INTO ".STRUCTURE_TABLE." (alias, title, parent_id, nav, group_access, page_type, meta_title, meta_description, meta_keywords, noindex, sort, date_create, date_modified, thumb_img_width, thumb_img_height) |
||
146 | VALUES ('".$post->alias."', '".$post->title."', '".$post->parent_id."', '".$post->nav."', '".$post->gids."', '".$post->page_type."', '".$post->meta_title."', '".$post->meta_description."', '".$post->meta_keywords."', '".$post->noindex."', '".$post->sort."', '".time()."', '".time()."', '".$post->thumb_img_width."', '".$post->thumb_img_height."')"); |
||
147 | $sid = $db->insert_id(); |
||
148 | |||
149 | # create body unit for html & php pages |
||
150 | switch($post->page_type) { |
||
151 | case 'html': |
||
152 | $db->query("INSERT INTO ".PAGES_HTML_TABLE." (sid, date_modified) VALUE ('".$sid."', '".time()."')"); |
||
153 | break; |
||
154 | |||
155 | case 'story': |
||
156 | $db->query("INSERT INTO ".PAGES_STORY_TABLE." (sid, date_modified) VALUE ('".$sid."', '".time()."')"); |
||
157 | break; |
||
158 | |||
159 | case 'php': |
||
160 | $db->query("INSERT INTO ".PAGES_PHP_TABLE." (sid, date_modified) VALUE ('".$sid."', '".time()."')"); |
||
161 | break; |
||
162 | |||
163 | case 'feed': |
||
164 | $db->query("UPDATE ".STRUCTURE_TABLE." SET rss='1' WHERE sid='".$sid."'"); |
||
165 | break; |
||
166 | } |
||
167 | |||
168 | # recount childs |
||
169 | $this->count_childs($post->parent_id); |
||
170 | |||
171 | # notice |
||
172 | $logger->info("Элемент структуры #".$sid." успешно добавлена."); |
||
173 | |||
174 | # go |
||
175 | if(isset($post->create_unit['ae'])) { |
||
176 | go(CP."?act=structure"); |
||
177 | } |
||
178 | |||
179 | if($post->page_type == "feed") { |
||
180 | go(CP."?act=feeds&part=control&page=".$sid); |
||
181 | } |
||
182 | |||
183 | go(CP."?act=pages&part=edit&page=".$sid); |
||
184 | } |
||
185 | |||
186 | # goback |
||
187 | goback(); |
||
188 | } |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Edit structure unit |
||
193 | * |
||
194 | * @param mixed $sid - Structure id |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | private function edit_unit($sid) { |
||
217 | } |
||
218 | |||
219 | |||
220 | /** |
||
221 | * Update structure unit |
||
222 | * |
||
223 | * @param mixed $sid - structure id |
||
224 | */ |
||
225 | private function update_unit($sid) { |
||
226 | |||
227 | global $db, $logger, $post; |
||
228 | |||
229 | # dont change parent, alias and nav flag for main page |
||
230 | If($sid == 1) { |
||
231 | $post->parent_id = 0; |
||
232 | $post->alias = "index"; |
||
233 | $post->nav = 1; |
||
234 | } |
||
235 | |||
236 | # check unit parametrs |
||
237 | $this->check_unit_parametrs(); |
||
238 | |||
239 | |||
240 | if(!isset($_SESSION['error'])) { |
||
241 | |||
242 | # if set new parent |
||
243 | if($post->parent_id != $post->now_parent_id) { |
||
244 | |||
245 | # Check that we are not trying to be a parent for ourselves |
||
246 | if($post->parent_id == $sid) { |
||
247 | $post->parent_id = $post->now_parent_id; |
||
248 | $logger->error("Не удалось изменить иерархию! Вы не можете назначить страницу подчиненной самой себе!"); |
||
249 | } |
||
250 | # ... and check that new parent is not a child |
||
251 | else { |
||
252 | $childs = $this->engine->load_tree($sid); |
||
253 | |||
254 | foreach((array)$childs AS $v) { |
||
255 | if($post->parent_id == $v['id']) { |
||
256 | $post->parent_id = $post->now_parent_id; |
||
257 | $logger->error("Не удалось изменить иерархию! Вы не можете поместить страницу в подчинение странице собственного нижнего уровня!"); |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | # get parent type |
||
264 | $q = $db->query("SELECT page_type FROM ".STRUCTURE_TABLE." WHERE id='".$post->parent_id."'"); |
||
265 | $d = $db->fetch_assoc($q); |
||
266 | |||
267 | # get page type |
||
268 | $q = $db->query("SELECT page_type FROM ".STRUCTURE_TABLE." WHERE id='".$sid."'"); |
||
269 | $n = $db->fetch_assoc($q); |
||
270 | |||
271 | # not add another page type to feed |
||
272 | if($d['page_type'] == "feed" && $n['page_type'] != "feed") { |
||
273 | $logger->error("Вы не можете установить для ленты в качестве дочерней страницы другой структурный элемент, кроме ленты."); |
||
274 | $post->parent_id = $post->now_parent_id; |
||
275 | } |
||
276 | |||
277 | # DB |
||
278 | $db->query("UPDATE ".STRUCTURE_TABLE." |
||
279 | SET |
||
280 | alias='".$post->alias."', |
||
281 | title='".$post->title."', |
||
282 | parent_id='".$post->parent_id."', |
||
283 | nav='".$post->nav."', |
||
284 | group_access='".$post->gids."', |
||
285 | meta_title='".$post->meta_title."', |
||
286 | meta_description='".$post->meta_description."', |
||
287 | meta_keywords='".$post->meta_keywords."', |
||
288 | noindex='".$post->noindex."', |
||
289 | sort='".$post->sort."', |
||
290 | date_modified='".time()."', |
||
291 | thumb_img_width='".$post->thumb_img_width."', |
||
292 | thumb_img_height='".$post->thumb_img_height."' |
||
293 | WHERE |
||
294 | id='".$sid."'"); |
||
295 | |||
296 | # if set new parent |
||
297 | if($post->parent_id != $post->now_parent_id) { |
||
298 | # recount childs |
||
299 | $this->count_childs($post->parent_id); |
||
300 | $this->count_childs($post->now_parent_id); |
||
301 | } |
||
302 | |||
303 | # notice |
||
304 | $logger->info("Страница #".$sid." успешно обновлена."); |
||
305 | |||
306 | # go |
||
307 | if(isset($post->update_unit['ae'])) { |
||
308 | go(CP."?act=structure"); |
||
309 | } |
||
310 | |||
311 | go(CP."?act=structure&part=edit&id=".$sid); |
||
312 | } |
||
313 | |||
314 | # goback |
||
315 | goback(); |
||
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * Remove structure unit |
||
321 | * |
||
322 | * @param mixed $sid - structure id |
||
323 | */ |
||
324 | private function delete_unit($sid) { |
||
325 | |||
326 | global $db, $logger; |
||
327 | |||
328 | $q = $db->query("SELECT childs, parent_id, page_type FROM ".STRUCTURE_TABLE." WHERE id='".$sid."'"); |
||
329 | $c = $db->fetch_assoc($q); |
||
330 | |||
331 | if($c['childs'] == 0) { |
||
332 | |||
333 | switch($c['page_type']) { |
||
334 | |||
335 | case 'html': # del content html |
||
336 | require_once _ROOCMS."/acp/pages_html.php"; |
||
337 | $this->unit = new ACP_Pages_HTML; |
||
338 | $this->unit->delete($sid); |
||
339 | break; |
||
340 | |||
341 | case 'story': # del content story |
||
342 | require_once _ROOCMS."/acp/pages_story.php"; |
||
343 | $this->unit = new ACP_Pages_Story; |
||
344 | $this->unit->delete($sid); |
||
345 | break; |
||
346 | |||
347 | case 'php': # del content php |
||
348 | require_once _ROOCMS."/acp/pages_php.php"; |
||
349 | $this->unit = new ACP_Pages_PHP; |
||
350 | $this->unit->delete($sid); |
||
351 | break; |
||
352 | |||
353 | case 'feed': # del content feed |
||
354 | require_once _ROOCMS."/acp/feeds_feed.php"; |
||
355 | $this->unit = new ACP_Feeds_Feed(); |
||
356 | $this->unit->delete_feed($sid); |
||
357 | break; |
||
358 | } |
||
359 | |||
360 | |||
361 | # structure unit |
||
362 | $db->query("DELETE FROM ".STRUCTURE_TABLE." WHERE id='".$sid."'"); |
||
363 | |||
364 | # notice |
||
365 | $logger->info("Страница #".$sid." успешно удалена"); |
||
366 | |||
367 | # recount parent childs |
||
368 | $this->count_childs($c['parent_id']); |
||
369 | } |
||
370 | else { |
||
371 | $logger->error("Невозможно удалить страницу, по причине имеющихся у страницы дочерних связей. Сначала перенесите или удалите дочерние страницы."); |
||
372 | } |
||
373 | |||
374 | # go |
||
375 | goback(); |
||
376 | } |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Check alias name on unique |
||
381 | * |
||
382 | * @param string $name - uname |
||
383 | * @param string $without - exclude uname |
||
384 | * |
||
385 | * @return bool |
||
386 | */ |
||
387 | private function check_unique_alias(string $name, $without="") { |
||
388 | |||
389 | global $db; |
||
390 | |||
391 | $res = false; |
||
392 | |||
393 | if(trim($without) != trim($name)) { |
||
394 | |||
395 | $without = trim($without); |
||
396 | $w = "alias!='".$without."'"; |
||
397 | |||
398 | if(!$db->check_id($name, STRUCTURE_TABLE, "alias", $w)) { |
||
399 | $res = true; |
||
400 | } |
||
401 | } |
||
402 | else { |
||
403 | $res = true; |
||
404 | } |
||
405 | |||
406 | return $res; |
||
407 | } |
||
408 | |||
409 | |||
410 | /** |
||
411 | * Check nav bool for use |
||
412 | */ |
||
413 | private function check_nav() { |
||
414 | |||
415 | global $post, $logger; |
||
416 | |||
417 | if(isset($this->engine->sitetree[$post->parent_id]['nav']) && $this->engine->sitetree[$post->parent_id]['nav'] == 0) { |
||
418 | $post->nav = 0; |
||
419 | $logger->info("Прежде чем включить в навигацию эту страницу сайта, вы должны включить в навигацию родительскую", false); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | |||
424 | /** |
||
425 | * Alias handler |
||
426 | */ |
||
427 | private function handler_alias() { |
||
440 | } |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Check unit data |
||
445 | */ |
||
446 | private function check_unit_parametrs() { |
||
480 | } |
||
481 | |||
482 | |||
483 | /** |
||
484 | * Recount subparts |
||
485 | * |
||
486 | * @param int $sid - structure id |
||
487 | */ |
||
488 | private function count_childs(int $sid) { |
||
502 |