1: <?php
2:
3: namespace Charcoal\Cms\Support\Traits;
4:
5: use Charcoal\Cms\SectionInterface;
6: use Charcoal\Model\ModelInterface;
7: use Charcoal\Cms\Service\Loader\SectionLoader;
8: use Slim\Exception\ContainerException;
9:
10: trait SectionLoaderAwareTrait
11: {
12: /**
13: * @var SectionInterface $section
14: */
15: private $section;
16:
17: /**
18: * @var SectionInterface[] $sections
19: */
20: private $sections;
21:
22: /**
23: * @var SectionInterface[] $masterSections
24: */
25: private $masterSections;
26:
27: /**
28: * @var SectionInterface[] $childrenSections
29: */
30: private $childrenSections;
31:
32: /**
33: * @var SectionLoader $sectionLoader City\\Loader\\SectionLoader.
34: */
35: private $sectionLoader;
36:
37: // ==========================================================================
38: // INIT
39: // ==========================================================================
40:
41: /**
42: * Must call section if the context object is of type Section.
43: * @param ModelInterface $context The current context.
44: * @return mixed
45: */
46: abstract public function setContextObject(ModelInterface $context);
47:
48: /**
49: * @return \ArrayAccess|\Traversable
50: */
51: public function sections()
52: {
53: return $this->sectionLoader()->all()->load();
54: }
55:
56: /**
57: * @return \ArrayAccess|\Traversable
58: */
59: public function masterSections()
60: {
61: return $this->sectionLoader()->masters();
62: }
63:
64: /**
65: * @return \ArrayAccess|\Traversable
66: */
67: public function childrenSections()
68: {
69: return $this->sectionLoader()->children();
70: }
71:
72: /**
73: * Gets latest route for the given slug.
74: * @return string The latest url.
75: */
76: public function routes()
77: {
78: return function ($arg) {
79: return $this->sectionLoader()->resolveRoute($arg);
80: };
81: }
82:
83: /**
84: * Gets the current section based on AbstractTemplate::Section and context.
85: * @param boolean $raw Option the receive the non-formatted section.
86: * @return array|SectionInterface
87: */
88: public function currentSection($raw = false)
89: {
90: if ($raw) {
91: return $this->section();
92: }
93:
94: return $this->formatSection($this->section());
95: }
96:
97: /**
98: * @param string $slug The section slug to load from.
99: * @return SectionInterface|array
100: */
101: public function sectionFromSlug($slug)
102: {
103: return $this->sectionLoader()->fromSlug($slug);
104: }
105:
106: // ==========================================================================
107: // GETTERS
108: // ==========================================================================
109:
110: /**
111: * @return SectionInterface
112: */
113: protected function section()
114: {
115: return $this->section;
116: }
117:
118: // ==========================================================================
119: // SETTERS
120: // ==========================================================================
121:
122: /**
123: * @param SectionInterface $section The current section.
124: * @return self
125: */
126: protected function setSection(SectionInterface $section)
127: {
128: $this->section = $section;
129:
130: return $this;
131: }
132:
133: // ==========================================================================
134: // DEPENDENCIES
135: // ==========================================================================
136:
137: /**
138: * @return SectionLoader
139: * @throws ContainerException When dependency is missing.
140: */
141: protected function sectionLoader()
142: {
143: if (!$this->sectionLoader instanceof SectionLoader) {
144: throw new ContainerException(sprintf(
145: 'Missing dependency for %s: %s',
146: get_called_class(),
147: SectionLoader::class
148: ));
149: }
150:
151: return $this->sectionLoader;
152: }
153:
154: /**
155: * @param SectionLoader $loader The section loader.
156: * @return self
157: */
158: protected function setSectionLoader(SectionLoader $loader)
159: {
160: $this->sectionLoader = $loader;
161:
162: return $this;
163: }
164:
165: // ==========================================================================
166: // FORMATTER
167: // ==========================================================================
168:
169: /**
170: * @param SectionInterface $section The section to format.
171: * @return array
172: */
173: protected function formatSection(SectionInterface $section)
174: {
175: $contentBlocks = $section->attachments('content-blocks');
176: $gallery = $section->attachments('image-gallery');
177: $documents = $section->attachments('document');
178:
179: return [
180: 'title' => (string)$section->title(),
181: 'summary' => (string)$section->summary(),
182: 'image' => (string)$section->image(),
183: 'content' => (string)$section->content(),
184: 'contentBlocks' => $contentBlocks,
185: 'gallery' => $gallery,
186: 'documents' => $documents
187: ];
188: }
189: }
190: