/** * 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); }
/** * Ensure all models have the same grouping. * * @param array $actions The actions collection. * @param ModelIdInterface $after The previous model id. * * @return void */ private function ensureSameGrouping(array $actions, ModelIdInterface $after = null) { $environment = $this->getEnvironment(); $groupingMode = ViewHelpers::getGroupingMode($environment); if ($groupingMode && $after && $after->getId()) { // when pasting after another item, inherit the grouping field $groupingField = $groupingMode['property']; $dataProvider = $environment->getDataProvider($after->getDataProviderName()); $previous = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($after->getId())); $groupingValue = $previous->getProperty($groupingField); foreach ($actions as $action) { /** @var ModelInterface $model */ $model = $action['model']; $model->setProperty($groupingField, $groupingValue); } } }
/** * Generate list view from current collection. * * @param CollectionInterface $collection The collection containing the models. * * @return string */ protected function viewList($collection) { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $groupingInformation = ViewHelpers::getGroupingMode($this->environment); // Set label. $this->setListViewLabel($collection, $groupingInformation); // Generate buttons - only if not in select mode! if (!$this->isSelectModeActive()) { $buttonRenderer = new ButtonRenderer($this->environment); $buttonRenderer->renderButtonsForCollection($collection); } // Add template. if (isset($groupingInformation['mode']) && $groupingInformation['mode'] != GroupAndSortingInformationInterface::GROUP_NONE) { $objTemplate = $this->getTemplate('dcbe_general_grouping'); } elseif (isset($groupingInformation['property']) && $groupingInformation['property'] != '') { $objTemplate = $this->getTemplate('dcbe_general_listView_sorting'); } else { $objTemplate = $this->getTemplate('dcbe_general_listView'); } $this->addToTemplate('tableName', strlen($definition->getName()) ? $definition->getName() : 'none', $objTemplate)->addToTemplate('collection', $collection, $objTemplate)->addToTemplate('select', $this->getEnvironment()->getInputProvider()->getParameter('act'), $objTemplate)->addToTemplate('action', ampersand(\Environment::get('request'), true), $objTemplate)->addToTemplate('mode', $groupingInformation ? $groupingInformation['mode'] : null, $objTemplate)->addToTemplate('tableHead', $this->getTableHead(), $objTemplate)->addToTemplate('pdp', '', $objTemplate)->addToTemplate('cdp', $definition->getName(), $objTemplate)->addToTemplate('selectButtons', $this->getSelectButtons(), $objTemplate)->addToTemplate('sortable', (bool) ViewHelpers::getManualSortingProperty($this->environment), $objTemplate)->addToTemplate('showColumns', $this->getViewSection()->getListingConfig()->getShowColumns(), $objTemplate); // Add breadcrumb, if we have one. $strBreadcrumb = $this->breadcrumb(); if ($strBreadcrumb != null) { $this->addToTemplate('breadcrumb', $strBreadcrumb, $objTemplate); } return $objTemplate->parse(); }
/** * Show parent view mode 4. * * @param CollectionInterface $collection The collection containing the models. * * @param ModelInterface $parentModel The parent model. * * @return string HTML output */ protected function viewParent($collection, $parentModel) { $definition = $this->getEnvironment()->getDataDefinition(); $parentProvider = $definition->getBasicDefinition()->getParentDataProvider(); $groupingInformation = ViewHelpers::getGroupingMode($this->environment); $dispatcher = $this->getEnvironment()->getEventDispatcher(); // Skip if we have no parent or parent collection. if (!$parentModel) { $dispatcher->dispatch(ContaoEvents::SYSTEM_LOG, new LogEvent(sprintf('The view for %s has either a empty parent data provider or collection.', $parentProvider), __CLASS__ . '::' . __FUNCTION__ . '()', TL_ERROR)); $dispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, new RedirectEvent('contao/main.php?act=error')); } // Add template. if ($groupingInformation && $groupingInformation['mode'] != GroupAndSortingInformationInterface::GROUP_NONE) { $objTemplate = $this->getTemplate('dcbe_general_grouping'); } else { $objTemplate = $this->getTemplate('dcbe_general_parentView'); } $this->addToTemplate('tableName', strlen($definition->getName()) ? $definition->getName() : 'none', $objTemplate)->addToTemplate('collection', $collection, $objTemplate)->addToTemplate('select', $this->isSelectModeActive(), $objTemplate)->addToTemplate('action', ampersand(\Environment::get('request'), true), $objTemplate)->addToTemplate('header', $this->renderFormattedHeaderFields($parentModel), $objTemplate)->addToTemplate('mode', $groupingInformation ? $groupingInformation['mode'] : null, $objTemplate)->addToTemplate('pdp', (string) $parentProvider, $objTemplate)->addToTemplate('cdp', $definition->getName(), $objTemplate)->addToTemplate('selectButtons', $this->getSelectButtons(), $objTemplate)->addToTemplate('headerButtons', $this->getHeaderButtons($parentModel), $objTemplate)->addToTemplate('sortable', (bool) ViewHelpers::getManualSortingProperty($this->environment), $objTemplate)->addToTemplate('showColumns', $this->getViewSection()->getListingConfig()->getShowColumns(), $objTemplate); $this->renderEntries($collection, $groupingInformation); // Add breadcrumb, if we have one. $strBreadcrumb = $this->breadcrumb(); if ($strBreadcrumb != null) { $this->addToTemplate('breadcrumb', $strBreadcrumb, $objTemplate); } return $objTemplate->parse(); }
/** * Generate list view from current collection. * * @param CollectionInterface $collection The collection containing the models. * * @return string */ protected function viewList($collection) { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $groupingInformation = ViewHelpers::getGroupingMode($this->environment); // Set label. $this->setListViewLabel($collection, $groupingInformation); // Generate buttons. foreach ($collection as $i => $objModel) { // Regular buttons - only if not in select mode! if (!$this->isSelectModeActive()) { $previous = $collection->get($i - 1) !== null ? $collection->get($i - 1) : null; $next = $collection->get($i + 1) !== null ? $collection->get($i + 1) : null; /** @var ModelInterface $objModel */ $objModel->setMeta($objModel::OPERATION_BUTTONS, $this->generateButtons($objModel, $previous, $next)); } } // Add template. if (isset($groupingInformation['mode']) && $groupingInformation['mode'] != GroupAndSortingInformationInterface::GROUP_NONE) { $objTemplate = $this->getTemplate('dcbe_general_grouping'); } elseif (isset($groupingInformation['property']) && $groupingInformation['property'] != '') { $objTemplate = $this->getTemplate('dcbe_general_listView_sorting'); } else { $objTemplate = $this->getTemplate('dcbe_general_listView'); } $this->addToTemplate('tableName', strlen($definition->getName()) ? $definition->getName() : 'none', $objTemplate)->addToTemplate('collection', $collection, $objTemplate)->addToTemplate('select', $this->getEnvironment()->getInputProvider()->getParameter('act'), $objTemplate)->addToTemplate('action', ampersand(\Environment::get('request'), true), $objTemplate)->addToTemplate('mode', $groupingInformation ? $groupingInformation['mode'] : null, $objTemplate)->addToTemplate('tableHead', $this->getTableHead(), $objTemplate)->addToTemplate('pdp', '', $objTemplate)->addToTemplate('cdp', $definition->getName(), $objTemplate)->addToTemplate('selectButtons', $this->getSelectButtons(), $objTemplate)->addToTemplate('sortable', (bool) ViewHelpers::getManualSortingProperty($this->environment), $objTemplate)->addToTemplate('showColumns', $this->getViewSection()->getListingConfig()->getShowColumns(), $objTemplate); // Add breadcrumb, if we have one. $strBreadcrumb = $this->breadcrumb(); if ($strBreadcrumb != null) { $this->addToTemplate('breadcrumb', $strBreadcrumb, $objTemplate); } return $objTemplate->parse(); }