Skip to content

Commit

Permalink
fixed YearType, added DateChoiceType, fixed code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed Nov 14, 2018
1 parent 2386916 commit 9b7914d
Show file tree
Hide file tree
Showing 32 changed files with 582 additions and 124 deletions.
10 changes: 7 additions & 3 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ $finder = PhpCsFixer\Finder::create()
->exclude('Resources')
->exclude('Fixtures')
->in([__DIR__.'/src', __DIR__.'/tests'])
->exclude('vendor')
->exclude('docs')
->in([__DIR__])
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'psr0' => false,
'strict_comparison' => true,
'strict_param' => true,
'strict_comparison' => false,
'strict_param' => false,
'array_syntax' => ['syntax' => 'short'],
'heredoc_to_nowdoc' => true,
'header_comment' => ['header' => $header],
Expand All @@ -36,8 +39,9 @@ return PhpCsFixer\Config::create()
'expectedException',
'expectedExceptionMessage',
],
'blank_line_before_statement' => ['statements' => ['break', 'case', 'continue', 'declare', 'default', 'die', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield']]
])
->setFinder($finder)
->setRiskyAllowed(true)
->setUsingCache(false)
;
;
6 changes: 6 additions & 0 deletions src/Backend/FilterConfigElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function modifyPalette(DataContainer $dc)
foreach ($config['filter']['types'] as $type) {
if (isset($type['name']) && $type['name'] === $filterConfigElement->type && isset($type['type'])) {
$foundType = $type['type'];

break;
}
}
Expand Down Expand Up @@ -81,6 +82,7 @@ public function prepareChoiceTypes(DataContainer $dc)
foreach ($config['filter']['types'] as $type) {
if (isset($type['name']) && $type['name'] === $filterConfigElement->type && isset($type['class'])) {
$class = $type['class'];

break;
}
}
Expand Down Expand Up @@ -162,6 +164,7 @@ public function checkPermission()
'Not enough permissions to create filter_element items in filter_element archive ID '.\Input::get('pid').'.'
);
}

break;

case 'cut':
Expand Down Expand Up @@ -189,6 +192,7 @@ public function checkPermission()
'Not enough permissions to '.\Input::get('act').' filter_element item ID '.$id.' of filter_element archive ID '.$objArchive->pid.'.'
);
}

break;

case 'select':
Expand All @@ -215,6 +219,7 @@ public function checkPermission()
$session = $session->all();
$session['CURRENT']['IDS'] = array_intersect($session['CURRENT']['IDS'], $objArchive->fetchEach('id'));
$session->replace($session);

break;

default:
Expand All @@ -225,6 +230,7 @@ public function checkPermission()
'Not enough permissions to access filter_element archive ID '.$id.'.'
);
}

