1: <?php
2:
3: namespace Charcoal\Cms\Support\Traits;
4:
5:
6: use Charcoal\Cms\NewsInterface;
7: use Charcoal\Cms\Service\Manager\NewsManager;
8:
9:
10: use Charcoal\Object\CategoryInterface;
11:
12:
13: use Slim\Exception\ContainerException;
14:
15: trait NewsManagerAwareTrait
16: {
17: 18: 19: 20:
21: private $currentNews;
22:
23: 24: 25:
26: private $newsManager;
27:
28:
29:
30:
31:
32: 33: 34: 35: 36:
37: public function newsList()
38: {
39: $entries = $this->newsManager()->entries();
40: foreach ($entries as $news) {
41: yield $this->newsFormatShort($news);
42: }
43: }
44:
45: 46: 47: 48: 49:
50: public function newsArchiveList()
51: {
52: $entries = $this->newsManager()->archive();
53: foreach ($entries as $entry) {
54: yield $this->newsFormatShort($entry);
55: }
56: }
57:
58: 59: 60: 61:
62: public function currentNews()
63: {
64: if ($this->currentNews) {
65: return $this->currentNews;
66: }
67:
68:
69: $news = $this->newsManager()->entry();
70:
71:
72: if ($news) {
73: $this->currentNews = $this->newsFormatFull($news);
74: }
75:
76: return $this->currentNews;
77: }
78:
79: 80: 81:
82: public function featNews()
83: {
84: $entries = $this->newsManager()->featList();
85:
86: if (count($entries) > 0) {
87: foreach ($entries as $entry) {
88: if ($entry->id()) {
89: yield $this->newsFormatFull($entry);
90: }
91: }
92: }
93: }
94:
95: 96: 97: 98:
99: public function nextNews()
100: {
101: $next = $this->newsManager()->next();
102:
103: if (!$next) {
104: return null;
105: }
106:
107: return $this->newsFormatNav($next);
108: }
109:
110: 111: 112: 113:
114: public function prevNews()
115: {
116: $prev = $this->newsManager()->prev();
117:
118: if (!$prev) {
119: return null;
120: }
121:
122: return $this->newsFormatNav($prev);
123: }
124:
125: 126: 127: 128:
129: public function numNews()
130: {
131: return $this->newsManager()->numNews();
132: }
133:
134: 135: 136:
137: public function numNewsPages()
138: {
139: return $this->newsManager()->numPages();
140: }
141:
142: 143: 144:
145: public function newsHasPager()
146: {
147: return $this->newsManager()->hasPager();
148: }
149:
150: 151: 152:
153: public function newsCategoryList()
154: {
155: $cats = $this->newsManager()->categoryItems();
156: foreach ($cats as $cat) {
157: yield $this->newsFormatCategory($cat);
158: }
159: }
160:
161: 162: 163: 164:
165: public function newsCategory(NewsInterface $news)
166: {
167: $id = $news->category();
168:
169: return $this->newsManager()->categoryItem($id);
170: }
171:
172:
173:
174:
175:
176: 177: 178: 179:
180: protected function getNewsDateFormat(NewsInterface $news)
181: {
182: return $this->dateHelper()->formatDate(
183: $news->newsDate()
184: );
185: }
186:
187: 188: 189: 190: 191:
192: protected function newsFormatShort(NewsInterface $news)
193: {
194: return [
195: 'title' => (string)$news->title(),
196: 'url' => (string)$news->url(),
197: 'date' => $this->getNewsDateFormat($news),
198: 'active' => ($this->currentNews() && ($this->currentNews()['id'] == $news->id())),
199: 'category' => $news->category(),
200: 'categoryName' => $this->newsCategory($news)->name()
201: ];
202: }
203:
204: 205: 206: 207: 208:
209: protected function newsFormatNav(NewsInterface $news)
210: {
211: return [
212: 'date' => $this->getNewsDateFormat($news),
213: 'title' => (string)$news->title(),
214: 'url' => $news->url(),
215: 'category' => $news->category(),
216: 'categoryName' => $this->newsCategory($news)->name()
217: ];
218: }
219:
220: 221: 222: 223:
224: protected function newsFormatFull(NewsInterface $news)
225: {
226: $contentBlocks = $news->attachments('content-blocks');
227: $gallery = $news->attachments('image-gallery');
228: $documents = $news->attachments('document');
229:
230: return [
231: 'id' => $news->id(),
232: 'title' => (string)$news->title(),
233: 'summary' => (string)$news->summary(),
234: 'content' => (string)$news->content(),
235: 'image' => $news->image(),
236: 'date' => $this->getNewsDateFormat($news),
237: 'contentBlocks' => $contentBlocks,
238: 'hasContentBlocks' => !!(count($contentBlocks)),
239: 'documents' => $documents,
240: 'hasDocuments' => !!(count($documents)),
241: 'gallery' => $gallery,
242: 'hasGallery' => !!(count($gallery)),
243: 'url' => $news->url(),
244: 'metaTitle' => (string)$news->metaTitle(),
245: 'category' => $news->category(),
246: 'categoryName' => $this->newsCategory($news)->name()
247: ];
248: }
249:
250: 251: 252: 253:
254: protected function newsFormatCategory(CategoryInterface $category)
255: {
256: return [
257: 'id' => $category->id(),
258: 'name' => (string)$category->name(),
259: ];
260: }
261:
262:
263:
264:
265:
266: 267: 268: 269:
270: protected function newsManager()
271: {
272: if (!$this->newsManager instanceof NewsManager) {
273: throw new ContainerException(sprintf(
274: 'Missing dependency for %s: %s',
275: get_called_class(),
276: NewsManager::class
277: ));
278: }
279:
280: return $this->newsManager;
281: }
282:
283: 284: 285: 286:
287: protected function setNewsManager(NewsManager $newsManager)
288: {
289: $this->newsManager = $newsManager;
290:
291: return $this;
292: }
293:
294: 295: 296: 297:
298: abstract protected function dateHelper();
299: }
300: