|
| 1 | +{{- /* |
| 2 | + This function determines the "current" event for a given city/event_group. |
| 3 | + |
| 4 | + Input: Page context (.) - the function will get the current event data from the page. |
| 5 | + |
| 6 | + Returns: The event object for the current/most recent event, or empty dict if none found. |
| 7 | + |
| 8 | + Logic: |
| 9 | + - First preference: Latest future event (enddate > now) by startdate |
| 10 | + - Second preference: Latest event by year (from name) for events without dates |
| 11 | + - Third preference: Latest past event by startdate |
| 12 | +*/ -}} |
| 13 | + |
| 14 | +{{- $currentEvent := dict -}} |
| 15 | +{{- $e := partial "functions/get-event-data" . -}} |
| 16 | + |
| 17 | +{{- if and $e $e.city -}} |
| 18 | + {{- /* Determine matching criteria: use event_group if available, otherwise city */ -}} |
| 19 | + {{- $matchTitle := $e.city -}} |
| 20 | + {{- if $e.event_group -}} |
| 21 | + {{- $matchTitle = $e.event_group -}} |
| 22 | + {{- end -}} |
| 23 | + |
| 24 | + {{- /* Get all events with dates */ -}} |
| 25 | + {{- $allEvents := partial "functions/get-all-events" . -}} |
| 26 | + |
| 27 | + {{- /* Get all events without dates (TBD) */ -}} |
| 28 | + {{- $tbdEvents := partial "functions/get-tbd-events" . -}} |
| 29 | + |
| 30 | + {{- /* Combine both sets */ -}} |
| 31 | + {{- $allEventsIncludingTBD := $allEvents -}} |
| 32 | + {{- range $tbdEvents -}} |
| 33 | + {{- $allEventsIncludingTBD = $allEventsIncludingTBD | append . -}} |
| 34 | + {{- end -}} |
| 35 | + |
| 36 | + {{- /* Filter events matching this city/event_group, excluding the current event */ -}} |
| 37 | + {{- $futureEvents := slice -}} |
| 38 | + {{- $eventsWithoutDates := slice -}} |
| 39 | + {{- $pastEvents := slice -}} |
| 40 | + |
| 41 | + {{- range $allEventsIncludingTBD -}} |
| 42 | + {{- /* Skip if this is the event we're checking */ -}} |
| 43 | + {{- if and $e.name (ne .name $e.name) -}} |
| 44 | + {{- /* Check if event matches city or event_group (case-insensitive) */ -}} |
| 45 | + {{- $eventMatchTitle := .city -}} |
| 46 | + {{- if .event_group -}} |
| 47 | + {{- $eventMatchTitle = .event_group -}} |
| 48 | + {{- end -}} |
| 49 | + |
| 50 | + {{- if or (eq (lower .city) (lower $matchTitle)) (eq (lower $eventMatchTitle) (lower $matchTitle)) -}} |
| 51 | + {{- /* Event matches this city/event_group */ -}} |
| 52 | + {{- if and .startdate .enddate -}} |
| 53 | + {{- /* Separate into future and past events based on enddate */ -}} |
| 54 | + {{- if gt (time .enddate) now -}} |
| 55 | + {{- $futureEvents = $futureEvents | append . -}} |
| 56 | + {{- else -}} |
| 57 | + {{- $pastEvents = $pastEvents | append . -}} |
| 58 | + {{- end -}} |
| 59 | + {{- else -}} |
| 60 | + {{- /* Event without dates - store separately */ -}} |
| 61 | + {{- $eventsWithoutDates = $eventsWithoutDates | append . -}} |
| 62 | + {{- end -}} |
| 63 | + {{- end -}} |
| 64 | + {{- end -}} |
| 65 | + {{- end -}} |
| 66 | + |
| 67 | + {{- /* Determine current event with priority: latest year (future dates or TBD) > past */ -}} |
| 68 | + {{- $candidateEvent := dict -}} |
| 69 | + {{- /* Get the year of the event being viewed */ -}} |
| 70 | + {{- $viewingYear := 0 -}} |
| 71 | + {{- if $e.year -}} |
| 72 | + {{- $viewingYear = int $e.year -}} |
| 73 | + {{- else if $e.name -}} |
| 74 | + {{- $yearStr := index (split $e.name "-") 0 -}} |
| 75 | + {{- $viewingYear = int $yearStr -}} |
| 76 | + {{- end -}} |
| 77 | + |
| 78 | + {{- /* Find the latest year among all OTHER events (excluding the one being viewed) */ -}} |
| 79 | + {{- $latestYear := 0 -}} |
| 80 | + {{- range $futureEvents -}} |
| 81 | + {{- $eventYear := 0 -}} |
| 82 | + {{- if .year -}} |
| 83 | + {{- $eventYear = int .year -}} |
| 84 | + {{- end -}} |
| 85 | + {{- if and $eventYear (gt $eventYear $latestYear) -}} |
| 86 | + {{- $latestYear = $eventYear -}} |
| 87 | + {{- end -}} |
| 88 | + {{- end -}} |
| 89 | + |
| 90 | + {{- range $eventsWithoutDates -}} |
| 91 | + {{- $eventYear := 0 -}} |
| 92 | + {{- if .name -}} |
| 93 | + {{- $yearStr := index (split .name "-") 0 -}} |
| 94 | + {{- $eventYear = int $yearStr -}} |
| 95 | + {{- end -}} |
| 96 | + {{- if and $eventYear (gt $eventYear $latestYear) -}} |
| 97 | + {{- $latestYear = $eventYear -}} |
| 98 | + {{- end -}} |
| 99 | + {{- end -}} |
| 100 | + |
| 101 | + {{- /* Only return a current event if another event has a newer year than what we're viewing */ -}} |
| 102 | + {{- if and (gt $latestYear 0) (gt $latestYear $viewingYear) -}} |
| 103 | + {{- /* Find the event from the latest year - prefer future with dates, then TBD */ -}} |
| 104 | + {{- $futureFromLatestYear := dict -}} |
| 105 | + {{- $tbdFromLatestYear := dict -}} |
| 106 | + |
| 107 | + {{- /* Look for future events with dates from the latest year */ -}} |
| 108 | + {{- range $futureEvents -}} |
| 109 | + {{- $eventYear := 0 -}} |
| 110 | + {{- if .year -}} |
| 111 | + {{- $eventYear = int .year -}} |
| 112 | + {{- end -}} |
| 113 | + {{- if eq $eventYear $latestYear -}} |
| 114 | + {{- if or (not $futureFromLatestYear) (gt .startdate $futureFromLatestYear.startdate) -}} |
| 115 | + {{- $futureFromLatestYear = . -}} |
| 116 | + {{- end -}} |
| 117 | + {{- end -}} |
| 118 | + {{- end -}} |
| 119 | + |
| 120 | + {{- /* Look for TBD events from the latest year */ -}} |
| 121 | + {{- range $eventsWithoutDates -}} |
| 122 | + {{- $eventYear := 0 -}} |
| 123 | + {{- if .name -}} |
| 124 | + {{- $yearStr := index (split .name "-") 0 -}} |
| 125 | + {{- $eventYear = int $yearStr -}} |
| 126 | + {{- end -}} |
| 127 | + {{- if eq $eventYear $latestYear -}} |
| 128 | + {{- $tbdFromLatestYear = . -}} |
| 129 | + {{- end -}} |
| 130 | + {{- end -}} |
| 131 | + |
| 132 | + {{- /* Prefer future event with dates, fallback to TBD */ -}} |
| 133 | + {{- if $futureFromLatestYear -}} |
| 134 | + {{- $candidateEvent = $futureFromLatestYear -}} |
| 135 | + {{- else if $tbdFromLatestYear -}} |
| 136 | + {{- $candidateEvent = $tbdFromLatestYear -}} |
| 137 | + {{- end -}} |
| 138 | + {{- else if eq $latestYear 0 -}} |
| 139 | + {{- /* No future/TBD events found, check past events only if viewing year is not newer */ -}} |
| 140 | + {{- if gt (len $pastEvents) 0 -}} |
| 141 | + {{- $latestPastYear := 0 -}} |
| 142 | + {{- $latestPastEvent := dict -}} |
| 143 | + {{- range $pastEvents -}} |
| 144 | + {{- $eventYear := 0 -}} |
| 145 | + {{- if .year -}} |
| 146 | + {{- $eventYear = int .year -}} |
| 147 | + {{- end -}} |
| 148 | + {{- if and $eventYear (gt $eventYear $latestPastYear) -}} |
| 149 | + {{- $latestPastYear = $eventYear -}} |
| 150 | + {{- $latestPastEvent = . -}} |
| 151 | + {{- end -}} |
| 152 | + {{- end -}} |
| 153 | + {{- /* Only use past event if its year is newer than viewing year */ -}} |
| 154 | + {{- if and $latestPastEvent (gt $latestPastYear $viewingYear) -}} |
| 155 | + {{- $candidateEvent = $latestPastEvent -}} |
| 156 | + {{- end -}} |
| 157 | + {{- end -}} |
| 158 | + {{- end -}} |
| 159 | + |
| 160 | + {{- $currentEvent = $candidateEvent -}} |
| 161 | +{{- end -}} |
| 162 | + |
| 163 | +{{- return $currentEvent -}} |
| 164 | + |
0 commit comments