Ejemplo n.º 1
0
 /**
  * Get the MetaModel instance referenced in the pid property of the Model.
  *
  * @param ModelInterface $model The model.
  *
  * @return IMetaModel
  *
  * @throws \InvalidArgumentException When the MetaModel could not be retrieved.
  */
 private function getMetaModelByModelPid($model)
 {
     $metaModel = $this->getMetaModelById($model->getProperty('pid'));
     if ($metaModel === null) {
         throw new \InvalidArgumentException('Could not retrieve MetaModel ' . $model->getProperty('pid'));
     }
     return $metaModel;
 }
Ejemplo n.º 2
0
 /**
  * Retrieve the MetaModel instance from a render settings model.
  *
  * @param ModelInterface $model The model to fetch the MetaModel instance for.
  *
  * @return IMetaModel
  */
 protected function getMetaModel($model)
 {
     if (!isset($this->metaModelCache[$model->getProperty('pid')])) {
         $dbResult = $this->getDatabase()->prepare('SELECT * FROM tl_metamodel_rendersettings WHERE id=?')->execute($model->getProperty('pid'))->row();
         $this->metaModelCache[$model->getProperty('pid')] = $this->getMetaModelById($dbResult['pid']);
     }
     return $this->metaModelCache[$model->getProperty('pid')];
 }
Ejemplo n.º 3
0
 /**
  * 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;
 }
 /**
  * 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;
 }
Ejemplo n.º 5
0
 /**
  * Update the sorting property values of all models.
  *
  * @return void
  */
 private function updateSorting()
 {
     $ids = $this->getModelIds();
     // If no "next" sibling, simply increment the sorting as we are at the end of the list.
     if (!$this->marker) {
         foreach ($this->results as $model) {
             $this->position += 128;
             /** @var ModelInterface $model */
             $model->setProperty($this->getSortingProperty(), $this->position);
         }
         return;
     }
     $delta = $this->determineDelta();
     // Loop over all models and increment sorting value.
     foreach ($this->results as $model) {
         $this->position += $delta;
         /** @var ModelInterface $model */
         $model->setProperty($this->getSortingProperty(), $this->position);
     }
     // When the sorting exceeds the sorting of the "next" sibling, we need to push the remaining siblings to the
     // end of the list.
     if ($this->marker->getProperty($this->getSortingProperty()) <= $this->position) {
         do {
             // Skip models about to be pasted.
             if (in_array($this->marker->getId(), $ids)) {
                 $this->marker = $this->siblingsCopy->shift();
                 continue;
             }
             $this->position += $delta;
             $this->marker->setProperty($this->getSortingProperty(), $this->position);
             $this->results->push($this->marker);
             $this->marker = $this->siblingsCopy->shift();
         } while ($this->marker);
     }
 }
Ejemplo n.º 6
0
 /**
  * Extract a condition value depending if it is a remote value or property.
  *
  * @param array          $condition The condition array.
  *
  * @param ModelInterface $parent    The parent model.
  *
  * @return mixed
  */
 protected static function getConditionValue($condition, $parent)
 {
     if (isset($condition['remote_value'])) {
         return $condition['remote_value'];
     }
     return $parent->getProperty($condition['property']);
 }
Ejemplo n.º 7
0
 /**
  * Retrieve the parameters for the label.
  *
  * @param EnvironmentInterface $environment The translator in use.
  *
  * @param ModelInterface       $model       The model.
  *
  * @return array
  */
 protected function getLabelParameters(EnvironmentInterface $environment, ModelInterface $model)
 {
     if ($model->getProperty('type') == 'simplelookup') {
         return $this->getLabelParametersWithAttributeAndUrlParam($environment, $model);
     }
     return $this->getLabelParametersNormal($environment, $model);
 }
