/**
  * Build reduced href required by legacy callbacks.
  *
  * @param CommandInterface $command
  * @return string
  */
 protected function buildHref(CommandInterface $command)
 {
     $arrParameters = (array) $command->getParameters();
     $strHref = '';
     foreach ($arrParameters as $key => $value) {
         $strHref .= sprintf('&%s=%s', $key, $value);
     }
     return $strHref;
 }
Ejemplo n.º 2
0
 /**
  * Get the correct label for a command button.
  *
  * @param CommandInterface $command The command.
  *
  * @return string
  */
 private function getCommandLabel(CommandInterface $command)
 {
     if (!strlen($label = $command->getLabel())) {
         $label = $command->getName();
     }
     $label = $this->translate($label);
     if (is_array($label)) {
         $label = $label[0];
         return $label;
     }
     return $label;
 }
Ejemplo n.º 3
0
 /**
  * Render a single header button.
  *
  * @param CommandInterface $command The command definition.
  *
  * @return string
  */
 protected function generateHeaderButton(CommandInterface $command)
 {
     $environment = $this->getEnvironment();
     $extra = $command->getExtra();
     $label = $this->translate($command->getLabel());
     $dispatcher = $environment->getEventDispatcher();
     if (isset($extra['href'])) {
         $href = $extra['href'];
     } else {
         $href = '';
         foreach ($command->getParameters() as $key => $value) {
             $href .= '&' . $key . '=' . $value;
         }
         /** @var AddToUrlEvent $event */
         $event = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent($href));
         $href = $event->getUrl();
     }
     if (!strlen($label)) {
         $label = $command->getName();
     }
     $buttonEvent = new GetGlobalButtonEvent($this->getEnvironment());
     $buttonEvent->setAccessKey(isset($extra['accesskey']) ? trim($extra['accesskey']) : null)->setAttributes(' ' . ltrim($extra['attributes']))->setClass($extra['class'])->setKey($command->getName())->setHref($href)->setLabel($label)->setTitle($this->translate($command->getDescription()));
     $environment->getEventDispatcher()->dispatch(GetGlobalButtonEvent::NAME, $buttonEvent);
     // Allow to override the button entirely.
     $html = $buttonEvent->getHtml();
     if ($html !== null) {
         return $html;
     }
     // Use the view native button building.
     return sprintf('<a href="%s" class="%s" title="%s"%s>%s</a> ', $buttonEvent->getHref(), $buttonEvent->getClass(), specialchars($buttonEvent->getTitle()), $buttonEvent->getAttributes(), $buttonEvent->getLabel());
 }
