Skip to content

Commit

Permalink
fixed error for year type on initial values, moved value type latest …
Browse files Browse the repository at this point in the history
…to abstract type
  • Loading branch information
koertho committed Jun 28, 2018
1 parent addc432 commit 709cd1e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 88 deletions.
1 change: 1 addition & 0 deletions src/Filter/AbstractType.php
Expand Up @@ -22,6 +22,7 @@ abstract class AbstractType
const VALUE_TYPE_SCALAR = 'scalar';
const VALUE_TYPE_ARRAY = 'array';
const VALUE_TYPE_CONTEXTUAL = 'contextual';
const VALUE_TYPE_LATEST = 'latest';

const VALUE_TYPES = [
self::VALUE_TYPE_SCALAR,
Expand Down
2 changes: 2 additions & 0 deletions src/Filter/Type/SortType.php
Expand Up @@ -16,6 +16,8 @@

class SortType extends ChoiceType
{
const TYPE = 'sort';

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Filter/Type/SqlType.php
Expand Up @@ -19,6 +19,8 @@

class SqlType extends AbstractType
{
const TYPE = 'sql';

/**
* {@inheritdoc}
*/
Expand Down
43 changes: 30 additions & 13 deletions src/Filter/Type/YearType.php
Expand Up @@ -23,8 +23,6 @@ class YearType extends ChoiceType
{
const TYPE = 'year';

const VALUE_TYPE_LATEST = 'latest';

const VALUE_TYPES = [
self::VALUE_TYPE_SCALAR,
self::VALUE_TYPE_CONTEXTUAL,
Expand Down Expand Up @@ -69,17 +67,7 @@ public function buildQuery(FilterQueryBuilder $builder, FilterConfigElementModel

if ($element->isInitial) {
if (static::VALUE_TYPE_LATEST === $element->initialValueType) {
$parentElement = $this->config->getElementByValue($element->parentField);
if ($parentElement && !empty($choices = $this->optionsChoice->getCachedChoices([
'element' => $element,
'filter' => $this->config->getFilter(),
'parentElement' => $parentElement,
'latest' => true,
]))) {
$value = array_pop($choices);
} else {
$value = Date::parse('Y');
}
$value = $this->getLatestValue($element);
} else {
$value = $data[$name] ?? $this->getInitialValue($element, $builder->getContextualValues());
}
Expand All @@ -104,6 +92,13 @@ public function buildQuery(FilterQueryBuilder $builder, FilterConfigElementModel
public function getOptions(FilterConfigElementModel $element, FormBuilderInterface $builder)
{
$options = parent::getOptions($element, $builder);
$data = $this->config->getData();
$name = $this->getName($element);
if ($element->addDefaultValue && !isset($data[$name])) {
if (static::VALUE_TYPE_LATEST === $element->defaultValueType) {
$options['data'] = $this->getLatestValue($element);
}
}
$cssClasses = 'year' === $this->getName($element) ? $this->getName($element) : ' year '.$this->getName($element);
if (isset($options['attr']['class'])) {
$options['attr']['class'] .= $cssClasses;
Expand Down Expand Up @@ -175,4 +170,26 @@ public function getYearEnd(int $year)
{
return mktime(23, 59, 59, 12, 31, $year);
}

/**
* @param FilterConfigElementModel $element
*
* @return mixed|string
*/
protected function getLatestValue(FilterConfigElementModel $element)
{
$choiceOptions = [
'element' => $element,
'elements' => $this->config->getElements(),
'filter' => $this->config->getFilter(),
'latest' => true,
];
if (!empty($choices = $this->optionsChoice->getCachedChoices($choiceOptions))) {
$value = array_pop($choices);
} else {
$value = Date::parse('Y');
}

return $value;
}
}
15 changes: 8 additions & 7 deletions src/Resources/contao/dca/tl_filter_config_element.php
Expand Up @@ -151,13 +151,14 @@
'time' => '{general_legend},title,type,isInitial;{config_legend},field,name,customValue,timeWidget,timeFormat,minTime,html5,maxTime;{visualization_legend},customLabel,hideLabel,inputGroup,addPlaceholder;{expert_legend},cssClass;{publish_legend},published;',
\HeimrichHannot\FilterBundle\Filter\Type\DateRangeType::TYPE
=> '{general_legend},title,type,isInitial;{config_legend},startElement,stopElement,name;{visualization_legend},customLabel,hideLabel;{expert_legend},cssClass;{publish_legend},published;',
'sql' => '{general_legend},title,type;{config_legend},whereSql;{publish_legend},published',
\HeimrichHannot\FilterBundle\Filter\Type\SqlType::TYPE
=> '{general_legend},title,type;{config_legend},whereSql;{publish_legend},published',
\HeimrichHannot\FilterBundle\Filter\Type\YearType::TYPE
=> '{general_legend},title,type,isInitial;
{config_legend},field,customOptions,sortOptionValues,customName,customOperator,addDefaultValue,expanded,submitOnChange;{date_legend},minDate,maxDate,dynamicOptions;{visualization_legend},addPlaceholder,customLabel,hideLabel,inputGroup{expert_legend},cssClass;{publish_legend},published;',
=> '{general_legend},title,type,isInitial;{config_legend},field,customOptions,sortOptionValues,customName,customOperator,addDefaultValue,expanded,submitOnChange,minDate,maxDate,dynamicOptions;{visualization_legend},addPlaceholder,customLabel,hideLabel,inputGroup{expert_legend},cssClass;{publish_legend},published;',
\HeimrichHannot\FilterBundle\Filter\Type\AutoItemType::TYPE
=> '{general_legend},title,type;{config_legend},field,operator;{publish_legend},published',
'sort' => '{general_legend},title,type;{config_legend},sortOptions,expanded,submitOnChange;{visualization_legend},addPlaceholder,customLabel,hideLabel;{publish_legend},published',
\HeimrichHannot\FilterBundle\Filter\Type\SortType::TYPE
=> '{general_legend},title,type;{config_legend},sortOptions,expanded,submitOnChange;{visualization_legend},addPlaceholder,customLabel,hideLabel;{publish_legend},published',
],
'subpalettes' => [
'customOptions' => 'options',
Expand All @@ -171,11 +172,11 @@
'customValue' => 'value',
'initialValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR => 'initialValue',
'initialValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'initialValueArray',
'initialValueType_' . \HeimrichHannot\FilterBundle\Filter\Type\YearType::VALUE_TYPE_LATEST => 'parentField',
'initialValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_LATEST => 'parentField',
'addDefaultValue' => 'defaultValueType',
'defaultValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR => 'defaultValue',
'defaultValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'defaultValueArray',
'defaultValueType_' . \HeimrichHannot\FilterBundle\Filter\Type\YearType::VALUE_TYPE_LATEST => 'parentField',
'defaultValueType_' . \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_LATEST => 'parentField',
'addStartAndStop' => 'startField,stopField',
'addParentSelector' => 'parentField',
'inputGroup' => 'inputGroupPrepend,inputGroupAppend',
Expand Down Expand Up @@ -934,7 +935,7 @@
'sql' => "char(1) NOT NULL default ''",
],
'dynamicOptions' => [
'label' => &$GLOBALS['TL_LANG']['tl_filter_config_element']['dynamicValues'],
'label' => &$GLOBALS['TL_LANG']['tl_filter_config_element']['dynamicOptions'],
'exclude' => true,
'inputType' => 'checkbox',
'eval' => ['tl_class' => 'w50', 'submitOnChange' => true],
Expand Down
71 changes: 36 additions & 35 deletions src/Resources/contao/languages/de/tl_filter_config_element.php
Expand Up @@ -110,40 +110,41 @@
*/
$lang['reference'] = [
'type' => [
'text' => 'Text',
'text_concat' => 'Konkatenierter Text',
'textarea' => 'Textarea',
'email' => 'E-Mail',
'integer' => 'Integer',
'money' => 'Geld',
'number' => 'Zahl',
'password' => 'Passwort',
'percent' => 'Prozent',
'search' => 'Suche',
'url' => 'URL',
'range' => 'Spanne (range)',
'tel' => 'Telefon',
'color' => 'Farbe',
'choice' => 'Choice',
'country' => 'Land',
'language' => 'Sprache',
'locale' => 'Region ("locale")',
'parent' => 'Elternentität',
'visible' => 'Veröffentlicht',
'button' => 'Button',
'reset' => 'Reset',
'submit' => 'Submit',
'hidden' => 'Hidden',
'checkbox' => 'Checkbox',
'radio' => 'Radio',
'other' => 'Sonstiges',
'initial' => 'Initial',
'date_time' => 'Datum & Zeit',
'date' => 'Datum',
'time' => 'Zeit',
'date_range' => 'Datumsspanne (date range)',
'sql' => 'SQL',
'sort' => 'Sortierung',
'text' => 'Text',
'text_concat' => 'Konkatenierter Text',
'textarea' => 'Textarea',
\HeimrichHannot\FilterBundle\Filter\Type\EmailType::TYPE => 'E-Mail',
'integer' => 'Integer',
'money' => 'Geld',
'number' => 'Zahl',
'password' => 'Passwort',
'percent' => 'Prozent',
'search' => 'Suche',
'url' => 'URL',
'range' => 'Spanne (range)',
'tel' => 'Telefon',
'color' => 'Farbe',
'choice' => 'Choice',
'country' => 'Land',
'language' => 'Sprache',
'locale' => 'Region ("locale")',
'parent' => 'Elternentität',
'visible' => 'Veröffentlicht',
'button' => 'Button',
'reset' => 'Reset',
'submit' => 'Submit',
'hidden' => 'Hidden',
'checkbox' => 'Checkbox',
'radio' => 'Radio',
'other' => 'Sonstiges',
'initial' => 'Initial',
\HeimrichHannot\FilterBundle\Filter\Type\DateTimeType::TYPE => 'Datum & Zeit',
\HeimrichHannot\FilterBundle\Filter\Type\DateType::TYPE => 'Datum',
'time' => 'Zeit',
\HeimrichHannot\FilterBundle\Filter\Type\DateRangeType::TYPE => 'Datumsspanne (date range)',
\HeimrichHannot\FilterBundle\Filter\Type\SqlType::TYPE => 'SQL',
\HeimrichHannot\FilterBundle\Filter\Type\SortType::TYPE => 'Sortierung',
\HeimrichHannot\FilterBundle\Filter\Type\YearType::TYPE => 'Jahr',
],
'roundingMode' => [
\Symfony\Component\Form\Extension\Core\DataTransformer\IntegerToLocalizedStringTransformer::ROUND_DOWN => 'Abrunden (zu 0 hin)',
Expand All @@ -160,5 +161,5 @@
],
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR => 'Skalar',
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'Array',
\HeimrichHannot\FilterBundle\Filter\Type\YearType::VALUE_TYPE_LATEST => 'Neuste',
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_LATEST => 'Neuste',
];
69 changes: 36 additions & 33 deletions src/Resources/contao/languages/en/tl_filter_config_element.php
Expand Up @@ -101,38 +101,41 @@
*/
$lang['reference'] = [
'type' => [
'text' => 'Text',
'text_concat' => 'Text combined',
'textarea' => 'Textarea',
'email' => 'Email',
'integer' => 'Integer',
'money' => 'Money',
'number' => 'Number',
'password' => 'Password',
'percent' => 'Percent',
'search' => 'Search',
'url' => 'Url',
'range' => 'Range',
'tel' => 'Tel',
'color' => 'Color',
'choice' => 'Choice',
'country' => 'Country',
'language' => 'Language',
'locale' => 'Locale',
'parent' => 'Parent entity',
'published' => 'Published',
'button' => 'Button',
'reset' => 'Reset',
'submit' => 'Submit',
'hidden' => 'Hidden',
'checkbox' => 'Checkbox',
'radio' => 'Radio',
'other' => 'Other',
'initial' => 'Initial',
'date_time' => 'Date & Time',
'date' => 'Date',
'time' => 'Time',
'date_range' => 'Date range',
'text' => 'Text',
'text_concat' => 'Text combined',
'textarea' => 'Textarea',
\HeimrichHannot\FilterBundle\Filter\Type\EmailType::TYPE => 'Email',
'integer' => 'Integer',
'money' => 'Money',
'number' => 'Number',
'password' => 'Password',
'percent' => 'Percent',
'search' => 'Search',
'url' => 'Url',
'range' => 'Range',
'tel' => 'Tel',
'color' => 'Color',
'choice' => 'Choice',
'country' => 'Country',
'language' => 'Language',
'locale' => 'Locale',
'parent' => 'Parent entity',
'published' => 'Published',
'button' => 'Button',
'reset' => 'Reset',
'submit' => 'Submit',
'hidden' => 'Hidden',
'checkbox' => 'Checkbox',
'radio' => 'Radio',
'other' => 'Other',
'initial' => 'Initial',
\HeimrichHannot\FilterBundle\Filter\Type\DateTimeType::TYPE => 'Date & Time',
\HeimrichHannot\FilterBundle\Filter\Type\DateType::TYPE => 'Date',
'time' => 'Time',
\HeimrichHannot\FilterBundle\Filter\Type\DateRangeType::TYPE => 'Date range',
\HeimrichHannot\FilterBundle\Filter\Type\SqlType::TYPE => 'SQL',
\HeimrichHannot\FilterBundle\Filter\Type\SortType::TYPE => 'Sorting',
\HeimrichHannot\FilterBundle\Filter\Type\YearType::TYPE => 'Year',
],
'roundingMode' => [
\Symfony\Component\Form\Extension\Core\DataTransformer\IntegerToLocalizedStringTransformer::ROUND_DOWN => 'Down (round towards zero)',
Expand All @@ -149,5 +152,5 @@
],
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR => 'Scalar',
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY => 'Array',
\HeimrichHannot\FilterBundle\Filter\Type\YearType::VALUE_TYPE_LATEST => 'Latest',
\HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_LATEST => 'Latest',
];

0 comments on commit 709cd1e

Please sign in to comment.