Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from kusiboss/remove-predefined-filters-from-s…
Browse files Browse the repository at this point in the history
…earch

filter fixes
  • Loading branch information
tuxes3 committed Jan 16, 2024
2 parents 89055a7 + e59408e commit 9d521f4
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Filter/Type/AjaxOneToManyFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getOperators(): array
];
}

public function getValueField(?string $value = '0'): string
public function getValueField(?string $value = '0', ?string $operator = null): string
{
$targetClass = $this->getOption(static::OPT_TARGET_CLASS);
$field = sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/AjaxRelationFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getOperators(): array
];
}

public function getValueField(?string $value = '0'): string
public function getValueField(?string $value = '0', ?string $operator = null): string
{
$jsonSearchUrl = $this->getOption(static::OPT_JSON_SEARCH_URL);
$field = sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/BooleanFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getOperators(): array
];
}

public function getValueField(?string $value = '1'): string
public function getValueField(?string $value = '1', ?string $operator = null): string
{
return sprintf(
'<select name="{name}" class="form-control"><option value="1" %s>ausgewählt</option><option value="0" %s>nicht ausgewählt</option></select>',
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/ChoiceFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getOperators(): array
];
}

public function getValueField(?string $value = null): string
public function getValueField(?string $value = null, ?string $operator = null): string
{
$result = '<select name="{name}" class="form-control"><option value=""></option>';

Expand Down
5 changes: 3 additions & 2 deletions src/Filter/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

class DateFilterType extends DatetimeFilterType
{
public function getValueField(?string $value = null): string
public function getValueField(?string $value = null, ?string $operator = null): string
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

return sprintf(
'<input type="text" name="{name}" value="%s" class="form-control" data-provide="datetimepicker" data-date-format="dd.mm.yyyy" data-min-view="2">',
$date->format('d.m.Y')
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Filter/Type/DatetimeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ public function getOperators(): array
static::CRITERIA_BEFORE => 'araise_table.filter.operator.before',
static::CRITERIA_AFTER => 'araise_table.filter.operator.after',
static::CRITERIA_IN_YEAR => 'araise_table.filter.operator.same_year',
static::CRITERIA_IS_EMPTY => 'araise_table.filter.operator.is_empty',
static::CRITERIA_IS_EMPTY => new FilterOperatorDto('araise_table.filter.operator.is_empty', [
FilterOperatorDto::OPT_HAS_FILTER_VALUE => false,
]),
];
}

public function getValueField(?string $value = null): string
public function getValueField(?string $value = null, ?string $operator = null): string
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

return sprintf(
'<input type="text" name="{name}" value="%s" class="form-control" data-provide="datetimepicker" data-date-format="dd.mm.yyyy HH:ii">',
$date->format(static::getDateFormat())
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
);
}

