|
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 = 'alpixel_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
|
|
|
$instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass(get_class($object)); |
|
24
|
|
|
if ($instanceAdmin !== null) { |
|
25
|
|
|
return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $object->getId()])); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', get_class($content))); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function listAction(Request $request = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if (false === $this->admin->isGranted('LIST')) { |
|
34
|
|
|
throw new AccessDeniedException(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->getInstancesAdmin(); |
|
38
|
|
|
// set the theme for the current Admin Form |
|
39
|
|
|
$datagrid = $this->admin->getDatagrid(); |
|
40
|
|
|
$formView = $datagrid->getForm()->createView(); |
|
41
|
|
|
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme()); |
|
42
|
|
|
|
|
43
|
|
|
return $this->render($this->admin->getTemplate('list'), [ |
|
44
|
|
|
'action' => 'list', |
|
45
|
|
|
'cmsContentType' => $this->getCMSParameter(), |
|
46
|
|
|
'form' => $formView, |
|
47
|
|
|
'datagrid' => $datagrid, |
|
48
|
|
|
'csrf_token' => $this->getCsrfToken('sonata.batch'), |
|
49
|
|
|
], null, $request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set instances of admin in $_cmsContentParameter. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getInstancesAdmin() |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($this->getCMSParameter() as $key => $value) { |
|
58
|
|
View Code Duplication |
if ($this->checkRolesCMS($value)) { |
|
|
|
|
|
|
59
|
|
|
$instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class']); |
|
60
|
|
|
if ($instanceAdmin !== null) { |
|
61
|
|
|
$this->_cmsContentParameter[$key]['admin'] = $instanceAdmin; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $role array Role in cms.yml |
|
69
|
|
|
* |
|
70
|
|
|
* Check role define in different parameter of block or content_types cms.yml |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
protected function checkRolesCMS(array $role) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$user = $this->getUser(); |
|
75
|
|
|
|
|
76
|
|
|
if (!$user) { |
|
77
|
|
|
throw new NotFoundHttpException(sprintf('unable to find user')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!array_key_exists('role', $role) || in_array($user->getRoles()[0], $role['role'])) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get content in cms.yml and set in $_cmsParameter. |
|
89
|
|
|
* |
|
90
|
|
|
* @return content of cms.yml |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getCMSParameter() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->_cmsContentParameter !== null) { |
|
95
|
|
|
return $this->_cmsContentParameter; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->_cmsContentParameter = $this->container->getParameter($this->_cmsParameter); |
|
99
|
|
|
|
|
100
|
|
|
return $this->_cmsContentParameter; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.