Hi,
I would like to set multiple tags like
Futur / Current / 2019 / 2018 / 2017 / …
My “Current” and “Futur” filters doesn’t work properly. “Current” selects only the events wich has the same date year from today, and “Futur” is selecting only the futur year events.
My controller
$datetags = $articles->sortBy('date_start', 'desc')->pluck('date_start', ',', true);
$datetags = array_unique(array_map(function($item) {
return date('Y', strtotime($item));
}, $datetags));
if($datetag = urldecode(param('date'))) {
$articles = $articles->filter(function($item) use($datetag) {
return $item->date_start()->toDate('Y') === $datetag;
});
}
my template :
<?php foreach($datetags as $datetag): ?>
<?php if($datetag > date('Y')): ?>
<li>
<a class="<?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => $datetag]]) ?>">À venir</a>
</li>
<?php elseif($datetag == date('Y')): ?>
<li>
<a class="<?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => $datetag]]) ?>">En cours</a>
</li>
<?php else: ?>
<li>
<a class="<?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => $datetag]]) ?>"><?= html($datetag) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
But what is the expected result?
Your template loop is a bit weird, as it would return an À venir
link for every year in the future, which would be a bit strange (unless there are no dates beyond 2021).
“Current” and “Futur” (sorry not "Incomming) should take in acount a period, not only the year.
Current : all events between date_start
and date_end
from today.
Futur : all events after the date_start
from today.
Looks like I have to use
$item->date_start()->toDate() > time() || $item->date_end()->toDate() > time();
/* for current events */
and
$item->date_start()->toDate() > time() ;
/* for futur events */
But I don’t know how to use them inside a filtering controller.
Ok, but in your logic, in which category go dates that are in the current year but not current or future?
In this case, the project is found in a tag with the year of its date_start
. 
I have to start my list of years with the current one and in that case, exclude the projects from “current”
The tag list:
<ul>
<li>
<a class="<?= param('date') == 'future' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => 'future']]) ?>">À venir</a>
</li>
<li>
<a class="<?= param('date') == 'current' ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => 'current']]) ?>">En cours</a>
</li>
<?php foreach ($datetags as $datetag) : ?>
<li>
<a class="<?= param('date') == $datetag ? 'active' : '' ?>" href="<?= url($page->url(), ['params' => ['date' => $datetag]]) ?>"><?= html($datetag) ?></a>
</li>
<?php endforeach ?>
</ul>
The controller:
<?php
return function($page) {
$articles = $page->children()->listed();
// 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));
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;
});
}
return [
'datetags' => $datetags,
'notes' => $articles,
];
};
1 Like
Looks like a perfect start, tags “futur” and “current” are working, but the loop doesn’t set any year from the past
I used the wrong field name…corrected above.
Thanks for your help on this, it will allow me to reuse this code later on other projects. Have a good evening!