/** * Set the value in the event. * * @param ModelToLabelEvent $event The event being emitted. * * @param string $value The label text to use. * * @return void */ public function update($event, $value) { if ($value === null) { return; } $event->setLabel($value); }
/** * Default handler for formatting a model. * * @param FormatModelLabelEvent $event The event. * * @return void */ public function handleFormatModelLabel(FormatModelLabelEvent $event) { $environment = $event->getEnvironment(); $model = $event->getModel(); $dataDefinition = $environment->getDataDefinition(); /** @var Contao2BackendViewDefinitionInterface $viewSection */ $viewSection = $dataDefinition->getDefinition(Contao2BackendViewDefinitionInterface::NAME); $listing = $viewSection->getListingConfig(); $properties = $dataDefinition->getPropertiesDefinition(); $formatter = $listing->getLabelFormatter($model->getProviderName()); $sorting = ViewHelpers::getGroupingMode($environment); $sortingDefinition = $sorting['sorting']; $firstSorting = ''; if ($sortingDefinition) { /** @var GroupAndSortingDefinitionInterface $sortingDefinition */ foreach ($sortingDefinition as $information) { /** @var GroupAndSortingInformationInterface $information */ if ($information->getProperty()) { $firstSorting = reset($sorting); break; } } } $args = array(); foreach ($formatter->getPropertyNames() as $propertyName) { if ($properties->hasProperty($propertyName)) { $property = $properties->getProperty($propertyName); $args[$propertyName] = (string) ViewHelpers::getReadableFieldValue($environment, $property, $model); } else { $args[$propertyName] = '-'; } } $modelToLabelEvent = new ModelToLabelEvent($environment, $model); $modelToLabelEvent->setArgs($args)->setLabel($formatter->getFormat())->setFormatter($formatter); $environment->getEventDispatcher()->dispatch(ModelToLabelEvent::NAME, $modelToLabelEvent); $label = array(); // Add columns. if ($listing->getShowColumns()) { $fields = $formatter->getPropertyNames(); $args = $modelToLabelEvent->getArgs(); if (!is_array($args)) { $label[] = array('colspan' => count($fields), 'class' => 'tl_file_list col_1', 'content' => $args); } else { foreach ($fields as $j => $propertyName) { $label[] = array('colspan' => 1, 'class' => 'tl_file_list col_' . $j . ($propertyName == $firstSorting ? ' ordered_by' : ''), 'content' => $args[$propertyName] != '' ? $args[$propertyName] : '-'); } } } else { if (!is_array($modelToLabelEvent->getArgs())) { $string = $modelToLabelEvent->getArgs(); } else { $string = vsprintf($modelToLabelEvent->getLabel(), $modelToLabelEvent->getArgs()); } if ($formatter->getMaxLength() !== null && strlen($string) > $formatter->getMaxLength()) { $string = substr($string, 0, $formatter->getMaxLength()); } $label[] = array('colspan' => null, 'class' => 'tl_file_list', 'content' => $string); } $event->setLabel($label); }
/** * Render the current item using the specified render setting. * * @param ModelToLabelEvent $event The event. * * @return void */ public static function render(ModelToLabelEvent $event) { $environment = $event->getEnvironment(); /** @var IMetaModelDataDefinition $definition */ $definition = $environment->getDataDefinition(); /** @var Contao2BackendViewDefinitionInterface $viewSection */ $viewSection = $definition->getDefinition(Contao2BackendViewDefinitionInterface::NAME); $listing = $viewSection->getListingConfig(); /** @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; } $data = array($nativeItem->parseValue('html5', $renderSetting)); if ($listing->getShowColumns()) { $event->setArgs($data[0]['html5']); return; } $template = new Template($renderSetting->get('template')); $renderSetting = self::removeInvariantAttributes($nativeItem, $renderSetting); $template->setData(array('settings' => $renderSetting, 'items' => new Items(array($nativeItem)), 'view' => $renderSetting, 'data' => $data)); $event->setArgs(array($template->parse('html5'))); }
/** * Draw the render setting. * * @param ModelToLabelEvent $event The event. * * @return void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_rendersettings') { return; } if ($event->getModel()->getProperty('isdefault')) { $event->setLabel($event->getLabel() . ' <span style="color:#b3b3b3; padding-left:3px">[' . $GLOBALS['TL_LANG']['MSC']['fallback'] . ']</span>'); } }
/** * Render the html for the input screen. * * @param ModelToLabelEvent $event The event. * * @return void */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dca') { return; } $environment = $event->getEnvironment(); $translator = $environment->getTranslator(); $model = $event->getModel(); if (!$model->getProperty('isdefault')) { return; } $event->setArgs(array_merge($event->getArgs(), array($translator->translate('MSC.fallback'))))->setLabel(sprintf('%s <span style="color:#b3b3b3; padding-left:3px">[%%s]</span>', $event->getLabel())); }
/** * Draw the render setting. * * @param ModelToLabelEvent $event The event. * * @return void */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dca_sortgroup') { return; } if ($event->getModel()->getProperty('isdefault')) { $event->setLabel(sprintf('%s <span style="color:#b3b3b3; padding-left:3px">[%s]</span>', $event->getLabel(), $event->getEnvironment()->getTranslator()->translate('MSC.fallback'))); } }
/** * Draw the attribute in the backend listing. * * @param ModelToLabelEvent $event The event. * * @return void */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_attribute') { return; } $services = $this->getServiceContainer(); $factory = $services->getAttributeFactory(); $model = $event->getModel(); $type = $model->getProperty('type'); $image = '<img src="' . $factory->getIconForType($type) . '" />'; $metaModel = $this->getMetaModelByModelPid($model); $attribute = $factory->createAttribute($model->getPropertiesAsArray(), $metaModel); if (!$attribute) { $translator = $event->getEnvironment()->getTranslator(); $event->setLabel('<div class="field_heading cte_type"><strong>%s</strong> <em>[%s]</em></div> <div class="field_type block"> <strong>%s</strong><br /> </div>')->setArgs(array($translator->translate('error_unknown_attribute.0', 'tl_metamodel_attribute'), $type, $translator->translate('error_unknown_attribute.1', 'tl_metamodel_attribute', array($type)))); return; } $colName = $attribute->getColName(); $name = $attribute->getName(); $arrDescription = deserialize($attribute->get('description')); if (is_array($arrDescription)) { $description = $arrDescription[$attribute->getMetaModel()->getActiveLanguage()]; if (!$description) { $description = $arrDescription[$attribute->getMetaModel()->getFallbackLanguage()]; } } else { $description = $attribute->getName(); } $event->setLabel('<div class="field_heading cte_type"><strong>%s</strong> <em>[%s]</em></div> <div class="field_type block"> %s<strong>%s</strong> - %s </div>')->setArgs(array($colName, $type, $image, $name, $description)); }
/** * Render a filter setting into html. * * @param ModelToLabelEvent $event The event. * * @return void */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_filtersetting' || in_array($event->getModel()->getProperty('type'), $this->getTypes())) { return; } $environment = $event->getEnvironment(); $model = $event->getModel(); $event->setLabel($this->getLabelPattern($environment, $model))->setArgs($this->getLabelParameters($environment, $model)); }
/** * Render an attribute or legend. * * @param ModelToLabelEvent $event The event. * * @return void */ public function handleModelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dcasetting') { return; } $model = $event->getModel(); switch ($model->getProperty('dcatype')) { case 'attribute': self::drawAttribute($event); break; case 'legend': self::drawLegend($event); break; default: break; } }
/** * Render the html for the input screen condition. * * @param ModelToLabelEvent $event The event. * * @return void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public function handleModelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dcasetting_condition') { return; } $environment = $event->getEnvironment(); $translator = $environment->getTranslator(); $model = $event->getModel(); $metaModels = $this->getMetaModel($environment); $attribute = $metaModels->getAttributeById($model->getProperty('attr_id')); $type = $model->getProperty('type'); $parameterValue = is_array($model->getProperty('value')) ? implode(', ', $model->getProperty('value')) : $model->getProperty('value'); $name = $translator->translate('conditionnames.' . $type, 'tl_metamodel_dcasetting_condition'); $image = $GLOBALS['METAMODELS']['attributes'][$type]['image']; if (!$image || !file_exists(TL_ROOT . '/' . $image)) { $image = 'system/modules/metamodels/assets/images/icons/filter_default.png'; } /** @var GenerateHtmlEvent $imageEvent */ $imageEvent = $event->getEnvironment()->getEventDispatcher()->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent($image)); $event->setLabel($this->getLabelText($translator, $type))->setArgs(array($imageEvent->getHtml(), $name, $attribute ? $attribute->getName() : '', $parameterValue)); }
/** * Create label for recipient. * * @param ModelToLabelEvent $event * * @return string * @SuppressWarnings(PHPMD.LongVariable) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public function createLabel(ModelToLabelEvent $event) { if ($event->getModel()->getProviderName() != 'orm_avisota_recipient') { return; } global $container; /** @var EntityModel $model */ $model = $event->getModel(); /** @var \Avisota\Contao\Entity\Recipient $recipient */ $recipient = $model->getEntity(); /** @var EventDispatcher $eventDispatcher */ $eventDispatcher = $container['event-dispatcher']; $label = $this->addExpandFoldIconToLabel($eventDispatcher); $label .= $this->addRecipientToLabel($recipient); $label .= $this->addAddedByToLabel($recipient); $label .= $this->addSubscriptionToLabel($recipient, $eventDispatcher); $event->setLabel($label); }
/** * Render a model in the backend list. * * @param ModelToLabelEvent $event The event. * * @return void */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel') { return; } $model = $event->getModel(); $translator = $event->getEnvironment()->getTranslator(); $database = $this->getDatabase(); if (!($model && $database->tableExists($model->getProviderName()))) { return; } $strLabel = vsprintf($event->getLabel(), $event->getArgs()); $strImage = ''; if ($model->getProperty('addImage')) { $arrSize = deserialize($model->getProperty('size')); $imageEvent = new ResizeImageEvent($model->getProperty('singleSRC'), $arrSize[0], $arrSize[1], $arrSize[2]); $event->getEnvironment()->getEventDispatcher()->dispatch(ContaoEvents::IMAGE_RESIZE, $event); $strImage = sprintf('<div class="image" style="padding-top:3px"><img src="%s" alt="%%1$s" /></div> ', $imageEvent->getImage(), htmlspecialchars($strLabel)); } $objCount = $database->prepare('SELECT count(*) AS itemCount FROM ' . $model->getProperty('tableName'))->execute(); /** @noinspection PhpUndefinedFieldInspection */ $count = $objCount->itemCount; $itemCount = sprintf('<span style="color:#b3b3b3; padding-left:3px">[%s]</span>', $translator->translatePluralized('itemFormatCount', $count, 'tl_metamodel', array($count))); $tableName = '<span style="color:#b3b3b3; padding-left:3px">(' . $model->getProperty('tableName') . ')</span>'; $event->setArgs(array('<span class="name">' . $strLabel . $tableName . $itemCount . '</span>' . $strImage)); }
/** * Format a model accordingly to the current configuration. * * Returns either an array when in tree mode or a string in (parented) list mode. * * @param ModelInterface $model The model that shall be formatted. * * @param bool $treeMode Flag if we are running in tree mode or not (optional, default: true). * * @return array */ public function formatModel(ModelInterface $model, $treeMode = true) { /** @var ListingConfigInterface $listing */ $definition = $this->getEnvironment()->getDataDefinition(); $listing = $definition->getDefinition(Contao2BackendViewDefinitionInterface::NAME)->getListingConfig(); $properties = $definition->getPropertiesDefinition(); $sorting = array_keys((array) $listing->getDefaultSortingFields()); $firstSorting = reset($sorting); $formatter = $this->getFormatter($model, $treeMode); $args = array(); foreach ($formatter->getPropertyNames() as $propertyName) { if ($properties->hasProperty($propertyName)) { $args[$propertyName] = (string) $model->getProperty($propertyName); } else { $args[$propertyName] = '-'; } } $event = new ModelToLabelEvent($this->getEnvironment(), $model); $event->setArgs($args)->setLabel($formatter->getFormat())->setFormatter($formatter); $this->getEnvironment()->getEventDispatcher()->dispatch($event::NAME, $event); $arrLabel = array(); // Add columns. if ($listing->getShowColumns()) { $fields = $formatter->getPropertyNames(); $args = $event->getArgs(); if (!is_array($args)) { $arrLabel[] = array('colspan' => count($fields), 'class' => 'tl_file_list col_1', 'content' => $args); } else { foreach ($fields as $j => $propertyName) { $arrLabel[] = array('colspan' => 1, 'class' => 'tl_file_list col_' . $j . ($propertyName == $firstSorting ? ' ordered_by' : ''), 'content' => $args[$propertyName] != '' ? $args[$propertyName] : '-'); } } } else { if (!is_array($event->getArgs())) { $string = $event->getArgs(); } else { $string = vsprintf($event->getLabel(), $event->getArgs()); } if ($formatter->getMaxLength() !== null && strlen($string) > $formatter->getMaxLength()) { $string = substr($string, 0, $formatter->getMaxLength()); } $arrLabel[] = array('colspan' => null, 'class' => 'tl_file_list', 'content' => $string); } return $arrLabel; }
/** * Draw the render setting. * * @param ModelToLabelEvent $event The event. * * @return void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public function modelToLabel(ModelToLabelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_rendersetting') { return; } $model = $event->getModel(); $attribute = $this->getMetaModel($model)->getAttributeById($model->getProperty('attr_id')); if ($attribute) { $type = $attribute->get('type'); $image = $GLOBALS['METAMODELS']['attributes'][$type]['image']; if (!$image || !file_exists(TL_ROOT . '/' . $image)) { $image = 'system/modules/metamodels/assets/images/icons/fields.png'; } $name = $attribute->getName(); $colName = $attribute->getColName(); } else { $translator = $event->getEnvironment()->getTranslator(); $image = 'system/modules/metamodels/assets/images/icons/fields.png'; $name = $translator->translate('error_unknown_id', 'error_unknown_attribute'); $colName = $translator->translate('error_unknown_column', 'error_unknown_attribute'); $type = $translator->translate('error_unknown_id', 'tl_metamodel_rendersettings', array($model->getProperty('attr_id'))); } /** @var GenerateHtmlEvent $imageEvent */ $imageEvent = $event->getEnvironment()->getEventDispatcher()->dispatch(ContaoEvents::IMAGE_GET_HTML, new GenerateHtmlEvent($image)); $event->setLabel('<div class="field_heading cte_type %s"><strong>%s</strong> <em>[%s]</em></div> <div class="field_type block"> %s<strong>%s</strong> </div>')->setArgs(array($model->getProperty('enabled') ? 'published' : 'unpublished', $colName, $type, $imageEvent->getHtml(), $name)); }