/** * 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; }
/** * 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 */ private static function decodeValue($environment, $model, $property, $value) { $event = new DecodePropertyValueForWidgetEvent($environment, $model); $event->setProperty($property)->setValue($value); $environment->getEventDispatcher()->dispatch(sprintf('%s', $event::NAME), $event); return $event->getValue(); }
/** * Decode a value from native data of the data provider to the widget via event. * * @param string $property The property. * * @param mixed $value The value of the property. * * @return mixed */ public function decodeValue($property, $value) { $environment = $this->getEnvironment(); $event = new DecodePropertyValueForWidgetEvent($environment, $this->model); $event->setProperty($property)->setValue($value); $environment->getEventDispatcher()->dispatch(EncodePropertyValueFromWidgetEvent::NAME, $event); return $event->getValue(); }
/** * 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; }
/** * Decode a value from native data of the data provider to the widget via event. * * @param string $property The property. * * @param mixed $value The value of the property. * * @return mixed */ public function decodeValue($property, $value) { $environment = $this->getEnvironment(); $event = new DecodePropertyValueForWidgetEvent($environment, $this->model); $event->setProperty($property)->setValue($value); $environment->getEventPropagator()->propagate($event::NAME, $event, array($environment->getDataDefinition()->getName(), $property)); return $event->getValue(); }
/** * 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) { /* FIXME TEMPORARY WORKAROUND! To be fixed in the core: @see \Widget::getAttributesFromDca() */ $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 == '' && !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(); // 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' && 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; }
/** * The subscriber creates the timestamp from the widget value. * * @param string $format The given date format. * @param string $value The given date example * * @dataProvider dataProvider * @test */ public function it_creates_timestamp_from_widget_value($format, $value) { $dateTime = \DateTime::createFromFormat($format, $value); $timestamp = $dateTime->getTimestamp(); $attribute = $this->mockAttribute($format); $attribute->expects($this->any())->method('widgetToValue')->will($this->returnValue($timestamp)); $model = $this->mockModelWithAttribute($attribute); $event = new DecodePropertyValueForWidgetEvent($this->mockEnvironment(), $model); $event->setProperty('date'); $event->setValue($timestamp); $this->eventDispatcher->expects($this->atLeastOnce())->method('dispatch')->with($this->anything(), $this->callback(function (ParseDateEvent $event) use($value) { $event->setResult($value); return true; })); $this->backendSubscriber->handleDecodePropertyValueForWidgetEvent($event); $this->assertEquals($value, $event->getValue()); }