1: <?php
2:
3: namespace Charcoal\Cms\Support\Traits;
4:
5: use Charcoal\Cms\EventInterface;
6: use Charcoal\Cms\Service\Manager\EventManager;
7: use Charcoal\Object\CategoryInterface;
8: use Slim\Exception\ContainerException;
9:
10: trait EventManagerAwareTrait
11: {
12: 13: 14: 15:
16: private $currentEvent;
17:
18: 19: 20:
21: private $eventManager;
22:
23:
24:
25:
26:
27: 28: 29: 30: 31:
32: public function eventsList()
33: {
34: $eventList = $this->eventManager()->entries();
35: foreach ($eventList as $event) {
36: yield $this->eventFormatShort($event);
37: }
38: }
39:
40: 41: 42: 43: 44:
45: public function eventArchiveList()
46: {
47: $entries = $this->eventManager()->archive();
48: foreach ($entries as $entry) {
49: yield $this->eventFormatShort($entry);
50: }
51: }
52:
53: 54: 55: 56:
57: public function currentEvent()
58: {
59: if ($this->currentEvent) {
60: return $this->currentEvent;
61: }
62:
63:
64: $event = $this->eventManager()->entry();
65:
66:
67: if ($event) {
68: $this->currentEvent = $this->eventFormatFull($event);
69: }
70:
71: return $this->currentEvent;
72: }
73:
74: 75: 76:
77: public function featEvents()
78: {
79: $entries = $this->eventManager()->featList();
80:
81: if (!$entries) {
82: return;
83: }
84:
85: foreach ($entries as $entry) {
86: if ($entry->id()) {
87: yield $this->eventFormatFull($entry);
88: }
89: }
90: }
91:
92: 93: 94: 95:
96: public function nextEvent()
97: {
98: $next = $this->eventManager()->next();
99:
100: if (!$next) {
101: return null;
102: }
103:
104: return $this->eventFormatNav($next);
105: }
106:
107: 108: 109: 110:
111: public function prevEvent()
112: {
113: $prev = $this->eventManager()->prev();
114:
115: if (!$prev) {
116: return null;
117: }
118:
119: return $this->eventFormatNav($prev);
120: }
121:
122: 123: 124: 125:
126: public function numEvent()
127: {
128: return $this->eventManager()->numEvent();
129: }
130:
131: 132: 133:
134: public function numEventPages()
135: {
136: return $this->eventManager()->numPages();
137: }
138:
139: 140: 141:
142: public function eventHasPager()
143: {
144: return $this->eventManager()->hasPager();
145: }
146:
147: 148: 149:
150: public function eventCategoryList()
151: {
152: $cats = $this->eventManager()->categoryItems();
153: foreach ($cats as $cat) {
154: yield $this->eventFormatCategory($cat);
155: }
156: }
157:
158: 159: 160: 161:
162: protected function eventCategory(EventInterface $event)
163: {
164: $id = $event->category();
165:
166: return $this->eventManager()->categoryItem($id);
167: }
168:
169:
170:
171:
172:
173: 174: 175: 176:
177: protected function getEventStartDateFormat(EventInterface $event)
178: {
179: return $this->dateHelper()->formatDate(
180: $event->startDate()
181: );
182: }
183:
184: 185: 186: 187:
188: protected function getEventEndDateFormat(EventInterface $event)
189: {
190: return $this->dateHelper()->formatDate(
191: $event->endDate()
192: );
193: }
194:
195: 196: 197: 198:
199: protected function getEventDateFormat(EventInterface $event)
200: {
201: return $this->dateHelper()->formatDate([
202: $event->startDate(),
203: $event->endDate()
204: ]);
205: }
206:
207: 208: 209: 210:
211: protected function getEventTimeFormat(EventInterface $event)
212: {
213: if ($event->dateNotes() != '') {
214: return $event->dateNotes();
215: }
216:
217: if (!$event->displayHours()) {
218: return null;
219: }
220:
221: return $this->dateHelper()->formatTime([
222: $event->startDate(),
223: $event->endDate(),
224: ]);
225: }
226:
227: 228: 229: 230: 231:
232: protected function eventFormatShort(EventInterface $event)
233: {
234: return [
235: 'title' => (string)$event->title(),
236: 'url' => (string)$event->url(),
237: 'startDate' => $this->getEventStartDateFormat($event),
238: 'endDate' => $this->getEventEndDateFormat($event),
239: 'date' => $this->getEventDateFormat($event),
240: 'time' => $this->getEventTimeFormat($event),
241: 'active' => ($this->currentEvent() && ($this->currentEvent()['id'] == $event->id()))
242: ];
243: }
244:
245: 246: 247: 248: 249:
250: protected function eventFormatNav(EventInterface $event)
251: {
252: return [
253: 'startDate' => $this->getEventStartDateFormat($event),
254: 'endDate' => $this->getEventEndDateFormat($event),
255: 'date' => $this->getEventDateFormat($event),
256: 'time' => $this->getEventTimeFormat($event),
257: 'title' => (string)$event->title(),
258: 'url' => $event->url()
259: ];
260: }
261:
262: 263: 264: 265:
266: protected function eventFormatFull(EventInterface $event)
267: {
268: $contentBlocks = $event->attachments('content-blocks');
269: $gallery = $event->attachments('image-gallery');
270: $documents = $event->attachments('document');
271:
272: return [
273: 'id' => $event->id(),
274: 'title' => (string)$event->title(),
275: 'summary' => (string)$event->summary(),
276: 'content' => (string)$event->content(),
277: 'image' => $event->image(),
278: 'startDate' => $this->getEventStartDateFormat($event),
279: 'endDate' => $this->getEventEndDateFormat($event),
280: 'date' => $this->getEventDateFormat($event),
281: 'time' => $this->getEventTimeFormat($event),
282: 'contentBlocks' => $contentBlocks,
283: 'hasContentBlocks' => !!(count($contentBlocks)),
284: 'documents' => $documents,
285: 'hasDocuments' => !!(count($documents)),
286: 'gallery' => $gallery,
287: 'hasGallery' => !!(count($gallery)),
288: 'url' => $event->url(),
289: 'metaTitle' => (string)$event->metaTitle(),
290: 'locationName' => (string)$event->locationName(),
291: 'dateNotes' => (string)$event->dateNotes(),
292: 'category' => $event->category()
293: ];
294: }
295:
296: 297: 298: 299:
300: protected function eventFormatCategory(CategoryInterface $category)
301: {
302: return [
303: 'id' => $category->id(),
304: 'name' => (string)$category->name(),
305: ];
306: }
307:
308:
309:
310:
311:
312: 313: 314: 315:
316: protected function eventManager()
317: {
318: if (!$this->eventManager instanceof EventManager) {
319: throw new ContainerException(sprintf(
320: 'Missing dependency for %s: %s',
321: get_called_class(),
322: EventManager::class
323: ));
324: }
325:
326: return $this->eventManager;
327: }
328:
329: 330: 331: 332:
333: protected function setEventManager(EventManager $eventManager)
334: {
335: $this->eventManager = $eventManager;
336:
337: return $this;
338: }
339:
340: 341: 342: 343:
344: abstract protected function dateHelper();
345: }
346: