1: <?php
2:
3: namespace Charcoal\Cms;
4:
5: use DateTime;
6: use DateTimeInterface;
7: use InvalidArgumentException;
8:
9:
10: use Psr\Http\Message\RequestInterface;
11: use Psr\Http\Message\ResponseInterface;
12:
13:
14: use Charcoal\Object\Content;
15: use Charcoal\Object\CategorizableInterface;
16: use Charcoal\Object\CategorizableTrait;
17: use Charcoal\Object\PublishableInterface;
18: use Charcoal\Object\PublishableTrait;
19: use Charcoal\Object\RoutableInterface;
20: use Charcoal\Object\RoutableTrait;
21:
22:
23: use Charcoal\Translator\Translation;
24:
25:
26: use Charcoal\Cms\MetatagInterface;
27: use Charcoal\Cms\NewsInterface;
28: use Charcoal\Cms\SearchableInterface;
29: use Charcoal\Cms\SearchableTrait;
30: use Charcoal\Cms\TemplateableInterface;
31:
32:
33: use Charcoal\Cms\Support\Helpers\DateHelper;
34:
35:
36: use Pimple\Container;
37:
38: 39: 40:
41: abstract class AbstractNews extends Content implements
42: CategorizableInterface,
43: MetatagInterface,
44: NewsInterface,
45: PublishableInterface,
46: RoutableInterface,
47: SearchableInterface,
48: TemplateableInterface
49: {
50: use CategorizableTrait;
51: use PublishableTrait;
52: use MetatagTrait;
53: use RoutableTrait;
54: use SearchableTrait;
55: use TemplateableTrait;
56:
57: 58: 59:
60: private $title;
61:
62: 63: 64:
65: private $subtitle;
66:
67: 68: 69:
70: private $summary;
71:
72: 73: 74:
75: private $content;
76:
77: 78: 79:
80: private $image;
81:
82: 83: 84:
85: private $newsDate;
86:
87: 88: 89:
90: private $infoUrl;
91:
92: 93: 94:
95: protected $keywords;
96:
97:
98:
99:
100:
101: 102: 103: 104:
105: public function __construct(array $data = null)
106: {
107: parent::__construct($data);
108:
109: if (is_callable([ $this, 'defaultData' ])) {
110: $this->setData($this->defaultData());
111: }
112: }
113:
114:
115:
116:
117:
118: 119: 120: 121:
122: public function dateTimeDate()
123: {
124: $newsDate = $this->newsDate();
125:
126: return $newsDate->format('Y-m-d H:i:s');
127: }
128:
129: 130: 131: 132:
133: public function verifyDates()
134: {
135: if (!$this->newsDate()) {
136: $this->setNewsDate('now');
137: }
138:
139: if (!$this->publishDate()) {
140: $this->setPublishDate('now');
141: }
142: }
143:
144: 145: 146:
147: public function adminDateFilter()
148: {
149: return $this->newsDate()->format('Y-m-d');
150: }
151:
152:
153:
154:
155:
156: 157: 158: 159:
160: public function setTitle($title)
161: {
162: $this->title = $this->translator()->translation($title);
163:
164: return $this;
165: }
166:
167: 168: 169: 170:
171: public function setSubtitle($subtitle)
172: {
173: $this->subtitle = $this->translator()->translation($subtitle);
174:
175: return $this;
176: }
177:
178: 179: 180: 181:
182: public function setSummary($summary)
183: {
184: $this->summary = $this->translator()->translation($summary);
185:
186: return $this;
187: }
188:
189: 190: 191: 192:
193: public function setContent($content)
194: {
195: $this->content = $this->translator()->translation($content);
196:
197: return $this;
198: }
199:
200: 201: 202: 203:
204: public function setImage($image)
205: {
206: $this->image = $this->translator()->translation($image);
207:
208: return $this;
209: }
210:
211: 212: 213: 214:
215: public function setInfoUrl($url)
216: {
217: $this->infoUrl = $this->translator()->translation($url);
218:
219: return $this;
220: }
221:
222: 223: 224: 225: 226:
227: public function setNewsDate($newsDate)
228: {
229: if ($newsDate === null || $newsDate === '') {
230: $this->newsDate = null;
231:
232: return $this;
233: }
234: if (is_string($newsDate)) {
235: $newsDate = new DateTime($newsDate);
236: }
237: if (!($newsDate instanceof DateTimeInterface)) {
238: throw new InvalidArgumentException(
239: 'Invalid "Revision Date" value. Must be a date/time string or a DateTimeInterface object.'
240: );
241: }
242: $this->newsDate = $newsDate;
243:
244: return $this;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function setKeywords($keywords)
254: {
255: $this->keywords = $this->parseAsMultiple($keywords);
256:
257: return $this;
258: }
259:
260:
261:
262:
263:
264: 265: 266:
267: public function title()
268: {
269: return $this->title;
270: }
271:
272: 273: 274:
275: public function subtitle()
276: {
277: return $this->subtitle;
278: }
279:
280: 281: 282:
283: public function summary()
284: {
285: return $this->summary;
286: }
287:
288: 289: 290:
291: public function infoUrl()
292: {
293: return $this->infoUrl;
294: }
295:
296: 297: 298:
299: public function newsDate()
300: {
301: return $this->newsDate;
302: }
303:
304: 305: 306:
307: public function content()
308: {
309: return $this->content;
310: }
311:
312: 313: 314:
315: public function image()
316: {
317: return $this->image;
318: }
319:
320:
321:
322:
323:
324: 325: 326: 327: 328: 329:
330: public function canonicalUrl()
331: {
332: return '';
333: }
334:
335: 336: 337:
338: public function defaultMetaTitle()
339: {
340: return $this->title();
341: }
342:
343: 344: 345:
346: public function defaultMetaDescription()
347: {
348: $content = $this->translator()->translation($this->content());
349: if ($content instanceof Translation) {
350: $desc = [];
351: foreach ($content->data() as $lang => $text) {
352: $desc[$lang] = strip_tags($text);
353: }
354:
355: return $this->translator()->translation($desc);
356: }
357:
358: return null;
359: }
360:
361: 362: 363:
364: public function defaultMetaImage()
365: {
366: return $this->image();
367: }
368:
369: 370: 371: 372: 373:
374: public function keywords()
375: {
376: return $this->keywords;
377: }
378:
379:
380:
381:
382:
383: 384: 385: 386: 387: 388: 389:
390: public function parseAsMultiple($value, $separator = ',')
391: {
392: if (!isset($value) ||
393: (is_string($value) && !strlen(trim($value))) ||
394: (is_array($value) && !count(array_filter($value, 'strlen')))
395: ) {
396: return [];
397: }
398:
399: 400: 401: 402: 403:
404: if (is_string($value)) {
405: return explode($separator, $value);
406: }
407:
408: 409: 410: 411: 412:
413: if (!is_array($value)) {
414: return [ $value ];
415: }
416:
417: return $value;
418: }
419:
420:
421:
422:
423:
424: 425: 426: 427: 428:
429: public function preSave()
430: {
431: $this->verifyDates();
432: $this->setSlug($this->generateSlug());
433: $this->generateDefaultMetaTags();
434:
435: return parent::preSave();
436: }
437:
438: 439: 440: 441: 442: 443:
444: public function preUpdate(array $properties = null)
445: {
446: $this->verifyDates();
447: $this->setSlug($this->generateSlug());
448: $this->generateDefaultMetaTags();
449:
450: return parent::preUpdate($properties);
451: }
452:
453: 454: 455:
456: public function postSave()
457: {
458:
459: $this->generateObjectRoute($this->slug());
460:
461: return parent::postSave();
462: }
463:
464: 465: 466: 467:
468: public function postUpdate(array $properties = null)
469: {
470:
471: $this->generateObjectRoute($this->slug());
472:
473: return parent::postUpdate($properties);
474: }
475:
476: 477: 478: 479: 480: 481:
482: public function isActiveRoute()
483: {
484: return (
485: $this->active() &&
486: $this->isPublished()
487: );
488: }
489: }
490: