1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the module FeatureType */ |
4
|
|
|
/* */ |
5
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
6
|
|
|
/* file that was distributed with this source code. */ |
7
|
|
|
/*************************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace FeatureType\Controller; |
10
|
|
|
|
11
|
|
|
use FeatureType\Event\FeatureTypeEvents; |
12
|
|
|
use FeatureType\Event\FeatureTypeAvMetaEvent; |
13
|
|
|
use FeatureType\FeatureType; |
14
|
|
|
use FeatureType\Form\FeatureTypeAvMetaUpdateForm; |
15
|
|
|
use FeatureType\Model\FeatureFeatureType; |
16
|
|
|
use FeatureType\Model\FeatureFeatureTypeQuery; |
17
|
|
|
use FeatureType\Model\FeatureTypeAvMeta; |
18
|
|
|
use FeatureType\Model\FeatureTypeAvMetaQuery; |
19
|
|
|
use FeatureType\Model\FeatureTypeQuery; |
20
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
21
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
22
|
|
|
use Thelia\Core\Security\AccessManager; |
23
|
|
|
use Thelia\Core\Security\Resource\AdminResources; |
24
|
|
|
use Thelia\Core\Translation\Translator; |
25
|
|
|
use Thelia\Files\Exception\ProcessFileException; |
26
|
|
|
use Thelia\Model\Lang; |
27
|
|
|
use Thelia\Model\LangQuery; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class FeatureTypeFeatureAvController |
31
|
|
|
* @package FeatureType\Controller |
32
|
|
|
* @author Gilles Bourgeat <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class FeatureTypeFeatureAvController extends FeatureTypeController |
35
|
|
|
{ |
36
|
|
|
/** @var Lang[] */ |
37
|
|
|
protected $langs = array(); |
38
|
|
|
|
39
|
|
|
/** @var FeatureFeatureType[] */ |
40
|
|
|
protected $featureFeatureTypes = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $feature_id |
44
|
|
|
* @return null|\Symfony\Component\HttpFoundation\Response|\Thelia\Core\HttpFoundation\Response |
45
|
|
|
*/ |
46
|
|
|
public function updateMetaAction($feature_id) |
47
|
|
|
{ |
48
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::FEATURE), null, AccessManager::UPDATE)) { |
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$form = $this->createForm("feature_type_av_meta.update"); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$formUpdate = $this->validateForm($form); |
56
|
|
|
|
57
|
|
|
$featureAvs = $formUpdate->get('feature_av')->getData(); |
58
|
|
|
|
59
|
|
|
foreach ($featureAvs as $featureAvId => $featureAv) { |
60
|
|
|
foreach ($featureAv['lang'] as $langId => $lang) { |
61
|
|
|
foreach ($lang['feature_type'] as $featureTypeId => $value) { |
62
|
|
|
$values = []; |
63
|
|
|
$values[$langId] = $value; |
64
|
|
|
$featureType = FeatureTypeQuery::create() |
65
|
|
|
->findOneById($featureTypeId); |
66
|
|
|
|
67
|
|
|
if ($featureType->getInputType() === "image") { |
68
|
|
|
if (null === $value) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$uploadedFileName = $this->uploadFile($value); |
73
|
|
|
$values[$langId] = $uploadedFileName; |
74
|
|
|
|
75
|
|
|
if (!$featureType->getIsMultilingualFeatureAvValue()) { |
76
|
|
|
$activeLangs = LangQuery::create() |
77
|
|
|
->filterByActive(1) |
78
|
|
|
->find(); |
79
|
|
|
|
80
|
|
|
/** @var Lang $lang */ |
81
|
|
|
foreach ($activeLangs as $lang) { |
82
|
|
|
$values[$lang->getId()] = $uploadedFileName; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($values as $langId => $langValue) { |
88
|
|
|
$this->dispatchEvent( |
89
|
|
|
$this->getFeatureFeatureType($featureTypeId, $feature_id), |
90
|
|
|
$featureAvId, |
91
|
|
|
$langId, |
92
|
|
|
$langValue |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->resetUpdateForm(); |
100
|
|
|
return $this->generateSuccessRedirect($form); |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
$this->setupFormErrorContext( |
103
|
|
|
$this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)), |
104
|
|
|
$e->getMessage(), |
105
|
|
|
$form, |
106
|
|
|
$e |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return $this->viewFeature($feature_id); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function deleteMetaAction($feature_id, $feature_type_id, $feature_av_id, $lang_id) |
114
|
|
|
{ |
115
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::FEATURE), null, AccessManager::DELETE)) { |
116
|
|
|
return $response; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$form = $this->createForm("feature_type.delete"); |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$this->validateForm($form); |
123
|
|
|
|
124
|
|
|
$featureType = FeatureTypeQuery::create() |
125
|
|
|
->findOneById($feature_type_id); |
126
|
|
|
|
127
|
|
|
$featureFeatureType = $this->getFeatureFeatureType($feature_type_id, $feature_id); |
128
|
|
|
|
129
|
|
|
$eventName = FeatureTypeEvents::FEATURE_TYPE_AV_META_DELETE; |
130
|
|
|
|
131
|
|
|
$featureAvMetaQuery = FeatureTypeAvMetaQuery::create() |
132
|
|
|
->filterByFeatureAvId($feature_av_id) |
133
|
|
|
->filterByFeatureFeatureTypeId($featureFeatureType->getId()); |
134
|
|
|
|
135
|
|
|
if ($featureType->getIsMultilingualFeatureAvValue()) { |
136
|
|
|
$featureAvMetaQuery->filterByLocale($this->getLocale($lang_id)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$featureAvMetas = $featureAvMetaQuery->find(); |
140
|
|
|
|
141
|
|
|
foreach ($featureAvMetas as $featureAvMeta) { |
142
|
|
|
$this->dispatch( |
143
|
|
|
$eventName, |
144
|
|
|
(new FeatureTypeAvMetaEvent($featureAvMeta)) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->resetUpdateForm(); |
149
|
|
|
return $this->generateSuccessRedirect($form); |
150
|
|
|
} catch (\Exception $e) { |
151
|
|
|
$this->setupFormErrorContext( |
152
|
|
|
$this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)), |
153
|
|
|
$e->getMessage(), |
154
|
|
|
$form, |
155
|
|
|
$e |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
return $this->viewFeature($feature_id); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param FeatureFeatureType $featureFeatureType |
164
|
|
|
* @param int $featureAvId |
165
|
|
|
* @param int $langId |
166
|
|
|
* @param string $value |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
protected function dispatchEvent(FeatureFeatureType $featureFeatureType, $featureAvId, $langId, $value) |
170
|
|
|
{ |
171
|
|
|
$eventName = FeatureTypeEvents::FEATURE_TYPE_AV_META_UPDATE; |
172
|
|
|
|
173
|
|
|
$featureAvMeta = FeatureTypeAvMetaQuery::create() |
174
|
|
|
->filterByFeatureAvId($featureAvId) |
175
|
|
|
->filterByFeatureFeatureTypeId($featureFeatureType->getId()) |
176
|
|
|
->filterByLocale($this->getLocale($langId)) |
177
|
|
|
->findOne(); |
178
|
|
|
|
179
|
|
|
// create if not exist |
180
|
|
|
if ($featureAvMeta === null) { |
181
|
|
|
$eventName = FeatureTypeEvents::FEATURE_TYPE_AV_META_CREATE; |
182
|
|
|
|
183
|
|
|
$featureAvMeta = (new FeatureTypeAvMeta()) |
184
|
|
|
->setFeatureAvId($featureAvId) |
185
|
|
|
->setFeatureFeatureTypeId($featureFeatureType->getId()) |
186
|
|
|
->setLocale($this->getLocale($langId)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$featureAvMeta->setValue($value); |
190
|
|
|
|
191
|
|
|
$this->dispatch( |
192
|
|
|
$eventName, |
193
|
|
|
(new FeatureTypeAvMetaEvent($featureAvMeta)) |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param int $featureTypeId |
199
|
|
|
* @param int $featureId |
200
|
|
|
* @return FeatureFeatureType |
201
|
|
|
* @throws \Exception |
202
|
|
|
*/ |
203
|
|
|
protected function getFeatureFeatureType($featureTypeId, $featureId) |
204
|
|
|
{ |
205
|
|
|
if (!isset($this->featureFeatureTypes[$featureTypeId])) { |
206
|
|
|
$this->featureFeatureTypes[$featureTypeId] = FeatureFeatureTypeQuery::create() |
207
|
|
|
->filterByFeatureTypeId($featureTypeId) |
208
|
|
|
->filterByFeatureId($featureId) |
209
|
|
|
->findOne(); |
210
|
|
|
|
211
|
|
|
if ($this->featureFeatureTypes[$featureTypeId] === null) { |
212
|
|
|
throw new \Exception('FeatureFeatureType not found'); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->featureFeatureTypes[$featureTypeId]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param int $langId |
221
|
|
|
* @return string |
222
|
|
|
* @throws \Exception |
223
|
|
|
*/ |
224
|
|
|
protected function getLocale($langId) |
225
|
|
|
{ |
226
|
|
|
if (!isset($this->langs[$langId])) { |
227
|
|
|
$this->langs[$langId] = LangQuery::create()->findPk($langId); |
228
|
|
|
|
229
|
|
|
if ($this->langs[$langId] === null) { |
230
|
|
|
throw new \Exception('Lang not found'); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $this->langs[$langId]->getLocale(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param UploadedFile $file |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
protected function uploadFile(UploadedFile $file) |
242
|
|
|
{ |
243
|
|
|
if ($file->getError() == UPLOAD_ERR_INI_SIZE) { |
244
|
|
|
$message = $this->getTranslator() |
245
|
|
|
->trans( |
246
|
|
|
'File is too large, please retry with a file having a size less than %size%.', |
247
|
|
|
array('%size%' => ini_get('upload_max_filesize')), |
248
|
|
|
'core' |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
throw new ProcessFileException($message, 403); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$validMimeTypes = [ |
255
|
|
|
'image/jpeg' => ["jpg", "jpeg"], |
256
|
|
|
'image/png' => ["png"], |
257
|
|
|
'image/gif' => ["gif"] |
258
|
|
|
]; |
259
|
|
|
$mimeType = $file->getMimeType(); |
260
|
|
|
if (!isset($validMimeTypes[$mimeType])) { |
261
|
|
|
$message = $this->getTranslator() |
262
|
|
|
->trans( |
263
|
|
|
'Only files having the following mime type are allowed: %types%', |
264
|
|
|
[ '%types%' => implode(', ', array_keys($validMimeTypes))] |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
throw new ProcessFileException($message, 415); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$regex = "#^(.+)\.(".implode("|", $validMimeTypes[$mimeType]).")$#i"; |
271
|
|
|
|
272
|
|
|
$realFileName = $file->getClientOriginalName(); |
273
|
|
|
if (!preg_match($regex, $realFileName)) { |
274
|
|
|
$message = $this->getTranslator() |
275
|
|
|
->trans( |
276
|
|
|
"There's a conflict between your file extension \"%ext\" and the mime type \"%mime\"", |
277
|
|
|
[ |
278
|
|
|
'%mime' => $mimeType, |
279
|
|
|
'%ext' => $file->getClientOriginalExtension() |
280
|
|
|
] |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
throw new ProcessFileException($message, 415); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$fileSystem = new Filesystem(); |
287
|
|
|
$fileSystem->mkdir(THELIA_WEB_DIR. DS .FeatureType::FEATURE_TYPE_AV_IMAGE_FOLDER); |
288
|
|
|
|
289
|
|
|
$fileName = $this->generateUniqueFileName().'_'.$realFileName; |
290
|
|
|
$file->move(THELIA_WEB_DIR. DS .FeatureType::FEATURE_TYPE_AV_IMAGE_FOLDER, $fileName); |
291
|
|
|
return DS . FeatureType::FEATURE_TYPE_AV_IMAGE_FOLDER. DS .$fileName; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
protected function generateUniqueFileName() |
298
|
|
|
{ |
299
|
|
|
// md5() reduces the similarity of the file names generated by |
300
|
|
|
// uniqid(), which is based on timestamps |
301
|
|
|
return substr(md5(uniqid()), 0, 10); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
protected function resetUpdateForm() { |
305
|
|
|
$this->getParserContext()->remove(FeatureTypeAvMetaUpdateForm::class.':form'); |
306
|
|
|
$theliaFormErrors = $this->getRequest()->getSession()->get('thelia.form-errors'); |
307
|
|
|
unset($theliaFormErrors[FeatureTypeAvMetaUpdateForm::class.':form']); |
308
|
|
|
$this->getRequest()->getSession()->set('thelia.form-errors', $theliaFormErrors); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|