Multi filters system with dates and tags

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');

};

I think you messed up your variables. e.g. you use datetag in the loop where you output the tags and vice versa. But somehow this seems to be on purpose but I don’t really understand it.

Yes, this was on purpose as you can see here, both filters are filtered from the first selected, and this is good for me.

If you check the filter called “Exposition”, this works. But if you uncheck the same filter, it returns to “2015”…


If I have to explain my wish, I just would like to make a link to remove the “param” selected but keeping the previous one.

So if the url is

.../date:XXXX/rubrique:exposition

I would like to just remove the /rubrique:exposition to get .../date:XXXX

I think I know what the problem is: When you want to unselect the word tag, you at the same time apply the datetag whether or not a datetag of any kind was set before or not. So your condition should be different and take that into account-

What is selected in this case is rather arbitrary, maybe the date that was last in memory.

Yes, you pointed the right thing, this came from a lack of conditions on the link to the filter tag.
Thanks a lot for your time
Best