Skip to content

Commit

Permalink
fixed submitOnChange for expanded choice type and added alwaysShow op…
Browse files Browse the repository at this point in the history
…tion to render elements always (reset)
  • Loading branch information
Rico Kaltofen committed Jun 18, 2018
1 parent 4cd8174 commit 5ef6ec0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
25 changes: 16 additions & 9 deletions src/Filter/Type/ChoiceType.php
Expand Up @@ -69,30 +69,37 @@ public function getChoices(FilterConfigElementModel $element)

public function getOptions(FilterConfigElementModel $element, FormBuilderInterface $builder)
{
$options = parent::getOptions($element, $builder);
$options['choices'] = $this->getChoices($element);
$options = parent::getOptions($element, $builder);
$options['choices'] = $this->getChoices($element);
$options['choice_translation_domain'] = false; // disable translation
$options['choices'] = array_filter($options['choices']); // remove empty elements (placeholders)
$options['choices'] = array_filter($options['choices']); // remove empty elements (placeholders)

if (isset($options['attr']['placeholder'])) {
$options['attr']['data-placeholder'] = $options['attr']['placeholder'];
$options['placeholder'] = $options['attr']['placeholder'];
$options['placeholder'] = $options['attr']['placeholder'];
unset($options['attr']['placeholder']);

$options['required'] = false;
$options['required'] = false;
$options['empty_data'] = '';
}

$options['expanded'] = (bool) $element->expanded;
$options['multiple'] = (bool) $element->multiple;
$options['expanded'] = (bool)$element->expanded;
$options['multiple'] = (bool)$element->multiple;

if ($element->submitOnChange) {
$options['attr']['onchange'] = 'this.form.submit()';

if ($options['expanded']) {
$options['choice_attr'] = function ($choiceValue, $key, $value) {
return ['onchange' => 'this.form.submit()'];
};
} else {
$options['attr']['onchange'] = 'this.form.submit()';
}
}

// forgiving array handling
if ($element->addDefaultValue) {
if (isset($options['multiple']) && true === (bool) $options['multiple'] && isset($options['data'])) {
if (isset($options['multiple']) && true === (bool)$options['multiple'] && isset($options['data'])) {
$options['data'] = !is_array($options['data']) ? [$options['data']] : $options['data'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/ResetType.php
Expand Up @@ -29,7 +29,7 @@ public function buildForm(FilterConfigElementModel $element, FormBuilderInterfac
{
$filter = $this->config->getFilter();

if (!$this->config->hasData() || (isset($filter['renderEmpty']) && true === (bool) $filter['renderEmpty'])) {
if (false === (bool)$element->alwaysShow && !$this->config->hasData() || (isset($filter['renderEmpty']) && true === (bool)$filter['renderEmpty'])) {
return;
}

Expand Down
14 changes: 9 additions & 5 deletions src/Model/FilterConfigElementModel.php
Expand Up @@ -88,6 +88,10 @@
* @property bool $addStartAndStop
* @property bool $ignoreFePreview
* @property bool $invertField
* @property string $whereSql
* @property array $sortOptions
* @property bool $submitOnChange
* @property bool $alwaysShow
* @property bool $published
* @property string $start
* @property string $stop
Expand Down Expand Up @@ -142,11 +146,11 @@ class FilterConfigElementModel extends \Model implements \JsonSerializable
*/
public function findPublishedByPid($intId, $intLimit = 0, array $arrOptions = [])
{
$t = static::$strTable;
$t = static::$strTable;
$arrColumns = ["$t.pid=?"];

if (isset($arrOptions['ignoreFePreview']) || !defined('BE_USER_LOGGED_IN') || !BE_USER_LOGGED_IN) {
$time = \Date::floorToMinute();
$time = \Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'".($time + 60)."') AND $t.published='1'";
}

Expand Down Expand Up @@ -180,11 +184,11 @@ public function findPublishedByPid($intId, $intLimit = 0, array $arrOptions = []
*/
public function findPublishedByPidAndTypes($intId, array $types = [], $intLimit = 0, array $arrOptions = [])
{
$t = static::$strTable;
$t = static::$strTable;
$arrColumns = ["$t.pid=?"];

if (isset($arrOptions['ignoreFePreview']) || !defined('BE_USER_LOGGED_IN') || !BE_USER_LOGGED_IN) {
$time = \Date::floorToMinute();
$time = \Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'".($time + 60)."') AND $t.published='1'";
}

Expand Down Expand Up @@ -237,7 +241,7 @@ public function getFormName(FilterConfig $config)
return null;
}

$type = $types[$this->type];
$type = $types[$this->type];
$class = $type['class'];

/** @var AbstractType $type */
Expand Down
9 changes: 8 additions & 1 deletion src/Resources/contao/dca/tl_filter_config_element.php
Expand Up @@ -137,7 +137,7 @@
=> '{general_legend},title,type,isInitial;{config_legend},field,customName,customOperator,addDefaultValue;{expert_legend},cssClass;{publish_legend},published;',
\HeimrichHannot\FilterBundle\Filter\Type\ButtonType::TYPE
=> '{general_legend},title,type;{config_legend},name,label;{expert_legend},cssClass;{publish_legend},published;',
'reset' => '{general_legend},title,type;{config_legend},customName;{visualization_legend},customLabel,hideLabel;{expert_legend},cssClass;{publish_legend},published;',
'reset' => '{general_legend},title,type;{config_legend},customName,alwaysShow;{visualization_legend},customLabel,hideLabel;{expert_legend},cssClass;{publish_legend},published;',
\HeimrichHannot\FilterBundle\Filter\Type\SubmitType::TYPE
=> '{general_legend},title,type;{config_legend},customName;{visualization_legend},customLabel,hideLabel;{expert_legend},cssClass;{publish_legend},published;',
\HeimrichHannot\FilterBundle\Filter\Type\CheckboxType::TYPE
Expand Down Expand Up @@ -919,5 +919,12 @@
'eval' => ['tl_class' => 'w50'],
'sql' => "char(1) NOT NULL default ''",
],
'alwaysShow' => [
'label' => &$GLOBALS['TL_LANG']['tl_filter_config_element']['alwaysShow'],
'exclude' => true,
'inputType' => 'checkbox',
'eval' => ['tl_class' => 'w50'],
'sql' => "char(1) NOT NULL default ''",
]
],
];
Expand Up @@ -73,7 +73,9 @@
$lang['start'] = ['Anzeigen ab', 'Filterelement erst ab diesem Tag auf der Webseite anzeigen.'];
$lang['stop'] = ['Anzeigen bis', 'Filterelement nur bis zu diesem Tag auf der Webseite anzeigen.'];
$lang['whereSql'] = ['Zusätzliches WHERE-SQL', 'Geben Sie hier SQL ein, welches dem WHERE-Statement hinzugefügt wird.'];
$lang['submitOnChange'] = ['Submit on Change', 'Wählen Sie diese Option, wenn der Nutzer das Formular bei Änderung abschicken soll.'];
$lang['submitOnChange'] = ['Formular bei Änderung absenden (submitOnChange)', 'Wählen Sie diese Option, wenn der Nutzer das Formular bei Änderung abschicken soll.'];
$lang['alwaysShow'] = ['Immer anzeigen', 'Das Filterelement immer anzeigen.'];


// sort
$lang['sortOptions_class'] = ['Sortier-Klasse', 'Wählen Sie hier die Sortierklasse aus.'];
Expand Down
13 changes: 8 additions & 5 deletions src/Resources/contao/languages/en/tl_filter_config_element.php
Expand Up @@ -67,10 +67,13 @@
$lang['startField'] = ['Start field', 'Select a field here.'];
$lang['stopField'] = ['Stop field', 'Select a field here.'];
$lang['cssClass'] = ['CSS class', 'Here you can enter one or more classes.'];
$lang['published'] = ['Publish Filterelement', 'Make the Filterelement publicly visible on the website.'];
$lang['start'] = ['Show from', 'Do not publish the Filterelement on the website before this date.'];
$lang['stop'] = ['Show until', 'Unpublish the Filterelement on the website after this date.'];
$lang['published'] = ['Publish filter element', 'Make the filter element publicly visible on the website.'];
$lang['start'] = ['Show from', 'Do not publish the filter element on the website before this date.'];
$lang['stop'] = ['Show until', 'Unpublish the filter element on the website after this date.'];
$lang['tstamp'] = ['Revision date', ''];
$lang['whereSql'] = ['Additional WHERE-SQL', 'Enter SQL, which will be added to the WHERE statement.'];
$lang['submitOnChange'] = ['Submit form on change (submitOnChange)', 'Select this option if you want the user to submit the form on change.'];
$lang['alwaysShow'] = ['Show always', 'Always show the filter element.'];

/**
* Legends
Expand Down Expand Up @@ -141,8 +144,8 @@
],
'percentType' => [
'fractional' => 'fractional (e.g .55)',
'integer' => 'integer (e.g. 55)'
'integer' => 'integer (e.g. 55)',
],
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR => 'Scalar',
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'Array'
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'Array',
];

0 comments on commit 5ef6ec0

Please sign in to comment.