Exemple #1
0
 /**
  * Get the widget instance.
  *
  * @param string $fieldName     The property name.
  *
  * @param string $serializedId  The serialized id of the model.
  *
  * @param string $propertyValue The property value.
  *
  * @return \Widget
  */
 protected function getWidget($fieldName, $serializedId, $propertyValue)
 {
     $environment = $this->getEnvironment();
     $property = $environment->getDataDefinition()->getPropertiesDefinition()->getProperty($fieldName);
     $propertyValues = new PropertyValueBag();
     if ($serializedId !== null) {
         $model = $this->getModelFromSerializedId($serializedId);
     } else {
         $dataProvider = $environment->getDataProvider();
         $model = $dataProvider->getEmptyModel();
     }
     $widgetManager = new ContaoWidgetManager($environment, $model);
     // Process input and update changed properties.
     $treeType = substr($property->getWidgetType(), 0, 4);
     $propertyValue = $this->getTreeValue($treeType, $propertyValue);
     if ($treeType == 'file' || $treeType == 'page') {
         $extra = $property->getExtra();
         if (is_array($propertyValue) && !isset($extra['multiple'])) {
             $propertyValue = $propertyValue[0];
         } else {
             $propertyValue = implode(',', (array) $propertyValue);
         }
     }
     $propertyValues->setPropertyValue($fieldName, $propertyValue);
     $widgetManager->processInput($propertyValues);
     $model->setProperty($fieldName, $propertyValues->getPropertyValue($fieldName));
     $widget = $widgetManager->getWidget($fieldName);
     return $widget;
 }
 /**
  * Update the error collection.
  *
  * @return void
  */
 private function updateErrorCollection()
 {
     $this->getErrorCollection()->reset();
     // copy error messages
     foreach ($this->propertyValues->getInvalidPropertyErrors() as $field => $errors) {
         foreach ($errors as $error) {
             $this->getErrorCollection()->addError('form.validation.error', array('field' => $field, 'message' => $error));
         }
     }
 }
 public function testGetMatch()
 {
     $condition = new PaletteConditionChain();
     $condition->setConjunction(PaletteConditionChain::AND_CONJUNCTION);
     $condition->addCondition(new PropertyValueCondition('prop1', '0'));
     $condition->addCondition(new PropertyValueCondition('prop2', '1'));
     $this->assertEquals(0, $condition->getMatchCount());
     $model = new DefaultModel();
     $model->setProperty('prop1', '0');
     $model->setProperty('prop2', '1');
     $this->assertEquals(2, $condition->getMatchCount($model));
     $model->setProperty('prop2', '0');
     $this->assertEquals(0, $condition->getMatchCount($model));
     $propertyValueBag = new PropertyValueBag();
     $propertyValueBag->setPropertyValue('prop1', '0');
     $propertyValueBag->setPropertyValue('prop2', '1');
     $this->assertEquals(2, $condition->getMatchCount(null, $propertyValueBag));
     $propertyValueBag->setPropertyValue('prop2', '3');
     $this->assertEquals(0, $condition->getMatchCount(null, $propertyValueBag));
 }
 /**
  * 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 $valueBag The input values to use (optional).
  *
  * @return \Widget
  *
  * @throws DcGeneralRuntimeException         When No widget could be build.
  * @throws DcGeneralInvalidArgumentException When property is not defined in the property definitions.
  */
 public function getWidget($property, PropertyValueBag $valueBag = null)
 {
     $environment = $this->getEnvironment();
     $dispatcher = $environment->getEventDispatcher();
     $propertyDefinitions = $environment->getDataDefinition()->getPropertiesDefinition();
     if (!$propertyDefinitions->hasProperty($property)) {
         throw new DcGeneralInvalidArgumentException('Property ' . $property . ' is not defined in propertyDefinitions.');
     }
     $model = clone $this->model;
     $model->setId($this->model->getId());
     if ($valueBag) {
         $values = new PropertyValueBag($valueBag->getArrayCopy());
         $this->environment->getController()->updateModelFromPropertyBag($model, $values);
     }
     $propertyDefinition = $propertyDefinitions->getProperty($property);
     $event = new BuildWidgetEvent($environment, $model, $propertyDefinition);
     $dispatcher->dispatch(DcGeneralFrontendEvents::BUILD_WIDGET, $event);
     if (!$event->getWidget()) {
         throw new DcGeneralRuntimeException(sprintf('Widget was not build for property %s::%s.', $this->model->getProviderName(), $property));
     }
     return $event->getWidget();
 }
 /**
  * {@inheritDoc}
  */
 public function processErrors(PropertyValueBag $propertyValues)
 {
     $propertyErrors = $propertyValues->getInvalidPropertyErrors();
     if ($propertyErrors) {
         $dispatcher = $this->getEnvironment()->getEventDispatcher();
         foreach ($propertyErrors as $property => $errors) {
             $widget = $this->getWidget($property);
             foreach ($errors as $error) {
                 $event = new ResolveWidgetErrorMessageEvent($this->getEnvironment(), $error);
                 $dispatcher->dispatch(ResolveWidgetErrorMessageEvent::NAME, $event);
                 $widget->addError($event->getError());
             }
         }
     }
 }
 /**
  * {@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();
 }
 /**
  * {@inheritDoc}
  */
 public function processErrors(PropertyValueBag $propertyValues)
 {
     $propertyErrors = $propertyValues->getInvalidPropertyErrors();
     $definitionName = $this->getEnvironment()->getDataDefinition()->getName();
     if ($propertyErrors) {
         $propagator = $this->getEnvironment()->getEventPropagator();
         foreach ($propertyErrors as $property => $errors) {
             $widget = $this->getWidget($property);
             foreach ($errors as $error) {
                 $event = new ResolveWidgetErrorMessageEvent($this->getEnvironment(), $error);
                 $propagator->propagate($event::NAME, $event, array($definitionName, $property));
                 $widget->addError($event->getError());
             }
         }
     }
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
Exemple #11
0
 /**
  * Process input and return all modified properties or null if there is no input.
  *
  * @param ContaoWidgetManager $widgetManager The widget manager in use.
  *
  * @return null|PropertyValueBag
  */
 protected function processInput($widgetManager)
 {
     $input = $this->getEnvironment()->getInputProvider();
     if ($input->getValue('FORM_SUBMIT') == $this->getDataDefinition()->getName()) {
         $propertyValues = new PropertyValueBag();
         $propertyNames = array_intersect($this->getDataDefinition()->getPropertiesDefinition()->getPropertyNames(), $input->getValue('FORM_INPUTS'));
         // Process input and update changed properties.
         foreach ($propertyNames as $propertyName) {
             $propertyValue = $input->hasValue($propertyName) ? $input->getValue($propertyName, true) : null;
             $propertyValues->setPropertyValue($propertyName, $propertyValue);
         }
         $widgetManager->processInput($propertyValues);
         return $propertyValues;
     }
     return null;
 }
 /**
  * {@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();
 }
 /**
  * {@inheritdoc}
  */
 public function match(ModelInterface $model = null, PropertyValueBag $input = null, PropertyInterface $property = null, LegendInterface $legend = null)
 {
     if ($input && $input->hasPropertyValue($this->propertyName)) {
         $values = $input->getPropertyValue($this->propertyName);
     } elseif ($model) {
         $values = $model->getProperty($this->propertyName);
     } else {
         return false;
     }
     if (!$values || !is_array($values)) {
         return false;
     }
     foreach ($values as $value) {
         if (in_array($value, $this->propertyValue, $this->strict)) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function match(ModelInterface $model = null, PropertyValueBag $input = null, PropertyInterface $property = null, LegendInterface $legend = null)
 {
     $propertyName = $this->getTablePropertyName();
     if ($input && $input->hasPropertyValue($propertyName)) {
         $value = $input->getPropertyValue($propertyName);
     } elseif ($model) {
         $value = $model->getProperty($propertyName);
     } else {
         return false;
     }
     return $this->desiredValue == (substr($value, 0, 3) === 'mm_');
 }