Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productpropertyfeature.php
1<?php
2
4
5use Bitrix\Catalog\Product\PropertyCatalogFeature;
12
14{
15 // region Actions
16
20 public function getFieldsAction(): array
21 {
22 return ['PRODUCT_PROPERTY_FEATURE' => $this->getViewFields()];
23 }
24
29 public function getAvailableFeaturesByPropertyAction(int $propertyId): ?array
30 {
31 $checkPropertyResult = $this->checkProperty($propertyId);
32 if (!$checkPropertyResult->isSuccess())
33 {
34 $this->addErrors($checkPropertyResult->getErrors());
35 return null;
36 }
37
38 $property = $this->getPropertyById($propertyId);
39
40 return ['FEATURES' => PropertyCatalogFeature::getPropertyFeatureList($property)];
41 }
42
47 public function addAction(array $fields): ?array
48 {
49 $checkFieldsResult = $this->checkFieldsBeforeModify($fields);
50 if (!$checkFieldsResult->isSuccess())
51 {
52 $this->addErrors($checkFieldsResult->getErrors());
53 return null;
54 }
55
56 $propertyId = (int)$fields['PROPERTY_ID'];
57 unset($fields['PROPERTY_ID']);
58
59 $addResult = PropertyCatalogFeature::addFeatures($propertyId, [$fields]);
60 if (!$addResult->isSuccess())
61 {
62 $this->addErrors($addResult->getErrors());
63 return null;
64 }
65
66 $propertyFeatureId = current($addResult->getData());
67 return ['PRODUCT_PROPERTY_FEATURE' => $this->get($propertyFeatureId)];
68 }
69
74 public function getAction(int $id): ?array
75 {
76 $r = $this->exists($id);
77 if ($r->isSuccess())
78 {
79 return ['PRODUCT_PROPERTY_FEATURE' => $this->get($id)];
80 }
81
82 $this->addErrors($r->getErrors());
83 return null;
84 }
85
93 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
94 {
95 $filter['PROPERTY.IBLOCK_ID'] = $this->getCatalogIds();
96
97 return new Page(
98 'PRODUCT_PROPERTY_FEATURES',
99 $this->getList($select, $filter, $order, $pageNavigation),
100 $this->count($filter)
101 );
102 }
103
109 public function updateAction(int $id, array $fields): ?array
110 {
111 $existsResult = $this->exists($id);
112 if (!$existsResult->isSuccess())
113 {
114 $this->addErrors($existsResult->getErrors());
115 return null;
116 }
117
118 $checkFieldsResult = $this->checkFieldsBeforeModify($fields);
119 if (!$checkFieldsResult->isSuccess())
120 {
121 $this->addErrors($checkFieldsResult->getErrors());
122 return null;
123 }
124
125 $propertyId = (int)$fields['PROPERTY_ID'];
126 unset($fields['PROPERTY_ID']);
127 $updateResult = PropertyCatalogFeature::updateFeatures($propertyId, [$fields]);
128 if (!$updateResult)
129 {
130 $this->addErrors($updateResult->getErrors());
131 return null;
132 }
133
134 return ['PRODUCT_PROPERTY_FEATURE' => $this->get($id)];
135 }
136
137 // endregion
138
142 protected function get($id)
143 {
144 return PropertyFeatureTable::getRow([
145 'select' => ['*', 'IBLOCK_ID' => 'PROPERTY.IBLOCK_ID'],
146 'filter' => ['=ID' => $id],
147 ]);
148 }
149
153 protected function exists($id)
154 {
155 $result = new Result();
156 $propertyFeature = $this->get($id);
157 if (!$propertyFeature || !$this->isIblockCatalog((int)$propertyFeature['IBLOCK_ID']))
158 {
159 $result->addError(new Error('Property feature does not exist'));
160 }
161
162 return $result;
163 }
164
168 protected function getEntityTable()
169 {
170 return PropertyFeatureTable::class;
171 }
172
176 protected function checkPermissionEntity($name, $arguments = [])
177 {
178 if ($name === 'getavailablefeaturesbyproperty')
179 {
180 return $this->checkReadPermissionEntity();
181 }
182
183 return parent::checkPermissionEntity($name);
184 }
185}
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])