/**
  * @param array               $options
  * @param DC_General|DcCompat $general
  *
  * @return array
  */
 public function getRecipientFieldOptions(DC_General $general, $options = array())
 {
     global $container;
     //TODO check general parameter
     /** @var EventDispatcher $eventDispatcher */
     $eventDispatcher = $container['event-dispatcher'];
     $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, new LoadLanguageFileEvent('orm_avisota_recipient'));
     $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_LOAD_DATA_CONTAINER, new LoadDataContainerEvent('orm_avisota_recipient'));
     $factory = DcGeneralFactory::deriveFromEnvironment($general->getEnvironment());
     $factory->setContainerName('orm_avisota_recipient');
     $containerFactory = $factory->createContainer();
     if ($containerFactory->hasPropertiesDefinition()) {
         $properties = $containerFactory->getPropertiesDefinition()->getProperties();
         foreach ($properties as $property) {
             if ($property->getWidgetType()) {
                 $options[$property->getName()] = $property->getLabel();
             }
         }
     }
     return $options;
 }
 /**
  * @param EnvironmentInterface $environment
  * @param array                $options
  *
  * @return array
  * @SuppressWarnings(PHPMD.LongVariable)
  */
 public function getRecipientPropertiesOptions(EnvironmentInterface $environment, $options = array())
 {
     global $container;
     if (!is_array($options)) {
         $options = (array) $options;
     }
     /** @var EventDispatcher $eventDispatcher */
     $eventDispatcher = $container['event-dispatcher'];
     $loadDataContainerEvent = new LoadDataContainerEvent('orm_avisota_recipient');
     $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_LOAD_DATA_CONTAINER, $loadDataContainerEvent);
     $loadLanguageFileEvent = new LoadLanguageFileEvent('orm_avisota_recipient');
     $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $loadLanguageFileEvent);
     $dcGeneralFactory = DcGeneralFactory::deriveFromEnvironment($environment);
     $dcGeneralFactory->setContainerName('orm_avisota_recipient');
     $containerFactory = $dcGeneralFactory->createContainer();
     $properties = $containerFactory->getPropertiesDefinition()->getProperties();
     foreach ($properties as $property) {
         $options[$property->getName()] = $property->getLabel() ?: $property->getName();
     }
     return $options;
 }
 /**
  * Do deep copy.
  *
  * @param array $deepCopyList The deep copy list.
  *
  * @return void
  */
 protected function doDeepCopy(array $deepCopyList)
 {
     if (empty($deepCopyList)) {
         return;
     }
     $factory = DcGeneralFactory::deriveFromEnvironment($this->getEnvironment());
     $dataDefinition = $this->getEnvironment()->getDataDefinition();
     $modelRelationshipDefinition = $dataDefinition->getModelRelationshipDefinition();
     $childConditions = $modelRelationshipDefinition->getChildConditions($dataDefinition->getName());
     foreach ($deepCopyList as $deepCopy) {
         /** @var ModelInterface $origin */
         $origin = $deepCopy['origin'];
         /** @var ModelInterface $model */
         $model = $deepCopy['model'];
         $parentId = ModelId::fromModel($model);
         foreach ($childConditions as $childCondition) {
             // create new destination environment
             $destinationName = $childCondition->getDestinationName();
             $factory->setContainerName($destinationName);
             $destinationEnvironment = $factory->createEnvironment();
             $destinationDataDefinition = $destinationEnvironment->getDataDefinition();
             $destinationViewDefinition = $destinationDataDefinition->getDefinition(Contao2BackendViewDefinitionInterface::NAME);
             $destinationDataProvider = $destinationEnvironment->getDataProvider();
             $destinationController = $destinationEnvironment->getController();
             /** @var Contao2BackendViewDefinitionInterface $destinationViewDefinition */
             /** @var DefaultController $destinationController */
             $listingConfig = $destinationViewDefinition->getListingConfig();
             $groupAndSortingCollection = $listingConfig->getGroupAndSortingDefinition();
             $groupAndSorting = $groupAndSortingCollection->getDefault();
             // ***** fetch the children
             $filter = $childCondition->getFilter($origin);
             // apply parent-child condition
             $config = $destinationDataProvider->getEmptyConfig();
             $config->setFilter($filter);
             // apply sorting
             $sorting = array();
             foreach ($groupAndSorting as $information) {
                 /** @var GroupAndSortingInformationInterface $information */
                 $sorting[$information->getProperty()] = $information->getSortingMode();
             }
             $config->setSorting($sorting);
             // receive children
             $children = $destinationDataProvider->fetchAll($config);
             // ***** do the deep copy
             $actions = array();
             // build the copy actions
             foreach ($children as $childModel) {
                 $childModelId = ModelId::fromModel($childModel);
                 $actions[] = array('model' => $childModel, 'item' => new Item(ItemInterface::DEEP_COPY, $parentId, $childModelId));
             }
             // do the deep copy
             $childrenModels = $destinationController->doActions($actions, null, null, $parentId);
             // ensure parent-child condition
             foreach ($childrenModels as $childrenModel) {
                 $childCondition->applyTo($model, $childrenModel);
             }
             $destinationDataProvider->saveEach($childrenModels);
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Retrieve a list of html buttons to use in the top panel (submit area).
  *
  * @param ModelInterface $parentModel The parent model.
  *
  * @return array
  */
 protected function getHeaderEditButtons($parentModel)
 {
     $environment = $this->getEnvironment();
     $parentDefinition = $environment->getParentDataDefinition();
     if ($parentDefinition && $parentDefinition->getBasicDefinition()->isEditable()) {
         $definition = $environment->getDataDefinition();
         $basicDefinition = $definition->getBasicDefinition();
         $parentName = $basicDefinition->getParentDataProvider();
         $propagator = $environment->getEventPropagator();
         $query = array('do' => $environment->getInputProvider()->getParameter('do'), 'act' => 'edit', 'table' => $parentName, 'id' => IdSerializer::fromModel($parentModel)->getSerialized());
         $factory = DcGeneralFactory::deriveFromEnvironment($this->environment);
         $factory->setContainerName($parentDefinition->getName());
         $parentContainer = $factory->createContainer();
         if ($parentContainer->getBasicDefinition()->getParentDataProvider()) {
             $container = $this->environment->getDataDefinition();
             $relationship = $container->getModelRelationshipDefinition()->getChildCondition($parentContainer->getBasicDefinition()->getParentDataProvider(), $parentContainer->getName());
             if ($relationship) {
                 $filter = $relationship->getInverseFilterFor($parentModel);
                 $parentsParentProvider = $this->environment->getDataProvider($parentContainer->getBasicDefinition()->getParentDataProvider());
                 $config = $parentsParentProvider->getEmptyConfig();
                 $config->setFilter($filter);
                 $parents = $parentsParentProvider->fetchAll($config);
                 if ($parents->length() == 1) {
                     $query['pid'] = IdSerializer::fromModel($parents->get(0))->getSerialized();
                 } elseif ($parents->length() > 1) {
                     return null;
                 }
             }
         }
         /** @var GenerateHtmlEvent $imageEvent */
         $imageEvent = $propagator->propagate(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent('edit.gif', $this->translate('editheader.0', $definition->getName())));
         return sprintf('<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>', 'contao/main.php?' . http_build_query($query), specialchars($this->translate('editheader.1', $definition->getName())), $imageEvent->getHtml());
     }
     return null;
 }
    /**
     * @param ActionEvent                   $event
     * @param null                          $eventName
     * @param EventDispatcherInterface|null $eventDispatcher
     * @SuppressWarnings(PHPMD.CamelCaseVariableName)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function injectAutocompleter(ActionEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
    {
        global $container, $TL_CSS, $TL_JAVASCRIPT, $TL_MOOTOOLS;
        static $injected;
        if (!$injected && $event->getEnvironment()->getDataDefinition()->getName() == 'orm_avisota_salutation') {
            // backwards compatibility
            if (!$eventDispatcher) {
                /** @var EventDispatcher $eventDispatcher */
                $eventDispatcher = $container['event-dispatcher'];
            }
            // load language file
            $loadEvent = new LoadLanguageFileEvent('orm_avisota_recipient');
            $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $loadEvent);
            // load data container
            $loadEvent = new LoadDataContainerEvent('orm_avisota_recipient');
            $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_LOAD_DATA_CONTAINER, $loadEvent);
            // inject styles
            $TL_CSS[] = 'assets/avisota/subscription-recipient/css/meio.autocomplete.css';
            // inject scripts
            $TL_JAVASCRIPT[] = 'assets/avisota/subscription-recipient/js/Meio.Autocomplete.js';
            $TL_JAVASCRIPT[] = 'assets/avisota/subscription-recipient/js/mootools-more-1.5.0.js';
            // build container for orm_avisota_recipient
            $factory = DcGeneralFactory::deriveFromEnvironment($event->getEnvironment());
            $factory->setContainerName('orm_avisota_recipient');
            $containerFactory = $factory->createContainer();
            // build token list
            $tokens = array();
            foreach ($containerFactory->getPropertiesDefinition()->getPropertyNames() as $propertyName) {
                $tokens[] = array('value' => $propertyName, 'text' => sprintf('##%s##', $propertyName));
            }
            $tokens = json_encode($tokens);
            // inject runtime code
            // TODO outsource in template
            $TL_MOOTOOLS[] = <<<EOF
