1: <?php
2:
3: namespace Charcoal\Cms\Route;
4:
5:
6: use Pimple\Container;
7:
8:
9: use Psr\Http\Message\RequestInterface;
10: use Psr\Http\Message\ResponseInterface;
11:
12:
13: use Charcoal\Translator\TranslatorAwareTrait;
14:
15:
16: use Charcoal\App\Route\TemplateRoute;
17:
18:
19: use Charcoal\Cms\NewsInterface;
20: use Charcoal\Object\RoutableInterface;
21:
22: 23: 24:
25: class NewsRoute extends TemplateRoute
26: {
27: use TranslatorAwareTrait;
28:
29: 30: 31: 32: 33:
34: private $path;
35:
36: 37: 38: 39: 40:
41: private $news;
42:
43: 44: 45: 46: 47:
48: private $objType = 'charcoal/cms/news';
49:
50: 51: 52:
53: public function __construct(array $data)
54: {
55: parent::__construct($data);
56: $this->path = ltrim($data['path'], '/');
57: }
58:
59: 60: 61: 62: 63: 64:
65: public function pathResolvable(Container $container)
66: {
67: $news = $this->loadNewsFromPath($container);
68: return !!$news->id();
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function __invoke(
78: Container $container,
79: RequestInterface $request,
80: ResponseInterface $response
81: ) {
82: $config = $this->config();
83:
84: $news = $this->loadNewsFromPath($container);
85:
86: $templateIdent = $news->templateIdent();
87: $templateController = $news->templateIdent();
88:
89: if (!$templateController) {
90: return $response->withStatus(404);
91: }
92:
93: $templateFactory = $container['template/factory'];
94:
95: $template = $templateFactory->create($templateController);
96: $template->init($request);
97:
98:
99: $template->setData($config['template_data']);
100: $template->setNews($news);
101:
102: $templateContent = $container['view']->render($templateIdent, $template);
103:
104: $response->write($templateContent);
105:
106: return $response;
107: }
108:
109: 110: 111: 112: 113:
114: protected function loadNewsFromPath(Container $container)
115: {
116: if (!$this->news) {
117: $config = $this->config();
118: $objType = (isset($config['obj_type']) ? $config['obj_type'] : $this->objType);
119:
120: $this->news = $container['model/factory']->create($objType);
121:
122: $langs = $container['translator']->availableLocales();
123: $lang = $this->news->loadFromL10n('slug', $this->path, $langs);
124: $container['translator']->setLocale($lang);
125: }
126:
127: return $this->news;
128: }
129: }
130: