/**
     * Render the widget for the named property.
     *
     * @param string           $property     The name of the property for which the widget shall be rendered.
     *
     * @param bool             $ignoreErrors Flag if the error property of the widget shall get cleared prior rendering.
     *
     * @param PropertyValueBag $inputValues  The input values to use (optional).
     *
     * @return string
     *
     * @throws DcGeneralRuntimeException For unknown properties.
     */
    public function renderWidget($property, $ignoreErrors = false, PropertyValueBag $inputValues = null)
    {
        $environment = $this->getEnvironment();
        $definition = $environment->getDataDefinition();
        $propertyDefinitions = $definition->getPropertiesDefinition();
        $propInfo = $propertyDefinitions->getProperty($property);
        $propExtra = $propInfo->getExtra();
        $widget = $this->getWidget($property, $inputValues);
        /** @var \Contao\Widget $widget */
        if (!$widget) {
            throw new DcGeneralRuntimeException('No widget for property ' . $property);
        }
        if ($ignoreErrors) {
            // Clean the errors array and fix up the CSS class.
            $reflection = new \ReflectionProperty(get_class($widget), 'arrErrors');
            $reflection->setAccessible(true);
            $reflection->setValue($widget, array());
            $reflection = new \ReflectionProperty(get_class($widget), 'strClass');
            $reflection->setAccessible(true);
            $reflection->setValue($widget, str_replace('error', '', $reflection->getValue($widget)));
        } else {
            if ($inputValues && $inputValues->hasPropertyValue($property) && $inputValues->isPropertyValueInvalid($property)) {
                foreach ($inputValues->getPropertyValueErrors($property) as $error) {
                    $widget->addError($error);
                }
            }
        }
        $strDatePicker = '';
        if (isset($propExtra['datepicker'])) {
            $strDatePicker = $this->buildDatePicker($widget);
        }
        $objTemplateFoo = new ContaoBackendViewTemplate('dcbe_general_field');
        $objTemplateFoo->setData(array('strName' => $property, 'strClass' => $widget->tl_class, 'widget' => $widget->parse(), 'hasErrors' => $widget->hasErrors(), 'strDatepicker' => $strDatePicker, 'blnUpdate' => false, 'strHelp' => $this->generateHelpText($property), 'strId' => $widget->id));
        $buffer = $objTemplateFoo->parse();
        if (isset($propExtra['rte']) && strncmp($propExtra['rte'], 'tiny', 4) === 0) {
            $propertyId = 'ctrl_' . $property;
            $buffer .= <<<EOF
<script>tinyMCE.execCommand('mceAddControl', false, '{$propertyId}');\$('{$propertyId}').erase('required');</script>
EOF;
        }
        return $buffer;
    }
Esempio n. 2
0
 /**
  * Create the edit mask.
  *
  * @return string
  *
  * @throws DcGeneralRuntimeException         If the data container is not editable, closed.
  *
  * @throws DcGeneralInvalidArgumentException If an unknown property is encountered in the palette.
  *
  * @SuppressWarnings(PHPMD.LongVariable)
  */
 public function execute()
 {
     $environment = $this->getEnvironment();
     $definition = $this->getDataDefinition();
     $dataProvider = $environment->getDataProvider($this->model->getProviderName());
     $dataProviderDefinition = $definition->getDataProviderDefinition();
     $dataProviderInformation = $dataProviderDefinition->getInformation($this->model->getProviderName());
     $inputProvider = $environment->getInputProvider();
     $palettesDefinition = $definition->getPalettesDefinition();
     $blnSubmitted = $inputProvider->getValue('FORM_SUBMIT') === $definition->getName();
     $blnIsAutoSubmit = $inputProvider->getValue('SUBMIT_TYPE') === 'auto';
     $widgetManager = new ContaoWidgetManager($environment, $this->model);
     $this->checkEditable($this->model);
     $this->checkCreatable($this->model);
     $environment->getEventDispatcher()->dispatch(PreEditModelEvent::NAME, new PreEditModelEvent($environment, $this->model));
     $environment->getEventDispatcher()->dispatch(DcGeneralEvents::ENFORCE_MODEL_RELATIONSHIP, new EnforceModelRelationshipEvent($this->getEnvironment(), $this->model));
     // Pass 1: Get the palette for the values stored in the model.
     $palette = $palettesDefinition->findPalette($this->model);
     $propertyValues = $this->processInput($widgetManager);
     if ($blnSubmitted && $propertyValues) {
         // Pass 2: Determine the real palette we want to work on if we have some data submitted.
         $palette = $palettesDefinition->findPalette($this->model, $propertyValues);
         // Update the model - the model might add some more errors to the propertyValueBag via exceptions.
         $this->getEnvironment()->getController()->updateModelFromPropertyBag($this->model, $propertyValues);
     }
     $fieldSets = $this->buildFieldSet($widgetManager, $palette, $propertyValues);
     if (!$blnIsAutoSubmit && $blnSubmitted && empty($this->errors)) {
         if ($this->doPersist()) {
             $this->handleSubmit($this->model);
         }
     }
     $objTemplate = new ContaoBackendViewTemplate('dcbe_general_edit');
     $objTemplate->setData(array('fieldsets' => $fieldSets, 'versions' => $dataProviderInformation->isVersioningEnabled() ? $dataProvider->getVersions($this->model->getId()) : null, 'subHeadline' => $this->getHeadline(), 'table' => $definition->getName(), 'enctype' => 'multipart/form-data', 'error' => $this->errors, 'editButtons' => $this->getEditButtons(), 'noReload' => (bool) $this->errors, 'breadcrumb' => $this->breadcrumb));
     if (in_array('ContaoCommunityAlliance\\DcGeneral\\Data\\MultiLanguageDataProviderInterface', class_implements($environment->getDataProvider($this->model->getProviderName())))) {
         /** @var MultiLanguageDataProviderInterface $dataProvider */
         $langsNative = array();
         require TL_ROOT . '/system/config/languages.php';
         $objTemplate->set('languages', $environment->getController()->getSupportedLanguages($this->model->getId()))->set('language', $dataProvider->getCurrentLanguage())->set('languageHeadline', $langsNative[$dataProvider->getCurrentLanguage()]);
     } else {
         $objTemplate->set('languages', null)->set('languageHeadline', '');
     }
     return $objTemplate->parse();
 }