1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Controller; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Controller\CRUDController as Controller; |
17
|
|
|
use Symfony\Component\Form\FormRenderer; |
18
|
|
|
use Symfony\Component\Form\FormView; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
24
|
|
|
*/ |
25
|
|
|
class MediaAdminController extends Controller |
26
|
|
|
{ |
27
|
|
|
public function createAction(?Request $request = null) |
28
|
|
|
{ |
29
|
|
|
$this->admin->checkAccess('create'); |
30
|
|
|
|
31
|
|
|
if (!$request->get('provider') && $request->isMethod('get')) { |
32
|
|
|
$pool = $this->get('sonata.media.pool'); |
33
|
|
|
|
34
|
|
|
return $this->render('@SonataMedia/MediaAdmin/select_provider.html.twig', [ |
35
|
|
|
'providers' => $pool->getProvidersByContext( |
36
|
|
|
$request->get('context', $pool->getDefaultContext()) |
37
|
|
|
), |
38
|
|
|
'action' => 'create', |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return parent::createAction(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function render($view, array $parameters = [], ?Response $response = null) |
46
|
|
|
{ |
47
|
|
|
$parameters['media_pool'] = $this->get('sonata.media.pool'); |
48
|
|
|
$parameters['persistent_parameters'] = $this->admin->getPersistentParameters(); |
49
|
|
|
|
50
|
|
|
return parent::renderWithExtraParams($view, $parameters, $response); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function listAction(?Request $request = null) |
54
|
|
|
{ |
55
|
|
|
$this->admin->checkAccess('list'); |
56
|
|
|
|
57
|
|
|
if ($listMode = $request->get('_list_mode', 'mosaic')) { |
58
|
|
|
$this->admin->setListMode($listMode); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$datagrid = $this->admin->getDatagrid(); |
62
|
|
|
|
63
|
|
|
$filters = $request->get('filter'); |
64
|
|
|
|
65
|
|
|
// set the default context |
66
|
|
|
if (!$filters || !\array_key_exists('context', $filters)) { |
67
|
|
|
$context = $this->admin->getPersistentParameter('context', $this->get('sonata.media.pool')->getDefaultContext()); |
|
|
|
|
68
|
|
|
} else { |
69
|
|
|
$context = $filters['context']['value']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$datagrid->setValue('context', null, $context); |
73
|
|
|
|
74
|
|
|
$rootCategory = null; |
75
|
|
|
if ($this->has('sonata.media.manager.category')) { |
76
|
|
|
$rootCategory = $this->get('sonata.media.manager.category')->getRootCategory($context); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (null !== $rootCategory && !$filters) { |
80
|
|
|
$datagrid->setValue('category', null, $rootCategory->getId()); |
81
|
|
|
} |
82
|
|
|
if ($this->has('sonata.media.manager.category') && $request->get('category')) { |
83
|
|
|
$category = $this->get('sonata.media.manager.category')->findOneBy([ |
84
|
|
|
'id' => (int) $request->get('category'), |
85
|
|
|
'context' => $context, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
if (!empty($category)) { |
89
|
|
|
$datagrid->setValue('category', null, $category->getId()); |
90
|
|
|
} else { |
91
|
|
|
$datagrid->setValue('category', null, $rootCategory->getId()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$formView = $datagrid->getForm()->createView(); |
96
|
|
|
|
97
|
|
|
$this->setFormTheme($formView, $this->admin->getFilterTheme()); |
98
|
|
|
|
99
|
|
|
return $this->render($this->admin->getTemplate('list'), [ |
|
|
|
|
100
|
|
|
'action' => 'list', |
101
|
|
|
'form' => $formView, |
102
|
|
|
'datagrid' => $datagrid, |
103
|
|
|
'root_category' => $rootCategory, |
104
|
|
|
'csrf_token' => $this->getCsrfToken('sonata.batch'), |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets the admin form theme to form view. Used for compatibility between Symfony versions. |
110
|
|
|
*/ |
111
|
|
|
private function setFormTheme(FormView $formView, array $theme) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$twig = $this->get('twig'); |
114
|
|
|
|
115
|
|
|
$twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.