Beispiel #1
0
 /**
  * Render paste top button. Returns null if no button should be rendered.
  *
  * @param string $sorting The sorting mode.
  *
  * @return string
  */
 protected function renderPasteTopButton($sorting)
 {
     $definition = $this->getEnvironment()->getDataDefinition();
     $dispatcher = $this->getEnvironment()->getEventDispatcher();
     $basicDefinition = $definition->getBasicDefinition();
     $clipboard = $this->getEnvironment()->getClipboard();
     $filter = new Filter();
     $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
     if ($sorting && $clipboard->isNotEmpty($filter)) {
         $allowPasteTop = ViewHelpers::getManualSortingProperty($this->environment);
         if ($allowPasteTop) {
             /** @var AddToUrlEvent $urlEvent */
             $urlEvent = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent('act=paste&after=' . IdSerializer::fromValues($definition->getName(), 0)->getSerialized()));
             /** @var GenerateHtmlEvent $imageEvent */
             $imageEvent = $dispatcher->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent('pasteafter.gif', $this->translate('pasteafter.0', $definition->getName()), 'class="blink"'));
             return sprintf('<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>', $urlEvent->getUrl(), specialchars($this->translate('pasteafter.0', $definition->getName())), $imageEvent->getHtml());
         }
     }
     return null;
 }
 /**
  * Handle "old" add to clipboard actions.
  *
  * @param ActionEvent $event The action event.
  *
  * @return void
  */
 private function addToClipboard(ActionEvent $event)
 {
     $actionName = $event->getAction()->getName();
     $environment = $event->getEnvironment();
     $input = $environment->getInputProvider();
     $clipboard = $environment->getClipboard();
     if ('create' === $actionName) {
         $modelId = IdSerializer::fromValues($environment->getDataDefinition()->getBasicDefinition()->getDataProvider(), null);
     } else {
         $modelIdRaw = $input->getParameter('source');
         $modelId = IdSerializer::fromSerialized($modelIdRaw);
     }
     $parentIdRaw = $input->getParameter('pid');
     if ($parentIdRaw) {
         $parentId = IdSerializer::fromSerialized($parentIdRaw);
     } else {
         $parentId = null;
     }
     // Push some entry into clipboard.
     if ($modelId) {
         $clipboardActionName = $this->translateActionName($actionName);
         if ($clipboardActionName) {
             // Remove other create items, there can only be one create item in the clipboard or many others
             if (Item::CREATE === $clipboardActionName) {
                 $clipboard->clear();
             } else {
                 $filter = new Filter();
                 $filter->andActionIs(ItemInterface::CREATE);
                 $items = $clipboard->fetch($filter);
                 foreach ($items as $item) {
                     $clipboard->remove($item);
                 }
             }
             // Only push item to clipboard if manual sorting is used.
             if (Item::COPY === $clipboardActionName && !ViewHelpers::getManualSortingProperty($environment)) {
                 return;
             }
             // create the new item
             $item = new Item($clipboardActionName, $parentId, $modelId);
             // Let the clipboard save it's values persistent.
             // TODO remove clear and allow adding multiple items
             $clipboard->clear()->push($item)->saveTo($environment);
             ViewHelpers::redirectHome($environment);
         }
     }
 }
Beispiel #3
0
 /**
  * Compile buttons from the table configuration array and return them as HTML.
  *
  * @param ModelInterface $model    The model for which the buttons shall be generated for.
  * @param ModelInterface $previous The previous model in the collection.
  * @param ModelInterface $next     The next model in the collection.
  *
  * @return string
  */
 protected function generateButtons(ModelInterface $model, ModelInterface $previous = null, ModelInterface $next = null)
 {
     $environment = $this->getEnvironment();
     $dataDefinition = $environment->getDataDefinition();
     $basicDefinition = $dataDefinition->getBasicDefinition();
     $commands = $this->getViewSection()->getModelCommands();
     $clipboard = $environment->getClipboard();
     $dispatcher = $environment->getEventDispatcher();
     $filter = new Filter();
     $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
     if ($parentDataProviderName = $basicDefinition->getParentDataProvider()) {
         $filter->andParentIsFromProvider($parentDataProviderName);
     } else {
         $filter->andHasNoParent();
     }
     if ($clipboard->isNotEmpty($filter)) {
         $circularIds = $clipboard->getCircularIds();
         $isCircular = in_array(ModelId::fromModel($model)->getSerialized(), $circularIds);
     } else {
         $circularIds = array();
         $isCircular = false;
     }
     $arrButtons = array();
     foreach ($commands->getCommands() as $command) {
         $arrButtons[$command->getName()] = $this->buildCommand($command, $model, $isCircular, $circularIds, $previous, $next);
     }
     if (ViewHelpers::getManualSortingProperty($this->environment)) {
         $clipboardIsEmpty = $clipboard->isEmpty($filter);
         if ($clipboardIsEmpty && BasicDefinitionInterface::MODE_HIERARCHICAL !== $basicDefinition->getMode()) {
             /** @var AddToUrlEvent $urlEvent */
             $urlEvent = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent('act=create&amp;after=' . ModelId::fromModel($model)->getSerialized()));
             /** @var GenerateHtmlEvent $imageEvent */
             $imageEvent = $dispatcher->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent('new.gif', $this->translate('pastenew.0', $this->getDataDefinition()->getName())));
             $arrButtons['pasteNew'] = sprintf('<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>', $urlEvent->getUrl(), specialchars($this->translate('pastenew.1', $this->getDataDefinition()->getName())), $imageEvent->getHtml());
         }
         // Add paste into/after icons.
         if (!$clipboardIsEmpty) {
             if ($clipboard->isCreate()) {
                 // Add ext. information.
                 $add2UrlAfter = sprintf('act=create&after=%s&', ModelId::fromModel($model)->getSerialized());
                 $add2UrlInto = sprintf('act=create&into=%s&', ModelId::fromModel($model)->getSerialized());
             } else {
                 // Add ext. information.
                 $add2UrlAfter = sprintf('act=paste&after=%s&', ModelId::fromModel($model)->getSerialized());
                 $add2UrlInto = sprintf('act=paste&into=%s&', ModelId::fromModel($model)->getSerialized());
             }
             /** @var AddToUrlEvent $urlAfter */
             $urlAfter = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent($add2UrlAfter));
             /** @var AddToUrlEvent $urlInto */
             $urlInto = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent($add2UrlInto));
             $models = $this->environment->getController()->getModelsFromClipboard($parentDataProviderName ? IdSerializer::fromValues($parentDataProviderName, null) : null);
             $buttonEvent = new GetPasteButtonEvent($this->getEnvironment());
             $buttonEvent->setModel($model)->setCircularReference($isCircular)->setPrevious($previous)->setNext($next)->setHrefAfter($urlAfter->getUrl())->setHrefInto($urlInto->getUrl())->setPasteAfterDisabled($clipboard->isCut() && $isCircular)->setPasteIntoDisabled($clipboard->isCut() && $isCircular)->setContainedModels($models);
             $this->getEnvironment()->getEventDispatcher()->dispatch(GetPasteButtonEvent::NAME, $buttonEvent);
             $arrButtons['pasteafter'] = $this->renderPasteAfterButton($buttonEvent);
             if ($this->getDataDefinition()->getBasicDefinition()->getMode() == BasicDefinitionInterface::MODE_HIERARCHICAL) {
                 $arrButtons['pasteinto'] = $this->renderPasteIntoButton($buttonEvent);
             }
         }
     }
     return implode(' ', $arrButtons);
 }
Beispiel #4
0
 /**
  * {@inheritDoc}
  */
 public function fetchModelFromProvider($id, $providerName = null)
 {
     if ($providerName === null) {
         if (is_string($id)) {
             $id = IdSerializer::fromSerialized($id);
         }
     } else {
         $id = IdSerializer::fromValues($providerName, $id);
     }
     $dataProvider = $this->getEnvironment()->getDataProvider($id->getDataProviderName());
     $item = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($id->getId()));
     return $item;
 }
Beispiel #5
0
 /**
  * Create the "new" button.
  *
  * @return CommandInterface|null
  */
 protected function getCreateModelCommand()
 {
     $environment = $this->getEnvironment();
     $definition = $environment->getDataDefinition();
     $basicDefinition = $definition->getBasicDefinition();
     $providerName = $environment->getDataDefinition()->getName();
     $mode = $basicDefinition->getMode();
     $config = $this->getEnvironment()->getController()->getBaseConfig();
     $sorting = $this->getManualSortingProperty();
     if ($serializedPid = $environment->getInputProvider()->getParameter('pid')) {
         $pid = IdSerializer::fromSerialized($serializedPid);
     } else {
         $pid = new IdSerializer();
     }
     if ($basicDefinition->isClosed()) {
         return null;
     }
     $command = new Command();
     $parameters = $command->getParameters();
     $extra = $command->getExtra();
     $extra['class'] = 'header_new';
     $extra['accesskey'] = 'n';
     $extra['attributes'] = 'onclick="Backend.getScrollOffset();"';
     $command->setName('button_new')->setLabel($this->translate('new.0', $providerName))->setDescription($this->translate('new.1', $providerName));
     $this->getPanel()->initialize($config);
     // Add new button.
     if ($mode == BasicDefinitionInterface::MODE_FLAT || $mode == BasicDefinitionInterface::MODE_PARENTEDLIST && !$sorting) {
         $parameters['act'] = 'create';
         // Add new button.
         if ($pid->getDataProviderName() && $pid->getId()) {
             $parameters['pid'] = $pid->getSerialized();
         }
     } elseif ($mode == BasicDefinitionInterface::MODE_PARENTEDLIST || $mode == BasicDefinitionInterface::MODE_HIERARCHICAL) {
         if ($environment->getClipboard()->isNotEmpty()) {
             return null;
         }
         $after = IdSerializer::fromValues($definition->getName(), 0);
         $parameters['act'] = 'paste';
         $parameters['mode'] = 'create';
         if ($mode == BasicDefinitionInterface::MODE_PARENTEDLIST) {
             $parameters['after'] = $after->getSerialized();
         }
         if ($pid->getDataProviderName() && $pid->getId()) {
             $parameters['pid'] = $pid->getSerialized();
         }
     }
     return $command;
 }