|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sonata\AdminBundle\Controller\CRUDController as Controller; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
9
|
|
|
|
|
10
|
|
|
class AdminBlockController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
private $_cmsParameter = 'cms.blocks'; |
|
13
|
|
|
private $_cmsContentParameter = null; |
|
14
|
|
|
private $_blockDefaultClass = 'Alpixel\Bundle\CMSBundle\Entity\Block'; |
|
15
|
|
|
|
|
16
|
|
View Code Duplication |
public function editContentAction() |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
$object = $this->admin->getSubject(); |
|
19
|
|
|
if (!$object) { |
|
20
|
|
|
throw new NotFoundHttpException(sprintf('unable to find the object')); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$content = $this->findContent(); |
|
24
|
|
|
|
|
25
|
|
|
if ($content !== null) { |
|
26
|
|
|
$instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($className); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
if ($instanceAdmin !== null) { |
|
29
|
|
|
return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $content->getId()])); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', get_class($content))); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function findContent() |
|
37
|
|
|
{ |
|
38
|
|
|
$classPassed = []; |
|
39
|
|
|
$className = ''; |
|
|
|
|
|
|
40
|
|
|
$content = null; |
|
41
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
42
|
|
|
foreach ($this->getCMSParameter() as $key => $value) { |
|
43
|
|
|
if ($this->_blockDefaultClass === $value['class'] || in_array($value['class'], $classPassed)) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$repository = $entityManager->getRepository($value['class']); |
|
48
|
|
|
$content = $repository->findOneByBlock($object->getId()); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
if ($content !== null) { |
|
51
|
|
|
$className = $value['class']; |
|
|
|
|
|
|
52
|
|
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$classPassed[] = $value['class']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($content === null) { |
|
59
|
|
|
$repository = $entityManager->getRepository($this->_blockDefaultClass); |
|
60
|
|
|
$content = $repository->findOneById($object); |
|
61
|
|
|
$className = $this->_blockDefaultClass; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
return $content; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function listAction(Request $request = null) |
|
67
|
|
|
{ |
|
68
|
|
|
if (false === $this->admin->isGranted('LIST')) { |
|
69
|
|
|
throw new AccessDeniedException(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->getInstancesAdmin(); |
|
73
|
|
|
// set the theme for the current Admin Form |
|
74
|
|
|
$datagrid = $this->admin->getDatagrid(); |
|
75
|
|
|
$formView = $datagrid->getForm()->createView(); |
|
76
|
|
|
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme()); |
|
77
|
|
|
|
|
78
|
|
|
return $this->render($this->admin->getTemplate('list'), [ |
|
79
|
|
|
'action' => 'list', |
|
80
|
|
|
'cmsContentType' => $this->getCMSParameter(), |
|
81
|
|
|
'form' => $formView, |
|
82
|
|
|
'datagrid' => $datagrid, |
|
83
|
|
|
'csrf_token' => $this->getCsrfToken('sonata.batch'), |
|
84
|
|
|
], null, $request); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set instances of admin in $_cmsContentParameter. |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getInstancesAdmin() |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($this->getCMSParameter() as $key => $value) { |
|
93
|
|
View Code Duplication |
if ($this->checkRolesCMS($value)) { |
|
|
|
|
|
|
94
|
|
|
$instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class']); |
|
95
|
|
|
if ($instanceAdmin !== null) { |
|
96
|
|
|
$this->_cmsContentParameter[$key]['admin'] = $instanceAdmin; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $role array Role in cms.yml |
|
104
|
|
|
* |
|
105
|
|
|
* Check role define in different parameter of block or content_types cms.yml |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
protected function checkRolesCMS(array $role) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$user = $this->getUser(); |
|
110
|
|
|
|
|
111
|
|
|
if (!$user) { |
|
112
|
|
|
throw new NotFoundHttpException(sprintf('unable to find user')); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!array_key_exists('role', $role) || in_array($user->getRoles()[0], $role['role'])) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get content in cms.yml and set in $_cmsParameter. |
|
124
|
|
|
* |
|
125
|
|
|
* @return content of cms.yml |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getCMSParameter() |
|
128
|
|
|
{ |
|
129
|
|
|
if ($this->_cmsContentParameter !== null) { |
|
130
|
|
|
return $this->_cmsContentParameter; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->_cmsContentParameter = $this->container->getParameter($this->_cmsParameter); |
|
134
|
|
|
|
|
135
|
|
|
return $this->_cmsContentParameter; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
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.