1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Admin; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Entity\Media; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
use MediaMonks\SonataMediaBundle\Provider\ProviderPool; |
8
|
|
|
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface; |
9
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
10
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
11
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
12
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
13
|
|
|
use Sonata\AdminBundle\Route\RouteCollection; |
14
|
|
|
use Sonata\CoreBundle\Validator\ErrorElement; |
15
|
|
|
use Symfony\Component\Form\FormError; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
class MediaAdmin extends AbstractAdmin |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ProviderPool |
22
|
|
|
*/ |
23
|
|
|
private $providerPool; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $originalProviderReference; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $code |
32
|
|
|
* @param string $class |
33
|
|
|
* @param string $baseControllerName |
34
|
|
|
* @param ProviderPool $providerPool |
35
|
|
|
*/ |
36
|
|
|
public function __construct($code, $class, $baseControllerName, ProviderPool $providerPool) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($code, $class, $baseControllerName); |
39
|
|
|
|
40
|
|
|
$this->providerPool = $providerPool; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ListMapper $listMapper |
45
|
|
|
*/ |
46
|
|
|
protected function configureListFields(ListMapper $listMapper) |
47
|
|
|
{ |
48
|
|
|
$listMapper |
49
|
|
|
->addIdentifier('title') |
50
|
|
|
->add( |
51
|
|
|
'type' |
52
|
|
|
) |
53
|
|
|
->add( |
54
|
|
|
'updatedAt', |
55
|
|
|
'datetime' |
56
|
|
|
) |
57
|
|
|
->add( |
58
|
|
|
'_action', |
59
|
|
|
'actions', |
60
|
|
|
[ |
61
|
|
|
'actions' => [ |
62
|
|
|
'edit' => [], |
63
|
|
|
'delete' => [], |
64
|
|
|
], |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param FormMapper $formMapper |
71
|
|
|
*/ |
72
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
73
|
|
|
{ |
74
|
|
|
/** |
75
|
|
|
* @var Media $media |
76
|
|
|
*/ |
77
|
|
|
$media = $this->getSubject(); |
78
|
|
|
if (!$media) { |
79
|
|
|
$media = $this->getNewInstance(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->originalProviderReference = $media->getProviderReference(); |
83
|
|
|
|
84
|
|
|
$provider = $this->getProvider($media); |
85
|
|
|
|
86
|
|
|
if ($media->getId()) { |
87
|
|
|
$provider->buildEditForm($formMapper); |
88
|
|
|
} else { |
89
|
|
|
$provider->buildCreateForm($formMapper); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Media $media |
95
|
|
|
*/ |
96
|
|
|
public function prePersist($media) |
97
|
|
|
{ |
98
|
|
|
$this->getProvider($media)->update($media, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Media $media |
103
|
|
|
*/ |
104
|
|
|
public function preUpdate($media) |
105
|
|
|
{ |
106
|
|
|
try { |
107
|
|
|
$this->getProvider($media)->update($media, $this->isProviderReferenceUpdated($media)); |
108
|
|
|
} |
109
|
|
|
catch (\Exception $e) { |
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $media |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function isProviderReferenceUpdated(Media $media) |
119
|
|
|
{ |
120
|
|
|
return $this->originalProviderReference !== $media->getProviderReference(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $media |
125
|
|
|
* @return ProviderInterface |
126
|
|
|
*/ |
127
|
|
|
protected function getProvider(MediaInterface $media) |
128
|
|
|
{ |
129
|
|
|
return $this->providerPool->getProvider($media->getProvider()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return MediaInterface |
134
|
|
|
*/ |
135
|
|
|
public function getNewInstance() |
136
|
|
|
{ |
137
|
|
|
$media = parent::getNewInstance(); |
138
|
|
|
if ($this->hasRequest()) { |
139
|
|
|
if ($this->getRequest()->isMethod('POST')) { |
140
|
|
|
$media->setProvider($this->getRequest()->get($this->getUniqid())['provider']); |
141
|
|
|
} elseif ($this->getRequest()->query->has('provider')) { |
142
|
|
|
$media->setProvider($this->getRequest()->query->get('provider')); |
143
|
|
|
} else { |
144
|
|
|
throw new \InvalidArgumentException('No provider was set'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$provider = $this->getProvider($media); |
149
|
|
|
$media->setType($provider->getType()); |
150
|
|
|
|
151
|
|
|
return $media; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $object |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function toString($object) |
159
|
|
|
{ |
160
|
|
|
return $object instanceof MediaInterface ? $object->getTitle() : 'Media'; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param RouteCollection $collection |
165
|
|
|
*/ |
166
|
|
|
public function configureRoutes(RouteCollection $collection) |
167
|
|
|
{ |
168
|
|
|
$collection->add('image', $this->getRouterIdParameter().'/image'); |
169
|
|
|
$collection->add('download', $this->getRouterIdParameter().'/download'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param DatagridMapper $datagridMapper |
174
|
|
|
*/ |
175
|
|
|
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
176
|
|
|
{ |
177
|
|
|
$datagridMapper->add('title'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function validate(ErrorElement $errorElement, $object) |
184
|
|
|
{ |
185
|
|
|
$this->getProvider($object)->validate($errorElement, $object); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|