I am creating some custom grouping using a date field and UPCOMING, TODAY, CURRENT groups. Is there any way I could created custom sorting on these groups to display them in a TODAY, CURRENT, UPCOMING order?
$callback = function($p) {
$close_date = date("Y-m-d",strtotime($p->close_date()));
date_default_timezone_set('America/Los_Angeles');
$today = date("Y-m-d");
$openning_date = date("Y-m-d",strtotime($p->opening()));
//if start_date is null, then it's an event
if ($p->start_date()->isNotEmpty()) {
$start_date = date("Y-m-d",strtotime($p->start_date()));
} else {
$start_date = null;
}
//check to see if "Current show"
if ($start_date == null) {
//this an event
if ($close_date == $today) return "Today";
return "Upcoming";
}
else {
//these are exhibitions
if ($openning_date == $today) return "Today";
if ($close_date > $today && $start_date < $today) return "Currently";
return "Upcoming";
}
};
//evaluate only the date
$groupedItems = $site->find('events', 'exhibitions')->
children()->visible()->
filter(function($child) {
return $child->date("Y-m-d", 'close_date') >= date("Y-m-d");
})
->group($callback);
//dump($groupedItems);
I think you have gone a little overboard there. In the events pages i’ve done, i filtered by comparing the article to today and only used it if its greater:
$currentdatetime = date('Y-m-d');
if ($openning_date >= $currentdatetime)
// up coming events
}
if ($openning_date == $currentdatetime)
// todays event
}
Im not sure how to do greater then today but not equal to today in one statement.
I’m trying to display a list of exhibitions (open & close dates) and events (only open date) like so:
Today
…
…
…
Currently
…
…
…
Upcoming
…
…
…
Need to use a callback function for the grouping to deal with the difference in dates. Trying to figure out a way to display the groupings in the order above and not the default ASC or DESC.
One way to get the desired order into your groups would be to return a number instead of upcoming etc. Then you could create a value map and get the value for the number from that mapping array.
For example:
//...
if ($start_date == null) {
//this an event
if ($close_date == $today) return "Today";
return 2; // instead of upcoming
}
//...
@o-o-o-o-o Keep in mind that each item you want to group must have the field you want to group by, if the field is missing, you will get an error message.