Ejemplo n.º 8
0
 /**
  * Check if the contect of the event is a allowed one.
  *
  * @param ContainerInterface $dataDefinition The data definition from the environment.
  *
  * @param string             $propertyName   The current property name.
  *
  * @param ModelInterface     $model          The current model.
  *
  * @return bool True => It is a allowed one | False => nope
  */
 protected function isAllowedContext($dataDefinition, $propertyName, $model)
 {
     // Check the name of the data def.
     if ($dataDefinition->getName() !== 'tl_metamodel_filtersetting') {
         return false;
     }
     // Check the name of the property.
     if ($propertyName !== 'attr_id2') {
         return false;
     }
     // Check the type.
     if ($model->getProperty('type') !== 'range' && $model->getProperty('type') !== 'rangedate') {
         return false;
     }
     // At the end, return true.
     return true;
 }
 /**
  * Check if entity is satisfied by
  * @param Entity $entity   The entity.
  * @param array  $condtion The condition.
  *
  * @return bool
  */
 private function satisfiesCondition(Entity $entity, $condtion)
 {
     if ($condtion['operation'] === static::OPERATION_EQUALS) {
         $operation = Comparison::EQUALS;
     } else {
         $operation = $condtion['operation'];
     }
     return Comparison::compare($entity->getProperty($condtion['property']), $condtion['value'], $operation);
 }
Ejemplo n.º 10
0
 /**
  * Calculate the resulting list.
  *
  * @return void
  */
 protected function calculate()
 {
     if (isset($this->results) || $this->models->length() == 0) {
         return;
     }
     $ids = $this->getModelIds();
     $this->results = clone $this->models;
     $this->siblingsCopy = clone $this->siblings;
     $this->scanToDesiredPosition();
     // If no "next" sibling, simply increment the sorting as we are at the end of the list.
     if (!$this->marker) {
         foreach ($this->results as $model) {
             $this->position += 128;
             /** @var ModelInterface $model */
             $model->setProperty($this->getSortingProperty(), $this->position);
         }
         return;
     }
     // Determine delta value: ((next sorting - current sorting) / amount of insert models).
     $delta = ($this->marker->getProperty($this->getSortingProperty()) - $this->position) / $this->results->length();
     // If delta too narrow, we need to make room.
     if ($delta < 2) {
         $delta = 128;
     }
     // Loop over all models and increment sorting value.
     foreach ($this->results as $model) {
         $this->position += $delta;
         /** @var ModelInterface $model */
         $model->setProperty($this->getSortingProperty(), $this->position);
     }
     // When the sorting exceeds the sorting of the "next" sibling, we need to push the remaining siblings to the
     // end of the list.
     if ($this->marker->getProperty($this->getSortingProperty()) <= $this->position) {
         do {
             // Skip models about to be pasted.
             if (in_array($this->marker->getId(), $ids)) {
                 $this->marker = $this->siblingsCopy->shift();
                 continue;
             }
             $this->position += $delta;
             $this->marker->setProperty($this->getSortingProperty(), $this->position);
             $this->results->push($this->marker);
             $this->marker = $this->siblingsCopy->shift();
         } while ($this->marker);
     }
 }
