Split into multiple filters
This commit is contained in:
parent
781bb446fd
commit
3ef25801b5
|
@ -2,7 +2,7 @@
|
|||
<h2>Events</h2>
|
||||
|
||||
{% include "talks-table" with {
|
||||
talks: { talks: [page], events: site.events }|format_talks|reverse,
|
||||
talks: { talks: [page], events: site.events }|all_talks|reverse,
|
||||
talk_page: true
|
||||
} %}
|
||||
</div>
|
||||
|
|
|
@ -16,7 +16,7 @@ talks:
|
|||
<p>Here are a list of my previous conference and user group talks:</p>
|
||||
|
||||
{% include "talks-table" with {
|
||||
talks: { talks: data.talks|merge(page.talks), events: site.events }|format_talks(false, true)
|
||||
talks: { talks: data.talks|merge(page.talks), events: site.events }|past_talks
|
||||
} %}
|
||||
|
||||
<p>Upcoming talks can be found on the <a href="{{ site.url }}/talks">talks page</a>.</p>
|
||||
|
|
|
@ -10,7 +10,7 @@ use: [talks]
|
|||
|
||||
<h2>Upcoming Talks</h2>
|
||||
|
||||
{% set upcoming_talks = { talks: data.talks, events: site.events }|format_talks(true) %}
|
||||
{% set upcoming_talks = { talks: data.talks, events: site.events }|upcoming_talks %}
|
||||
{% if upcoming_talks %}
|
||||
{% include "talks-table" with { talks: upcoming_talks } %}
|
||||
{% else %}
|
||||
|
@ -20,7 +20,7 @@ use: [talks]
|
|||
<h2>Last 5 Talks</h2>
|
||||
|
||||
{% include "talks-table" with {
|
||||
talks: { talks: data.talks, events: site.events }|format_talks(false, true)|slice(0,5)
|
||||
talks: { talks: data.talks, events: site.events }|past_talks|slice(0,5)
|
||||
} %}
|
||||
|
||||
<p>All previous talks can be found in the <a href="{{ site.url }}/talks/archive">talks archive</a>.</p>
|
||||
|
|
|
@ -2,7 +2,11 @@
|
|||
|
||||
namespace FormatTalksBundle\Twig;
|
||||
|
||||
class FormatTalksExtension extends \Twig_Extension
|
||||
use Illuminate\Support\Collection;
|
||||
use Twig_Extension;
|
||||
use Twig_SimpleFilter;
|
||||
|
||||
class FormatTalksExtension extends Twig_Extension
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -10,16 +14,74 @@ class FormatTalksExtension extends \Twig_Extension
|
|||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFilter('format_talks', [$this, 'formatTalks']),
|
||||
new Twig_SimpleFilter('all_talks', [$this, 'getAll']),
|
||||
new Twig_SimpleFilter('upcoming_talks', [$this, 'getUpcoming']),
|
||||
new Twig_SimpleFilter('past_talks', [$this, 'getPast']),
|
||||
];
|
||||
}
|
||||
|
||||
public function formatTalks($data, $onlyUpcoming = false, $onlyPrevious = false)
|
||||
/**
|
||||
* Get all upcoming and previous talks.
|
||||
*
|
||||
* Used to display the talk table on a specific talk page.
|
||||
*
|
||||
* @param array $data An associative array of talk and event data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll(array $data) {
|
||||
return $this->sort($this->format($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all upcoming talks.
|
||||
*
|
||||
* Used on the main talks page.
|
||||
*
|
||||
* @param array $data The talk and event data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUpcoming(array $data) {
|
||||
$today = (new \DateTime())->format('Y-m-d');
|
||||
|
||||
$talks = $this->format($data)->filter(function ($talk) use ($today) {
|
||||
return $talk['event']['date'] >= $today;
|
||||
});
|
||||
|
||||
return $this->sort($talks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all past talks.
|
||||
*
|
||||
* Used on the main talks page and the talks archive.
|
||||
*
|
||||
* @param array $data The talk and event data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPast(array $data) {
|
||||
$today = (new \DateTime())->format('Y-m-d');
|
||||
|
||||
$talks = $this->format($data)->filter(function ($talk) use ($today) {
|
||||
return $talk['event']['date'] < $today;
|
||||
});
|
||||
|
||||
return $this->sort($talks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the talk data into the required format.
|
||||
*
|
||||
* @param array $data The talk and event data.
|
||||
*
|
||||
* @return Collection The event and talk data.
|
||||
*/
|
||||
public function format(array $data)
|
||||
{
|
||||
$events = collect($data['events']);
|
||||
|
||||
$today = (new \DateTime())->format('Y-m-d');
|
||||
|
||||
return collect($data['talks'])->flatMap(function ($talk) use ($events) {
|
||||
// Build an associative array with the talk, as well as the
|
||||
// specified event data (e.g. date and time) as well as the shared
|
||||
|
@ -30,17 +92,19 @@ class FormatTalksExtension extends \Twig_Extension
|
|||
|
||||
return compact('event', 'talk');
|
||||
});
|
||||
})->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();
|
||||
/**
|
||||
* Sort and return the talks.
|
||||
*
|
||||
* @param Collection $talks The talk data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function sort(Collection $talks)
|
||||
{
|
||||
return $talks->sortByDesc('event.date')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue