Conditions | 31 |
Paths | > 20000 |
Total Lines | 259 |
Code Lines | 114 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
404 | public function __formAction() |
||
405 | { |
||
406 | $fields = $_POST['fields']; |
||
407 | $this->_errors = array(); |
||
408 | $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT); |
||
409 | $providerClass = null; |
||
410 | |||
411 | if (trim($fields['name']) == '') { |
||
412 | $this->_errors['name'] = __('This is a required field'); |
||
413 | } elseif (strpos($fields['name'], '\\') !== false) { |
||
414 | $this->_errors['name'] = __('This field contains invalid characters') . ' (\\)'; |
||
415 | } |
||
416 | |||
417 | if (trim($fields['source']) == '') { |
||
418 | $this->_errors['source'] = __('This is a required field'); |
||
419 | } |
||
420 | |||
421 | $filters = isset($fields['filters']) ? $fields['filters'] : array(); |
||
422 | |||
423 | // See if a Provided Datasource is saved |
||
424 | if (!empty($providers)) { |
||
425 | foreach ($providers as $providerClass => $provider) { |
||
426 | if ($fields['source'] == call_user_func(array($providerClass, 'getSource'))) { |
||
427 | call_user_func_array(array($providerClass, 'validate'), array(&$fields, &$this->_errors)); |
||
428 | break; |
||
429 | } |
||
430 | |||
431 | unset($providerClass); |
||
432 | } |
||
433 | } |
||
434 | |||
435 | $classname = Lang::createHandle($fields['name'], 255, '_', false, true, array('@^[^a-z\d]+@i' => '', '/[^\w-\.]/i' => '')); |
||
436 | $rootelement = str_replace('_', '-', $classname); |
||
437 | $extends = 'SectionEvent'; |
||
438 | |||
439 | // Check to make sure the classname is not empty after handlisation. |
||
440 | if (empty($classname) && !isset($this->_errors['name'])) { |
||
441 | $this->_errors['name'] = __('Please ensure name contains at least one Latin-based character.', array($classname)); |
||
442 | } |
||
443 | |||
444 | $file = EVENTS . '/event.' . $classname . '.php'; |
||
445 | $isDuplicate = false; |
||
446 | $queueForDeletion = null; |
||
447 | |||
448 | if ($this->_context[0] == 'new' && is_file($file)) { |
||
449 | $isDuplicate = true; |
||
450 | } elseif ($this->_context[0] == 'edit') { |
||
451 | $existing_handle = $this->_context[1]; |
||
452 | |||
453 | if ($classname != $existing_handle && is_file($file)) { |
||
454 | $isDuplicate = true; |
||
455 | } elseif ($classname != $existing_handle) { |
||
456 | $queueForDeletion = EVENTS . '/event.' . $existing_handle . '.php'; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | // Duplicate |
||
461 | if ($isDuplicate) { |
||
462 | $this->_errors['name'] = __('An Event with the name %s already exists', array('<code>' . $classname . '</code>')); |
||
463 | } |
||
464 | |||
465 | if (empty($this->_errors)) { |
||
466 | $source = $fields['source']; |
||
467 | $params = array( |
||
468 | 'rootelement' => $rootelement, |
||
469 | ); |
||
470 | |||
471 | $about = array( |
||
472 | 'name' => $fields['name'], |
||
473 | 'version' => 'Symphony ' . Symphony::Configuration()->get('version', 'symphony'), |
||
474 | 'release date' => DateTimeObj::getGMT('c'), |
||
475 | 'author name' => Symphony::Author()->getFullName(), |
||
476 | 'author website' => URL, |
||
477 | 'author email' => Symphony::Author()->get('email') |
||
478 | ); |
||
479 | |||
480 | // If there is a provider, get their template |
||
481 | if ($providerClass) { |
||
482 | $eventShell = file_get_contents(call_user_func(array($providerClass, 'getTemplate'))); |
||
483 | } else { |
||
484 | $eventShell = file_get_contents($this->getTemplate('blueprints.event')); |
||
485 | $about['trigger condition'] = $rootelement; |
||
486 | } |
||
487 | |||
488 | $this->__injectAboutInformation($eventShell, $about); |
||
489 | |||
490 | // Replace the name |
||
491 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
492 | |||
493 | // Build the templates |
||
494 | if ($providerClass) { |
||
495 | $eventShell = call_user_func(array($providerClass, 'prepare'), $fields, $params, $eventShell); |
||
496 | } else { |
||
497 | $this->__injectFilters($eventShell, $filters); |
||
498 | |||
499 | // Add Documentation |
||
500 | $ajaxEventDoc = new contentAjaxEventDocumentation(); |
||
501 | $doc_parts = array(); |
||
502 | |||
503 | // Add Documentation (Success/Failure) |
||
504 | $ajaxEventDoc->addEntrySuccessDoc($doc_parts, $rootelement, $filters); |
||
505 | $ajaxEventDoc->addEntryFailureDoc($doc_parts, $rootelement, $filters); |
||
506 | |||
507 | // Filters |
||
508 | $ajaxEventDoc->addDefaultFiltersDoc($doc_parts, $rootelement, $filters); |
||
509 | |||
510 | // Frontend Markup |
||
511 | $ajaxEventDoc->addFrontendMarkupDoc($doc_parts, $rootelement, $fields['source'], $filters); |
||
512 | $ajaxEventDoc->addSendMailFilterDoc($doc_parts, $filters); |
||
513 | |||
514 | /** |
||
515 | * Allows adding documentation for new filters. A reference to the $documentation |
||
516 | * array is provided, along with selected filters |
||
517 | * @delegate AppendEventFilterDocumentation |
||
518 | * @param string $context |
||
519 | * '/blueprints/events/(edit|new|info)/' |
||
520 | * @param array $selected |
||
521 | * An array of all the selected filters for this Event |
||
522 | * @param array $documentation |
||
523 | * An array of all the documentation XMLElements, passed by reference |
||
524 | * @param string $rootelment |
||
525 | * The name of this event, as a handle. |
||
526 | */ |
||
527 | Symphony::ExtensionManager()->notifyMembers('AppendEventFilterDocumentation', '/blueprints/events/', array( |
||
528 | 'selected' => $filters, |
||
529 | 'documentation' => &$doc_parts, |
||
530 | 'rootelement' => $rootelement |
||
531 | )); |
||
532 | |||
533 | $documentation = join(PHP_EOL, array_map(function($part) { |
||
534 | return rtrim($part->generate(true, 4)); |
||
535 | }, $doc_parts)); |
||
536 | $documentation = str_replace('\'', '\\\'', $documentation); |
||
537 | |||
538 | $eventShell = str_replace('<!-- CLASS EXTENDS -->', $extends, $eventShell); |
||
539 | $eventShell = str_replace('<!-- DOCUMENTATION -->', General::tabsToSpaces($documentation, 4), $eventShell); |
||
540 | } |
||
541 | |||
542 | $eventShell = str_replace('<!-- ROOT ELEMENT -->', $rootelement, $eventShell); |
||
543 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
544 | $eventShell = str_replace('<!-- SOURCE -->', addslashes($source), $eventShell); |
||
545 | |||
546 | // Remove left over placeholders |
||
547 | $eventShell = preg_replace(array('/<!--[\w ]++-->/'), '', $eventShell); |
||
548 | |||
549 | if ($this->_context[0] == 'new') { |
||
550 | /** |
||
551 | * Prior to creating an Event, the file path where it will be written to |
||
552 | * is provided and well as the contents of that file. |
||
553 | * |
||
554 | * @delegate EventsPreCreate |
||
555 | * @since Symphony 2.2 |
||
556 | * @param string $context |
||
557 | * '/blueprints/events/' |
||
558 | * @param string $file |
||
559 | * The path to the Event file |
||
560 | * @param string $contents |
||
561 | * The contents for this Event as a string passed by reference |
||
562 | * @param array $filters |
||
563 | * An array of the filters attached to this event |
||
564 | */ |
||
565 | Symphony::ExtensionManager()->notifyMembers('EventPreCreate', '/blueprints/events/', array( |
||
566 | 'file' => $file, |
||
567 | 'contents' => &$eventShell, |
||
568 | 'filters' => $filters |
||
569 | )); |
||
570 | } else { |
||
571 | /** |
||
572 | * Prior to editing an Event, the file path where it will be written to |
||
573 | * is provided and well as the contents of that file. |
||
574 | * |
||
575 | * @delegate EventPreEdit |
||
576 | * @since Symphony 2.2 |
||
577 | * @param string $context |
||
578 | * '/blueprints/events/' |
||
579 | * @param string $file |
||
580 | * The path to the Event file |
||
581 | * @param string $contents |
||
582 | * The contents for this Event as a string passed by reference |
||
583 | * @param array $filters |
||
584 | * An array of the filters attached to this event |
||
585 | */ |
||
586 | Symphony::ExtensionManager()->notifyMembers('EventPreEdit', '/blueprints/events/', array( |
||
587 | 'file' => $file, |
||
588 | 'contents' => &$eventShell, |
||
589 | 'filters' => $filters |
||
590 | )); |
||
591 | } |
||
592 | |||
593 | // Write the file |
||
594 | if (!General::writeFile($file, $eventShell, Symphony::Configuration()->get('write_mode', 'file'))) { |
||
595 | $this->pageAlert( |
||
596 | __('Failed to write Event to disk.') |
||
597 | . ' ' . __('Please check permissions on %s.', array('<code>/workspace/events</code>')), |
||
598 | Alert::ERROR |
||
599 | ); |
||
600 | |||
601 | // Write successful |
||
602 | } else { |
||
603 | if (function_exists('opcache_invalidate')) { |
||
604 | opcache_invalidate($file, true); |
||
605 | } |
||
606 | |||
607 | // Attach this event to pages |
||
608 | $connections = $fields['connections']; |
||
609 | ResourceManager::setPages(ResourceManager::RESOURCE_TYPE_EVENT, is_null($existing_handle) ? $classname : $existing_handle, $connections); |
||
610 | |||
611 | if ($queueForDeletion) { |
||
612 | General::deleteFile($queueForDeletion); |
||
613 | |||
614 | $pages = PageManager::fetch(false, array('events', 'id'), array(" |
||
615 | `events` REGEXP '[[:<:]]" . $existing_handle . "[[:>:]]' |
||
616 | ")); |
||
617 | |||
618 | if (is_array($pages) && !empty($pages)) { |
||
619 | foreach ($pages as $page) { |
||
620 | $page['events'] = preg_replace('/\b'.$existing_handle.'\b/i', $classname, $page['events']); |
||
621 | |||
622 | PageManager::edit($page['id'], $page); |
||
623 | } |
||
624 | } |
||
625 | } |
||
626 | |||
627 | if ($this->_context[0] == 'new') { |
||
628 | /** |
||
629 | * After creating the Event, the path to the Event file is provided |
||
630 | * |
||
631 | * @delegate EventPostCreate |
||
632 | * @since Symphony 2.2 |
||
633 | * @param string $context |
||
634 | * '/blueprints/events/' |
||
635 | * @param string $file |
||
636 | * The path to the Event file |
||
637 | */ |
||
638 | Symphony::ExtensionManager()->notifyMembers('EventPostCreate', '/blueprints/events/', array( |
||
639 | 'file' => $file |
||
640 | )); |
||
641 | } else { |
||
642 | /** |
||
643 | * After editing the Event, the path to the Event file is provided |
||
644 | * |
||
645 | * @delegate EventPostEdit |
||
646 | * @since Symphony 2.2 |
||
647 | * @param string $context |
||
648 | * '/blueprints/events/' |
||
649 | * @param string $file |
||
650 | * The path to the Event file |
||
651 | * @param string $previous_file |
||
652 | * The path of the previous Event file in the case where an Event may |
||
653 | * have been renamed. To get the handle from this value, see |
||
654 | * `EventManager::__getHandleFromFilename` |
||
655 | */ |
||
656 | Symphony::ExtensionManager()->notifyMembers('EventPostEdit', '/blueprints/events/', array( |
||
657 | 'file' => $file, |
||
658 | 'previous_file' => ($queueForDeletion) ? $queueForDeletion : null |
||
659 | )); |
||
660 | } |
||
661 | |||
662 | redirect(SYMPHONY_URL . '/blueprints/events/edit/'.$classname.'/'.($this->_context[0] == 'new' ? 'created' : 'saved') . '/'); |
||
663 | } |
||
691 |