/** * Add the filter for the item with the given id from the parent data provider to the given config. * * @param ModelIdInterface $idParent The id of the parent item. * * @param ConfigInterface $config The config to add the filter to. * * @return ConfigInterface * * @throws DcGeneralRuntimeException When the parent item is not found. */ private function addParentFilter($idParent, $config) { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $providerName = $definition->getBasicDefinition()->getDataProvider(); $parentProviderName = $idParent->getDataProviderName(); $parentProvider = $environment->getDataProvider($parentProviderName); if ($definition->getBasicDefinition()->getParentDataProvider() !== $parentProviderName) { throw new DcGeneralRuntimeException('Unexpected parent provider ' . $parentProviderName . ' (expected ' . $definition->getBasicDefinition()->getParentDataProvider() . ')'); } if ($parentProvider) { $objParent = $parentProvider->fetch($parentProvider->getEmptyConfig()->setId($idParent->getId())); if (!$objParent) { throw new DcGeneralRuntimeException('Parent item ' . $idParent->getSerialized() . ' not found in ' . $parentProviderName); } $condition = $definition->getModelRelationshipDefinition()->getChildCondition($parentProviderName, $providerName); if ($condition) { $arrBaseFilter = $config->getFilter(); $arrFilter = $condition->getFilter($objParent); if ($arrBaseFilter) { $arrFilter = array_merge($arrBaseFilter, $arrFilter); } $config->setFilter(array(array('operation' => 'AND', 'children' => $arrFilter))); } } return $config; }
/** * Prepare a filter and return it. * * @return IFilter */ public function build() { $filter = $this->getMetaModel()->getEmptyFilter(); if ($this->configuration->getFilter()) { $this->calculateSubfilter(array('operation' => 'AND', 'children' => $this->configuration->getFilter()), $filter); } return $filter; }
/** * Fetch a single record by id. * * This data provider only supports retrieving by id so use $objConfig->setId() to populate the config with an Id. * * @param ConfigInterface $objConfig The configuration to use. * * @return ModelInterface * * @throws DcGeneralException If config object does not contain an Id. */ public function fetch(ConfigInterface $objConfig) { if (!$objConfig->getId()) { throw new DcGeneralException('Error, no id passed, TableRowsAsRecordsDriver is only intended for edit mode.', 1); } $strQuery = sprintf('SELECT %s FROM %s WHERE %s=?', $this->buildFieldQuery($objConfig), $this->strSource, $this->strGroupCol); if ($this->strSortCol) { $strQuery .= ' ORDER BY ' . $this->strSortCol; } $objResult = $this->objDatabase->prepare($strQuery)->execute($objConfig->getId()); $objModel = $this->getEmptyModel(); if ($objResult->numRows) { $objModel->setPropertyRaw('rows', $objResult->fetchAllAssoc()); } $objModel->setID($objConfig->getId()); return $objModel; }
/** * Add the parent filtering to the given data config if any defined. * * @param ConfigInterface $config The data config. * * @return void */ protected function addParentFilter($config) { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $basicDefinition = $definition->getBasicDefinition(); $relationships = $definition->getModelRelationshipDefinition(); if (!$basicDefinition->getParentDataProvider()) { return; } // Apply parent filtering, do this only for root elements. if ($objParentCondition = $relationships->getChildCondition($basicDefinition->getParentDataProvider(), $basicDefinition->getRootDataProvider())) { $arrBaseFilter = $config->getFilter(); $arrFilter = $objParentCondition->getFilter($this->loadParentModel()); if ($arrBaseFilter) { $arrFilter = array_merge($arrBaseFilter, $arrFilter); } $config->setFilter($arrFilter); } }
/** * Add the parent filtering to the given data config if any defined. * * @param ConfigInterface $config The data config. * * @param ModelInterface $parentModel The parent model. * * @return void * * @throws \RuntimeException When the parent provider does not match. */ private function addParentFilter(ConfigInterface $config, ModelInterface $parentModel) { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $basicDefinition = $definition->getBasicDefinition(); $relationships = $definition->getModelRelationshipDefinition(); if ($basicDefinition->getParentDataProvider() !== $parentModel->getProviderName()) { throw new \RuntimeException(sprintf('Parent provider mismatch: %s vs. %s', $basicDefinition->getParentDataProvider(), $parentModel->getProviderName())); } if (!$basicDefinition->getParentDataProvider()) { return; } // Apply parent filtering, do this only for root elements. if ($parentCondition = $relationships->getChildCondition($basicDefinition->getParentDataProvider(), $basicDefinition->getRootDataProvider())) { $baseFilter = $config->getFilter(); $filter = $parentCondition->getFilter($parentModel); if ($baseFilter) { $filter = array_merge($baseFilter, $filter); } $config->setFilter($filter); } }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { $input = $this->getInputProvider(); $value = null; $field = null; if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_field')) { $field = $input->getValue('tl_field'); $value = $input->getValue('tl_value'); $this->setPersistent($field, $value); } elseif ($input->hasPersistentValue('search')) { $persistent = $this->getPersistent(); if ($persistent) { $field = $persistent['field']; $value = $persistent['value']; } } $this->setSelectedProperty($field); $this->setValue($value); if (!($this->getSelectedProperty() && $this->getValue())) { return; } $arrCurrent = $objConfig->getFilter(); if (!is_array($arrCurrent)) { $arrCurrent = array(); } $objConfig->setFilter(array_merge_recursive($arrCurrent, array(array('operation' => 'AND', 'children' => array(array('operation' => 'LIKE', 'property' => $this->getSelectedProperty(), 'value' => sprintf('*%s*', $this->getValue()))))))); }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { if (is_null($objElement)) { $objTempConfig = $this->getOtherConfig(); $arrTotal = $this->getEnvironment()->getDataProvider()->fetchAll($objTempConfig->setIdOnly(true)); $this->intTotal = $arrTotal ? count($arrTotal) : 0; $offset = 0; // TODO: we need to determine the perPage some better way. $amount = $GLOBALS['TL_CONFIG']['resultsPerPage']; $input = $this->getInputProvider(); if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_limit')) { $limit = explode(',', $input->getValue('tl_limit')); $offset = $limit[0]; $amount = $limit[1]; $this->setPersistent($offset, $amount); } $persistent = $this->getPersistent(); if ($persistent) { $offset = $persistent['offset']; $amount = $persistent['amount']; // Hotfix the offset - we also might want to store it persistent. // Another way would be to always stick on the "last" page when we hit the upper limit. if ($offset > $this->intTotal) { $offset = 0; } } if (!is_null($offset)) { $this->setOffset($offset); $this->setAmount($amount); } } $objConfig->setStart($this->getOffset()); $objConfig->setAmount($this->getAmount()); }
/** * {@inheritdoc} */ public function prepare(Config $config) { $config->setFilter($this->conditions); return $this; }
/** * {@inheritDoc} * * @throws DcGeneralRuntimeException if improper values have been passed (i.e. not exactly one field requested). */ public function getFilterOptions(ConfigInterface $objConfig) { $arrProperties = $objConfig->getFields(); $strProperty = $arrProperties[0]; if (count($arrProperties) != 1) { throw new DcGeneralRuntimeException('objConfig must contain exactly one property to be retrieved.'); } $arrParams = array(); $objValues = $this->objDatabase->prepare(sprintf('SELECT DISTINCT(%s) FROM %s %s', $strProperty, $this->strSource, $this->buildWhereQuery($objConfig, $arrParams)))->execute($arrParams); $objCollection = $this->getEmptyFilterOptionCollection(); while ($objValues->next()) { $objCollection->add($objValues->{$strProperty}, $objValues->{$strProperty}); } return $objCollection; }
/** * Initialize the sorting from the panel. Fallback to default sorting if nothing given. * * @param PanelContainerInterface $panel The current panel. * * @param ConfigInterface $dataConfig The current config. * * @param ListingConfigInterface $listingConfig The listing config. * * @return void */ public static function initializeSorting($panel, $dataConfig, $listingConfig) { // Store default sorting start initializing the panel with an empty sorting. $sorting = $dataConfig->getSorting(); $dataConfig->setSorting(array()); $panel->initialize($dataConfig); // Restore default sorting if panel did not set any. if ($sorting && !$dataConfig->getSorting()) { $dataConfig->setSorting($sorting); } // Initialize sorting if not present yet. if (!$dataConfig->getSorting() && $listingConfig->getGroupAndSortingDefinition()->hasDefault()) { $newSorting = array(); foreach ($listingConfig->getGroupAndSortingDefinition()->getDefault() as $information) { /** @var GroupAndSortingInformationInterface $information */ $newSorting[$information->getProperty()] = strtoupper($information->getSortingMode()); } $dataConfig->setSorting($newSorting); } }
/** * {@inheritdoc} */ public function getFilterOptions(ConfigInterface $config) { $properties = $config->getFields(); $property = $properties[0]; if (count($properties) != 1) { throw new \Exception('Config must contain exactly one property to be retrieved.'); } $entityManager = $this->getEntityManager(); $queryBuilder = $entityManager->createQueryBuilder(); $values = $queryBuilder->select('DISTINCT e.' . $property)->from($this->entityClassName, 'e')->orderBy('e.' . $property)->getQuery()->getResult(); $collection = new DefaultFilterOptionCollection(); if ($values) { foreach ($values as $value) { $collection->add($value[$property], $value[$property]); } } return $collection; }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { $this->updateValue(); if ($this->getPropertyName() && $this->getValue() && $objElement !== $this) { $arrCurrent = $objConfig->getFilter(); if (!is_array($arrCurrent)) { $arrCurrent = array(); } $objConfig->setFilter(FilterBuilder::fromArray($arrCurrent)->getFilter()->andPropertyEquals($this->getPropertyName(), $this->getValue())->getAllAsArray()); } // Finally load the filter options. if ($objElement === null) { $this->loadFilterOptions(); } }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { if (is_null($objElement)) { $input = $this->getInputProvider(); $value = null; if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_sort')) { $value = $input->getValue('tl_sort'); $this->setPersistent($value); $this->setSelected($this->getPersistent()); } else { $this->setSelected($this->getPersistent()); } } $current = $objConfig->getSorting(); if (!is_array($current)) { $current = array(); } $arrSecondOrder = $this->getAdditionalSorting(); if (!$this->getSelected()) { if ($arrSecondOrder) { $filtered = array_intersect(array_keys($arrSecondOrder), $this->getPropertyNames()); $this->setSelected($filtered[0]); } // Still nothing selected? - use the first. if (!$this->getSelected()) { $all = $this->getPropertyNames(); $this->setSelected($all[0]); } } if ($this->getSelected()) { $current[$this->getSelected()] = $this->flagToDirection($this->getFlag()); } $objConfig->setSorting($current); }
/** * Fetch a variant of a single record by id. * * @param ConfigInterface $objConfig The config holding the id of the base model. * * @return null|ModelInterface */ public function createVariant(ConfigInterface $objConfig) { $objItem = $this->getMetaModel()->findById($objConfig->getId())->varCopy(); if (!$objItem) { return null; } $model = new Model($objItem); $model->setMeta($model::IS_CHANGED, true); return $model; }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { if ($objElement === null) { $this->calculateTotal(); $offset = 0; $amount = $this->getItemsPerPage(); $input = $this->getInputProvider(); if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_limit')) { $limit = explode(',', $input->getValue('tl_limit')); $offset = $limit[0]; $amount = $limit[1]; $this->setPersistent($offset, $amount); } $persistent = $this->getPersistent(); if ($persistent) { $offset = $persistent['offset']; $amount = $persistent['amount']; // Hotfix the offset - we also might want to store it persistent. // Another way would be to always stick on the "last" page when we hit the upper limit. if ($offset > $this->intTotal) { $offset = 0; } } if ($offset !== null) { $this->setOffset($offset); $this->setAmount($amount); } } $objConfig->setStart($this->getOffset()); $objConfig->setAmount($this->getAmount()); }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { if ($objElement === null) { $input = $this->getInputProvider(); $value = null; if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_sort')) { $value = $input->getValue('tl_sort'); $this->setPersistent($value); } $persistent = $this->getPersistent(); if (!$persistent) { if ($this->getGroupAndSortingDefinition()->hasDefault()) { $persistent = $this->getGroupAndSortingDefinition()->getDefault()->getName(); } $this->setPersistent($value); } $this->setSelected($persistent); } $current = $objConfig->getSorting(); if (!is_array($current)) { $current = array(); } if ($this->getSelectedDefinition()) { foreach ($this->getSelectedDefinition() as $information) { $current[$information->getProperty()] = $information->getSortingMode(); } } $objConfig->setSorting($current); }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { $input = $this->getInputProvider(); $value = null; if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue($this->getPropertyName())) { $value = $input->getValue($this->getPropertyName()); $this->setPersistent($value); } if ($input->hasPersistentValue('filter')) { $persistent = $this->getPersistent(); $value = $persistent; } if (!is_null($value)) { $this->setValue($value); } if ($this->getPropertyName() && $this->getValue() && $objElement !== $this) { $arrCurrent = $objConfig->getFilter(); if (!is_array($arrCurrent)) { $arrCurrent = array(); } $objConfig->setFilter(array_merge_recursive($arrCurrent, array(array('operation' => 'AND', 'children' => array(array('operation' => '=', 'property' => $this->getPropertyName(), 'value' => $this->getValue())))))); } // Finally load the filter options. if (is_null($objElement)) { $objTempConfig = $this->getOtherConfig(); $objTempConfig->setFields(array($this->getPropertyName())); $objFilterOptions = $this->getEnvironment()->getDataProvider()->getFilterOptions($objTempConfig); $arrOptions = array(); /** @var ModelInterface $objOption */ foreach ($objFilterOptions as $filterKey => $filterValue) { $arrOptions[(string) $filterKey] = $filterValue; } $this->arrfilterOptions = $arrOptions; } }