Skip to content

Instantly share code, notes, and snippets.

@jeromecoupe
Last active May 26, 2016 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeromecoupe/7206f0addbaa7300d7fe4864a1403fda to your computer and use it in GitHub Desktop.
Save jeromecoupe/7206f0addbaa7300d7fe4864a1403fda to your computer and use it in GitHub Desktop.
Handling discontinuous events with just Matrix dates (plugins)
{% extends "_layouts/default" %}
{% block content %}
{#
I have to build an event heavy website
1. Events can have one or more dates
Duplicating events for multiple dates makes no sense
2. The users should be able to query for date intervals
3. The system should only return events which have a date matching the date interval set by the user (or by me in the templates)
If an events has multiple occurences in the date range, it should be displayed multiple times
4. From the user standpoint, having a Matrix field ith one or multiple dates makes the most sense
- The matrix field is called 'timetable'
- The matrix block is called 'mxBlockDate'
- The matrix date field is called 'mxDate'
These are best way I've found to make it work without having to go and reach for a third party calendar solution like Solspace Calendar.
#}
{# test with custom plugin #}
<h2>Custom plugin</h2>
{% set startDate = date('now') %}
{% set endDate = date('now')|date_modify('+12 days') %}
<p>FROM: {{ startDate|date('Y-m-d G:i') }} TO {{ endDate|date('Y-m-d G:i') }}</p>
{% set mxBlocks = craft.businessLogic.getMatrixBlocks( 'timetable', 'mxDate', startDate, endDate ) %}
{% set entryIds = [] %}
{% for mxBlock in mxBlocks %}
{% set entryIds = entryIds|merge([mxBlock.owner.id]) %}
{% endfor %}
<pre>{{ dump(entryIds) }}</pre>
{% set events = craft.entries.id(entryIds).fixedOrder(true).find() %}
{% for item in events %}
<p>EVENT TITLE: {{ item.title }}</p>
{% endfor %}
{#
For 8 events in the system
Time: 0.39034s
Memory: 21,639Kb
Total Queries: 30
- the queries are caused by the calls to mxBlock.owner
- 2 queries are added for each event in the date range
#}
{# test with lowblocks #}
<h2>Lowblocks plugin</h2>
{% set startDate = date('now') %}
{% set endDate = date('now')|date_modify('+12 days') %}
{% set mxblocks = craft.lowblocks.blocks.fieldId(106).mxDate( 'and', '>= ' ~ startDate|date('Y-m-d G:i'), '<= ' ~ endDate|date('Y-m-d G:i') ).order('mxDate asc').limit(null) %}
{% set entryIds = [] %}
{% for mxblock in mxblocks %}
{% set entryIds = entryIds|merge([mxblock.owner.id]) %}
{% endfor %}
<pre>{{ dump(entryIds) }}</pre>
{% set events = craft.entries.id(entryIds).fixedOrder(true).find() %}
{% for item in events %}
<p>EVENT TITLE: {{ item.title }}</p>
{% endfor %}
{#
Time: 0.34932s
Memory: 19,692Kb
Total Queries: 30
- the queries are caused by the calls to mxBlock.owner
- 2 queries are added for each event in the date range
#}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment