|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Node; |
|
6
|
|
|
use Sonata\AdminBundle\Controller\CRUDController as Controller; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
9
|
|
|
|
|
10
|
|
|
class AdminNodeController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
public function editContentAction() |
|
13
|
|
|
{ |
|
14
|
|
|
$object = $this->admin->getSubject(); |
|
15
|
|
|
|
|
16
|
|
|
if (!$object) { |
|
17
|
|
|
throw new NotFoundHttpException(sprintf('unable to find the object')); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$content = $this->findContentFromNode($object); |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
if ($content !== null) { |
|
|
|
|
|
|
23
|
|
|
$instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass(get_class($content)); |
|
24
|
|
|
|
|
25
|
|
|
if ($instanceAdmin !== null) { |
|
26
|
|
|
return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $content->getId()])); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', $contentType['class'])); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function createTranslationAction(Request $request) |
|
34
|
|
|
{ |
|
35
|
|
|
$object = $this->admin->getSubject(); |
|
36
|
|
|
$locale = $request->query->get('locale'); |
|
37
|
|
|
|
|
38
|
|
|
if($locale === null || $object === null) |
|
39
|
|
|
return $this->createNotFoundException(); |
|
40
|
|
|
|
|
41
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
42
|
|
|
$translation = $entityManager->getRepository('CMSBundle:Node') |
|
43
|
|
|
->findTranslation($object, $locale); |
|
44
|
|
|
|
|
45
|
|
|
if ($translation !== null) { |
|
46
|
|
|
return $this->redirect($this->admin->generateUrl('editContent', ['id' => $translation->getId()])); |
|
47
|
|
|
} else { |
|
48
|
|
|
if($object->getTranslationSource() !== null) { |
|
49
|
|
|
$source = $object->getTranslationSource(); |
|
50
|
|
|
} else { |
|
51
|
|
|
$source = $object; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$content = $this->findContentFromNode($source); |
|
55
|
|
|
|
|
56
|
|
|
$translatedContent = clone $content; |
|
57
|
|
|
|
|
58
|
|
|
$node = $translatedContent->getNode(); |
|
59
|
|
|
$node->setLocale($locale); |
|
60
|
|
|
$node->setTranslationSource($source); |
|
61
|
|
|
$node->setTitle(sprintf('Version %s de la page "%s"', strtoupper($locale), $node->getTitle())); |
|
62
|
|
|
$entityManager->persist($translatedContent); |
|
63
|
|
|
$entityManager->persist($node); |
|
64
|
|
|
|
|
65
|
|
|
$entityManager->flush(); |
|
66
|
|
|
return $this->redirect($this->admin->generateUrl('editContent', ['id' => $translatedContent->getNode()->getId()])); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function listAction(Request $request = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if (false === $this->admin->isGranted('LIST')) { |
|
73
|
|
|
throw new AccessDeniedException(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$datagrid = $this->admin->getDatagrid(); |
|
77
|
|
|
$formView = $datagrid->getForm()->createView(); |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
if (!$this->container->hasParameter('cms.content_types')) { |
|
|
|
|
|
|
80
|
|
|
throw $this->createNotFoundException('cms.content_types parameters in '.__FILE__.' file at line '.__LINE__.' in '.__FUNCTION__.' method, has not been not found, maybe you must be configured cms.yml file'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$cmsContentType = $this->container->getParameter('cms.content_types'); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($cmsContentType as $key => $value) { |
|
86
|
|
View Code Duplication |
if ($this->checkRolesCMS($value)) { |
|
|
|
|
|
|
87
|
|
|
if ($instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class'])) { |
|
88
|
|
|
$cmsContentType[$key]['admin'] = $instanceAdmin; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// set the theme for the current Admin Form |
|
94
|
|
|
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme()); |
|
95
|
|
|
|
|
96
|
|
|
return $this->render($this->admin->getTemplate('list'), [ |
|
97
|
|
|
'action' => 'list', |
|
98
|
|
|
'cmsContentType' => $cmsContentType, |
|
99
|
|
|
'form' => $formView, |
|
100
|
|
|
'datagrid' => $datagrid, |
|
101
|
|
|
'csrf_token' => $this->getCsrfToken('sonata.batch'), |
|
102
|
|
|
], null, $request); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
protected function checkRolesCMS(array $role) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$user = $this->getUser(); |
|
108
|
|
|
|
|
109
|
|
|
if (!$user) { |
|
110
|
|
|
throw new NotFoundHttpException(sprintf('unable to find user in %s file', __FILE__)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$userRole = $user->getRoles()[0]; |
|
114
|
|
|
|
|
115
|
|
|
if (!array_key_exists('role', $role) || in_array($userRole, $role['role'])) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function findContentFromNode(Node $node) |
|
123
|
|
|
{ |
|
124
|
|
View Code Duplication |
if (!$this->container->hasParameter('cms.content_types')) { |
|
|
|
|
|
|
125
|
|
|
throw new NotFoundHttpException('cms.content_types parameters in '.__FILE__.' file at line '.__LINE__.' in '.__FUNCTION__.' method, has not been not found, maybe you must be configured cms.yml file'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$cmsContentType = $this->container->getParameter('cms.content_types'); |
|
129
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
130
|
|
|
|
|
131
|
|
|
if (array_key_exists($node->getType(), $cmsContentType)) { |
|
132
|
|
|
$contentType = $cmsContentType[$node->getType()]; |
|
133
|
|
|
return $entityManager |
|
134
|
|
|
->getRepository($contentType['class']) |
|
135
|
|
|
->findOneByNode($node); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.