1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Provider; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Entity\Media; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
11
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
12
|
|
|
use Symfony\Component\Validator\Constraints as Constraint; |
13
|
|
|
|
14
|
|
|
class ImageProvider extends AbstractProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param FormMapper $formMapper |
18
|
|
|
*/ |
19
|
|
|
public function buildEditForm(FormMapper $formMapper) |
20
|
|
|
{ |
21
|
|
|
$formMapper |
22
|
|
|
->with('General') |
23
|
|
|
->add('providerName', HiddenType::class) |
24
|
|
|
->add( |
25
|
|
|
'binaryContent', |
26
|
|
|
'file', |
27
|
|
|
[ |
28
|
|
|
'required' => false, |
29
|
|
|
'constraints' => [ |
30
|
|
|
new Constraint\File(), |
31
|
|
|
], |
32
|
|
|
'label' => 'Replacement Image', |
33
|
|
|
] |
34
|
|
|
) |
35
|
|
|
->add('title') |
36
|
|
|
->add('description') |
37
|
|
|
->add('authorName') |
38
|
|
|
->add('copyright') |
39
|
|
|
->add('tags') |
40
|
|
|
->add( |
41
|
|
|
'pointOfInterest', |
42
|
|
|
ChoiceType::class, |
43
|
|
|
[ |
44
|
|
|
'required' => false, |
45
|
|
|
'label' => 'Point Of Interest', |
46
|
|
|
'choices' => $this->getPointOfInterestChoices(), |
47
|
|
|
] |
48
|
|
|
) |
49
|
|
|
->end(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param FormMapper $formMapper |
54
|
|
|
*/ |
55
|
|
|
public function buildCreateForm(FormMapper $formMapper) |
56
|
|
|
{ |
57
|
|
|
$formMapper |
58
|
|
|
->add('providerName', HiddenType::class) |
59
|
|
|
->add( |
60
|
|
|
'binaryContent', |
61
|
|
|
FileType::class, |
62
|
|
|
[ |
63
|
|
|
'multiple' => false, |
64
|
|
|
'data_class' => null, |
65
|
|
|
'constraints' => [ |
66
|
|
|
new Constraint\NotBlank(), |
67
|
|
|
new Constraint\NotNull(), |
68
|
|
|
new Constraint\File(), |
69
|
|
|
], |
70
|
|
|
'label' => 'Image', |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param MediaInterface $media |
77
|
|
|
*/ |
78
|
|
|
public function update(MediaInterface $media) |
79
|
|
|
{ |
80
|
|
|
if (!is_null($media->getBinaryContent())) { |
81
|
|
|
$filename = $this->handleFileUpload($media); |
|
|
|
|
82
|
|
|
$media->setProviderReference($filename); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Media $media |
88
|
|
|
* @return string |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
protected function handleFileUpload(Media $media) |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* @var UploadedFile $file |
95
|
|
|
*/ |
96
|
|
|
$file = $media->getBinaryContent(); |
97
|
|
|
|
98
|
|
|
$filename = sprintf( |
99
|
|
|
'%s_%d.%s', |
100
|
|
|
sha1($file->getClientOriginalName()), |
101
|
|
|
time(), |
102
|
|
|
$file->getClientOriginalExtension() |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$stream = fopen($file->getRealPath(), 'r+'); |
106
|
|
|
$this->getFilesystem()->writeStream($filename, $stream); |
107
|
|
|
@fclose($stream); // this sometime messes up |
|
|
|
|
108
|
|
|
|
109
|
|
|
$media->setProviderMetadata( |
110
|
|
|
array_merge( |
111
|
|
|
$media->getProviderMetaData(), |
112
|
|
|
[ |
113
|
|
|
'originalName' => $file->getClientOriginalName(), |
114
|
|
|
'originalExtension' => $file->getClientOriginalExtension(), |
115
|
|
|
'mimeType' => $file->getClientMimeType(), |
116
|
|
|
'size' => $file->getSize(), |
117
|
|
|
] |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
$media->setImage($filename); |
121
|
|
|
|
122
|
|
|
if (empty($media->getTitle())) { |
123
|
|
|
$media->setTitle(str_replace('_', ' ', pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME))); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $filename; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param MediaInterface $media |
131
|
|
|
* @param array $options |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function toArray(MediaInterface $media, array $options = []) |
135
|
|
|
{ |
136
|
|
|
return parent::toArray($media, $options) + [ |
137
|
|
|
'type' => $this->getTypeName(), |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getIcon() |
145
|
|
|
{ |
146
|
|
|
return 'photo'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getName() |
153
|
|
|
{ |
154
|
|
|
return 'Image'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getTypeName() |
158
|
|
|
{ |
159
|
|
|
return 'image'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getMediaTemplate() |
166
|
|
|
{ |
167
|
|
|
return 'MediaMonksSonataMediaBundle:Provider:image_media.html.twig'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.