<script>
var element = \$('ctrl_salutation');
if (element) {
\tvar tokens = {$tokens};
\tvar options = {
\t\tfilter: {
\t\t\ttype: 'contains',
\t\t\tpath: 'text'
\t\t},
\t\ttokenize: {
\t\t\tget: function(element) {
\t\t\t\tvar text     = element.get('value');
\t\t\t\tvar position = element.getCaretPosition();
\t\t\t\tvar start    = text.lastIndexOf(' ', position - 1);
\t\t\t\tvar end      = text.indexOf(' ', position);

\t\t\t\tif (start == -1) {
\t\t\t\t\tstart = 0;
\t\t\t\t}
\t\t\t\telse {
\t\t\t\t\tstart ++;
\t\t\t\t}
\t\t\t\tif (end == -1) {
\t\t\t\t\tend = text.length;
\t\t\t\t}

\t\t\t\tvar token = text.substring(start, end);
\t\t\t\tconsole.log('position: ' + position + ', start: ' + start + ', end: ' + end + ', token: ' + token);

\t\t\t\treturn token;
\t\t\t},
\t\t\tset: function(element, token) {
\t\t\t\tvar text     = element.get('value');
\t\t\t\tvar position = element.getCaretPosition();
\t\t\t\tvar start    = text.lastIndexOf(' ', position - 1);
\t\t\t\tvar end      = text.indexOf(' ', position);

\t\t\t\tif (start == -1) {
\t\t\t\t\tstart = 0;
\t\t\t\t}
\t\t\t\telse {
\t\t\t\t\tstart ++;
\t\t\t\t}
\t\t\t\tif (end == -1) {
\t\t\t\t\tend = text.length;
\t\t\t\t}

\t\t\t\ttext = text.substring(0, start) + token + text.substring(end);

\t\t\t\telement.set('value', text);
\t\t\t\telement.setCaretPosition(start + token.length);
\t\t\t}
\t\t}
\t};
\tnew Meio.Autocomplete(element, tokens, options);
}
</script>
EOF;
            $injected = true;
        }
    }