Skip to content

Commit

Permalink
see cl 1.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed Oct 16, 2019
1 parent 3798edf commit cf1a72f
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 190 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.9.0] - 2019-10-16

#### Changed
- list preselect to also take into account the filter and the sorting of the associated list config
- moved getCurrentSorting to ListManager
- made more select fields in backend "chosen"s

## [1.8.4] - 2019-10-01

#### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/ContentElement/ContentListPreselect.php
Expand Up @@ -268,7 +268,7 @@ protected function getWildcard(): array
*/
protected function getListTitle(): string
{
if (null === ($listConfig = System::getContainer()->get('huh.list.list-config-registry')->getComputedListConfig((int) $this->getModel()->listConfig))) {
if (null === ($listConfig = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk('tl_list_config', $this->getModel()->listConfig))) {
return '';
}

Expand Down
21 changes: 19 additions & 2 deletions src/DataContainer/ContentContainer.php
Expand Up @@ -72,6 +72,21 @@ public function onLoad(DataContainer $dc)
$this->toggleFilterPreselect($content, $dc);
}

public function getListPreselectListConfigs(DataContainer $dc): array
{
$options = [];

if (null !== ($listConfig = $this->modelUtil->findModelInstancesBy('tl_list_config', ['tl_list_config.hideForListPreselect!=?'], [true]))) {
while ($listConfig->next()) {
$options[$listConfig->id] = $listConfig->title;
}
}

asort($options);

return $options;
}

/**
* Get list of preselect choices.
*
Expand Down Expand Up @@ -128,13 +143,15 @@ public function getListPreselectChoices(DataContainer $dc): array
/** @var FilterPreselectModel $preselections */
$preselections = $this->framework->createInstance(FilterPreselectModel::class);

$queryBuilder = $manager->getFilterConfig()->getQueryBuilder()->resetQueryParts();
$queryBuilder = $manager->getFilterConfig()->getQueryBuilder();

if ($preselections = $preselections->findPublishedByPidAndTableAndField($content->id, 'tl_content', 'filterPreselect')) {
$queryBuilder = $this->filterPreselectUtil->getPreselectQueryBuilder($filterConfig->getId(), $queryBuilder, $preselections->getModels());
}

$items = $queryBuilder->select($fields)->from($filter->dataContainer)->execute()->fetchAll();
$manager->applyListConfigSortingToQueryBuilder($queryBuilder);

$items = $queryBuilder->select($fields)->execute()->fetchAll();

$total = \count($items);

Expand Down
66 changes: 2 additions & 64 deletions src/Lists/DefaultList.php
Expand Up @@ -436,7 +436,7 @@ public function applyListConfigToQueryBuilder(int $totalCount, FilterQueryBuilde
return;
}
// sorting
$currentSorting = $this->getCurrentSorting();
$currentSorting = $this->_manager->getCurrentSorting();

if (ListConfig::SORTING_MODE_RANDOM == $currentSorting['order']) {
$randomSeed = $this->_manager->getRequest()->getGet(RandomPagination::PARAM_RANDOM) ?: rand(1, 500);
Expand Down Expand Up @@ -520,7 +520,7 @@ public function splitResults($offset, $total, $limit, $randomSeed = null): ?arra
public function generateTableHeader(): array
{
$headerFields = [];
$currentSorting = $this->getCurrentSorting();
$currentSorting = $this->_manager->getCurrentSorting();
$listConfig = $this->_manager->getListConfig();
$filter = (object) $this->_manager->getFilterConfig()->getFilter();
$urlUtil = System::getContainer()->get('huh.utils.url');
Expand Down Expand Up @@ -660,68 +660,6 @@ public function shareTokenExpiredOrEmpty($entity, $now)
return !$shareToken || !$entity->shareTokenTime || ($entity->shareTokenTime > $now + $interval);
}

/**
* {@inheritdoc}
*/
public function getCurrentSorting(): array
{
$listConfig = $this->_manager->getListConfig();
$filter = (object) $this->_manager->getFilterConfig();
$request = $this->_manager->getRequest();
$sortingAllowed = $listConfig->isTableList && $listConfig->hasHeader && $listConfig->sortingHeader;

// GET parameter
if ($sortingAllowed && ($orderField = $request->getGet('order')) && ($sort = $request->getGet('sort'))) {
// anti sql injection: check if field exists
/** @var Database $db */
$db = $this->_manager->getFramework()->getAdapter(Database::class);

if ($db->getInstance()->fieldExists($orderField, $filter->dataContainer)
&& \in_array($sort, ListConfig::SORTING_DIRECTIONS)) {
$currentSorting = [
'order' => $request->getGet('order'),
'sort' => $request->getGet('sort'),
];
} else {
$currentSorting = [];
}
} // initial
else {
switch ($listConfig->sortingMode) {
case ListConfig::SORTING_MODE_TEXT:
$currentSorting = [
'order' => $listConfig->sortingText,
];

break;

case ListConfig::SORTING_MODE_RANDOM:
$currentSorting = [
'order' => ListConfig::SORTING_MODE_RANDOM,
];

break;

case ListConfig::SORTING_MODE_MANUAL:
$currentSorting = [
'order' => ListConfig::SORTING_MODE_MANUAL,
];

break;

default:
$currentSorting = [
'order' => $listConfig->sortingField,
'sort' => $listConfig->sortingDirection,
];

break;
}
}

return $currentSorting;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Lists/ListInterface.php
Expand Up @@ -69,11 +69,6 @@ public function shareTokenExpiredOrEmpty($entity, $now);
*/
public function generateTableHeader(): ?array;

/**
* @return array
*/
public function getCurrentSorting(): ?array;

/**
* Applies the list config to the query builder.
*
Expand Down
84 changes: 84 additions & 0 deletions src/Manager/ListManager.php
Expand Up @@ -11,11 +11,15 @@
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Database;
use Contao\DataContainer;
use Contao\StringUtil;
use Contao\System;
use HeimrichHannot\FilterBundle\Config\FilterConfig;
use HeimrichHannot\FilterBundle\Manager\FilterManager;
use HeimrichHannot\FilterBundle\QueryBuilder\FilterQueryBuilder;
use HeimrichHannot\ListBundle\Backend\ListConfig;
use HeimrichHannot\ListBundle\Lists\ListInterface;
use HeimrichHannot\ListBundle\Model\ListConfigModel;
use HeimrichHannot\ListBundle\Pagination\RandomPagination;
use HeimrichHannot\ListBundle\Registry\ListConfigElementRegistry;
use HeimrichHannot\ListBundle\Registry\ListConfigRegistry;
use HeimrichHannot\RequestBundle\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -323,6 +327,86 @@ public function getListTemplateByName(string $name)
return System::getContainer()->get('huh.utils.template')->getTemplate($name);
}

public function getCurrentSorting(): array
{
$listConfig = $this->getListConfig();
$filter = (object) $this->getFilterConfig();
$request = $this->getRequest();
$sortingAllowed = $listConfig->isTableList && $listConfig->hasHeader && $listConfig->sortingHeader;

// GET parameter
if ($sortingAllowed && ($orderField = $request->getGet('order')) && ($sort = $request->getGet('sort'))) {
// anti sql injection: check if field exists
/** @var Database $db */
$db = $this->getFramework()->getAdapter(Database::class);

if ($db->getInstance()->fieldExists($orderField, $filter->dataContainer)
&& \in_array($sort, ListConfig::SORTING_DIRECTIONS)) {
$currentSorting = [
'order' => $request->getGet('order'),
'sort' => $request->getGet('sort'),
];
} else {
$currentSorting = [];
}
} // initial
else {
switch ($listConfig->sortingMode) {
case ListConfig::SORTING_MODE_TEXT:
$currentSorting = [
'order' => $listConfig->sortingText,
];

break;

case ListConfig::SORTING_MODE_RANDOM:
$currentSorting = [
'order' => ListConfig::SORTING_MODE_RANDOM,
];

break;

case ListConfig::SORTING_MODE_MANUAL:
$currentSorting = [
'order' => ListConfig::SORTING_MODE_MANUAL,
];

break;

default:
$currentSorting = [
'order' => $listConfig->sortingField,
'sort' => $listConfig->sortingDirection,
];

break;
}
}

return $currentSorting;
}

public function applyListConfigSortingToQueryBuilder(FilterQueryBuilder $queryBuilder)
{
$listConfig = $this->getListConfig();
$currentSorting = $this->getCurrentSorting();

if (ListConfig::SORTING_MODE_RANDOM == $currentSorting['order']) {
$randomSeed = $this->getRequest()->getGet(RandomPagination::PARAM_RANDOM) ?: rand(1, 500);
$queryBuilder->orderBy('RAND("'.(int) $randomSeed.'")');
} elseif (ListConfig::SORTING_MODE_MANUAL == $currentSorting['order']) {
$sortingItems = StringUtil::deserialize($listConfig->sortingItems, true);

if (!empty($sortingItems)) {
$queryBuilder->orderBy('FIELD(id,'.implode(',', $sortingItems).')', ' ');
}
} else {
if (!empty($currentSorting)) {
$queryBuilder->orderBy($currentSorting['order'], $currentSorting['sort']);
}
}
}

/**
* {@inheritdoc}
*/
Expand Down
13 changes: 6 additions & 7 deletions src/Resources/contao/dca/tl_content.php
Expand Up @@ -15,13 +15,12 @@
*/
$fields = [
'listConfig' => [
'label' => &$GLOBALS['TL_LANG']['tl_content']['listConfig'],
'exclude' => true,
'inputType' => 'select',
'foreignKey' => 'tl_list_config.title',
'relation' => ['type' => 'belongsTo', 'load' => 'lazy'],
'eval' => ['tl_class' => 'w50 clr', 'mandatory' => true, 'submitOnChange' => true, 'includeBlankOption' => true],
'sql' => "int(10) NOT NULL default '0'",
'label' => &$GLOBALS['TL_LANG']['tl_content']['listConfig'],
'exclude' => true,
'inputType' => 'select',
'options_callback' => [\HeimrichHannot\ListBundle\DataContainer\ContentContainer::class, 'getListPreselectListConfigs'],
'eval' => ['tl_class' => 'w50 clr', 'mandatory' => true, 'submitOnChange' => true, 'includeBlankOption' => true, 'chosen' => true],
'sql' => "int(10) NOT NULL default '0'",
],
'listPreselect' => [
'label' => &$GLOBALS['TL_LANG']['tl_content']['listPreselect'],
Expand Down

0 comments on commit cf1a72f

Please sign in to comment.