Example #1
0
 /**
  * {@inheritdoc}
  */
 public function showAll(Action $action)
 {
     if ($this->environment->getDataDefinition()->getBasicDefinition()->isEditOnlyMode()) {
         return $this->edit($action);
     }
     return sprintf($this->notImplMsg, 'showAll - Mode ' . $this->environment->getDataDefinition()->getBasicDefinition()->getMode());
 }
 /**
  * Build a widget for a given property.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @param PropertyInterface    $property    The property.
  *
  * @param ModelInterface       $model       The current model.
  *
  * @return \Widget
  */
 public function buildWidget(EnvironmentInterface $environment, PropertyInterface $property, ModelInterface $model)
 {
     $dispatcher = $environment->getEventDispatcher();
     $propertyName = $property->getName();
     $propExtra = $property->getExtra();
     $defName = $environment->getDataDefinition()->getName();
     $strClass = $this->getWidgetClass($property);
     $event = new DecodePropertyValueForWidgetEvent($environment, $model);
     $event->setProperty($propertyName)->setValue($model->getProperty($propertyName));
     $dispatcher->dispatch($event::NAME, $event);
     $varValue = $event->getValue();
     $propExtra['required'] = $varValue == '' && !empty($propExtra['mandatory']);
     $propExtra['tableless'] = true;
     $arrConfig = array('inputType' => $property->getWidgetType(), 'label' => array($property->getLabel(), $property->getDescription()), 'options' => $this->getOptionsForWidget($environment, $property, $model), 'eval' => $propExtra);
     if (isset($propExtra['reference'])) {
         $arrConfig['reference'] = $propExtra['reference'];
     }
     $event = new GetAttributesFromDcaEvent($arrConfig, $property->getName(), $varValue, $propertyName, $defName, new DcCompat($environment, $model, $propertyName));
     $dispatcher->dispatch(ContaoEvents::WIDGET_GET_ATTRIBUTES_FROM_DCA, $event);
     $preparedConfig = $event->getResult();
     $widget = new $strClass($preparedConfig, new DcCompat($environment, $model, $propertyName));
     $widget->currentRecord = $model->getId();
     $event = new ManipulateWidgetEvent($environment, $model, $property, $widget);
     $dispatcher->dispatch(ManipulateWidgetEvent::NAME, $event);
     return $widget;
 }
 /**
  * Get the group header.
  *
  * @param EnvironmentInterface $environment    The environment.
  *
  * @param ModelInterface       $model          The model interface.
  *
  * @param string               $field          The grouping field name.
  *
  * @param int                  $groupingMode   The grouping mode.
  *
  * @param int                  $groupingLength The grouping length.
  *
  * @return string
  */
 public function formatGroupHeader($environment, $model, $field, $groupingMode, $groupingLength)
 {
     $property = $environment->getDataDefinition()->getPropertiesDefinition()->getProperty($field);
     // No property? Get out!
     if (!$property) {
         return '-';
     }
     $translator = $environment->getTranslator();
     $value = $model->getProperty($property->getName());
     $evaluation = $property->getExtra();
     if ($property->getWidgetType() == 'checkbox' && !$evaluation['multiple']) {
         return $this->formatCheckboxOptionLabel($value, $translator);
     } elseif ($groupingMode != GroupAndSortingInformationInterface::GROUP_NONE) {
         return $this->formatByGroupingMode($value, $groupingMode, $groupingLength, $environment, $property, $model);
     }
     $value = ViewHelpers::getReadableFieldValue($environment, $property, $model);
     if (isset($evaluation['reference'])) {
         $remoteNew = $evaluation['reference'][$value];
     } elseif (array_is_assoc($property->getOptions())) {
         $options = $property->getOptions();
         $remoteNew = $options[$value];
     } else {
         $remoteNew = $value;
     }
     if (is_array($remoteNew)) {
         $remoteNew = $remoteNew[0];
     }
     if (empty($remoteNew)) {
         $remoteNew = '-';
     }
     return $remoteNew;
 }
Example #4
0
 /**
  * Translate a string via the translator.
  *
  * @param string $path The path within the translation where the string can be found.
  *
  * @return string
  */
 protected function translate($path)
 {
     $value = $this->translator->translate($path, $this->environment->getDataDefinition()->getName());
     if ($path !== $value) {
         return $value;
     }
     return $this->translator->translate($path);
 }
 /**
  * Populate the file picker $GLOBALS['TL_DCA'] to make the contao/file.php happy.
  *
  * The backend file contao/file.php is using hard coded direct array access on the TL_DCA array therefore we need to
  * dump all of this there.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.Superglobals)
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  */
 protected function populateFilePickers(EnvironmentInterface $environment)
 {
     $definition = $environment->getDataDefinition();
     $name = $definition->getName();
     if (!isset($GLOBALS['TL_DCA'])) {
         $GLOBALS['TL_DCA'] = array();
     }
     if (!isset($GLOBALS['TL_DCA'][$name])) {
         $GLOBALS['TL_DCA'][$name] = array();
     }
     if (!isset($GLOBALS['TL_DCA'][$name]['fields'])) {
         $GLOBALS['TL_DCA'][$name]['fields'] = array();
     }
     $dca =& $GLOBALS['TL_DCA'][$name]['fields'];
     foreach ($environment->getDataDefinition()->getPropertiesDefinition()->getProperties() as $property) {
         if ($property->getWidgetType() == 'fileTree') {
             $dca[$property->getName()]['eval'] = $property->getExtra();
         }
     }
 }
 /**
  * Instantiates and adds the data providers implementing ContaoDataProviderInformation to the environment.
  *
  * @param EnvironmentInterface $environment The environment to populate.
  *
  * @return void
  *
  * @throws DcGeneralRuntimeException When a data provider has already been added to the environment.
  */
 public function populate(EnvironmentInterface $environment)
 {
     $definition = $environment->getDataDefinition();
     foreach ($definition->getDataProviderDefinition() as $information) {
         if ($information instanceof ContaoDataProviderInformation) {
             if ($environment->hasDataProvider($information->getName())) {
                 throw new DcGeneralRuntimeException(sprintf('Data provider %s already added to environment.', $information->getName()));
             }
             $providerClass = new \ReflectionClass($information->getClassName());
             /** @var DataProviderInterface $dataProvider */
             $dataProvider = $providerClass->newInstance();
             if ($initializationData = $information->getInitializationData()) {
                 $dataProvider->setBaseConfig($initializationData);
             }
             $environment->addDataProvider($information->getName(), $dataProvider);
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function populate(EnvironmentInterface $environment)
 {
     if (!$environment->getSessionStorage()) {
         $environment->setSessionStorage(new SessionStorage('DC_GENERAL_' . strtoupper($environment->getDataDefinition()->getName())));
     }
     if (!$environment->getInputProvider()) {
         $environment->setInputProvider(new InputProvider());
     }
     if (!$environment->getClipboard()) {
         $environment->setClipboard(new Clipboard());
     }
     if (!$environment->getBaseConfigRegistry()) {
         $baseConfigRegistry = new BaseConfigRegistry();
         $baseConfigRegistry->setEnvironment($environment);
         $environment->setBaseConfigRegistry($baseConfigRegistry);
     }
     $this->populateController($environment);
 }
 /**
  * Load property values.
  *
  * @return void
  */
 private function loadPropertyValues()
 {
     $flatten = $this->flatten($this->getData());
     $this->propertyValues = new PropertyValueBag($flatten);
     $this->model->setPropertiesAsArray($flatten);
     foreach ($this->getForms() as $form) {
         foreach ($form->getFields() as $field) {
             $defintion = $this->environment->getDataDefinition()->getPropertiesDefinition();
             $defintion->addProperty($field);
             if (!$this->isSubmit()) {
                 continue;
             }
             $value = $this->environment->getInputProvider()->getValue($field->getName(), true);
             // Set value to property values and to model. If validation failed, the widget manager loads data
             // from the model.
             $this->model->setProperty($field->getName(), $value);
             $this->propertyValues->setPropertyValue($field->getName(), $value);
         }
     }
 }
 /**
  * Create a controller instance in the environment if none has been defined yet.
  *
  * @param EnvironmentInterface $environment The environment to populate.
  *
  * @return void
  *
  * @internal
  */
 public function populateController(EnvironmentInterface $environment)
 {
     // Already populated, get out then.
     if ($environment->getController()) {
         return;
     }
     $definition = $environment->getDataDefinition();
     // If we encounter an extended definition, that one may override.
     if (!$definition->hasDefinition(ExtendedDca::NAME)) {
         return;
     }
     /** @var ExtendedDca $extendedDefinition */
     $extendedDefinition = $definition->getDefinition(ExtendedDca::NAME);
     $class = $extendedDefinition->getControllerClass();
     if (!$class) {
         return;
     }
     $controllerClass = new \ReflectionClass($class);
     /** @var ControllerInterface $controller */
     $controller = $controllerClass->newInstance();
     $controller->setEnvironment($environment);
     $environment->setController($controller);
 }
Example #10
0
 /**
  * Check if the language has been switched.
  *
  * If so, the value in the session will be updated and the page reloaded.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @return void
  */
 private function checkLanguageSubmit($environment)
 {
     $sessionStorage = $environment->getSessionStorage();
     $inputProvider = $environment->getInputProvider();
     if ($inputProvider->getValue('FORM_SUBMIT') !== 'language_switch') {
         return;
     }
     $modelId = $inputProvider->getParameter('id') && $inputProvider->getParameter('id') ? IdSerializer::fromSerialized($inputProvider->getParameter('id'))->getId() : null;
     $languages = $environment->getController()->getSupportedLanguages($modelId);
     $providerName = $environment->getDataDefinition()->getName();
     // Get/Check the new language.
     if ($inputProvider->hasValue('language') && array_key_exists($inputProvider->getValue('language'), $languages)) {
         $session['ml_support'][$providerName][$modelId] = $inputProvider->getValue('language');
         $sessionStorage->set('dc_general', $session);
     }
     $environment->getEventDispatcher()->dispatch(ContaoEvents::CONTROLLER_RELOAD, new ReloadEvent());
 }
Example #11
0
 /**
  * Decode a value from native data of the data provider to the widget via event.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @param ModelInterface       $model       The model.
  *
  * @param string               $property    The property.
  *
  * @param mixed                $value       The value of the property.
  *
  * @return mixed
  */
 public function decodeValue($environment, $model, $property, $value)
 {
     $event = new DecodePropertyValueForWidgetEvent($environment, $model);
     $event->setProperty($property)->setValue($value);
     $environment->getEventPropagator()->propagate($event::NAME, $event, array($environment->getDataDefinition()->getName(), $property));
     return $event->getValue();
 }
Example #12
0
 /**
  * Convert a model to it's labels and human readable values.
  *
  * @param ModelInterface       $model       The model to display.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @return array
  *
  * @throws DcGeneralRuntimeException When a property could not be retrieved.
  */
 protected function convertModel($model, $environment)
 {
     $definition = $environment->getDataDefinition();
     $properties = $definition->getPropertiesDefinition();
     $palette = $definition->getPalettesDefinition()->findPalette($model);
     $values = array();
     $labels = array();
     // Show only allowed fields.
     foreach ($palette->getVisibleProperties($model) as $paletteProperty) {
         $property = $properties->getProperty($paletteProperty->getName());
         if (!$property) {
             throw new DcGeneralRuntimeException('Unable to retrieve property ' . $paletteProperty->getName());
         }
         // Make it human readable.
         $values[$paletteProperty->getName()] = ViewHelpers::getReadableFieldValue($this->getEnvironment(), $property, $model);
         $labels[$paletteProperty->getName()] = $this->getPropertyLabel($property);
     }
     return array('labels' => $labels, 'values' => $values);
 }
Example #13
0
 /**
  * Create a panel instance in the view if none has been defined yet.
  *
  * @param EnvironmentInterface $environment The environment to populate.
  *
  * @return void
  *
  * @internal
  */
 protected function populatePanel(EnvironmentInterface $environment)
 {
     // Already populated or not in Backend? Get out then.
     if (!($environment->getView() instanceof BaseView && TL_MODE == 'BE')) {
         return;
     }
     $definition = $environment->getDataDefinition();
     if (!$definition->hasDefinition(Contao2BackendViewDefinitionInterface::NAME)) {
         return;
     }
     /** @var BackendViewInterface $view */
     $view = $environment->getView();
     // Already populated.
     if ($view->getPanel()) {
         return;
     }
     $panel = new DefaultPanelContainer();
     $panel->setEnvironment($environment);
     $view->setPanel($panel);
     /** @var Contao2BackendViewDefinitionInterface $backendViewDefinition  */
     $backendViewDefinition = $definition->getDefinition(Contao2BackendViewDefinitionInterface::NAME);
     /** @var PanelLayoutInterface $panelLayout */
     $panelLayout = $backendViewDefinition->getPanelLayout();
     foreach ($panelLayout->getRows() as $panelKey => $row) {
         // We need a new panel.
         $panelRow = new DefaultPanel();
         $panel->addPanel($panelKey, $panelRow);
         foreach ($row as $element) {
             if ($element instanceof FilterElementInformationInterface) {
                 $panelElement = new DefaultFilterElement();
                 $panelElement->setPropertyName($element->getPropertyName());
                 $panelRow->addElement($element->getName(), $panelElement);
             } elseif ($element instanceof LimitElementInformationInterface) {
                 $panelElement = new DefaultLimitElement();
                 $panelRow->addElement($element->getName(), $panelElement);
             } elseif ($element instanceof SearchElementInformationInterface) {
                 $panelElement = new DefaultSearchElement();
                 foreach ($element->getPropertyNames() as $propName) {
                     $panelElement->addProperty($propName);
                 }
                 $panelRow->addElement($element->getName(), $panelElement);
             } elseif ($element instanceof SortElementInformationInterface) {
                 $panelElement = new DefaultSortElement();
                 foreach ($element->getPropertyNames() as $propName) {
                     $panelElement->addProperty($propName, $element->getPropertyFlag($propName));
                 }
                 $panelRow->addElement($element->getName(), $panelElement);
             } elseif ($element instanceof SubmitElementInformationInterface) {
                 $panelElement = new DefaultSubmitElement();
                 $panelRow->addElement($element->getName(), $panelElement);
             }
         }
     }
 }
Example #14
0
 /**
  * Show all entries from one table.
  *
  * @return string
  */
 public function showAll()
 {
     if ($this->environment->getDataDefinition()->getBasicDefinition()->isEditOnlyMode()) {
         return $this->edit();
     }
     return sprintf($this->notImplMsg, 'showAll - Mode ' . $this->getViewSection()->getListingConfig()->getGroupingMode());
 }
 /**
  * Create the edit mask.
  *
  * @param EnvironmentInterface $environment   The view in use.
  *
  * @param ModelInterface       $model         The model with the current data.
  *
  * @param ModelInterface       $originalModel The data from the original data.
  *
  * @param callable             $preFunction   The function to call before saving an item.
  *
  * @param callable             $postFunction  The function to call after saving an item.
  */
 public function __construct($environment, $model, $originalModel, $preFunction, $postFunction)
 {
     $providerName = $model->getProviderName();
     $this->environment = $environment;
     $this->translator = $environment->getTranslator();
     $this->dispatcher = $environment->getEventDispatcher();
     $this->definition = $environment->getDataDefinition();
     $this->modelProvider = $this->definition->getDataProviderDefinition()->getInformation($providerName);
     $this->model = $model;
     $this->originalModel = $originalModel;
     $this->preFunction = $preFunction;
     $this->postFunction = $postFunction;
 }
Example #16
0
 /**
  * Check the PA and PI with a model and a normal flat build.
  *
  * @param ModelInterface $containedModel The model to check.
  *
  * @return void
  */
 protected function checkModelWithoutVariants($containedModel)
 {
     $parentDefinition = $this->environment->getDataDefinition()->getBasicDefinition()->getParentDataProvider();
     $this->disablePA = $this->currentModel->getId() == $containedModel->getId() || $parentDefinition && $this->currentModel->getProperty('pid') == $containedModel->getProperty('pid');
     $this->disablePI = $this->circularReference || $this->currentModel->getId() == $containedModel->getId() || $parentDefinition && $this->currentModel->getProperty('pid') == $containedModel->getId();
 }
Example #17
0
 /**
  * {@inheritDoc}
  */
 public static function getManualSortingProperty(EnvironmentInterface $environment)
 {
     /** @var BackendViewInterface $view */
     $view = $environment->getView();
     $definition = null;
     foreach ($view->getPanel() as $panel) {
         /** @var PanelInterface $panel */
         $sort = $panel->getElement('sort');
         if ($sort) {
             /** @var SortElementInterface $sort */
             $definition = $sort->getSelectedDefinition();
         }
     }
     if ($definition === null) {
         /** @var Contao2BackendViewDefinitionInterface $viewDefinition */
         $dataDefinition = $environment->getDataDefinition();
         $viewDefinition = $dataDefinition->getDefinition(Contao2BackendViewDefinitionInterface::NAME);
         $groupAndSortingDefinition = $viewDefinition->getListingConfig()->getGroupAndSortingDefinition();
         if ($groupAndSortingDefinition->hasDefault()) {
             $definition = $groupAndSortingDefinition->getDefault();
         }
     }
     if ($definition) {
         foreach ($definition as $information) {
             if ($information->isManualSorting()) {
                 return $information->getProperty();
             }
         }
     }
     return null;
 }
Example #18
0
 /**
  * Copy each children.
  *
  * @param ModelIdInterface     $modelId     The model id.
  * @param ModelInterface       $copiedModel The copied model.
  * @param EnvironmentInterface $environment The environment.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.LongVariableName)
  */
 protected function copyEachChilds(ModelIdInterface $modelId, ModelInterface $copiedModel, EnvironmentInterface $environment)
 {
     $childDataProviderName = $environment->getInputProvider()->getParameter('ctable');
     $dataProvider = $environment->getDataProvider();
     $childDataProvider = $environment->getDataProvider($childDataProviderName);
     $modelRelationship = $environment->getParentDataDefinition()->getModelRelationshipDefinition();
     $childCondition = $modelRelationship->getChildCondition($copiedModel->getProviderName(), $childDataProviderName);
     if (!$childCondition) {
         return;
     }
     $parentModel = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
     $parentFilters = $childCondition->getFilter($parentModel);
     $copiedFilters = $childCondition->getFilter($copiedModel);
     $filter = array();
     // Todo can many filter has operation equal
     foreach ($parentFilters as $index => $parentFilter) {
         if ($parentFilter['operation'] !== '=') {
             continue;
         }
         $filter = array('parent' => $parentFilter, 'copied' => $copiedFilters[$index]);
     }
     $childModels = $childDataProvider->fetchAll($childDataProvider->getEmptyConfig()->setFilter(array($filter['parent'])));
     if ($childModels->count() < 1) {
         return;
     }
     foreach ($childModels->getModelIds() as $index => $modelId) {
         $childModel = $childModels->get($index);
         $copyModelId = ModelId::fromModel($childModel);
         $copiedChildModel = null;
         if ($environment->getDataDefinition()->getName() !== $copyModelId->getDataProviderName()) {
             $copiedChildModel = $this->copyParent($copyModelId, $environment);
         }
         if ($environment->getDataDefinition()->getName() === $copyModelId->getDataProviderName() && !$copiedChildModel) {
             $copiedChildModel = $this->copyHandler($copyModelId, $environment);
         }
         $copiedChildModel->setProperty($filter['copied']['property'], $filter['copied']['value']);
         $childDataProvider->save($copiedChildModel);
     }
 }