1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Node; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\Debug\Exception\ContextErrorException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
class NodeController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @Method({"GET", "POST"}) |
16
|
|
|
* |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param $slug |
19
|
|
|
* |
20
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
21
|
|
|
*/ |
22
|
|
|
public function dispatchAction(Request $request, $slug) |
23
|
|
|
{ |
24
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
25
|
|
|
$node = $entityManager->getRepository('AlpixelCMSBundle:Node') |
26
|
|
|
->findOneBySlugAndLocale($slug, $request->getLocale()); |
27
|
|
|
|
28
|
|
|
if ($node !== null) { |
29
|
|
|
if ($node->getPublished() === false && !$this->isAuthenticated($request)) { |
30
|
|
|
throw $this->createNotFoundException(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$contentType = $this->get('alpixel_cms.helper.cms')->getContentTypeFromNodeElementClass($node); |
34
|
|
|
$controller = explode('::', $contentType['controller']); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
if (count($controller) !== 2) { |
38
|
|
|
throw new \LogicException('The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"'); |
39
|
|
|
} elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) { |
40
|
|
|
throw new \LogicException(sprintf( |
41
|
|
|
'Unable to find the "%s" controller or the method "%s" doesn\'t exist.', |
42
|
|
|
$controller[0], |
43
|
|
|
$controller[1] |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->forward($contentType['controller'], [ |
48
|
|
|
'_route' => $request->attributes->get('_route'), |
49
|
|
|
'_route_params' => $request->attributes->get('_route_params'), |
50
|
|
|
'object' => $node, |
51
|
|
|
]); |
52
|
|
|
} catch (\LogicException $e) { |
53
|
|
|
$environment = $this->container->get('kernel')->getEnvironment(); |
54
|
|
|
if ($environment === 'prod') { |
55
|
|
|
$logger = $this->get('logger'); |
56
|
|
|
$logger->error($e->getMessage()); |
57
|
|
|
} else { |
58
|
|
|
throw $e; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
//Trying to find another node with this slug, in another language |
63
|
|
|
$node = $entityManager->getRepository('AlpixelCMSBundle:Node') |
64
|
|
|
->findOnePublishedBySlug($slug); |
65
|
|
|
|
66
|
|
|
if ($node !== null) { |
67
|
|
|
$translation = $entityManager->getRepository('AlpixelCMSBundle:Node') |
68
|
|
|
->findTranslation($node, $request->getLocale()); |
69
|
|
|
if ($translation !== null) { |
70
|
|
|
return $this->redirect($this->generateUrl('alpixel_cms', [ |
71
|
|
|
'slug' => $translation->getSlug(), |
72
|
|
|
'_locale' => $translation->getLocale(), |
73
|
|
|
])); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw $this->createNotFoundException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $node |
83
|
|
|
* |
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function displayNodeAdminBarAction(Request $request, $node) |
87
|
|
|
{ |
88
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
89
|
|
|
$node = $entityManager->getRepository('AlpixelCMSBundle:Node')->find($node); |
90
|
|
|
|
91
|
|
|
$response = new Response(); |
92
|
|
|
$response->setPrivate(); |
93
|
|
|
$response->setMaxAge(900); |
94
|
|
|
|
95
|
|
|
if ($this->isAuthenticated($request)) { |
96
|
|
|
$content = $this->renderView('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [ |
97
|
|
|
'node' => $node, |
98
|
|
|
'link' => $this->generateUrl('alpixel_admin_cms_node_forwardEdit', [ |
99
|
|
|
'type' => $node->getType(), |
100
|
|
|
'id' => $node->getId(), |
101
|
|
|
]), |
102
|
|
|
]); |
103
|
|
|
$response->setContent($content); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $response; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $link |
111
|
|
|
* |
112
|
|
|
* @return Response |
113
|
|
|
*/ |
114
|
|
|
public function displayCustomAdminBarAction(Request $request, $link) |
115
|
|
|
{ |
116
|
|
|
$response = new Response(); |
117
|
|
|
$response->setPrivate(); |
118
|
|
|
$response->setMaxAge(900); |
119
|
|
|
|
120
|
|
|
if ($this->isAuthenticated($request)) { |
121
|
|
|
$content = $this->renderView('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [ |
122
|
|
|
'link' => $link, |
123
|
|
|
]); |
124
|
|
|
$response->setContent($content); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $response; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Request $request |
132
|
|
|
*/ |
133
|
|
|
private function isAuthenticated(Request $request) |
134
|
|
|
{ |
135
|
|
|
$canEdit = $request->cookies->get('can_edit'); |
136
|
|
|
|
137
|
|
|
if (isset($canEdit)) { |
138
|
|
|
if ($request->getSession()->has('_security_admin')) { |
139
|
|
|
try { |
140
|
|
|
$token = unserialize($request->getSession()->get('_security_admin')); |
141
|
|
|
$user = $token->getUser(); |
142
|
|
|
|
143
|
|
|
return $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret').$user->getSalt()); |
144
|
|
|
} catch (ContextErrorException $e) { |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|