Hi
I have a multi-filters system with 4 types of filters : 3 filters with date futur/current/ongoing and one for tags. I want to “check” and “uncheck” them to choose between dates or tags, or both.
Sometimes, when I “uncheck” a word tag, the link is redirecting into an arbitray date, instead of just removing this word from the URL.
Where does it come from ? What I’m doing wrong ?
<section class="filtres">
<article class="dates">
<ul>
<?php foreach ($datefutur as $article) : ?>
<?php if ($datetag == urldecode(param('date') == 'future')) : ?>
<li>
<a class="pastille artistique <?= param('date') == 'future' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => array_merge(params(),['date' => 'future']) ]) ?>"><?= t('àvenir') ?></a>
</li>
<?php else : ?>
<li>
<a class="pastille artistique <?= param('date') == 'future' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['rubrique' => urlencode($tag)]]) ?>"><?= t('àvenir') ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
<?php foreach ($datecurrent as $article) : ?>
<?php if ($datetag == urldecode(param('date') == 'current')) : ?>
<li>
<a class="pastille artistique <?= param('date') == 'current' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => array_merge(params(),['date' => 'current']) ]) ?>"><?= t('encours') ?></a>
</li>
<?php else : ?>
<li>
<a class="pastille artistique <?= param('date') == 'current' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['rubrique' => urlencode($tag)]]) ?>"><?= t('encours') ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
<?php foreach ($datetags as $datetag) : ?>
<?php if ($datetag == urldecode(param('date'))) : ?>
<li>
<a class="pastille artistique <?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['rubrique' => urlencode($tag)]]) ?>"><?= html($datetag) ?></a>
</li>
<?php else : ?>
<li>
<a class="pastille artistique <?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => array_merge(params(),['date' => $datetag]) ]) ?>"><?= html($datetag) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</article>
<article class="tags">
<ul>
<?php foreach($tags as $tag): ?>
<?php if ($tag == urldecode(param('rubrique'))) : ?>
<li>
<a class="pastille artistique <?= param('rubrique') == urlencode($tag) ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => urlencode($datetag)]]) ?>"><?= t($tag) ?></a>
</li>
<?php else: ?>
<li>
<a class="pastille artistique <?= param('rubrique') == urlencode($tag) ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => array_merge(params(),['rubrique' => urlencode($tag)]) ]) ?>"><?= t($tag) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</article>
</section>
In case you want to see the controller, here it is :
<?php
return function($site, $page, $pages) {
$query = get('q');
$results = $site->index()->not('presentation', 'mentions-legales', 'infos-pratiques', 'nous-soutenir', 'error', 'feries')->bettersearch($query, 'title|text|nomprenom|commissaire|chapeau|sstitre');
$cart = cart_logic();
$count = cart_count();
// fetch the basic set of pages
$articles = $site->find('artistique')->children()->listed();
// fetch all tags
// add the tag filter
if($tag = urldecode(param('rubrique'))) {
$articles = $articles->filterBy('rubrique', $tag, ',');
}
if($datetag = urldecode(param('date'))) {
$articles = $articles->filter(function($item) use($datetag) {
// start date in the future
if ($datetag === 'future') {
return $item->date_start()->toDate() > time();
}
// start date in the past/now and end date now or in the future
if ($datetag === 'current') {
return $item->date_start()->toDate() <= time() && $item->date_end()->toDate() >= time();
}
return $item->date_start()->toDate('Y') === $datetag && $item->date_start()->toDate() <= time();
});
}
// We only want years up until this year
$datetags = $articles->sortBy('date_start', 'desc')->filter(function($article) {
return $article->date_end()->toDate() <= time();
})->pluck('date_start', ',', true);
$datetags = array_unique(array_map(function($item) {
return date('Y', strtotime($item));
}, $datetags));
$datefutur = $articles->sortBy('date_start', 'asc')->filter(function($article) {
return $article->date_start()->toDate() > time();
})->pluck('date_start', ',', true);
$datefutur = array_unique(array_map(function($item) {
return 'àvenir';
}, $datefutur));
$datecurrent = $articles->sortBy('date_start', 'asc')->filter(function($article) {
return $article->date_start()->toDate() <= time() && $article->date_end()->toDate() >= time();
})->pluck('date_start', ',', true);
$datecurrent = array_unique(array_map(function($item) {
return 'current';
}, $datecurrent));
$tags = $articles->pluck('rubrique', ',', true);
$collator = new Collator('fr_CA');
$collator->sort($tags);
// apply pagination
$articles = $articles->sortBy('date_start', 'desc')->paginate(6);
$pagination = $articles->sortBy('date_start', 'desc')->pagination();
return compact('query', 'datetags', 'datetag', 'datefutur', 'datecurrent', 'articles', 'tags', 'tag', 'pagination', 'results', 'query', 'cart', 'count');
};