Skip to content

Instantly share code, notes, and snippets.

@carlcs
Last active September 30, 2015 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlcs/adbb84b5521f7d010c8e to your computer and use it in GitHub Desktop.
Save carlcs/adbb84b5521f7d010c8e to your computer and use it in GitHub Desktop.
{% set bread = craft.entries({ section: 'bread' }).limit(null) %}
{#
# Users with bread on 3 days
#}
{% set users3Days = craft.users({
group: 'customers',
relatedTo: [
'and',
{ targetElement: bread, field: 'breadForMonday' },
{ targetElement: bread, field: 'breadForWednesday' },
{ targetElement: bread, field: 'breadForFriday' }
]
}) %}
{#
# Users with bread per weekday
#}
{% set usersMonday = craft.users({
group: 'customers',
relatedTo: { targetElement: bread, field: 'breadForMonday' }
}) %}
{% set usersWednesday = craft.users({
group: 'customers',
relatedTo: { targetElement: bread, field: 'breadForWednesday' }
}) %}
{% set usersFriday = craft.users({
group: 'customers',
relatedTo: { targetElement: bread, field: 'breadForFriday' }
}) %}
{#
# Users with bread on 1 day (grouped per weekday)
#}
{% set usersIdsMonday = usersMonday.ids() %}
{% set usersIdsWednesday = usersWednesday.ids() %}
{% set usersIdsFriday = usersFriday.ids() %}
{% set usersOnlyMonday = craft.users({
id: usersIdsMonday|without(usersIdsWednesday)|without(usersIdsFriday)
}) %}
{% set usersOnlyWednesday = craft.users({
id: usersIdsWednesday|without(usersIdsMonday)|without(usersIdsFriday)
}) %}
{% set usersOnlyFriday = craft.users({
id: usersIdsFriday|without(usersIdsMonday)|without(usersIdsWednesday)
}) %}
{#
# Users with bread on 1 day
#}
{% set users1Days = craft.users({
id: usersOnlyMonday.ids()|merge(usersOnlyWednesday.ids())|merge(usersOnlyFriday.ids())
}) %}
{#
# Users with bread on 1, 2 or 3 days
#}
{% set usersAnyDay = craft.users({
group: 'customers',
relatedTo: { targetElement: bread }
}) %}
{#
# Users with bread on no day at all
#}
{% set users0Days = craft.users({
id: 'and, not ' ~ usersIdsAnyDay.ids()|join(', not ')
}) %}
{#
# Users with bread on 2 days
#}
{% set users2Days = craft.users({
id: usersAnyDay.ids()|without(users0Days.ids())|without(users1Days.ids())|without(users3Days.ids())
}) %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment