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