Skip to content

Commit

Permalink
fixed non existing array check before using array_flip
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Kaltofen committed Jul 10, 2018
1 parent 73dcfca commit 8c6ab91
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 119 deletions.
6 changes: 3 additions & 3 deletions src/Backend/Content.php
Expand Up @@ -46,11 +46,11 @@ public function onLoad(DataContainer $dc)
*/
protected function toggleFilterPreselect(ContentModel $content, DataContainer $dc)
{
if ($content->filter < 1) {
if ($content->filterConfig < 1) {
return;
}

$GLOBALS['TL_DCA']['tl_content']['palettes']['filter_preselect'] = str_replace('filter;', 'filter,filterPreselect,filterPreselectNoRedirect;', $GLOBALS['TL_DCA']['tl_content']['palettes']['filter_preselect']);
$GLOBALS['TL_DCA']['tl_content']['palettes']['filter_hyperlink'] = str_replace('filter;', 'filter,filterPreselect;', $GLOBALS['TL_DCA']['tl_content']['palettes']['filter_hyperlink']);
$GLOBALS['TL_DCA']['tl_content']['palettes']['filter_preselect'] = str_replace('filterConfig;', 'filterConfig,filterPreselect,filterPreselectNoRedirect;', $GLOBALS['TL_DCA']['tl_content']['palettes']['filter_preselect']);
$GLOBALS['TL_DCA']['tl_content']['palettes']['filter_hyperlink'] = str_replace('filterConfig;', 'filterConfig,filterPreselect;', $GLOBALS['TL_DCA']['tl_content']['palettes']['filter_hyperlink']);
}
}
18 changes: 13 additions & 5 deletions src/Backend/FilterConfigElement.php
Expand Up @@ -31,8 +31,9 @@ public function __construct(ContaoFrameworkInterface $framework)
public function modifyPalette(DataContainer $dc)
{
if (null === ($filterConfigElement = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk(
'tl_filter_config_element', $dc->id))
) {
'tl_filter_config_element',
$dc->id
))) {
return null;
}

Expand Down Expand Up @@ -63,8 +64,9 @@ public function modifyPalette(DataContainer $dc)
public function prepareChoiceTypes(DataContainer $dc)
{
if (null === ($filterConfigElement = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk(
'tl_filter_config_element', $dc->id))
) {
'tl_filter_config_element',
$dc->id
))) {
return null;
}

Expand Down Expand Up @@ -98,7 +100,13 @@ public function prepareChoiceTypes(DataContainer $dc)
return null;
}

$options = array_flip($choiceType->getChoices($filterConfigElement));
$choices = $choiceType->getChoices($filterConfigElement);

if (!is_array($choices)) {
return null;
}

$options = array_flip($choices);

// prepare scalar fields
$dca['fields']['defaultValue']['inputType'] = 'select';
Expand Down
8 changes: 7 additions & 1 deletion src/Backend/FilterPreselect.php
Expand Up @@ -132,7 +132,13 @@ protected function prepareElementChoices(int $id): array
return [];
}

$options = array_flip($choiceType->getChoices($filterConfigElement));
$choices = $choiceType->getChoices($filterConfigElement);

if (!is_array($choices)) {
return [];
}

$options = array_flip($choices);

$dca['fields']['initialValue']['inputType'] = 'select';
$dca['fields']['initialValue']['options'] = $options;
Expand Down
6 changes: 3 additions & 3 deletions src/Choice/FieldOptionsChoice.php
Expand Up @@ -156,13 +156,13 @@ protected function getWidgetOptions(FilterConfigElementModel $element, array $fi
{
$options = [];

if (!isset($GLOBALS['TL_FFL'][$dca['inputType']])) {
if (!isset($GLOBALS['TL_FFL'][$dca['inputType']]) && (System::getContainer()->get('huh.utils.container')->isBackend() && !isset($GLOBALS['BE_FFL'][$dca['inputType']]))) {
return $options;
}

$class = $GLOBALS['TL_FFL'][$dca['inputType']];
$class = $GLOBALS['TL_FFL'][$dca['inputType']] ?? $GLOBALS['BE_FFL'][$dca['inputType']] ?? null;

if (!class_exists($class)) {
if (null === $class || !class_exists($class)) {
return $options;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ContentElement/ContentFilterHyperlink.php
Expand Up @@ -80,7 +80,7 @@ protected function compile()
*/
protected function getFilterUrl(): ?string
{
if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filter)) || null === ($elements = $filterConfig->getElements())) {
if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filterConfig)) || null === ($elements = $filterConfig->getElements())) {
return null;
}

Expand All @@ -91,7 +91,7 @@ protected function getFilterUrl(): ?string
return null;
}

if (null === ($url = $filterConfig->getPreselectAction(System::getContainer()->get('huh.filter.util.filter_preselect')->getPreselectData($this->filter, $preselections->getModels())))) {
if (null === ($url = $filterConfig->getPreselectAction(System::getContainer()->get('huh.filter.util.filter_preselect')->getPreselectData($this->filterConfig, $preselections->getModels())))) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ContentElement/ContentFilterPreselect.php
Expand Up @@ -35,7 +35,7 @@ public function generate()

$this->preselect();

return parent::generate(); // TODO: Change the autogenerated stub
return parent::generate();
}

/**
Expand All @@ -47,7 +47,7 @@ protected function getWildcard(): array
{
$wildcard = [];

if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filter)) || null === ($elements = $filterConfig->getElements())) {
if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filterConfig)) || null === ($elements = $filterConfig->getElements())) {
return $wildcard;
}

Expand Down Expand Up @@ -78,9 +78,9 @@ protected function preselect()
return;
}

$data = System::getContainer()->get('huh.filter.util.filter_preselect')->getPreselectData($this->filter, $preselections->getModels());
$data = System::getContainer()->get('huh.filter.util.filter_preselect')->getPreselectData($this->filterConfig, $preselections->getModels());

if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filter)) || null === ($elements = $filterConfig->getElements())) {
if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($this->filterConfig)) || null === ($elements = $filterConfig->getElements())) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Resources/contao/dca/tl_content.php
Expand Up @@ -8,15 +8,15 @@
* Palettes
*/
$dc['palettes']['__selector__'][] = 'filter';
$dc['palettes']['filter_preselect'] = '{type_legend},type;{filter_legend},filter;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests;{invisible_legend:hide},invisible,start,stop';
$dc['palettes']['filter_hyperlink'] = '{type_legend},type,headline;{filter_legend},filter;{link_legend},target,linkTitle,embed,titleText,rel;{imglink_legend:hide},useImage;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop';
$dc['palettes']['filter_preselect'] = '{type_legend},type;{filter_legend},filterConfig;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests;{invisible_legend:hide},invisible,start,stop';
$dc['palettes']['filter_hyperlink'] = '{type_legend},type,headline;{filter_legend},filterConfig;{link_legend},target,linkTitle,embed,titleText,rel;{imglink_legend:hide},useImage;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop';

/**
* Fields
*/
$fields = [
'filter' => [
'label' => &$GLOBALS['TL_LANG']['tl_content']['filter'],
'filterConfig' => [
'label' => &$GLOBALS['TL_LANG']['tl_content']['filterConfig'],
'exclude' => true,
'inputType' => 'select',
'foreignKey' => 'tl_filter_config.title',
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/dca/tl_filter_preselect.php
Expand Up @@ -48,7 +48,7 @@
return [];
}

if (null === ($config = System::getContainer()->get('huh.filter.manager')->findById($content->filter))) {
if (null === ($config = System::getContainer()->get('huh.filter.manager')->findById($content->filterConfig))) {
return [];
}

Expand Down
8 changes: 4 additions & 4 deletions src/Resources/contao/languages/de/tl_content.php
Expand Up @@ -3,10 +3,10 @@
$lang = &$GLOBALS['TL_LANG']['tl_content'];

/**
* Fields
*/
$lang['filter'] = ['Filter', 'Wählen Sie eine Filter-Konfiguration aus.'];
$lang['filterPreselect'] = ['Filtervorauswahl', 'Treffen Sie hier eine Filtervorauswahl.'];
* Fields
*/
$lang['filterConfig'] = ['Filter', 'Wählen Sie eine Filter-Konfiguration aus.'];
$lang['filterPreselect'] = ['Filtervorauswahl', 'Treffen Sie hier eine Filtervorauswahl.'];
$lang['filterPreselectNoRedirect'] = ['Keine Weiterleitung nach Filtervorauswahl', 'Aktivieren Sie diese Option, wenn der Nutzer nach der Filtervorauswahl nicht zur Filter-URL weitergeleitet werden soll.'];


Expand Down
63 changes: 63 additions & 0 deletions src/Util/FilterPreselectUtil.php
Expand Up @@ -11,8 +11,10 @@
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\StringUtil;
use Contao\System;
use DeepCopy\Filter\Filter;
use HeimrichHannot\FilterBundle\Filter\AbstractType;
use HeimrichHannot\FilterBundle\Model\FilterPreselectModel;
use HeimrichHannot\FilterBundle\QueryBuilder\FilterQueryBuilder;

class FilterPreselectUtil
{
Expand All @@ -26,6 +28,67 @@ public function __construct(ContaoFrameworkInterface $framework)
$this->framework = $framework;
}

/**
* Get preselected query builder based on given preselection.
*
* @param int $id The filter id
* @param FilterQueryBuilder $queryBuilder The query builder
* @param FilterPreselectModel[] $preselections list of preselections
*
* @return FilterQueryBuilder The modified query builder
*/
public function getPreselectQueryBuilder(int $id, FilterQueryBuilder $queryBuilder, array $preselections): FilterQueryBuilder
{
if (null === ($filterConfig = System::getContainer()->get('huh.filter.manager')->findById($id)) || null === ($elements = $filterConfig->getElements())) {
return $queryBuilder;
}

$types = \System::getContainer()->get('huh.filter.choice.type')->getCachedChoices();

if (!is_array($types) || empty($types)) {
return $queryBuilder;
}

$filterData = $filterConfig->getData();
$preselectionData = $this->getPreselectData($id, $preselections);

$filterConfig->setData($preselectionData);

/** @var FilterPreselectModel $preselection */
foreach ($preselections as $preselection) {
$element = $filterConfig->getElementByValue($preselection->element);

if (!isset($types[$element->type])) {
continue;
}

$config = $types[$element->type];
$class = $config['class'];

if (!class_exists($class)) {
continue;
}

/** @var \HeimrichHannot\FilterBundle\Filter\AbstractType $type */
$type = new $class($filterConfig);

if (!is_subclass_of($type, \HeimrichHannot\FilterBundle\Filter\AbstractType::class)) {
continue;
}

if (null === ($name = $type->getName($element))) {
continue;
}

$type->buildQuery($queryBuilder, $element);
}

// restore filter data
$filterConfig->setData($filterData);

return $queryBuilder;
}

/**
* Get preselected data based on given preselection.
*
Expand Down

0 comments on commit 8c6ab91

Please sign in to comment.