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\Admin; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
17
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
18
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
19
|
|
|
use Sonata\AdminBundle\Form\Type\ModelListType; |
20
|
|
|
use Sonata\BlockBundle\Meta\Metadata; |
21
|
|
|
use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer; |
22
|
|
|
use Sonata\MediaBundle\Model\CategoryManagerInterface; |
23
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
24
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
25
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
26
|
|
|
|
27
|
|
|
abstract class BaseMediaAdmin extends AbstractAdmin |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Pool |
31
|
|
|
*/ |
32
|
|
|
protected $pool; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CategoryManagerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $categoryManager; |
38
|
|
|
|
39
|
|
|
protected $classnameLabel = 'Media'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $code |
43
|
|
|
* @param string $class |
44
|
|
|
* @param string $baseControllerName |
45
|
|
|
* @param CategoryManagerInterface $categoryManager |
46
|
|
|
*/ |
47
|
|
|
public function __construct($code, $class, $baseControllerName, Pool $pool, ?CategoryManagerInterface $categoryManager = null) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($code, $class, $baseControllerName); |
50
|
|
|
|
51
|
|
|
$this->pool = $pool; |
52
|
|
|
|
53
|
|
|
$this->categoryManager = $categoryManager; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function prePersist($media): void |
57
|
|
|
{ |
58
|
|
|
$parameters = $this->getPersistentParameters(); |
59
|
|
|
$media->setContext($parameters['context']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getPersistentParameters() |
63
|
|
|
{ |
64
|
|
|
$parameters = parent::getPersistentParameters(); |
65
|
|
|
|
66
|
|
|
if (!$this->hasRequest()) { |
67
|
|
|
return $parameters; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$filter = $this->getRequest()->get('filter'); |
71
|
|
|
if ($filter && \array_key_exists('context', $this->getRequest()->get('filter'))) { |
72
|
|
|
$context = $filter['context']['value']; |
73
|
|
|
} else { |
74
|
|
|
$context = $this->getRequest()->get('context', $this->pool->getDefaultContext()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$providers = $this->pool->getProvidersByContext($context); |
78
|
|
|
$provider = $this->getRequest()->get('provider'); |
79
|
|
|
|
80
|
|
|
// if the context has only one provider, set it into the request |
81
|
|
|
// so the intermediate provider selection is skipped |
82
|
|
|
if (1 === \count($providers) && null === $provider) { |
83
|
|
|
$provider = array_shift($providers)->getName(); |
84
|
|
|
$this->getRequest()->query->set('provider', $provider); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// if there is a post server error, provider is not posted and in case of |
88
|
|
|
// multiple providers, it has to be persistent to not being lost |
89
|
|
|
if (1 < \count($providers) && null !== $provider) { |
90
|
|
|
$parameters['provider'] = $provider; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$categoryId = $this->getRequest()->get('category'); |
94
|
|
|
|
95
|
|
|
if (null !== $this->categoryManager && !$categoryId) { |
96
|
|
|
$categoryId = $this->categoryManager->getRootCategory($context)->getId(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return array_merge($parameters, [ |
100
|
|
|
'context' => $context, |
101
|
|
|
'category' => $categoryId, |
102
|
|
|
'hide_context' => (bool) $this->getRequest()->get('hide_context'), |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getNewInstance() |
107
|
|
|
{ |
108
|
|
|
$media = parent::getNewInstance(); |
109
|
|
|
|
110
|
|
|
if ($this->hasRequest()) { |
111
|
|
|
if ($this->getRequest()->isMethod('POST')) { |
112
|
|
|
$uniqid = $this->getUniqid(); |
113
|
|
|
$media->setProviderName($this->getRequest()->get($uniqid)['providerName']); |
114
|
|
|
} else { |
115
|
|
|
$media->setProviderName($this->getRequest()->get('provider')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$media->setContext($context = $this->getRequest()->get('context')); |
119
|
|
|
|
120
|
|
|
if (null !== $this->categoryManager && $categoryId = $this->getPersistentParameter('category')) { |
121
|
|
|
$category = $this->categoryManager->find($categoryId); |
122
|
|
|
|
123
|
|
|
if ($category && $category->getContext()->getId() === $context) { |
|
|
|
|
124
|
|
|
$media->setCategory($category); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $media; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return Pool |
134
|
|
|
*/ |
135
|
|
|
public function getPool() |
136
|
|
|
{ |
137
|
|
|
return $this->pool; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getObjectMetadata($object) |
141
|
|
|
{ |
142
|
|
|
$provider = $this->pool->getProvider($object->getProviderName()); |
143
|
|
|
|
144
|
|
|
$url = $provider->generatePublicUrl( |
145
|
|
|
$object, |
146
|
|
|
$provider->getFormatName($object, MediaProviderInterface::FORMAT_ADMIN) |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return new Metadata($object->getName(), $object->getDescription(), $url); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
protected function configureListFields(ListMapper $listMapper): void |
153
|
|
|
{ |
154
|
|
|
$listMapper |
155
|
|
|
->addIdentifier('name') |
156
|
|
|
->add('description') |
157
|
|
|
->add('enabled') |
158
|
|
|
->add('size') |
159
|
|
|
; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function configureFormFields(FormMapper $formMapper): void |
163
|
|
|
{ |
164
|
|
|
$media = $this->getSubject(); |
165
|
|
|
|
166
|
|
|
if (!$media) { |
167
|
|
|
$media = $this->getNewInstance(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!$media || !$media->getProviderName()) { |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$formMapper->add('providerName', HiddenType::class); |
175
|
|
|
|
176
|
|
|
$formMapper->getFormBuilder()->addModelTransformer(new ProviderDataTransformer($this->pool, $this->getClass()), true); |
177
|
|
|
|
178
|
|
|
$provider = $this->pool->getProvider($media->getProviderName()); |
179
|
|
|
|
180
|
|
|
if ($media->getId()) { |
181
|
|
|
$provider->buildEditForm($formMapper); |
182
|
|
|
} else { |
183
|
|
|
$provider->buildCreateForm($formMapper); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (null !== $this->categoryManager) { |
187
|
|
|
$formMapper->add('category', ModelListType::class, [], [ |
188
|
|
|
'link_parameters' => [ |
189
|
|
|
'context' => $media->getContext(), |
190
|
|
|
'hide_context' => true, |
191
|
|
|
'mode' => 'tree', |
192
|
|
|
], |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.