Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productproperty.php
1<?php
2
4
11
13{
14 private string $customUserType = '';
15
16 // region Actions
17
21 public function getFieldsAction(): array
22 {
23 return ['PRODUCT_PROPERTY' => $this->getViewFields()];
24 }
25
33 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
34 {
35 if (!isset($filter['IBLOCK_ID']))
36 {
37 $filter['IBLOCK_ID'] = $this->getCatalogIds();
38 }
39 else
40 {
41 $iblockId = (int)($filter['IBLOCK_ID']);
42 if (!$this->isIblockCatalog($iblockId))
43 {
44 return new Page('PRODUCT_PROPERTIES', [], 0);
45 }
46 }
47
48 return new Page(
49 'PRODUCT_PROPERTIES',
50 $this->getList($select, $filter, $order, $pageNavigation),
51 $this->count($filter)
52 );
53 }
54
59 public function getAction(int $id): ?array
60 {
61 $r = $this->exists($id);
62 if ($r->isSuccess())
63 {
64 return ['PRODUCT_PROPERTY' => $this->get($id)];
65 }
66
67 $this->addErrors($r->getErrors());
68 return null;
69 }
70
75 public function addAction(array $fields): ?array
76 {
77 if (!$this->isIblockCatalog((int)$fields['IBLOCK_ID']))
78 {
79 $this->addError(new Error('The specified iblock is not a product catalog'));
80 return null;
81 }
82
83 $iblockPermissionsCheckResult = $this->checkIblockModifyPermission($fields['IBLOCK_ID']);
84 if (!$iblockPermissionsCheckResult->isSuccess())
85 {
86 $this->addErrors($iblockPermissionsCheckResult->getErrors());
87 return null;
88 }
89
90 $typeCheckResult = $this->checkPropertyType($fields);
91 if (!$typeCheckResult->isSuccess())
92 {
93 $this->addErrors($typeCheckResult->getErrors());
94 return null;
95 }
96
97 $this->processCustomTypesBeforeAdd($fields);
98
99 $application = self::getApplication();
100 $application->ResetException();
101
102 $addResult = (new \CIBlockProperty())->Add($fields);
103 if (!$addResult)
104 {
105 if ($application->GetException())
106 {
107 $this->addError(new Error($application->GetException()->GetString()));
108 }
109 else
110 {
111 $this->addError(new Error('Error adding property'));
112 }
113 return null;
114 }
115
116 $this->processCustomTypesAfterAdd((int)$addResult, $fields);
117
118 return ['PRODUCT_PROPERTY' => $this->get($addResult)];
119 }
120
126 public function updateAction(int $id, array $fields): ?array
127 {
128 $existsResult = $this->exists($id);
129 if (!$existsResult->isSuccess())
130 {
131 $this->addErrors($existsResult->getErrors());
132 return null;
133 }
134
135 $property = $this->getPropertyById($id);
136 $type = [
137 'PROPERTY_TYPE' => $fields['PROPERTY_TYPE'] ?? $property['PROPERTY_TYPE'],
138 'USER_TYPE' => $fields['USER_TYPE'] ?? $property['USER_TYPE'],
139 ];
140 $typeCheckResult = $this->checkPropertyType($type);
141 if (!$typeCheckResult->isSuccess())
142 {
143 $this->addErrors($typeCheckResult->getErrors());
144 return null;
145 }
146
147 $application = self::getApplication();
148 $application->ResetException();
149
150 $updateResult = (new \CIBlockProperty())->Update($id, $fields);
151 if (!$updateResult)
152 {
153 if ($application->GetException())
154 {
155 $this->addError(new Error($application->GetException()->GetString()));
156 }
157 else
158 {
159 $this->addError(new Error('Error updating product property'));
160 }
161 return null;
162 }
163
164 return ['PRODUCT_PROPERTY' => $this->get($id)];
165 }
166
171 public function deleteAction(int $id): ?bool
172 {
173 $existsResult = $this->exists($id);
174 if (!$existsResult->isSuccess())
175 {
176 $this->addErrors($existsResult->getErrors());
177 return null;
178 }
179
180 $application = self::getApplication();
181 $application->ResetException();
182
183 $deleteResult = \CIBlockProperty::Delete($id);
184 if (!$deleteResult)
185 {
186 if ($application->GetException())
187 {
188 $this->addError(new Error($application->GetException()->GetString()));
189 }
190 else
191 {
192 $this->addError(new Error('Error deleting product property'));
193 }
194 return null;
195 }
196
197 return true;
198 }
199
200 // endregion
201
205 public function getEntityTable()
206 {
207 return PropertyTable::class;
208 }
209
213 protected function exists($id)
214 {
215 $result = new Result();
216 $property = $this->get($id);
217 if (!$property || !$this->isIblockCatalog((int)$property['IBLOCK_ID']))
218 {
219 $result->addError(new Error('Property does not exist'));
220 }
221
222 return $result;
223 }
224
229 private function checkPropertyType(array $fields): Result
230 {
231 $result = new Result();
232
233 $type = [
234 'PROPERTY_TYPE' => $fields['PROPERTY_TYPE'] ?? null,
235 'USER_TYPE' => $fields['USER_TYPE'] ?? null,
236 ];
237
238 if (!in_array($type, Enum::getProductPropertyTypes(), false))
239 {
240 $result->addError(new Error('Invalid property type specified'));
241 }
242
243 return $result;
244 }
245
246 private function processCustomTypesBeforeAdd(array &$fields)
247 {
248 if (
249 $fields['PROPERTY_TYPE'] === \Bitrix\Iblock\PropertyTable::TYPE_LIST
250 && $fields['USER_TYPE'] === Enum::PROPERTY_USER_TYPE_BOOL_ENUM
251 )
252 {
253 $fields['LIST_TYPE'] = \Bitrix\Iblock\PropertyTable::CHECKBOX;
254 $this->customUserType = Enum::PROPERTY_USER_TYPE_BOOL_ENUM;
255 unset($fields['USER_TYPE']);
256 }
257 }
258
259 private function processCustomTypesAfterAdd(int $id, array $fields)
260 {
261 if ($this->customUserType === Enum::PROPERTY_USER_TYPE_BOOL_ENUM)
262 {
263 \CIBlockPropertyEnum::Add([
264 'PROPERTY_ID' => $id,
265 'VALUE' => Catalog\RestView\Product::BOOLEAN_VALUE_YES,
266 ]);
267 }
268 }
269}
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])