Total Complexity | 130 |
Total Lines | 956 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like contentBlueprintsPages 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 contentBlueprintsPages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class contentBlueprintsPages extends AdministrationPage |
||
14 | { |
||
15 | public $_errors = array(); |
||
16 | protected $_hilights = array(); |
||
17 | |||
18 | public function insertBreadcrumbsUsingPageIdentifier($page_id, $preserve_last = true) |
||
59 | )); |
||
60 | } |
||
61 | |||
62 | public function __viewIndex() |
||
209 | } |
||
210 | } |
||
211 | |||
212 | public function __viewNew() |
||
213 | { |
||
214 | $this->__viewEdit(); |
||
215 | } |
||
216 | |||
217 | public function __viewEdit() |
||
218 | { |
||
219 | $this->setPageType('form'); |
||
220 | $fields = array("title"=>null, "handle"=>null, "parent"=>null, "params"=>null, "type"=>null, "data_sources"=>null); |
||
221 | $existing = $fields; |
||
222 | $canonical_link = '/blueprints/pages/'; |
||
223 | $nesting = (Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes'); |
||
224 | |||
225 | // Verify page exists: |
||
226 | if ($this->_context[0] === 'edit') { |
||
227 | if (!$page_id = (int)$this->_context[1]) { |
||
228 | redirect(SYMPHONY_URL . '/blueprints/pages/'); |
||
229 | } |
||
230 | |||
231 | $existing = PageManager::fetchPageByID($page_id); |
||
232 | $canonical_link .= 'edit/' . $page_id . '/'; |
||
233 | |||
234 | if (!$existing) { |
||
235 | Administration::instance()->errorPageNotFound(); |
||
236 | } else { |
||
237 | $existing['type'] = PageManager::fetchPageTypes($page_id); |
||
238 | } |
||
239 | } else { |
||
240 | $canonical_link .= 'new/'; |
||
241 | } |
||
242 | |||
243 | // Status message: |
||
244 | if (isset($this->_context[2])) { |
||
245 | $flag = $this->_context[2]; |
||
246 | $parent_link_suffix = $message = ''; |
||
247 | $time = Widget::Time(); |
||
248 | |||
249 | if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) { |
||
250 | $parent_link_suffix = "?parent=" . $_REQUEST['parent']; |
||
251 | } elseif ($nesting && isset($existing) && !is_null($existing['parent'])) { |
||
252 | $parent_link_suffix = '?parent=' . $existing['parent']; |
||
253 | } |
||
254 | |||
255 | switch ($flag) { |
||
256 | case 'saved': |
||
257 | $message = __('Page updated at %s.', array($time->generate())); |
||
258 | break; |
||
259 | case 'created': |
||
260 | $message = __('Page created at %s.', array($time->generate())); |
||
261 | } |
||
262 | |||
263 | $this->pageAlert( |
||
264 | $message |
||
265 | . ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/' . $parent_link_suffix . '" accesskey="c">' |
||
266 | . __('Create another?') |
||
267 | . '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">' |
||
268 | . __('View all Pages') |
||
269 | . '</a>', |
||
270 | Alert::SUCCESS |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | // Find values: |
||
275 | if (isset($_POST['fields'])) { |
||
276 | $fields = $_POST['fields']; |
||
277 | } elseif ($this->_context[0] == 'edit') { |
||
278 | $fields = $existing; |
||
279 | |||
280 | if (!is_null($fields['type'])) { |
||
281 | $fields['type'] = implode(', ', $fields['type']); |
||
282 | } |
||
283 | |||
284 | $fields['data_sources'] = preg_split('/,/i', $fields['data_sources'], -1, PREG_SPLIT_NO_EMPTY); |
||
285 | $fields['events'] = preg_split('/,/i', $fields['events'], -1, PREG_SPLIT_NO_EMPTY); |
||
286 | } elseif (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) { |
||
287 | $fields['parent'] = $_REQUEST['parent']; |
||
288 | $canonical_link .= '?parent=' . urlencode($_REQUEST['parent']); |
||
289 | } |
||
290 | |||
291 | $title = $fields['title']; |
||
292 | |||
293 | if (trim($title) == '') { |
||
294 | $title = $existing['title']; |
||
295 | } |
||
296 | |||
297 | $this->setTitle(__( |
||
298 | ($title ? '%1$s – %2$s – %3$s' : '%2$s – %3$s'), |
||
299 | array( |
||
300 | $title, |
||
301 | __('Pages'), |
||
302 | __('Symphony') |
||
303 | ) |
||
304 | )); |
||
305 | $this->addElementToHead(new XMLElement('link', null, array( |
||
306 | 'rel' => 'canonical', |
||
307 | 'href' => SYMPHONY_URL . $canonical_link, |
||
308 | ))); |
||
309 | |||
310 | $page_id = isset($page_id) ? $page_id : null; |
||
311 | |||
312 | if (!empty($title)) { |
||
313 | $page_url = URL . '/' . PageManager::resolvePagePath($page_id) . '/'; |
||
314 | |||
315 | $this->appendSubheading($title, array( |
||
316 | Widget::Anchor(__('View Page'), $page_url, __('View Page on Frontend'), 'button', null, array('target' => '_blank', 'accesskey' => 'v')) |
||
317 | )); |
||
318 | } else { |
||
319 | $this->appendSubheading(!empty($title) ? $title : __('Untitled')); |
||
320 | } |
||
321 | |||
322 | if (isset($page_id)) { |
||
323 | $this->insertBreadcrumbsUsingPageIdentifier($page_id, false); |
||
324 | } else { |
||
325 | $_GET['parent'] = isset($_GET['parent']) ? $_GET['parent'] : null; |
||
326 | $this->insertBreadcrumbsUsingPageIdentifier((int)$_GET['parent'], true); |
||
327 | } |
||
328 | |||
329 | // Title -------------------------------------------------------------- |
||
330 | |||
331 | $fieldset = new XMLElement('fieldset'); |
||
332 | $fieldset->setAttribute('class', 'settings'); |
||
333 | $fieldset->appendChild(new XMLElement('legend', __('Page Settings'))); |
||
334 | |||
335 | $label = Widget::Label(__('Name')); |
||
336 | $label->appendChild(Widget::Input( |
||
337 | 'fields[title]', General::sanitize($fields['title']) |
||
338 | )); |
||
339 | |||
340 | if (isset($this->_errors['title'])) { |
||
341 | $label = Widget::Error($label, $this->_errors['title']); |
||
342 | } |
||
343 | |||
344 | $fieldset->appendChild($label); |
||
345 | |||
346 | // Handle ------------------------------------------------------------- |
||
347 | |||
348 | $group = new XMLElement('div'); |
||
349 | $group->setAttribute('class', 'two columns'); |
||
350 | $column = new XMLElement('div'); |
||
351 | $column->setAttribute('class', 'column'); |
||
352 | |||
353 | $label = Widget::Label(__('Handle')); |
||
354 | $label->appendChild(Widget::Input( |
||
355 | 'fields[handle]', $fields['handle'] |
||
356 | )); |
||
357 | |||
358 | if (isset($this->_errors['handle'])) { |
||
359 | $label = Widget::Error($label, $this->_errors['handle']); |
||
360 | } |
||
361 | |||
362 | $column->appendChild($label); |
||
363 | |||
364 | // Parent --------------------------------------------------------- |
||
365 | |||
366 | $label = Widget::Label(__('Parent Page')); |
||
367 | |||
368 | $where = array( |
||
369 | sprintf('id != %d', $page_id) |
||
370 | ); |
||
371 | $pages = PageManager::fetch(false, array('id'), $where, 'title ASC'); |
||
372 | |||
373 | $options = array( |
||
374 | array('', false, '/') |
||
375 | ); |
||
376 | |||
377 | if (!empty($pages)) { |
||
378 | foreach ($pages as $page) { |
||
379 | $options[] = array( |
||
380 | $page['id'], $fields['parent'] == $page['id'], |
||
381 | '/' . PageManager::resolvePagePath($page['id']) |
||
382 | ); |
||
383 | } |
||
384 | |||
385 | usort($options, array($this, '__compare_pages')); |
||
386 | } |
||
387 | |||
388 | $label->appendChild(Widget::Select( |
||
389 | 'fields[parent]', $options |
||
390 | )); |
||
391 | $column->appendChild($label); |
||
392 | $group->appendChild($column); |
||
393 | |||
394 | // Parameters --------------------------------------------------------- |
||
395 | |||
396 | $column = new XMLElement('div'); |
||
397 | $column->setAttribute('class', 'column'); |
||
398 | |||
399 | $label = Widget::Label(__('Parameters')); |
||
400 | $label->appendChild(Widget::Input( |
||
401 | 'fields[params]', $fields['params'], 'text', array('placeholder' => 'param1/param2') |
||
402 | )); |
||
403 | $column->appendChild($label); |
||
404 | |||
405 | // Type ----------------------------------------------------------- |
||
406 | |||
407 | $label = Widget::Label(__('Type')); |
||
408 | $label->appendChild(Widget::Input('fields[type]', $fields['type'])); |
||
409 | |||
410 | if (isset($this->_errors['type'])) { |
||
411 | $label = Widget::Error($label, $this->_errors['type']); |
||
412 | } |
||
413 | |||
414 | $column->appendChild($label); |
||
415 | |||
416 | $tags = new XMLElement('ul'); |
||
417 | $tags->setAttribute('class', 'tags'); |
||
418 | $tags->setAttribute('data-interactive', 'data-interactive'); |
||
419 | |||
420 | $types = PageManager::fetchAvailablePageTypes(); |
||
421 | |||
422 | foreach ($types as $type) { |
||
423 | $tags->appendChild(new XMLElement('li', $type)); |
||
424 | } |
||
425 | |||
426 | $column->appendChild($tags); |
||
427 | $group->appendChild($column); |
||
428 | $fieldset->appendChild($group); |
||
429 | $this->Form->appendChild($fieldset); |
||
430 | |||
431 | // Events ------------------------------------------------------------- |
||
432 | |||
433 | $fieldset = new XMLElement('fieldset'); |
||
434 | $fieldset->setAttribute('class', 'settings'); |
||
435 | $fieldset->appendChild(new XMLElement('legend', __('Page Resources'))); |
||
436 | |||
437 | $group = new XMLElement('div'); |
||
438 | $group->setAttribute('class', 'two columns'); |
||
439 | |||
440 | $label = Widget::Label(__('Events')); |
||
441 | $label->setAttribute('class', 'column'); |
||
442 | |||
443 | $events = ResourceManager::fetch(ResourceManager::RESOURCE_TYPE_EVENT, array(), array(), 'name ASC'); |
||
444 | $options = array(); |
||
445 | |||
446 | if (is_array($events) && !empty($events)) { |
||
447 | if (!isset($fields['events'])) { |
||
448 | $fields['events'] = array(); |
||
449 | } |
||
450 | |||
451 | foreach ($events as $name => $about) { |
||
452 | $options[] = array( |
||
453 | $name, in_array($name, $fields['events']), $about['name'] |
||
454 | ); |
||
455 | } |
||
456 | } |
||
457 | |||
458 | $label->appendChild(Widget::Select('fields[events][]', $options, array('multiple' => 'multiple'))); |
||
459 | $group->appendChild($label); |
||
460 | |||
461 | // Data Sources ------------------------------------------------------- |
||
462 | |||
463 | $label = Widget::Label(__('Data Sources')); |
||
464 | $label->setAttribute('class', 'column'); |
||
465 | |||
466 | $datasources = ResourceManager::fetch(ResourceManager::RESOURCE_TYPE_DS, array(), array(), 'name ASC'); |
||
467 | $options = array(); |
||
468 | |||
469 | if (is_array($datasources) && !empty($datasources)) { |
||
470 | if (!isset($fields['data_sources'])) { |
||
471 | $fields['data_sources'] = array(); |
||
472 | } |
||
473 | |||
474 | foreach ($datasources as $name => $about) { |
||
475 | $options[] = array( |
||
476 | $name, in_array($name, $fields['data_sources']), $about['name'] |
||
477 | ); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | $label->appendChild(Widget::Select('fields[data_sources][]', $options, array('multiple' => 'multiple'))); |
||
482 | $group->appendChild($label); |
||
483 | $fieldset->appendChild($group); |
||
484 | $this->Form->appendChild($fieldset); |
||
485 | |||
486 | // Controls ----------------------------------------------------------- |
||
487 | |||
488 | /** |
||
489 | * After all Page related Fields have been added to the DOM, just before the |
||
490 | * actions. |
||
491 | * |
||
492 | * @delegate AppendPageContent |
||
493 | * @param string $context |
||
494 | * '/blueprints/pages/' |
||
495 | * @param XMLElement $form |
||
496 | * @param array $fields |
||
497 | * @param array $errors |
||
498 | */ |
||
499 | Symphony::ExtensionManager()->notifyMembers( |
||
500 | 'AppendPageContent', |
||
501 | '/blueprints/pages/', |
||
502 | array( |
||
503 | 'form' => &$this->Form, |
||
504 | 'fields' => &$fields, |
||
505 | 'errors' => $this->_errors |
||
506 | ) |
||
507 | ); |
||
508 | |||
509 | $div = new XMLElement('div'); |
||
510 | $div->setAttribute('class', 'actions'); |
||
511 | $div->appendChild(Widget::Input( |
||
512 | 'action[save]', ($this->_context[0] == 'edit' ? __('Save Changes') : __('Create Page')), |
||
513 | 'submit', array('accesskey' => 's') |
||
514 | )); |
||
515 | |||
516 | if ($this->_context[0] == 'edit') { |
||
517 | $button = new XMLElement('button', __('Delete')); |
||
518 | $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this page'), 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this page?'))); |
||
519 | $div->appendChild($button); |
||
520 | } |
||
521 | |||
522 | $this->Form->appendChild($div); |
||
523 | |||
524 | if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) { |
||
525 | $this->Form->appendChild(new XMLElement('input', null, array('type' => 'hidden', 'name' => 'parent', 'value' => $_REQUEST['parent']))); |
||
526 | } |
||
527 | } |
||
528 | |||
529 | public function __compare_pages($a, $b) |
||
530 | { |
||
531 | return strnatcasecmp($a[2], $b[2]); |
||
532 | } |
||
533 | |||
534 | public function __actionIndex() |
||
560 | } |
||
561 | } |
||
562 | } |
||
563 | |||
564 | public function __actionTemplate() |
||
565 | { |
||
566 | $filename = $this->_context[1] . '.xsl'; |
||
567 | $file_abs = PAGES . '/' . $filename; |
||
568 | $fields = $_POST['fields']; |
||
569 | $this->_errors = array(); |
||
570 | |||
571 | if (!isset($fields['body']) || trim($fields['body']) == '') { |
||
572 | $this->_errors['body'] = __('This is a required field.'); |
||
573 | } elseif (!General::validateXML($fields['body'], $errors, false, new XSLTProcess())) { |
||
574 | $this->_errors['body'] = __('This document is not well formed.') . ' ' . __('The following error was returned:') . ' <code>' . $errors[0]['message'] . '</code>'; |
||
575 | } |
||
576 | |||
577 | if (empty($this->_errors)) { |
||
578 | /** |
||
579 | * Just before a Page Template is about to written to disk |
||
580 | * |
||
581 | * @delegate PageTemplatePreEdit |
||
582 | * @since Symphony 2.2.2 |
||
583 | * @param string $context |
||
584 | * '/blueprints/pages/template/' |
||
585 | * @param string $file |
||
586 | * The path to the Page Template file |
||
587 | * @param string $contents |
||
588 | * The contents of the `$fields['body']`, passed by reference |
||
589 | */ |
||
590 | Symphony::ExtensionManager()->notifyMembers('PageTemplatePreEdit', '/blueprints/pages/template/', array('file' => $file_abs, 'contents' => &$fields['body'])); |
||
591 | |||
592 | if (!PageManager::writePageFiles($file_abs, $fields['body'])) { |
||
593 | $this->pageAlert( |
||
594 | __('Page Template could not be written to disk.') |
||
595 | . ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')), |
||
596 | Alert::ERROR |
||
597 | ); |
||
598 | } else { |
||
599 | /** |
||
600 | * Just after a Page Template has been edited and written to disk |
||
601 | * |
||
602 | * @delegate PageTemplatePostEdit |
||
603 | * @since Symphony 2.2.2 |
||
604 | * @param string $context |
||
605 | * '/blueprints/pages/template/' |
||
606 | * @param string $file |
||
607 | * The path to the Page Template file |
||
608 | */ |
||
609 | Symphony::ExtensionManager()->notifyMembers('PageTemplatePostEdit', '/blueprints/pages/template/', array('file' => $file_abs)); |
||
610 | |||
611 | redirect(SYMPHONY_URL . '/blueprints/pages/template/' . $this->_context[1] . '/saved/'); |
||
612 | } |
||
613 | } |
||
614 | } |
||
615 | |||
616 | public function __actionNew() |
||
617 | { |
||
618 | $this->__actionEdit(); |
||
619 | } |
||
620 | |||
621 | public function __actionEdit() |
||
885 | ); |
||
886 | } |
||
887 | } |
||
888 | } |
||
889 | |||
890 | public function __actionDelete($pages, $redirect) |
||
969 | } |
||
970 | } |
||
971 | } |
||
972 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.