示例#1
0
 /**
  * Render a property option.
  *
  * @param RenderReadablePropertyValueEvent $event    The event to store the value to.
  * @param PropertyInterface                $property The property holding the options.
  * @param mixed                            $value    The value to format.
  *
  * @return void
  */
 private static function renderOptionValueReadable(RenderReadablePropertyValueEvent $event, $property, $value)
 {
     $options = $property->getOptions();
     if (!$options) {
         $options = self::getOptions($event->getEnvironment(), $event->getModel(), $event->getProperty());
         if ($options) {
             $property->setOptions($options);
         }
     }
     if (array_is_assoc($options)) {
         $event->setRendered($options[$value]);
     }
 }
示例#2
0
 /**
  * Render a property value to readable text.
  *
  * @param RenderReadablePropertyValueEvent $event The event being processed.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.Superglobals)
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  */
 public static function renderReadablePropertyValue(RenderReadablePropertyValueEvent $event)
 {
     if ($event->getRendered() !== null) {
         return;
     }
     $dispatcher = $event->getEnvironment()->getEventDispatcher();
     $property = $event->getProperty();
     $value = self::decodeValue($event->getEnvironment(), $event->getModel(), $event->getProperty()->getName(), $event->getValue());
     $extra = $property->getExtra();
     // TODO: refactor - foreign key handling is not yet supported.
     /*
             if (isset($arrFieldConfig['foreignKey']))
             {
        $temp = array();
        $chunks = explode('.', $arrFieldConfig['foreignKey'], 2);
     
     
        foreach ((array) $value as $v)
        {
     //                    $objKey = $this->Database->prepare("SELECT " . $chunks[1] . " AS value FROM " . $chunks[0] . " WHERE id=?")
     //                            ->limit(1)
     //                            ->execute($v);
     //
     //                    if ($objKey->numRows)
     //                    {
     //                        $temp[] = $objKey->value;
     //                    }
        }
     
     //                $row[$i] = implode(', ', $temp);
             }
             // Decode array
             else
     */
     if (is_array($value)) {
         foreach ($value as $kk => $vv) {
             if (is_array($vv)) {
                 $vals = array_values($vv);
                 $value[$kk] = $vals[0] . ' (' . $vals[1] . ')';
             }
         }
         $event->setRendered(implode(', ', $value));
     } elseif (isset($extra['rgxp'])) {
         // Date format.
         if ($extra['rgxp'] == 'date' || $extra['rgxp'] == 'time' || $extra['rgxp'] == 'datim') {
             $event->setRendered(self::parseDateTime($dispatcher, $GLOBALS['TL_CONFIG'][$extra['rgxp'] . 'Format'], $value));
         }
     } elseif ($property->getName() == 'tstamp') {
         // Date and time format.
         $dateEvent = new ParseDateEvent($value, $GLOBALS['TL_CONFIG']['timeFormat']);
         $dispatcher->dispatch(ContaoEvents::DATE_PARSE, $dateEvent);
         $event->setRendered($dateEvent->getResult());
     } elseif ($property->getWidgetType() == 'checkbox' && !$extra['multiple']) {
         $event->setRendered(strlen($value) ? $GLOBALS['TL_LANG']['MSC']['yes'] : $GLOBALS['TL_LANG']['MSC']['no']);
     } elseif ($property->getWidgetType() == 'textarea' && (!empty($extra['allowHtml']) || !empty($extra['preserveTags']))) {
         $event->setRendered(nl2br_html5(specialchars($value)));
     } elseif (isset($extra['reference']) && is_array($extra['reference'])) {
         if (isset($extra['reference'][$value])) {
             $event->setRendered(is_array($extra['reference'][$value]) ? $extra['reference'][$value][0] : $extra['reference'][$value]);
         }
     } elseif ($value instanceof \DateTime) {
         $dateEvent = new ParseDateEvent($value->getTimestamp(), $GLOBALS['TL_CONFIG']['datimFormat']);
         $dispatcher->dispatch(ContaoEvents::DATE_PARSE, $dateEvent);
         $event->setRendered($dateEvent->getResult());
     } else {
         $options = $property->getOptions();
         if (!$options) {
             $options = self::getOptions($event->getEnvironment(), $event->getModel(), $event->getProperty());
             if ($options) {
                 $property->setOptions($options);
             }
         }
         if (array_is_assoc($options)) {
             $event->setRendered($options[$value]);
         }
     }
 }
示例#3
0
 /**
  * Render a model for use in a group header.
  *
  * @param RenderReadablePropertyValueEvent $event The event.
  *
  * @return void
  */
 public static function getReadableValue(RenderReadablePropertyValueEvent $event)
 {
     $environment = $event->getEnvironment();
     /** @var IMetaModelDataDefinition $definition */
     $definition = $environment->getDataDefinition();
     /** @var Model $model */
     $model = $event->getModel();
     if (!$model instanceof Model) {
         return;
     }
     $nativeItem = $model->getItem();
     $metaModel = $nativeItem->getMetaModel();
     $renderSetting = $metaModel->getServiceContainer()->getRenderSettingFactory()->createCollection($metaModel, $definition->getMetaModelDefinition()->getActiveRenderSetting());
     if (!$renderSetting) {
         return;
     }
     $result = $nativeItem->parseAttribute($event->getProperty()->getName(), 'text', $renderSetting);
     if (!isset($result['text'])) {
         $event->setRendered(sprintf('Unexpected behaviour, attribute %s text representation was not rendered.', $event->getProperty()->getName()));
         return;
     }
     $event->setRendered($result['text']);
 }