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\EventInterface;
20: use Charcoal\Object\RoutableInterface;
21:
22: 23: 24:
25: class EventRoute extends TemplateRoute
26: {
27: use TranslatorAwareTrait;
28:
29: 30: 31: 32: 33:
34: private $path;
35:
36: 37: 38: 39: 40:
41: private $event;
42:
43: 44: 45: 46: 47:
48: private $objType = 'charcoal/cms/event';
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: $event = $this->loadEventFromPath($container);
68: return !!$event->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: $event = $this->loadEventFromPath($container);
85:
86: $templateIdent = $event->templateIdent();
87: $templateController = $event->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->setEvent($event);
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 loadEventFromPath(Container $container)
115: {
116: if (!$this->event) {
117: $config = $this->config();
118: $objType = (isset($config['obj_type']) ? $config['obj_type'] : $this->objType);
119:
120: $this->event = $container['model/factory']->create($objType);
121: $this->setTranslator($container['translator']);
122:
123: $langs = $container['translator']->availableLocales();
124: $lang = $this->event->loadFromL10n('slug', $this->path, $langs);
125: $container['translator']->setLocale($lang);
126: }
127:
128: return $this->event;
129: }
130: }
131: