Skip to content

Commit

Permalink
add some new filter options
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 21, 2023
1 parent 1927ce5 commit 43bdad5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 23 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ services:
- mariadb:/var/lib/mysql
networks:
- default
- pontsun

volumes:
mariadb:
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/AutocompleteGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function index(Request $request, BookRepository $bookRepository, string $
}
$json['results'][] = ['value' => $item['item'], 'text' => $item['item']];
}
if (($type !== 'authors') && $query === '' && $request->get('create', true) !== true) {
$json['results'][] = ['value' => 'no_'.$type, 'text' => '[No '.$type.' defined]'];
}
if (!$exactmatch && strlen($query) > 2 && $request->get('create', true) === true) {
$json['results'][] = ['value' => $query, 'text' => 'New: '.$query];
}
Expand Down
78 changes: 70 additions & 8 deletions src/Form/BookFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
},
]);

$builder->add('serieIndexGTE', Type\SearchType::class, [
'required' => false,
'mapped'=>false,
'target_callback' => function (QueryBuilder $qb, ?string $searchValue): void {
if ($searchValue !== null) {
$qb->andWhere('book.serieIndex >= :indexGTE');
$qb->setParameter('indexGTE', $searchValue);
}
},
]);

$builder->add('serieIndexLTE', Type\SearchType::class, [
'required' => false,
'mapped'=>false,

'target_callback' => function (QueryBuilder $qb, ?string $searchValue): void {
if ($searchValue !== null) {
$qb->andWhere('book.serieIndex <= :indexLTE');
$qb->setParameter('indexLTE', $searchValue);
}
},
]);

$builder->add('authors', Type\TextType::class, [
'autocomplete' => true,
'tom_select_options' => [
Expand All @@ -54,6 +77,31 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
},
]);

$builder->add('authorsNot', Type\TextType::class, [
'autocomplete' => true,
'tom_select_options' => [
'create' => false,
],
'mapped' => false,
'label' => 'Author not in',
'required' => false,
'autocomplete_url' => $this->router->generate('app_autocomplete_group', ['type' => 'authors', 'create' => false]),
'target_callback' => function (QueryBuilder $qb, ?string $searchValue): void {
if ($searchValue === null || $searchValue === '') {
return;
}
$authors = explode(',', $searchValue);

$orModule = $qb->expr()->orX();

foreach ($authors as $key => $author) {
$orModule->add('JSON_CONTAINS(book.authors, :authorNot'.$key.')=0');
$qb->setParameter('authorNot'.$key, json_encode([$author]));
}
$qb->andWhere($orModule);
},
]);

$builder->add('tags', Type\TextType::class, [
'autocomplete' => true,
'tom_select_options' => [
Expand All @@ -71,8 +119,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$orModule = $qb->expr()->orX();

foreach ($tags as $key => $tag) {
$orModule->add('JSON_CONTAINS(book.tags, :tag'.$key.')=1');
$qb->setParameter('tag'.$key, json_encode([$tag]));
if ($tag === 'no_tags') {
$orModule->add('book.tags = \'[]\'');
} else {
$orModule->add('JSON_CONTAINS(book.tags, :tag'.$key.')=1');
$qb->setParameter('tag'.$key, json_encode([$tag]));
}
}
$qb->andWhere($orModule);
},
Expand All @@ -95,8 +147,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$orModule = $qb->expr()->orX();

foreach ($series as $key => $serie) {
$orModule->add('book.serie=:serie'.$key);
$qb->setParameter('serie'.$key, $serie);
if ($serie === 'no_serie') {
$orModule->add('book.serie = \'[]\'');
} else {
$orModule->add('book.serie=:serie'.$key);
$qb->setParameter('serie'.$key, $serie);
}
}
$qb->andWhere($orModule);
},
Expand All @@ -119,8 +175,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$orModule = $qb->expr()->orX();

foreach ($publishers as $key => $publisher) {
$orModule->add('book.publisher=:publisher'.$key);
$qb->setParameter('publisher'.$key, $publisher);
if ($publisher === 'no_publisher') {
$orModule->add('book.publisher = \'[]\'');
} else {
$orModule->add('book.publisher=:publisher'.$key);
$qb->setParameter('publisher'.$key, $publisher);
}
}
$qb->andWhere($orModule);
},
Expand Down Expand Up @@ -189,10 +249,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->add('orderBy', Type\ChoiceType::class, [
'choices' => [
'title' => 'title',
'created' => 'created',
'updated' => 'updated',
'id' => 'id',
'serieIndex' => 'serieIndex',
],
'data' => 'title',
'data' => 'created',
'mapped' => false,
'target_callback' => function (QueryBuilder $qb, ?string $orderByValue): void {
$params = $qb->getParameters()->toArray();
Expand All @@ -203,7 +265,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$orderByValue = 'serieIndex';
}
if ($orderByValue === null) {
$orderByValue = 'title';
$orderByValue = 'created';
}
$qb->orderBy('book.'.$orderByValue, 'ASC');
$qb->addOrderBy('book.serieIndex', 'ASC');
Expand Down
5 changes: 4 additions & 1 deletion src/Service/FilteredBookUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class FilteredBookUrlGenerator
public const FIELDS_DEFAULT_VALUE = [
'title' => '',
'authors' => [],
'authorsNot' => [],
'serieIndexLTE' => '',
'serieIndexGTE' => '',
'tags' => [],
'serie' => '',
'publisher' => '',
Expand Down Expand Up @@ -52,7 +55,7 @@ public function getParametersArrayForCurrent(bool $onlyModified = false): array
foreach (self::FIELDS_DEFAULT_VALUE as $key => $value) {
if (array_key_exists($key, $queryParams)) {
$value = $queryParams[$key];
if (($key === 'authors' || $key === 'tags') && is_string($value)) {
if (($key === 'authors' || $key === 'authorsNot' || $key === 'tags') && is_string($value)) {
$value = array_filter(explode(',', $value));
}
}
Expand Down
6 changes: 4 additions & 2 deletions templates/book/_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
{{ component('InlineEditBook', {'book':book, 'field':'authors'}) }}
</div>
<div>
{{ component('InlineEditBook', {'book':book, 'field':'serie'}) }}
{{ component('InlineEditBook', {'book':book, 'field':'serie'}) }}

<div class="w-25"> {{ component('InlineEditBook', {'book':book, 'field':'serieIndex'}) }}</div>
{% if book.serie is not null %}
<div class="w-25"> {{ component('InlineEditBook', {'book':book, 'field':'serieIndex'}) }}</div>
{% endif %}
{{ component('FieldGuesser',{'book':book}) }}
</div>
</td>
Expand Down
13 changes: 8 additions & 5 deletions templates/book/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
</td>
</tr>
<tr>
<th>Authors</th><td>{{ component('InlineEditBook', {'book':book, 'field':'authors', inline: true, suggestions:suggestions}) }}</td>
<th>Authors</th>
<td>{{ component('InlineEditBook', {'book':book, 'field':'authors', inline: false, suggestions:suggestions}) }}</td>
</tr>
<tr>
<th>Summary</th><td>
Expand All @@ -48,9 +49,11 @@
<a href="{{ filter_book_url({'serie':book.serie}) }}"><i class="bi bi-box-arrow-right"></i></a>
{% endif %}
</div>
<div class="w-25 d-inline-block">
{{ component('InlineEditBook', {'book':book, 'field':'serieIndex', inline: true, suggestions:suggestions}) }}
</div>
{% if book.serie is not null %}
<div class="w-25 d-inline-block">
{{ component('InlineEditBook', {'book':book, 'field':'serieIndex', inline: true, suggestions:suggestions}) }}
</div>
{% endif %}

</td>
</tr>
Expand All @@ -59,7 +62,7 @@
</tr>

<tr>
<th>Tags</th><td>{{ component('InlineEditBook', {'book':book, 'field':'tags', inline: true, suggestions:suggestions}) }}</td>
<th>Tags</th><td>{{ component('InlineEditBook', {'book':book, 'field':'tags', inline: false, suggestions:suggestions}) }}</td>
</tr>
<tr>
<th>Publishdate</th><td>
Expand Down
18 changes: 12 additions & 6 deletions templates/default/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,35 @@
</div>
<div class="row">
<div class="col-md-3">
{{ form_row(form.tags) }}
{{ form_row(form.authorsNot) }}
</div>
<div class="col-md-3">
{{ form_row(form.tags) }}
</div>
<div class="col-md-2">
{{ form_row(form.favorite) }}
</div>
<div class="col-md-3">
<div class="col-md-2">
{{ form_row(form.verified) }}
</div>
<div class="col-md-3">
<div class="col-md-2">
{{ form_row(form.read) }}
</div>
</div>
<div class="row">
<div class="col-md-3">
{{ form_row(form.publisher) }}
{{ form_row(form.serieIndexGTE) }}
</div>
<div class="col-md-3">
{{ form_row(form.orderBy) }}

{{ form_row(form.serieIndexLTE) }}
</div>

<div class="col-md-3">
{{ form_row(form.publisher) }}
</div>
<div class="col-md-3">
{{ form_row(form.orderBy) }}

</div>
</div>
{{ form_rest(form) }}
Expand Down

0 comments on commit 43bdad5

Please sign in to comment.