2017-10-28 00:31:21 +01:00
|
|
|
<?php
|
|
|
|
|
2017-10-28 12:17:09 +01:00
|
|
|
namespace FormatTalksBundle\Twig;
|
2017-10-28 00:31:21 +01:00
|
|
|
|
2017-10-28 12:17:09 +01:00
|
|
|
class FormatTalksExtension extends \Twig_Extension
|
2017-10-28 00:31:21 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
return [
|
2017-10-28 12:17:09 +01:00
|
|
|
new \Twig_SimpleFilter('format_talks', [$this, 'formatTalks']),
|
2017-10-28 00:31:21 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-10-28 09:08:34 +01:00
|
|
|
public function formatTalks($data, $onlyUpcoming = false, $onlyPrevious = false)
|
2017-10-28 00:31:21 +01:00
|
|
|
{
|
2017-10-28 02:28:44 +01:00
|
|
|
$event_data = $data['events'];
|
|
|
|
|
|
|
|
$talks = [];
|
|
|
|
foreach ($data['talks'] as $talk) {
|
2017-10-28 09:08:34 +01:00
|
|
|
foreach ($talk['events'] as $event) {
|
|
|
|
$event = array_merge($event, $event_data[$event['event']]);
|
2017-10-28 02:28:44 +01:00
|
|
|
|
2017-10-28 09:08:34 +01:00
|
|
|
$talks[] = compact('talk', 'event');
|
|
|
|
}
|
2017-10-28 02:28:44 +01:00
|
|
|
}
|
|
|
|
|
2017-10-28 09:08:34 +01:00
|
|
|
$today = (new \DateTime())->format('Y-m-d');
|
|
|
|
|
2017-10-28 02:28:44 +01:00
|
|
|
return collect($talks)
|
2017-10-28 09:08:34 +01:00
|
|
|
->filter(function ($talk) use ($today, $onlyPrevious, $onlyUpcoming) {
|
|
|
|
if ($onlyUpcoming) {
|
|
|
|
return $talk['event']['date'] > $today;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onlyPrevious) {
|
|
|
|
return $talk['event']['date'] < $today;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})->sortByDesc('event.date')->all();
|
2017-10-28 00:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'format_talks';
|
|
|
|
}
|
|
|
|
}
|