Ejemplo n.º 4
0
 /**
  * Render a command button.
  *
  * @param CommandInterface $objCommand           The command to render the button for.
  *
  * @param ModelInterface   $objModel             The model to which the command shall get applied.
  *
  * @param bool             $blnCircularReference Determinator if there exists a circular reference between the model
  *                                               and the model(s) contained in the clipboard.
  *
  * @param array            $arrChildRecordIds    List of the ids of all child models of the current model.
  *
  * @param ModelInterface   $previous             The previous model in the collection.
  *
  * @param ModelInterface   $next                 The next model in the collection.
  *
  * @return string
  */
 protected function buildCommand($objCommand, $objModel, $blnCircularReference, $arrChildRecordIds, $previous, $next)
 {
     $environment = $this->getEnvironment();
     $inputProvider = $environment->getInputProvider();
     $dispatcher = $environment->getEventDispatcher();
     $dataDefinitionName = $environment->getDataDefinition()->getName();
     $commandName = $objCommand->getName();
     $parameters = (array) $objCommand->getParameters();
     $extra = (array) $objCommand->getExtra();
     $extraAttributes = !empty($extra['attributes']) ? $extra['attributes'] : null;
     $attributes = '';
     // Set basic information.
     $opLabel = $objCommand->getLabel();
     if (strlen($opLabel)) {
         $label = $opLabel;
     } else {
         $label = $commandName;
     }
     $label = $this->translate($label, $dataDefinitionName);
     if (is_array($label)) {
         $label = $label[0];
     }
     $opDesc = $this->translate($objCommand->getDescription(), $dataDefinitionName);
     if (strlen($opDesc)) {
         $title = sprintf($opDesc, $objModel->getID());
     } else {
         $title = sprintf('%s id %s', $label, $objModel->getID());
     }
     // Toggle has to trigger the javascript.
     if ($objCommand instanceof ToggleCommandInterface) {
         $parameters['act'] = $commandName;
         $icon = $extra['icon'];
         $iconDisabled = isset($extra['icon_disabled']) ? $extra['icon_disabled'] : 'invisible.gif';
         $attributes = sprintf('onclick="Backend.getScrollOffset(); return BackendGeneral.toggleVisibility(this, \'%s\', \'%s\');"', $icon, $iconDisabled);
         $dataProvider = $this->getEnvironment()->getDataProvider($objModel->getProviderName());
         if ($objCommand instanceof TranslatedToggleCommandInterface && $dataProvider instanceof MultiLanguageDataProviderInterface) {
             $language = $dataProvider->getCurrentLanguage();
             $dataProvider->setCurrentLanguage($objCommand->getLanguage());
             $propModel = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($objModel->getId())->setFields($objCommand->getToggleProperty()));
             $dataProvider->setCurrentLanguage($language);
         } else {
             $propModel = $objModel;
         }
         $state = $objCommand->isInverse() ? !$propModel->getProperty($objCommand->getToggleProperty()) : $propModel->getProperty($objCommand->getToggleProperty());
         if (!$state) {
             $extra['icon'] = $iconDisabled ?: 'invisible.gif';
         }
     }
     if ($extraAttributes) {
         $attributes .= ltrim(sprintf($extraAttributes, $objModel->getID()));
     }
     $serializedModelId = ModelId::fromModel($objModel)->getSerialized();
     // Cut needs some special information.
     if ($objCommand instanceof CutCommandInterface) {
         $parameters = array();
         $parameters['act'] = $commandName;
         // If we have a pid add it, used for mode 4 and all parent -> current views.
         if ($inputProvider->hasParameter('pid')) {
             $parameters['pid'] = $inputProvider->getParameter('pid');
         }
         // Source is the id of the element which should move.
         $parameters['source'] = $serializedModelId;
     } elseif ($objCommand instanceof CopyCommandInterface) {
         // The copy operation.
         $parameters = array();
         $parameters['act'] = $commandName;
         // If we have a pid add it, used for mode 4 and all parent -> current views.
         if ($inputProvider->hasParameter('pid')) {
             $parameters['pid'] = $inputProvider->getParameter('pid');
         }
         // Source is the id of the element which should move.
         $parameters['source'] = $serializedModelId;
     } else {
         // TODO: Shall we interface this option?
         $idParam = isset($extra['idparam']) ? $extra['idparam'] : null;
         if ($idParam) {
             $parameters[$idParam] = $serializedModelId;
         } else {
             $parameters['id'] = $serializedModelId;
         }
     }
     $strHref = '';
     foreach ($parameters as $key => $value) {
         $strHref .= sprintf('&%s=%s', $key, $value);
     }
     /** @var AddToUrlEvent $event */
     $event = $dispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, new AddToUrlEvent($strHref));
     $strHref = $event->getUrl();
     $buttonEvent = new GetOperationButtonEvent($this->getEnvironment());
     $buttonEvent->setCommand($objCommand)->setObjModel($objModel)->setAttributes($attributes)->setLabel($label)->setTitle($title)->setHref($strHref)->setChildRecordIds($arrChildRecordIds)->setCircularReference($blnCircularReference)->setPrevious($previous)->setNext($next)->setDisabled($objCommand->isDisabled());
     $dispatcher->dispatch(GetOperationButtonEvent::NAME, $buttonEvent);
     // If the event created a button, use it.
     if ($buttonEvent->getHtml() !== null) {
         return trim($buttonEvent->getHtml());
     }
     $icon = $extra['icon'];
     if ($buttonEvent->isDisabled()) {
         /** @var GenerateHtmlEvent $event */
         $event = $dispatcher->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent(substr_replace($icon, '_1', strrpos($icon, '.'), 0), $buttonEvent->getLabel()));
         return $event->getHtml();
     }
     /** @var GenerateHtmlEvent $event */
     $event = $dispatcher->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent($icon, $buttonEvent->getLabel()));
     return sprintf(' <a href="%s" title="%s" %s>%s</a>', $buttonEvent->getHref(), specialchars($buttonEvent->getTitle()), $buttonEvent->getAttributes(), $event->getHtml());
 }