Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ProductController.php
1<?php
2
4
15
17{
18 private ?array $property;
19
20 public function __construct(array $options = [])
21 {
22 $options['productId'] ??= 0;
23 $options['productId'] = is_numeric($options['productId']) ? (int)$options['productId'] : 0;
24
25 $options['iblockId'] ??= 0;
26 $options['iblockId'] = (int)$options['iblockId'];
27
28 if (empty($options['iblockId']) && !empty($options['productId']))
29 {
30 $options['iblockId'] = (int)\CIBlockElement::GetIBlockByID($options['productId']);
31 }
32
33 if (empty($options['iblockId']))
34 {
35 throw new ArgumentException('Parameter "iblockId" must be defined in options.');
36 }
37
38 $iblockInfo = ServiceContainer::getIblockInfo($options['iblockId']);
39 if (!$iblockInfo)
40 {
41 throw new ArgumentException("Iblock {{$options['iblockId']}} is not supported.");
42 }
43
44 $options['fieldName'] ??= MorePhotoImage::CODE;
45 $options['fieldName'] = (string)$options['fieldName'];
46
47 if ($options['fieldName'] === '')
48 {
49 throw new ArgumentException('Parameter "fieldName" must be defined in options.');
50 }
51
52 $this->property = $this->loadProperty($options['iblockId'], $options['fieldName']);
53
54 parent::__construct($options);
55 }
56
57 private function loadProperty(int $iblockId, string $fieldName): ?array
58 {
59 return PropertyTable::getRow([
60 'select' => ['ID', 'FILE_TYPE'],
61 'filter' => [
62 '=IBLOCK_ID' => $iblockId,
63 '=ACTIVE' => 'Y',
64 '=PROPERTY_TYPE' => PropertyTable::TYPE_FILE,
65 '=CODE' => $fieldName,
66 ],
67 ]);
68 }
69
70 public function isAvailable(): bool
71 {
72 return AccessController::getCurrent()->check(ActionDictionary::ACTION_CATALOG_READ);
73 }
74
76 {
77 $configuration = new Configuration();
78
79 $acceptedFileTypes = [];
80
81 if (!empty($this->property['FILE_TYPE']))
82 {
83 $propertyFileTypes = $this->prepareAcceptedFileTypes($this->property['FILE_TYPE']);
84 if (!empty($propertyFileTypes))
85 {
86 $acceptedFileTypes = array_intersect(Configuration::getImageExtensions(), $propertyFileTypes);
87 }
88 }
89
90 if (!empty($acceptedFileTypes))
91 {
92 $configuration->setAcceptedFileTypes($acceptedFileTypes);
93 }
94 else
95 {
96 $configuration->acceptOnlyImages();
97 }
98
99 return $configuration;
100 }
101
102 private function prepareAcceptedFileTypes(string $fileTypes): array
103 {
104 $imageExtensions = explode(',', $fileTypes);
105
106 return array_map(static fn ($extension) => '.' . trim($extension), $imageExtensions);
107 }
108
109 public function canUpload(): bool
110 {
111 return CurrentUser::get()->canDoOperation(ActionDictionary::ACTION_STORE_VIEW);
112 }
113
114 public function verifyFileOwner(FileOwnershipCollection $files): void
115 {
116 }
117
118 public function canView(): bool
119 {
120 return false;
121 }
122
123 public function canRemove(): bool
124 {
125 return false;
126 }
127}