Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productimage.php
1<?php
2
4
18
19final class ProductImage extends Controller
20{
21 //region Actions
31 public function getFieldsAction(): array
32 {
33 return ['PRODUCT_IMAGE' => $this->getViewFields()];
34 }
35
45 public function listAction(int $productId, array $select = [], \CRestServer $restServer = null): ?Page
46 {
47 if ($productId <= 0)
48 {
49 $this->addError(new Error('Empty productID'));
50 return null;
51 }
52
53 $product = $this->getProduct($productId);
54 if (!$product)
55 {
56 $this->addError(new Error('Product was not found'));
57 return null;
58 }
59
60
61 $r = $this->checkPermissionProductRead($product);
62 if (!$r->isSuccess())
63 {
64 $this->addErrors($r->getErrors());
65 return null;
66 }
67
68 $result = [];
69 foreach ($product->getImageCollection() as $image)
70 {
71 $result[] = $this->prepareFileStructure($image, $restServer, $select);
72 }
73
74 return new Page(
75 'PRODUCT_IMAGES',
76 $result,
77 count($result)
78 );
79 }
80
90 public function getAction(int $id, int $productId, \CRestServer $restServer = null): ?array
91 {
92 $product = $this->getProduct($productId);
93 if (!$product)
94 {
95 $this->addError(new Error('Product was not found'));
96 return null;
97 }
98
99 $r = $this->checkPermissionProductRead($product);
100 if (!$r->isSuccess())
101 {
102 $this->addErrors($r->getErrors());
103 return null;
104 }
105
106 $r = $this->hasImage($id, $product);
107 if (!$r->isSuccess())
108 {
109 $this->addErrors($r->getErrors());
110 return null;
111 }
112
114 $image = $product->getImageCollection()->findById($id);
115
116 return ['PRODUCT_IMAGE' => $this->prepareFileStructure($image, $restServer)];
117 }
118
128 public function addAction(array $fields, array $fileContent, \CRestServer $restServer = null): ?array
129 {
130 if (!Loader::includeModule('rest'))
131 {
132 return null;
133 }
134
135 $product = $this->getProduct((int)$fields['PRODUCT_ID']);
136 if (!$product)
137 {
138 $this->addError(new Error('Product was not found'));
139 return null;
140 }
141
142 $r = $this->checkPermissionProductWrite($product);
143 if (!$r->isSuccess())
144 {
145 $this->addErrors($r->getErrors());
146 return null;
147 }
148
149 $fileData = \CRestUtil::saveFile($fileContent);
150 if (!$fileData)
151 {
152 $this->addError(new Error('Could not save image.'));
153 return null;
154 }
155
156 $checkPictureResult = \CFile::CheckFile($fileData, 0 ,false, \CFile::GetImageExtensions());
157 if ($checkPictureResult !== '')
158 {
159 $this->addError(new Error($checkPictureResult));
160 return null;
161 }
162
163 if ($fields['TYPE'] === DetailImage::CODE)
164 {
165 $product->getImageCollection()->getDetailImage()->setFileStructure($fileData);
166 }
167 elseif ($fields['TYPE'] === PreviewImage::CODE)
168 {
169 $product->getImageCollection()->getPreviewImage()->setFileStructure($fileData);
170 }
171 else
172 {
173 if (!$product->getPropertyCollection()->findByCode(MorePhotoImage::CODE))
174 {
175 $this->addError(
176 new Error(
177 "Image product property does not exists. Create" . MorePhotoImage::CODE . " property"
178 )
179 );
180
181 return null;
182 }
183
184 $product->getImageCollection()->addValue($fileData);
185 }
186
187 $r = $product->save();
188 if (!$r->isSuccess())
189 {
190 $this->addErrors($r->getErrors());
191 }
192
193 if ($fields['TYPE'] === DetailImage::CODE)
194 {
195 $image = $product->getImageCollection()->getDetailImage();
196 }
197 elseif ($fields['TYPE'] === PreviewImage::CODE)
198 {
199 $image = $product->getImageCollection()->getPreviewImage();
200 }
201 else
202 {
203 $morePhotos = $product->getImageCollection()->getMorePhotos();
204 $image = end($morePhotos);
205 }
206
207 return ['PRODUCT_IMAGE' => $this->prepareFileStructure($image, $restServer)];
208 }
209
218 public function deleteAction(int $id, int $productId): ?bool
219 {
220 $product = $this->getProduct($productId);
221 if (!$product)
222 {
223 $this->addError(new Error('Product was not found'));
224 return null;
225 }
226
227 $r = $this->checkPermissionProductWrite($product);
228 if (!$r->isSuccess())
229 {
230 $this->addErrors($r->getErrors());
231 return null;
232 }
233
234 $r = $this->hasImage($id, $product);
235 if (!$r->isSuccess())
236 {
237 $this->addErrors($r->getErrors());
238 return null;
239 }
240
241 $product
242 ->getImageCollection()
243 ->findById($id)
244 ->remove()
245 ;
246
247 $r = $product->save();
248 if (!$r->isSuccess())
249 {
250 $this->addErrors($r->getErrors());
251 return null;
252 }
253
254 return true;
255 }
256 //endregion
257
258 private function prepareFileStructure(
259 BaseImage $baseImage,
260 \CRestServer $restServer = null,
261 array $selectedFields = null
262 ): array
263 {
264 $result = [];
265 if (!$selectedFields)
266 {
267 $selectedFields = array_keys($this->getViewManager()->getView($this)->getFields());
268 }
269
270 foreach ($selectedFields as $name)
271 {
272 if ($name === 'ID')
273 {
274 $result[$name] = $baseImage->getField('ID');
275 }
276 if ($name === 'NAME')
277 {
278 $result[$name] = $baseImage->getField('FILE_NAME');
279 }
280 elseif ($name === 'DETAIL_URL')
281 {
282 $result[$name] = $baseImage->getSource();
283 }
284 elseif ($name === 'DOWNLOAD_URL')
285 {
286 $result[$name] =
287 $restServer
288 ? \CRestUtil::getDownloadUrl(['id' => $baseImage->getId()], $restServer)
289 : $baseImage->getSource()
290 ;
291 }
292 elseif ($name === 'CREATE_TIME')
293 {
294 $result[$name] = $baseImage->getField('TIMESTAMP_X');
295 }
296 elseif ($name === 'PRODUCT_ID')
297 {
298 $result[$name] = $baseImage->getParent()->getId();
299 }
300 elseif ($name === 'TYPE')
301 {
302 $result[$name] = $baseImage->getCode();
303 }
304 }
305
306 return $result;
307 }
308
309 protected function getEntityTable()
310 {
311 return new FileTable();
312 }
313
314 private function getProduct(int $productId): ?BaseEntity
315 {
316 $product = ServiceContainer::getRepositoryFacade()->loadProduct($productId);
317 if ($product)
318 {
319 return $product;
320 }
321
322 return ServiceContainer::getRepositoryFacade()->loadVariation($productId);
323 }
324
325 private function hasImage(int $id, BaseEntity $product): Result
326 {
327 $r = $this->exists($id);
328 if (!$r->isSuccess())
329 {
330 return $r;
331 }
332
333 $image = $product->getImageCollection()->findById($id);
334 if (!$image)
335 {
336 $r->addError(new Error('Image does not exist'));
337 }
338
339 return $r;
340 }
341
342 protected function exists($id)
343 {
344 $r = new Result();
345 if (!isset($this->get($id)['ID']))
346 {
347 $r->addError(new Error('Image does not exist'));
348 }
349
350 return $r;
351 }
352
353 private function checkPermissionProductRead(BaseEntity $product): Result
354 {
355 $r = $this->checkReadPermissionEntity();
356 if (!$r->isSuccess())
357 {
358 return $r;
359 }
360
361 return $this->checkPermissionProduct($product, self::IBLOCK_ELEMENT_READ, 200040300010);
362 }
363
364 private function checkPermissionProductWrite(BaseEntity $product): Result
365 {
366 $r = $this->checkModifyPermissionEntity();
367 if (!$r->isSuccess())
368 {
369 return $r;
370 }
371
372 return $this->checkPermissionProduct($product, self::IBLOCK_ELEMENT_EDIT, 200040300020);
373 }
374
375 private function checkPermissionProduct(BaseEntity $product, string $permission, int $errorCode): Result
376 {
377 $r = new Result();
378 if(!\CIBlockElementRights::UserHasRightTo($product->getIblockId(), $product->getId(), $permission))
379 {
380 $r->addError(new Error('Access Denied', $errorCode));
381 }
382
383 return $r;
384 }
385
387 {
388 $r = new Result();
389
390 if (!$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW))
391 {
392 $r->addError(new Error('Access Denied', 200040300020));
393 }
394
395 return $r;
396 }
397
398 protected function checkReadPermissionEntity(): Result
399 {
400 $r = new Result();
401
402 if (
403 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
404 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW)
405 )
406 {
407 $r->addError(new Error('Access Denied', 200040300010));
408 }
409
410 return $r;
411 }
412}
addAction(array $fields, array $fileContent, \CRestServer $restServer=null)
listAction(int $productId, array $select=[], \CRestServer $restServer=null)
deleteAction(int $id, int $productId)