1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Markus Tacker <[email protected]> |
5
|
|
|
* @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BCRM\WebBundle\Controller; |
9
|
|
|
|
10
|
|
|
use BCRM\BackendBundle\Entity\Event\Event; |
11
|
|
|
use BCRM\BackendBundle\Entity\Event\EventRepository; |
12
|
|
|
use BCRM\BackendBundle\Exception\FileNotFoundException; |
13
|
|
|
use BCRM\WebBundle\Content\ContentReader; |
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
17
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Renders the page. |
25
|
|
|
*/ |
26
|
|
|
class WebController |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ContentReader |
30
|
|
|
*/ |
31
|
|
|
private $reader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Symfony\Component\Form\FormFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $formFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
40
|
|
|
*/ |
41
|
|
|
private $router; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \BCRM\BackendBundle\Entity\Event\EventRepository |
45
|
|
|
*/ |
46
|
|
|
private $eventRepo; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface |
50
|
|
|
*/ |
51
|
|
|
private $renderer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int Unix timestamp of assets modification |
55
|
|
|
*/ |
56
|
|
|
private $assetsVersion; |
57
|
|
|
|
58
|
|
|
public function __construct( |
59
|
|
|
ContentReader $reader, |
60
|
|
|
FormFactoryInterface $formFactory, |
61
|
|
|
RouterInterface $router, |
62
|
|
|
EventRepository $eventRepo, |
63
|
|
|
EngineInterface $renderer, |
64
|
|
|
$assetsVersion |
65
|
|
|
) |
66
|
|
|
{ |
67
|
|
|
$this->reader = $reader; |
68
|
|
|
$this->formFactory = $formFactory; |
69
|
|
|
$this->router = $router; |
70
|
|
|
$this->eventRepo = $eventRepo; |
71
|
|
|
$this->renderer = $renderer; |
72
|
|
|
$this->assetsVersion = $assetsVersion; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Render the index page. |
77
|
|
|
* |
78
|
|
|
* @param Request $request |
79
|
|
|
* |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
public function indexAction(Request $request) |
83
|
|
|
{ |
84
|
|
|
$response = $this->pageAction($request, 'Index'); |
|
|
|
|
85
|
|
|
if ($response->isNotModified($request)) { |
86
|
|
|
return $response; |
87
|
|
|
} |
88
|
|
|
$nextEvent = $this->eventRepo->getNextEvent(); |
89
|
|
|
|
90
|
|
|
$data = array( |
91
|
|
|
'page' => $this->reader->getPage('Index.md'), |
92
|
|
|
'path' => 'Index', |
93
|
|
|
'sponsors' => $this->reader->getPage('Sponsoren/Index.md') |
94
|
|
|
); |
95
|
|
|
if ($nextEvent->isDefined()) { |
96
|
|
|
/* @var Event $event */ |
97
|
|
|
$event = $nextEvent->get(); |
98
|
|
|
if (Carbon::createFromTimestamp($event->getRegistrationEnd()->getTimestamp())->isFuture()) { |
99
|
|
|
$data ['nextEvent'] = $nextEvent; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return $this->renderer->renderResponse('BCRMWebBundle:Web:index.html.twig', $data, $response); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Render a content page. |
107
|
|
|
* |
108
|
|
|
* @param Request $request |
109
|
|
|
* @param string $path |
110
|
|
|
* |
111
|
|
|
* @return Response|array |
112
|
|
|
* @throws NotFoundHttpException |
113
|
|
|
*/ |
114
|
|
|
public function pageAction(Request $request, $path) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$pageInfo = $this->reader->getInfo($path . '.md'); |
118
|
|
|
} catch (FileNotFoundException $e) { |
119
|
|
|
throw new NotFoundHttpException(); |
120
|
|
|
} |
121
|
|
|
$response = new Response(); |
122
|
|
|
$response->setETag($pageInfo->getEtag()); |
123
|
|
|
$lastModified = $pageInfo->getLastModified(); |
124
|
|
View Code Duplication |
if ($this->assetsVersion - $pageInfo->getLastModified()->getTimestamp() > 0) { |
|
|
|
|
125
|
|
|
$lastModified = new \DateTime('@' . $this->assetsVersion); |
126
|
|
|
} |
127
|
|
|
$response->setLastModified($lastModified); |
128
|
|
|
$response->setPublic(); |
129
|
|
|
if ($response->isNotModified($request)) { |
130
|
|
|
return $response; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$p = $this->reader->getPage($path . '.md'); |
134
|
|
|
if ($p->isHidden()) throw new NotFoundHttpException(); |
135
|
|
|
|
136
|
|
|
return $this->renderer->renderResponse('BCRMWebBundle:Web:page.html.twig', array( |
137
|
|
|
'page' => $p, |
138
|
|
|
'path' => $path, |
139
|
|
|
'sponsors' => $this->reader->getPage('Sponsoren/Index.md') |
140
|
|
|
), $response); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Render a content page without the body. Used for ajax calls. |
145
|
|
|
* |
146
|
|
|
* @param Request $request |
147
|
|
|
* @param $path |
148
|
|
|
* |
149
|
|
|
* @return Response |
150
|
|
|
*/ |
151
|
|
|
public function contentAction(Request $request, $path) |
152
|
|
|
{ |
153
|
|
|
$pageInfo = $this->reader->getInfo($path . '.md'); |
154
|
|
|
$response = new Response(); |
155
|
|
|
$response->setETag($pageInfo->getEtag()); |
156
|
|
|
$lastModified = $pageInfo->getLastModified(); |
157
|
|
View Code Duplication |
if ($this->assetsVersion - $pageInfo->getLastModified()->getTimestamp() > 0) { |
|
|
|
|
158
|
|
|
$lastModified = new \DateTime('@' . $this->assetsVersion); |
159
|
|
|
} |
160
|
|
|
$response->setLastModified($lastModified); |
161
|
|
|
$response->setPublic(); |
162
|
|
|
if ($response->isNotModified($request)) { |
163
|
|
|
return $response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$p = $this->reader->getPage($path . '.md'); |
167
|
|
|
$response->setContent($p->getContent()); |
168
|
|
|
return $response; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|