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