Expand Down
73 changes: 73 additions & 0 deletions src/Filter/Type/FilterOperatorDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);
/*
* Copyright (c) 2024, whatwedo GmbH
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

namespace araise\TableBundle\Filter\Type;

use Symfony\Component\OptionsResolver\OptionsResolver;

class FilterOperatorDto
{
public const OPT_HAS_FILTER_VALUE = 'has_filter_value';

public function __construct(
public readonly string $messageKey,
private array $options = []
) {
$resolver = new OptionsResolver();
$resolver->setDefaults([
self::OPT_HAS_FILTER_VALUE => true,
]);
$this->options = $resolver->resolve($options);
}

public function getOption(string $key): mixed
{
return $this->options[$key] ?? null;
}

public function getOptions(): array
{
return $this->options;
}

public function setOption(string $key, mixed $value): void
{
$this->options[$key] = $value;
}

public function setOptions(array $options): void
{
$this->options = $options;
}

public function __toString(): string
{
return $this->messageKey;
}
}
4 changes: 2 additions & 2 deletions src/Filter/Type/FilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ abstract class FilterType implements FilterTypeInterface

protected array $options = [];

public function __construct(?string $coumn = null, array $joins = [])
public function __construct(?string $column = null, array $joins = [])
{
$this->options = [
self::OPT_COLUMN => $coumn,
self::OPT_COLUMN => $column,
self::OPT_JOINS => $joins,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/FilterTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getJoins(): array;

public function getOperators(): array;

public function getValueField(?string $value): string;
public function getValueField(?string $value, ?string $operator): string;

public function toDql(string $operator, string $value, string $parameterName, QueryBuilder $queryBuilder);

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Type/NumberFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getOperators(): array
];
}

public function getValueField(?string $value = null): string
public function getValueField(?string $value = null, ?string $operator = null): string
{
if (! is_numeric($value)) {
$value = '0';
Expand Down
10 changes: 7 additions & 3 deletions src/Filter/Type/TextFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public function getOperators(): array
static::CRITERIA_NOT_EQUAL => 'araise_table.filter.operator.not_equal',
static::CRITERIA_STARTS_WITH => 'araise_table.filter.operator.starts_with',
static::CRITERIA_ENDS_WITH => 'araise_table.filter.operator.ends_with',
static::CRITERIA_IS_EMPTY => 'araise_table.filter.operator.is_empty',
static::CRITERIA_IS_NOT_EMPTY => 'araise_table.filter.operator.is_not_empty',
static::CRITERIA_IS_EMPTY => new FilterOperatorDto('araise_table.filter.operator.is_empty', [
FilterOperatorDto::OPT_HAS_FILTER_VALUE => false,
]),
static::CRITERIA_IS_NOT_EMPTY => new FilterOperatorDto('araise_table.filter.operator.is_not_empty', [
FilterOperatorDto::OPT_HAS_FILTER_VALUE => false,
]),
];
}

public function getValueField(?string $value = ''): string
public function getValueField(?string $value = '', ?string $operator = null): string
{
if (! $value) {
$value = '';
Expand Down
27 changes: 24 additions & 3 deletions src/Resources/assets/controllers/filter_controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
static targets = ['filters', 'filterspanel', 'backdrop', 'filterGroupList', 'singleFilterRemove', 'filterGroupFilterHeaderFirst', 'filterGroupFilterHeaderOthers', 'dropdown']
static targets = ['filters', 'filterspanel', 'backdrop', 'filterGroupList', 'singleFilterRemove', 'filterGroupFilterHeaderFirst', 'filterGroupFilterHeaderOthers', 'dropdown',]

connect() {
this.updateGui();
Expand All @@ -15,10 +15,13 @@ export default class extends Controller {
const valueField = parentBlock.querySelector('[name^="index_filter_value"]');

operatorSelect.options.length = 0;
const operatorsOptions = JSON.parse(choosenOption.getAttribute('data-operators-options'));
Object
.entries(JSON.parse(choosenOption.getAttribute('data-operator-options')))
.entries(JSON.parse(choosenOption.getAttribute('data-operators')))
.forEach(([key, value]) => {
operatorSelect.add(new Option(value, key));
const option = new Option(value, key);
option.setAttribute('data-has-filter-value', operatorsOptions[key]['has_filter_value']);
operatorSelect.add(option);
})
;

Expand All @@ -31,6 +34,20 @@ export default class extends Controller {
valueField.parentNode.replaceChild(doc.body, valueField);
}

filterOperatorChanged(event) {
this.updateFilterOperatorValueGui(event.target);
}

updateFilterOperatorValueGui(operatorSelect) {
const option = operatorSelect.options[operatorSelect.selectedIndex];
const valueField = operatorSelect.parentElement.nextElementSibling;
if (option.getAttribute('data-has-filter-value') === 'true') {
valueField.classList.remove('hidden');
} else {
valueField.classList.add('hidden');
}
}

open() {
this.filterspanelTarget.classList.add('translate-x-0');
this.filterspanelTarget.classList.remove('translate-x-full');
Expand Down Expand Up @@ -179,6 +196,10 @@ export default class extends Controller {
if (this.filterGroupFilterHeaderOthersTargets.length > 0) {
this.filterGroupFilterHeaderOthersTarget.classList.add('hidden');
}

document.querySelectorAll('select[name^="index_filter_operator"]')
.forEach((element) => this.updateFilterOperatorValueGui(element))
;
}

reset(event) {
Expand Down
15 changes: 10 additions & 5 deletions src/Resources/views/tailwind_2/_filter.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
{% for f in filters %}
<option
value="{{ f.acronym }}"
data-operator-options="{{ f.type.operators|araise_table_filter_operators }}"
data-operators="{{ f.type.operators|araise_table_filter_operators }}"
data-operators-options="{{ f.type.operators|araise_table_filter_operators_options }}"
data-value-template="{{ f.type.valueField|raw|e }}"
class="form-control"
{% if f.acronym == filter.acronym %}selected="selected"{% endif %}
Expand All @@ -17,14 +18,18 @@
</select>
</div>
<div class="whatwedo-table-filter-selection flex-initial w-3/12">
<select name="{{ araise_table_parameter(table, 'filter_operator')~indexSuffix }}" class="w-full cursor-pointer whatwedo-utility-paragraph whatwedo-crud-button--action-white">
<select
{{ stimulus_action('araise/table-bundle/filter', 'filterOperatorChanged') }}
name="{{ araise_table_parameter(table, 'filter_operator')~indexSuffix }}"
class="w-full cursor-pointer whatwedo-utility-paragraph whatwedo-crud-button--action-white"
>
{% for key, value in filter.type.operators %}
<option value="{{ key }}" {% if key == filterOperator %}selected="selected"{% endif %}>{{ value|trans }}</option>
<option data-has-filter-value="{{ value.getOption('has_filter_value') ?? true ? 'true' : 'false' }}" value="{{ key }}" {% if key == filterOperator %}selected="selected"{% endif %}>{{ value|trans }}</option>
{% endfor %}
</select>
</div>
<div class="whatwedo-table-filter-amount flex-none w-4/23">
{{ filter.type.valueField(filterValue)|replace({ '{name}': araise_table_parameter(table, 'filter_value')~indexSuffix })|raw }}
{{ filter.type.valueField(filterValue, filterOperator)|replace({ '{name}': araise_table_parameter(table, 'filter_value')~indexSuffix })|raw }}
</div>
<div class="whatwedo-table-filter-actions flex-auto text-right items-center w-2/12">
<div class="flex justify-end">
Expand Down Expand Up @@ -188,7 +193,7 @@

<div class="px-4 sm:px-6" {{ stimulus_target('araise/table-bundle/filter', 'filterGroupList') }}>
{{ _self.filter_header(table) }}
{% for groupIndex, group in table.filterExtension.getFilterData(false) %}
{% for groupIndex, group in table.filterExtension.getFilterData(true) %}
{{ _self.filter_group(table, groupIndex, group) }}
{% else %}
{{ _self.filter_group(table, 0, []) }}
Expand Down
24 changes: 17 additions & 7 deletions src/Twig/TableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace araise\TableBundle\Twig;

use araise\TableBundle\Factory\TableFactory;
use araise\TableBundle\Filter\Type\FilterOperatorDto;
use araise\TableBundle\Helper\RouterHelper;
use araise\TableBundle\Table\Column;
use araise\TableBundle\Table\Table;
Expand Down Expand Up @@ -50,7 +51,14 @@ public function getFilters(): array
return [
new TwigFilter('araise_table_filter_operators', function ($data) {
foreach (array_keys($data) as $key) {
$data[$key] = $this->translator->trans($data[$key]);
$data[$key] = $this->translator->trans(strval($data[$key]));
}

return json_encode($data, JSON_THROW_ON_ERROR);
}),
new TwigFilter('araise_table_filter_operators_options', function ($data) {
foreach (array_keys($data) as $key) {
$data[$key] = ($data[$key] instanceof FilterOperatorDto ? $data[$key] : new FilterOperatorDto($key))->getOptions();
}

return json_encode($data, JSON_THROW_ON_ERROR);
Expand All @@ -62,13 +70,10 @@ public function pathReplaceArguments(array $arguments, bool $returnParameters =
{
$request = $this->requestStack->getCurrentRequest();

$attributes = array_filter(
$request->attributes->all(),
static fn ($key) => ! str_starts_with($key, '_'),
ARRAY_FILTER_USE_KEY
);
$attributes = $this->filterKeys($request->attributes->all(), fn ($key) => !str_starts_with($key, '_'));
$queryParameters = $this->filterKeys($request->query->all(), fn ($key) => !str_ends_with($key, RouterHelper::PARAMETER_FILTER_PREDEFINED));

$parameters = array_replace(array_merge($attributes, $request->query->all()), $arguments);
$parameters = array_replace(array_merge($attributes, $queryParameters), $arguments);
if ($returnParameters) {
return $this->post2Name($parameters);
}
Expand Down Expand Up @@ -117,4 +122,9 @@ protected function flattenArray($array, $prefix = '')
}
return $result;
}

protected function filterKeys($array, $callback): array
{
return array_filter($array, $callback, ARRAY_FILTER_USE_KEY);
}
}

0 comments on commit 9d521f4

Please sign in to comment.