Skip to content

Instantly share code, notes, and snippets.

@jeromecoupe
Last active May 26, 2016 10:39
Show Gist options
  • Save jeromecoupe/2cd46e16403bd02263a9b9195921348a to your computer and use it in GitHub Desktop.
Save jeromecoupe/2cd46e16403bd02263a9b9195921348a to your computer and use it in GitHub Desktop.
Discontinuous events with multiple dates: native method with date fields and matrix
{% extends "_layouts/default" %}
{% block content %}
{#
Tackling discontinuous events without duplication
- Each event has a 'startDate' and an 'endDate' (those are just a date interval, the first date corresponds the the first occurrence of the event and the mast one corresponds to the last occurence of the events). This matching is done manually but we could also use the preparse plugin.
- Each event has got a matrix field 'timetable' with just a block 'mxBlockDate' and date field 'mxDate' for each occurence of the event
1. When the users query for a day or date range when the event does not occur, we shouldn't display antyting
2. For each day during which the event occurs, we want to display the event's details
#}
<h1>Pure Vanilla (object)</h1>
{% set startDate = date('2016-05-23') %}
{% set endDate = startDate|date_modify('+5 days') %}
<p>FROM: {{ startDate|date('Y-m-d 00:01') }} TO {{ endDate|date('Y-m-d 23:59') }}</p>
{% set events = craft.entries.find({
section: 'testevents',
order: 'eventEndDate asc',
eventStartDate: '<= ' ~ endDate|date('Y-m-d 23:59'),
eventEndDate: '>= ' ~ startDate|date('Y-m-d 00:00'),
with: [
'timetable'
]
}) %}
{# build calendar object #}
{#
- for each date in an event, create an object with the date and the title
- append each of those object to a global calendar object
- sort calendar object by date
#}
{% set calendar = {} %}
{% for event in events %}
{% for date in event.timetable %}
{% set calendarDate = {
'date': date.mxDate|date('Y-m-d H:i'),
'title': event.title
} %}
{% set key = date.mxDate|date('Ymd-Hi') %}
{% set calendar = calendar|merge({ (key):calendarDate }) %}
{% endfor %}
{% endfor %}
{% set calendar = calendar|sort %}
<pre>{{ dump(calendar) }}</pre>
{# display calendar #}
{#
- get the first date and assign it to current date
- loop through all items in calendar
- compare current date to event date. If they are not equal, display a date title
#}
{% set calendarFirstItem = calendar|first %}
{% set calendarFirstDate = calendarFirstItem.date %}
{% set calendarCurrentDate = calendarFirstDate %}
{% for calendarDate in calendar %}
{% if loop.first %}<h2>{{ calendarDate.date|date('d M, Y') }}</h2>{% endif %}
{% if calendarDate.date|date('Y-m-d') != calendarCurrentDate|date('Y-m-d') %}
<h2>{{ calendarDate.date|date('d M, Y') }}</h2>
{% set calendarCurrentDate = calendarDate.date %}
{% endif %}
<p>{{ calendarDate.title }} - {{ calendarDate.date|date('d m, Y') }}</p>
{% endfor %}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment