Overview

Namespaces

  • Charcoal
    • Admin
      • Widget
        • Cms
    • Cms
      • Config
      • Mixin
        • Traits
      • Route
      • Section
      • Service
        • Loader
        • Manager
      • ServiceProvider
      • Support
        • Helpers
        • Interfaces
        • Traits
    • Property

Classes

  • Charcoal\Admin\Widget\Cms\HierarchicalSectionTableWidget
  • Charcoal\Admin\Widget\Cms\SectionTableWidget
  • Charcoal\Cms\AbstractDocument
  • Charcoal\Cms\AbstractEvent
  • Charcoal\Cms\AbstractFaq
  • Charcoal\Cms\AbstractImage
  • Charcoal\Cms\AbstractNews
  • Charcoal\Cms\AbstractSection
  • Charcoal\Cms\AbstractText
  • Charcoal\Cms\AbstractVideo
  • Charcoal\Cms\Config
  • Charcoal\Cms\Config\CmsConfig
  • Charcoal\Cms\Config\EventConfig
  • Charcoal\Cms\Config\NewsConfig
  • Charcoal\Cms\Config\SectionConfig
  • Charcoal\Cms\Document
  • Charcoal\Cms\DocumentCategory
  • Charcoal\Cms\EmptySection
  • Charcoal\Cms\Event
  • Charcoal\Cms\EventCategory
  • Charcoal\Cms\ExternalSection
  • Charcoal\Cms\Faq
  • Charcoal\Cms\FaqCategory
  • Charcoal\Cms\Image
  • Charcoal\Cms\ImageCategory
  • Charcoal\Cms\News
  • Charcoal\Cms\NewsCategory
  • Charcoal\Cms\Route\EventRoute
  • Charcoal\Cms\Route\GenericRoute
  • Charcoal\Cms\Route\NewsRoute
  • Charcoal\Cms\Route\SectionRoute
  • Charcoal\Cms\Section
  • Charcoal\Cms\Section\BlocksSection
  • Charcoal\Cms\Section\ContentSection
  • Charcoal\Cms\Service\Loader\AbstractLoader
  • Charcoal\Cms\Service\Loader\EventLoader
  • Charcoal\Cms\Service\Loader\NewsLoader
  • Charcoal\Cms\Service\Loader\SectionLoader
  • Charcoal\Cms\Service\Manager\AbstractManager
  • Charcoal\Cms\Service\Manager\EventManager
  • Charcoal\Cms\Service\Manager\NewsManager
  • Charcoal\Cms\ServiceProvider\CmsServiceProvider
  • Charcoal\Cms\Support\Helpers\DateHelper
  • Charcoal\Cms\Tag
  • Charcoal\Cms\Text
  • Charcoal\Cms\TextCategory
  • Charcoal\Cms\Video
  • Charcoal\Cms\VideoCategory
  • Charcoal\Property\TemplateOptionsProperty
  • Charcoal\Property\TemplateProperty

Interfaces

  • Charcoal\Cms\DocumentInterface
  • Charcoal\Cms\EventInterface
  • Charcoal\Cms\FaqInterface
  • Charcoal\Cms\ImageInterface
  • Charcoal\Cms\MetatagInterface
  • Charcoal\Cms\Mixin\HasContentBlocksInterface
  • Charcoal\Cms\NewsInterface
  • Charcoal\Cms\SearchableInterface
  • Charcoal\Cms\SectionInterface
  • Charcoal\Cms\Support\Interfaces\EventManagerAwareInterface
  • Charcoal\Cms\Support\Interfaces\NewsManagerAwareInterface
  • Charcoal\Cms\Support\Interfaces\SectionLoaderAwareInterface
  • Charcoal\Cms\TemplateableInterface
  • Charcoal\Cms\TextInterface
  • Charcoal\Cms\VideoInterface

Traits

  • Charcoal\Admin\Widget\Cms\SectionTableTrait
  • Charcoal\Cms\MetatagTrait
  • Charcoal\Cms\Mixin\Traits\HasContentBlocksTrait
  • Charcoal\Cms\SearchableTrait
  • Charcoal\Cms\Support\Traits\DateHelperAwareTrait
  • Charcoal\Cms\Support\Traits\EventManagerAwareTrait
  • Charcoal\Cms\Support\Traits\NewsManagerAwareTrait
  • Charcoal\Cms\Support\Traits\SectionLoaderAwareTrait
  • Charcoal\Cms\TemplateableTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\Cms\Support\Traits;
  4: 
  5: // local dependencies
  6: use Charcoal\Cms\NewsInterface;
  7: use Charcoal\Cms\Service\Manager\NewsManager;
  8: 
  9: // dependencies from `charcoal-base`
 10: use Charcoal\Object\CategoryInterface;
 11: 
 12: // dependencies from pimple
 13: use Slim\Exception\ContainerException;
 14: 
 15: trait NewsManagerAwareTrait
 16: {
 17:     /**
 18:      * Currently displayed news.
 19:      * @var array $news City/Object/News
 20:      */
 21:     private $currentNews;
 22: 
 23:     /**
 24:      * @var NewsManager $newsManager
 25:      */
 26:     private $newsManager;
 27: 
 28:     // ==========================================================================
 29:     // FUNCTIONS
 30:     // ==========================================================================
 31: 
 32:     /**
 33:      * Formatted news list
 34:      * Returns the entries for the current page.
 35:      * @return \Generator|void
 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:      * Formatted news archive list
 47:      * Returns the entries for the current page.
 48:      * @return \Generator|void
 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:      * Current news.
 60:      * @return array The properties of the current news
 61:      */
 62:     public function currentNews()
 63:     {
 64:         if ($this->currentNews) {
 65:             return $this->currentNews;
 66:         }
 67: 
 68:         // The the current news
 69:         $news = $this->newsManager()->entry();
 70: 
 71:         // Format the news if there's any
 72:         if ($news) {
 73:             $this->currentNews = $this->newsFormatFull($news);
 74:         }
 75: 
 76:         return $this->currentNews;
 77:     }
 78: 
 79:     /**
 80:      * @return \Generator
 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:      * Next news in list.
 97:      * @return array The next news properties.
 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:      * Next news in list.
112:      * @return array The next news properties.
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:      * Amount of news (total)
127:      * @return integer How many news?
128:      */
129:     public function numNews()
130:     {
131:         return $this->newsManager()->numNews();
132:     }
133: 
134:     /**
135:      * @return float
136:      */
137:     public function numNewsPages()
138:     {
139:         return $this->newsManager()->numPages();
140:     }
141: 
142:     /**
143:      * @return boolean
144:      */
145:     public function newsHasPager()
146:     {
147:         return $this->newsManager()->hasPager();
148:     }
149: 
150:     /**
151:      * @return \Generator
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:      * @param NewsInterface $news Charcoal\Cms\NewsInterface.
163:      * @return CategoryInterface
164:      */
165:     public function newsCategory(NewsInterface $news)
166:     {
167:         $id = $news->category();
168: 
169:         return $this->newsManager()->categoryItem($id);
170:     }
171: 
172:     // ==========================================================================
173:     // FORMATTER
174:     // ==========================================================================
175: 
176:     /**
177:      * @param NewsInterface $news Charcoal\Cms\NewsInterface.
178:      * @return string
179:      */
180:     protected function getNewsDateFormat(NewsInterface $news)
181:     {
182:         return $this->dateHelper()->formatDate(
183:             $news->newsDate()
184:         );
185:     }
186: 
187:     /**
188:      * Formatting expected in templates
189:      * @param NewsInterface $news A single news.
190:      * @return array The needed news properties.
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:      * Formatting expected in templates
206:      * @param NewsInterface $news A single news.
207:      * @return array The needed news properties.
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:      * @param NewsInterface $news The current news.
222:      * @return array The needed properties.
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:      * @param CategoryInterface $category The category item.
252:      * @return array The formatted category item.
253:      */
254:     protected function newsFormatCategory(CategoryInterface $category)
255:     {
256:         return [
257:             'id'   => $category->id(),
258:             'name' => (string)$category->name(),
259:         ];
260:     }
261: 
262:     // ==========================================================================
263:     // DEPENDENCIES
264:     // ==========================================================================
265: 
266:     /**
267:      * @return NewsManager
268:      * @throws ContainerException When dependency is missing.
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:      * @param NewsManager $newsManager The news Manager class.
285:      * @return self
286:      */
287:     protected function setNewsManager(NewsManager $newsManager)
288:     {
289:         $this->newsManager = $newsManager;
290: 
291:         return $this;
292:     }
293: 
294:     /**
295:      * dateHelperAwareTrait dependency
296:      * @return mixed
297:      */
298:     abstract protected function dateHelper();
299: }
300: 
API documentation generated by ApiGen