Skip to content

Commit

Permalink
Add condition to filter multiple characters
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Sep 2, 2020
2 parents 9a429d7 + 8a6927b commit d6f61c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
27 changes: 20 additions & 7 deletions src/FilterSetting/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,27 @@ protected function isActiveFrontendFilterValue($arrWidget, $arrFilterUrl, $strKe
*/
protected function getFrontendFilterValue($arrWidget, $arrFilterUrl, $strKeyOption)
{
$arrCurrent = !empty($arrWidget['value']) ? \explode(',', $arrWidget['value']) : [];
if ($this->get('filtermultiple')) {
$arrCurrent = !empty($arrWidget['value']) ? \explode(',', $arrWidget['value']) : [];

// Toggle if active.
// toggle if active.
if ($this->isActiveFrontendFilterValue($arrWidget, $arrFilterUrl, $strKeyOption)) {
$arrCurrent = \array_diff($arrCurrent, [$strKeyOption]);
} else {
$arrCurrent[] = $strKeyOption;
}

return \implode(',', $arrCurrent);
}

// toggle if active.
if ($this->isActiveFrontendFilterValue($arrWidget, $arrFilterUrl, $strKeyOption)) {
$arrCurrent = \array_diff($arrCurrent, [$strKeyOption]);
$current = '';
} else {
$arrCurrent[] = $strKeyOption;
$current = $strKeyOption;
}

return \implode(',', $arrCurrent);
return $current;
}

/**
Expand Down Expand Up @@ -152,10 +163,12 @@ public function prepareRules(IMetaModelFilter $objFilter, $arrFilterUrl)
$objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
$strParamName = $this->getParamName();
$strParamValue = $arrFilterUrl[$strParamName];
$strWhat = $strParamValue . '%';

if ($objAttribute && $strParamName && $strParamValue) {
$arrIds = $objAttribute->searchFor($strWhat);
$arrIds = [];
foreach (\explode(',', $strParamValue) as $paramKey) {
$arrIds = array_merge($arrIds, $objAttribute->searchFor($paramKey . '%'));
}
$objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList($arrIds));
return;
}
Expand Down
29 changes: 26 additions & 3 deletions src/Resources/contao/dca/tl_metamodel_filtersetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'template',
'shownumbers',
'hideempty',
'filtermultiple',
'onlypossible',
'skipfilteroptions'
],
Expand All @@ -43,7 +44,7 @@
'default' => true,
'inputType' => 'checkbox',
'eval' => [
'tl_class' => 'clr w50',
'tl_class' => 'clr w50 cbx m12',
],
'sql' => 'char(1) NOT NULL default \'1\''
];
Expand All @@ -54,7 +55,18 @@
'default' => true,
'inputType' => 'checkbox',
'eval' => [
'tl_class' => 'w50',
'tl_class' => 'w50 cbx m12',
],
'sql' => 'char(1) NOT NULL default \'1\''
];

$GLOBALS['TL_DCA']['tl_metamodel_filtersetting']['fields']['filtermultiple'] = [
'label' => &$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['allowmultiple'],
'exclude' => true,
'default' => false,
'inputType' => 'checkbox',
'eval' => [
'tl_class' => 'w50 cbx',
],
'sql' => 'char(1) NOT NULL default \'1\''
];
Expand All @@ -65,7 +77,18 @@
'default' => true,
'inputType' => 'checkbox',
'eval' => [
'tl_class' => 'w50',
'tl_class' => 'clr w50 m12',
],
'sql' => 'char(1) NOT NULL default \'1\''
];

$GLOBALS['TL_DCA']['tl_metamodel_filtersetting']['fields']['skipfilteroptions'] = [
'label' => &$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['skipfilteroptions'],
'exclude' => true,
'default' => false,
'inputType' => 'checkbox',
'eval' => [
'tl_class' => 'w50 m12',
],
'sql' => 'char(1) NOT NULL default \'1\''
];
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/filter_register.
*
* (c) 2012-2018 The MetaModels team.
* (c) 2012-2020 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -15,7 +15,8 @@
* @author Christian Schiffler <[email protected]>
* @author Stefan Heimes <[email protected]>
* @author Sven Baumann <[email protected]>
* @copyright 2012-2018 The MetaModels team.
* @author Ingolf Steinhardt <[email protected]>
* @copyright 2012-2020 The MetaModels team.
* @license https://github.com/MetaModels/filter_register/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand All @@ -30,3 +31,6 @@
$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['hideempty'][0] = 'Hide empty values';
$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['hideempty'][1] =
'Here you can choose if empty values should hide or not in the list.';
$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['filtermultiple'][0] = 'Filter multiple characters';
$GLOBALS['TL_LANG']['tl_metamodel_filtersetting']['filtermultiple'][1] =
'Here you can allow filtering by several characters.';

0 comments on commit d6f61c3

Please sign in to comment.