Ejemplo n.º 11
0
 /**
  * Check if all values are unique, but only for the fields which have the option enabled.
  *
  * @return bool True => everything is okay | False => One value is not unique.
  */
 protected function allValuesUnique()
 {
     // Init some vars.
     $environment = $this->getEnvironment();
     $translator = $environment->getTranslator();
     $dataProvider = $environment->getDataProvider($this->model->getProviderName());
     $propertyNames = $this->getDataDefinition()->getPropertiesDefinition()->getPropertyNames();
     // Run each and check the unique flag.
     foreach ($propertyNames as $propertyName) {
         $definition = $this->getDataDefinition()->getPropertiesDefinition()->getProperty($propertyName);
         $extra = $definition->getExtra();
         $value = $this->model->getProperty($propertyName);
         // Check the flag and the value.
         if (isset($extra['unique']) && $extra['unique'] && $value != '') {
             // Check the database. If return true the value is already in the database.
             if (!$dataProvider->isUniqueValue($propertyName, $value, $this->model->getId())) {
                 $this->errors[] = $translator->translate('not_unique', 'MSC', array($propertyName));
                 return false;
             }
         }
     }
     return true;
 }
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function getMatchCount(ModelInterface $model = null, PropertyValueBag $input = null)
 {
     if ($input && $input->hasPropertyValue('attr_id')) {
         $value = $input->getPropertyValue('attr_id');
     } elseif ($model) {
         $value = $model->getProperty('attr_id');
     } else {
         return false;
     }
     return $this->getTypeOfAttribute($value) == $this->getAttributeType() ? $this->getWeight() : false;
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function getMatchCount(ModelInterface $model = null, PropertyValueBag $input = null)
 {
     if (!$this->propertyName) {
         return false;
     }
     if ($input && $input->hasPropertyValue($this->propertyName)) {
         $value = $input->getPropertyValue($this->propertyName);
     } elseif ($model) {
         $value = $model->getProperty($this->propertyName);
     } else {
         return false;
     }
     return $this->strict ? $value === false : !$value ? $this->getWeight() : false;
 }
Ejemplo n.º 14
0
 /**
  * Retrieve the label pattern.
  *
  * @param EnvironmentInterface $environment The translator in use.
  *
  * @param ModelInterface       $model       The filter setting to render.
  *
  * @return string
  */
 protected function getLabelPattern(EnvironmentInterface $environment, ModelInterface $model)
 {
     $translator = $environment->getTranslator();
     $type = $model->getProperty('type');
     $combined = 'typedesc.' . $type;
     if (($resultPattern = $translator->translate($combined, 'tl_metamodel_filtersetting')) == $combined) {
         $resultPattern = $translator->translate('typedesc._default_', 'tl_metamodel_filtersetting');
     }
     return $resultPattern;
 }
Ejemplo n.º 15
0
 /**
  * Get for a field the readable value.
  *
  * @param EnvironmentInterface $environment The environment.
  *
  * @param PropertyInterface    $property    The property to be rendered.
  *
  * @param ModelInterface       $model       The model from which the property value shall be retrieved from.
  *
  * @return mixed
  */
 public static function getReadableFieldValue(EnvironmentInterface $environment, PropertyInterface $property, ModelInterface $model)
 {
     $event = new RenderReadablePropertyValueEvent($environment, $model, $property, $model->getProperty($property->getName()));
     $environment->getEventDispatcher()->dispatch($event::NAME, $event);
     if ($event->getRendered() !== null) {
         return $event->getRendered();
     }
     return $event->getValue();
 }
Ejemplo n.º 16
0
 /**
  * Retrieve the MetaModel attached to the model filter setting.
  *
  * @param ModelInterface $model The model for which to retrieve the MetaModel.
  *
  * @return IMetaModel
  */
 public function getMetaModel(ModelInterface $model)
 {
     $filterSetting = $this->getServiceContainer()->getFilterFactory()->createCollection($model->getProperty('fid'));
     return $filterSetting->getMetaModel();
 }
Ejemplo n.º 17
0
 /**
  * Retrieve the MetaModel the given model is attached to.
  *
  * @param ModelInterface $model The input screen model for which to retrieve the MetaModel.
  *
  * @return IMetaModel
  *
  * @throws DcGeneralInvalidArgumentException When an invalid model has been passed or the model does not have an id.
  */
 protected function getMetaModelFromModel(ModelInterface $model)
 {
     if (!($model->getProviderName() == 'tl_metamodel_dcasetting' && $model->getProperty('pid'))) {
         throw new DcGeneralInvalidArgumentException(sprintf('Model must originate from tl_metamodel_dcasetting and be saved, this one originates from %s and ' . 'has pid %s', $model->getProviderName(), $model->getProperty('pid')));
     }
     $metaModelId = $this->getDatabase()->prepare('SELECT pid FROM tl_metamodel_dca WHERE id=?')->execute($model->getProperty('pid'));
     return $this->getMetaModelById($metaModelId->pid);
 }
Ejemplo n.º 18
0
 /**
  * Build a widget for a given property.
  *
  * @param PropertyInterface $property The property.
  *
  * @param ModelInterface    $model    The current model.
  *
  * @return \Widget
  *
  * @throws DcGeneralRuntimeException When not running in TL_MODE BE.
  *
  * @SuppressWarnings(PHPMD.Superglobals)
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  */
 public function buildWidget(PropertyInterface $property, ModelInterface $model)
 {
     if (TL_MODE !== 'BE') {
         throw new DcGeneralRuntimeException(sprintf('WidgetBuilder only supports TL_MODE "BE". Running in TL_MODE "%s".', TL_MODE));
     }
     $environment = $this->getEnvironment();
     $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();
     if (isset($propExtra['rgxp']) && in_array($propExtra['rgxp'], array('date', 'time', 'datim')) && empty($propExtra['mandatory']) && is_numeric($varValue) && $varValue == 0) {
         $varValue = '';
     }
     $propExtra['required'] = $varValue == '' && !empty($propExtra['mandatory']);
     $arrConfig = array('inputType' => $property->getWidgetType(), 'label' => array($property->getLabel(), $property->getDescription()), 'options' => $this->getOptionsForWidget($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);
     $arrPrepared = $event->getResult();
     if ($arrConfig['inputType'] == 'checkbox' && isset($GLOBALS['TL_DCA'][$defName]['subpalettes']) && is_array($GLOBALS['TL_DCA'][$defName]['subpalettes']) && in_array($propertyName, array_keys($GLOBALS['TL_DCA'][$defName]['subpalettes'])) && $arrConfig['eval']['submitOnChange']) {
         // We have to override the onclick, do not append to it as Contao adds it's own code here in
         // \Widget::getAttributesFromDca() which kills our sub palette handling!
         $arrPrepared['onclick'] = "Backend.autoSubmit('" . $defName . "');";
     }
     $objWidget = new $strClass($arrPrepared, new DcCompat($environment, $model, $propertyName));
     // OH: what is this? source: DataContainer 232.
     $objWidget->currentRecord = $model->getId();
     $objWidget->xlabel .= $this->getXLabel($property);
     $event = new ManipulateWidgetEvent($environment, $model, $property, $objWidget);
     $dispatcher->dispatch(ManipulateWidgetEvent::NAME, $event);
     return $objWidget;
 }
Ejemplo n.º 19
0
 /**
  * Check if two models have the same values in all properties.
  *
  * @param ModelInterface $objModel1 The first model to compare.
  *
  * @param ModelInterface $objModel2 The second model to compare.
  *
  * @return boolean True - If both models are same, false if not.
  */
 public function sameModels($objModel1, $objModel2)
 {
     foreach ($objModel1 as $key => $value) {
         if ($key == 'id') {
             continue;
         }
         if (is_array($value)) {
             if (!is_array($objModel2->getProperty($key))) {
                 return false;
             }
             if (serialize($value) != serialize($objModel2->getProperty($key))) {
                 return false;
             }
         } elseif ($value != $objModel2->getProperty($key)) {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 20
0
 /**
  * {@inheritdoc}
  */
 public function match(ModelInterface $model = null, PropertyValueBag $input = null, PropertyInterface $property = null, LegendInterface $legend = null)
 {
     if ($input && $input->hasPropertyValue($this->propertyName)) {
         $value = $input->getPropertyValue($this->propertyName);
     } elseif ($model) {
         $value = $model->getProperty($this->propertyName);
     } else {
         return false;
     }
     return $this->strict ? $value === $this->propertyValue : $value == $this->propertyValue;
 }
Ejemplo n.º 21
0
 /**
  * {@inheritdoc}
  */
 public function match(ModelInterface $model = null, PropertyValueBag $input = null, PropertyInterface $property = null, LegendInterface $legend = null)
 {
     if ($input && $input->hasPropertyValue('attr_id')) {
         $value = $input->getPropertyValue('attr_id');
     } elseif ($model) {
         $value = $model->getProperty('attr_id');
     } else {
         return false;
     }
     return $this->getTypeOfAttribute($value) == $this->getAttributeType();
 }
Ejemplo n.º 22
0
 /**
  * Apply the filter values for a given model to the given rule.
  *
  * @param array          $filter The filter rule to which the values shall get applied.
  *
  * @param ModelInterface $model  The model to fetch the values from.
  *
  * @return array
  */
 public function parseFilter($filter, $model)
 {
     $arrApplied = array('operation' => $filter['operation']);
     if (isset($filter['local'])) {
         $arrApplied['property'] = $filter['local'];
     }
     if (isset($filter['remote'])) {
         $arrApplied['value'] = $model->getProperty($filter['remote']);
     }
     if (isset($filter['remote_value'])) {
         $arrApplied['value'] = $filter['remote_value'];
     }
     if (isset($filter['value'])) {
         $arrApplied['value'] = $filter['value'];
     }
     if (isset($filter['children'])) {
         foreach ($filter['children'] as $child) {
             $arrApplied['children'][] = $this->parseFilter($child, $model);
         }
     }
     return $arrApplied;
 }
Ejemplo n.º 23
0
 /**
  * Render the header of the parent view with information from the parent table.
  *
  * @param ModelInterface $parentModel The parent model.
  *
  * @return array
  */
 protected function renderFormattedHeaderFields($parentModel)
 {
     $environment = $this->getEnvironment();
     $propagator = $this->getEnvironment()->getEventPropagator();
     $definition = $environment->getDataDefinition();
     $viewDefinition = $this->getViewSection();
     $listingDefinition = $viewDefinition->getListingConfig();
     $headerFields = $listingDefinition->getHeaderPropertyNames();
     $parentDefinition = $environment->getParentDataDefinition();
     $parentName = $definition->getBasicDefinition()->getParentDataProvider();
     $add = array();
     foreach ($headerFields as $v) {
         $value = deserialize($parentModel->getProperty($v));
         if ($v == 'tstamp') {
             $value = date($GLOBALS['TL_CONFIG']['datimFormat'], $value);
         }
         $property = $parentDefinition->getPropertiesDefinition()->getProperty($v);
         // FIXME: foreignKey is not implemented yet.
         if ($property && $v != 'tstamp') {
             $evaluation = $property->getExtra();
             $reference = isset($evaluation['reference']) ? $evaluation['reference'] : null;
             $options = $property->getOptions();
             if (is_array($value)) {
                 $value = implode(', ', $value);
             } elseif ($property->getWidgetType() == 'checkbox' && !$evaluation['multiple']) {
                 $value = strlen($value) ? $this->translate('yes', 'MSC') : $this->translate('no', 'MSC');
             } elseif ($value && in_array($evaluation['rgxp'], array('date', 'time', 'datim'))) {
                 $event = new ParseDateEvent($value, $GLOBALS['TL_CONFIG'][$evaluation['rgxp'] . 'Format']);
                 $propagator->propagate(ContaoEvents::DATE_PARSE, $event);
                 $value = $event->getResult();
             } elseif (is_array($reference[$value])) {
                 $value = $reference[$value][0];
             } elseif (isset($reference[$value])) {
                 $value = $reference[$value];
             } elseif ($evaluation['isAssociative'] || array_is_assoc($options)) {
                 $value = $options[$value];
             }
         }
         // Add the sorting field.
         if ($value != '') {
             $lang = $this->translate(sprintf('%s.0', $v), $parentName);
             $key = $lang ? $lang : $v;
             $add[$key] = $value;
         }
     }
     $event = new GetParentHeaderEvent($environment);
     $event->setAdditional($add);
     $propagator->propagate($event::NAME, $event, $this->getEnvironment()->getDataDefinition()->getName());
     if (!$event->getAdditional() !== null) {
         $add = $event->getAdditional();
     }
     // Set header data.
     $arrHeader = array();
     foreach ($add as $k => $v) {
         if (is_array($v)) {
             $v = $v[0];
         }
         $arrHeader[$k] = $v;
     }
     return $arrHeader;
 }
Ejemplo n.º 24
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)
 {
     $this->disablePA = $this->currentModel->getId() == $containedModel->getId() || $this->currentModel->getProperty('pid') == $containedModel->getProperty('pid');
     $this->disablePI = $this->circularReference || $this->currentModel->getId() == $containedModel->getId() || $this->currentModel->getProperty('pid') == $containedModel->getId();
 }
Ejemplo n.º 25
0
 /**
  * Retrieve the instance of a widget for the given property.
  *
  * @param string           $property    Name of the property for which the widget shall be retrieved.
  *
  * @param PropertyValueBag $inputValues The input values to use (optional).
  *
  * @throws DcGeneralInvalidArgumentException When an unknown property has been passed.
  *
  * @return \Widget
  */
 public function getWidget($property, PropertyValueBag $inputValues = null)
 {
     $environment = $this->getEnvironment();
     $defName = $environment->getDataDefinition()->getName();
     $propertyDefinitions = $environment->getDataDefinition()->getPropertiesDefinition();
     if (!$propertyDefinitions->hasProperty($property)) {
         throw new DcGeneralInvalidArgumentException('Property ' . $property . ' is not defined in propertyDefinitions.');
     }
     $event = new BuildWidgetEvent($environment, $this->model, $propertyDefinitions->getProperty($property));
     $environment->getEventPropagator()->propagate($event::NAME, $event, array($defName, $property));
     if ($event->getWidget()) {
         return $event->getWidget();
     }
     $propInfo = $propertyDefinitions->getProperty($property);
     $propExtra = $propInfo->getExtra();
     $varValue = $this->decodeValue($property, $this->model->getProperty($property));
     $xLabel = $this->getXLabel($propInfo);
     $strClass = $GLOBALS['BE_FFL'][$propInfo->getWidgetType()];
     if (!class_exists($strClass)) {
         return null;
     }
     // FIXME TEMPORARY WORKAROUND! To be fixed in the core: Controller::prepareForWidget(..).
     if (in_array($propExtra['rgxp'], array('date', 'time', 'datim')) && !$propExtra['mandatory'] && is_numeric($varValue) && $varValue == 0) {
         $varValue = '';
     }
     // OH: why not $required = $mandatory always? source: DataContainer 226.
     // OH: the whole prepareForWidget(..) thing is an only mess
     // Widgets should parse the configuration by themselves, depending on what they need.
     $propExtra['required'] = $varValue == '' && $propExtra['mandatory'];
     if ($inputValues) {
         $model = clone $this->model;
         $model->setId($this->model->getId());
         $this->environment->getController()->updateModelFromPropertyBag($model, $inputValues);
     } else {
         $model = $this->model;
     }
     $options = $propInfo->getOptions();
     $event = new GetPropertyOptionsEvent($environment, $model);
     $event->setPropertyName($property);
     $event->setOptions($options);
     $environment->getEventPropagator()->propagate($event::NAME, $event, $environment->getDataDefinition()->getName(), $property);
     if ($event->getOptions() !== $options) {
         $options = $event->getOptions();
     }
     $arrConfig = array('inputType' => $propInfo->getWidgetType(), 'label' => array($propInfo->getLabel(), $propInfo->getDescription()), 'options' => $options, 'eval' => $propExtra);
     if (isset($propExtra['reference'])) {
         $arrConfig['reference'] = $propExtra['reference'];
     }
     $event = new GetAttributesFromDcaEvent($arrConfig, $propInfo->getName(), $varValue, $property, $defName, new DcCompat($environment, $this->model, $property));
     $environment->getEventPropagator()->propagate(ContaoEvents::WIDGET_GET_ATTRIBUTES_FROM_DCA, $event, $environment->getDataDefinition()->getName(), $property);
     $arrPrepared = $event->getResult();
     // Bugfix CS: ajax subpalettes are really broken.
     // Therefore we reset to the default checkbox behaviour here and submit the entire form.
     // This way, the javascript needed by the widget (wizards) will be correctly evaluated.
     if ($arrConfig['inputType'] == 'checkbox' && is_array($GLOBALS['TL_DCA'][$defName]['subpalettes']) && in_array($property, array_keys($GLOBALS['TL_DCA'][$defName]['subpalettes'])) && $arrConfig['eval']['submitOnChange']) {
         $arrPrepared['onclick'] = $arrConfig['eval']['submitOnChange'] ? "Backend.autoSubmit('" . $defName . "')" : '';
     }
     $objWidget = new $strClass($arrPrepared);
     // OH: what is this? source: DataContainer 232.
     $objWidget->currentRecord = $this->model->getId();
     $objWidget->wizard .= $xLabel;
     $event = new ManipulateWidgetEvent($environment, $this->model, $propInfo, $objWidget);
     $environment->getEventPropagator()->propagate($event::NAME, $event, array($defName, $property));
     return $objWidget;
 }
Ejemplo n.º 26
0
 /**
  * {@inheritdoc}
  */
 public function __get($name)
 {
     return $this->model->getProperty($name);
 }
 /**
  * Save a model to the database.
  *
  * In general, this method fetches the solely property "rows" from the model and updates the local table against
  * these contents.
  *
  * The parent id (id of the model) will get checked and reflected also for new items.
  *
  * When rows with duplicate ids are encountered (like from MCW for example), the dupes are inserted as new rows.
  *
  * @param ModelInterface $objItem   The model to save.
  *
  * @param bool           $recursive Ignored as not relevant in this data provider.
  *
  * @return ModelInterface The passed Model.
  *
  * @throws DcGeneralException When the passed model does not contain a property named "rows", an Exception is
  *                            thrown.
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function save(ModelInterface $objItem, $recursive = false)
 {
     $arrData = $objItem->getProperty('rows');
     if (!($objItem->getID() && $arrData)) {
         throw new DcGeneralException('invalid input data in model.', 1);
     }
     $arrKeep = array();
     foreach ($arrData as $arrRow) {
         $arrSQL = $arrRow;
         // Update all.
         $intId = intval($arrRow['id']);
         // Work around the fact that multicolumnwizard does not clear any hidden fields when copying a dataset.
         // therefore we do consider any dupe as new dataset and save it accordingly.
         if (in_array($intId, $arrKeep)) {
             $intId = 0;
             unset($arrSQL['id']);
         }
         if ($intId > 0) {
             $this->objDatabase->prepare(sprintf('UPDATE %s %%s WHERE id=? AND %s=?', $this->strSource, $this->strGroupCol))->set($arrSQL)->execute($intId, $objItem->getId());
             $arrKeep[] = $intId;
         } else {
             // Force group col value.
             $arrSQL[$this->strGroupCol] = $objItem->getId();
             $arrKeep[] = $this->objDatabase->prepare(sprintf('INSERT INTO %s %%s', $this->strSource))->set($arrSQL)->execute()->insertId;
         }
     }
     // House keeping, kill the rest.
     $this->objDatabase->prepare(sprintf('DELETE FROM  %s WHERE %s=? AND id NOT IN (%s)', $this->strSource, $this->strGroupCol, implode(',', $arrKeep)))->execute($objItem->getId());
     return $objItem;
 }
Ejemplo n.º 28
0
 /**
  * Get form data of an validated form.
  *
  * @param Entity $entity Entity.
  *
  * @return array
  */
 public function getData(Entity $entity = null)
 {
     if (!$entity) {
         return $this->data;
     }
     $data = $this->data;
     foreach ($this->bindValues as $fieldName => $property) {
         $data[$fieldName] = $entity->getProperty($property);
     }
     return $data;
 }
 /**
  * Handle a property in a cloned model.
  *
  * @param ModelInterface        $model        The cloned model.
  *
  * @param PropertyInterface     $property     The property to handle.
  *
  * @param DataProviderInterface $dataProvider The data provider the model originates from.
  *
  * @return void
  */
 private function handleClonedModelProperty(ModelInterface $model, PropertyInterface $property, DataProviderInterface $dataProvider)
 {
     $extra = $property->getExtra();
     $propName = $property->getName();
     // Check doNotCopy.
     if (isset($extra['doNotCopy']) && $extra['doNotCopy'] === true) {
         $model->setProperty($propName, null);
         return;
     }
     // Check uniqueness.
     if (isset($extra['unique']) && $extra['unique'] === true && !$dataProvider->isUniqueValue($propName, $model->getProperty($propName))) {
         // Implicit "do not copy" unique values, they cannot be unique anymore.
         $model->setProperty($propName, null);
     }
 }
Ejemplo n.º 30
0
 /**
  * {@inheritdoc}
  */
 public function match(ModelInterface $model = null, PropertyValueBag $input = null, PropertyInterface $property = null, LegendInterface $legend = null)
 {
     if ($input && $input->hasPropertyValue('pid')) {
         $value = $input->getPropertyValue('pid');
     } elseif ($model) {
         $value = $model->getProperty('pid');
     } else {
         return false;
     }
     return $this->getInputScreenRenderMode($value) == $this->getRenderMode();
 }