break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Backend/FilterPreselect.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function adjustLabel($row, $label)
switch ($row['initialValueType']) {
case \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_SCALAR:
$label = $choices[$row['initialValue']] ?? $row['initialValue'];

break;

case \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_ARRAY:
$values = array_map(
function ($item) {
Expand All @@ -60,7 +62,9 @@ function ($item) {
);

$label = implode(',', array_intersect_key($choices, array_flip(array_filter($values))));

break;

case \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_CONTEXTUAL:
$label = \HeimrichHannot\FilterBundle\Filter\AbstractType::VALUE_TYPE_CONTEXTUAL;
}
Expand Down Expand Up @@ -113,6 +117,7 @@ protected function prepareElementChoices(int $id): array
foreach ($config['filter']['types'] as $type) {
if (isset($type['name']) && $type['name'] === $filterConfigElement->type && isset($type['class'])) {
$class = $type['class'];

break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Choice/CountryChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function collect()
foreach ($options as $key => $option) {
if (!\is_array($option) && (!isset($option['label']) || !isset($option['value']))) {
$choices[$option] = $key;

continue;
}

Expand Down
117 changes: 117 additions & 0 deletions src/Choice/DateChoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\FilterBundle\Choice;

use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Model\Collection;
use Contao\StringUtil;
use Contao\System;
use HeimrichHannot\FilterBundle\Filter\AbstractType;
use HeimrichHannot\FilterBundle\Filter\Type\SkipParentsType;
use HeimrichHannot\FilterBundle\Model\FilterConfigElementModel;
use HeimrichHannot\UtilsBundle\Choice\AbstractChoice;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;

class DateChoice extends AbstractChoice
{
/**
* @var ModelUtil
*/
private $modelUtil;

public function __construct(ContaoFrameworkInterface $framework, ModelUtil $modelUtil)
{
parent::__construct($framework);
$this->modelUtil = $modelUtil;
}

/**
* @return array
*/
protected function collect()
{
if (!\is_array($this->getContext()) || empty($this->getContext())) {
return [];
}
$context = $this->getContext();

$filter = $context['filter'];
/** @var FilterConfigElementModel $element */
$element = $context['element'];
/** @var FilterConfigElementModel[]|Collection $elements */
$elements = $context['elements'];
$columns = [];
$values = [];

foreach ($elements as $entry) {
switch ($entry->type) {
case SkipParentsType::TYPE:
$condition = SkipParentsType::generateModelArrays($filter, $entry);

if (\is_array($condition)) {
$columns = array_merge($columns, $condition['columns']);
$values = array_merge($values, $condition['values']);
}

break;

default:
if ($entry->isInitial && $entry->id !== $element->id) {
switch ($entry->initialValueType) {
case AbstractType::VALUE_TYPE_SCALAR:
$operator = System::getContainer()->get('huh.utils.database')->transformVerboseOperator($entry->operator);

$columns[] = $entry->field.$operator.'?';
$values[] = $entry->initialValue;

break;

case AbstractType::VALUE_TYPE_ARRAY:
$value = array_column(StringUtil::deserialize($entry->initialValueArray), 'value');

if (empty($value) || empty($value[0])) {
continue;
}
$columns[] = $entry->field.' IN ('.implode(',', $value).')';

break;
}
}

break;
}
}
$options = [];

if (isset($context['latest']) && true === $context['latest']) {
$options['order'] = $element->field.' DESC';
$options['limit'] = 1;
}

if (empty($columns)) {
return [];
}
$items = $this->modelUtil->findModelInstancesBy($filter['dataContainer'], $columns, $values, $options);

if (!$items) {
return [];
}
$dates = [];

foreach ($items as $entry) {
$date = date($element->dateFormat, $entry->{$element->field});
$translatedDate = System::getContainer()->get('huh.utils.date')->translateMonths($date);

$dates[$translatedDate] = $translatedDate;
}
krsort($dates, SORT_NUMERIC);

return $dates;
}
}
2 changes: 2 additions & 0 deletions src/Choice/FieldOptionsChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ protected function getDcaOptions(FilterConfigElementModel $element, array $filte
break;
}
$options = $this->getTagWidgetOptions($element, $filter, $dca);

break;

default:
$options = $this->getWidgetOptions($element, $filter, $dca);
}
Expand Down
1 change: 1 addition & 0 deletions src/Choice/LanguageChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function collect()
foreach ($options as $key => $option) {
if (!\is_array($option) && (!isset($option['label']) || !isset($option['value']))) {
$choices[$option] = $key;

continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Choice/LocaleChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function collect()
foreach ($options as $key => $option) {
if (!\is_array($option) && (!isset($option['label']) || !isset($option['value']))) {
$choices[$option] = $key;

continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Choice/SortChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function collect()
if ($groupChoices) {
$group = $type['type'];
$choices[$group][] = $type['name'];

continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Choice/TypeChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected function collect()
if ($groupChoices) {
$group = $type['type'];
$choices[$group][] = $type['name'];

continue;
}

Expand Down
15 changes: 13 additions & 2 deletions src/Choice/YearChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Model\Collection;
use Contao\StringUtil;
use Contao\System;
use HeimrichHannot\FilterBundle\Filter\AbstractType;
use HeimrichHannot\FilterBundle\Model\FilterConfigElementModel;
use HeimrichHannot\UtilsBundle\Choice\AbstractChoice;
Expand Down Expand Up @@ -46,24 +47,32 @@ protected function collect()
$elements = $context['elements'];
$columns = [];
$values = [];

foreach ($elements as $entry) {
if ($entry->isInitial && $entry->id !== $element->id) {
switch ($entry->initialValueType) {
case AbstractType::VALUE_TYPE_SCALAR:
$columns[] = $entry->field.' = ?';
$operator = System::getContainer()->get('huh.utils.database')->transformVerboseOperator($entry->operator);

$columns[] = $entry->field.$operator.'?';
$values[] = $entry->initialValue;

break;

case AbstractType::VALUE_TYPE_ARRAY:
$value = array_column(StringUtil::deserialize($entry->initialValueArray), 'value');

if (empty($value) || empty($value[0])) {
continue;
}
$columns[] = $entry->field.' IN ('.implode(',', $value).')';

break;
}
}
}
$options = [];

if (isset($context['latest']) && true === $context['latest']) {
$options['order'] = $element->field.' DESC';
$options['limit'] = 1;
Expand All @@ -73,12 +82,14 @@ protected function collect()
return [];
}
$items = $this->modelUtil->findModelInstancesBy($filter['dataContainer'], $columns, $values, $options);

if (!$items) {
return [];
}
$years = [];

foreach ($items as $entry) {
$date = date('Y', $entry->date);
$date = date('Y', $entry->{$element->field});
$years[$date] = $date;
}
krsort($years, SORT_NUMERIC);
Expand Down
1 change: 1 addition & 0 deletions src/Config/FilterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ public function getUrl()

if (!empty($filter['parentFilter'])) {
$parentFilter = $this->framework->getAdapter(FilterConfigModel::class)->findById($filter['parentFilter'])->row();

if (!empty($parentFilter)) {
$filter['action'] = $parentFilter['action'];
$filter['name'] = $parentFilter['name'];
Expand Down
1 change: 1 addition & 0 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getExtensionConfig($extensionName, array $extensionConfigs, Cont
// enable form plugin
if (!isset($extensionConfig['form'])) {
$extensionConfig['form']['enabled'] = true;

break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/FilterConfigElementCallbackListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public function getValueTypeOptions(DC_Table $dca)
{
$choices = AbstractType::VALUE_TYPES;
$filter = $dca->activeRecord->fetchAllAssoc()[0];

if (empty($filter)) {
return $choices;
}
$types = \Contao\System::getContainer()->getParameter('huh.filter')['filter']['types'];
$typeIndex = array_search($filter['type'], array_column($types, 'name'), true);

if (!$typeIndex) {
return $choices;
}
Expand Down
Loading

0 comments on commit 9b7914d

Please sign in to comment.