Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productpropertyenum.php
1<?php
2
4
12
14{
15 // region Actions
16
20 public function getFieldsAction(): array
21 {
22 return ['PRODUCT_PROPERTY_ENUM' => $this->getViewFields()];
23 }
24
32 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
33 {
34 $filter['PROPERTY.IBLOCK_ID'] = $this->getCatalogIds();
35
36 return new Page(
37 'PRODUCT_PROPERTY_ENUMS',
38 $this->getList($select, $filter, $order, $pageNavigation),
39 $this->count($filter)
40 );
41 }
42
47 public function getAction(int $id): ?array
48 {
49 $r = $this->exists($id);
50 if ($r->isSuccess())
51 {
52 return ['PRODUCT_PROPERTY_ENUM' => $this->get($id)];
53 }
54
55 $this->addErrors($r->getErrors());
56 return null;
57 }
58
59 public function addAction(array $fields): ?array
60 {
61 $checkFieldsResult = $this->checkFieldsBeforeModify($fields);
62 if (!$checkFieldsResult->isSuccess())
63 {
64 $this->addErrors($checkFieldsResult->getErrors());
65 return null;
66 }
67
68 $property = $this->getPropertyById($fields['PROPERTY_ID']);
69 $propertyType = $property['PROPERTY_TYPE'];
70 if ($propertyType !== PropertyTable::TYPE_LIST)
71 {
72 $this->addError(new Error('Only list properties are supported'));
73 return null;
74 }
75
76 $application = self::getApplication();
77 $application->ResetException();
78
79 $addResult = PropertyEnumerationTable::add($fields);
80 if (!$addResult->isSuccess())
81 {
82 $this->addErrors($addResult->getErrors());
83 return null;
84 }
85
86 return ['PRODUCT_PROPERTY_ENUM' => $this->get($addResult->getId())];
87 }
88
94 public function updateAction(int $id, array $fields): ?array
95 {
96 $existsResult = $this->exists($id);
97 if (!$existsResult->isSuccess())
98 {
99 $this->addErrors($existsResult->getErrors());
100 return null;
101 }
102
103 $checkFieldsResult = $this->checkFieldsBeforeModify($fields);
104 if (!$checkFieldsResult->isSuccess())
105 {
106 $this->addErrors($checkFieldsResult->getErrors());
107 return null;
108 }
109
110 $propertyId = $this->get($id)['PROPERTY_ID'];
111 $updateResult = PropertyEnumerationTable::update([
112 'ID' => $id,
113 'PROPERTY_ID' => $propertyId,
114 ], $fields);
115 if (!$updateResult)
116 {
117 $this->addErrors($updateResult->getErrors());
118 return null;
119 }
120
121 return ['PRODUCT_PROPERTY_ENUM' => $this->get($id)];
122 }
123
128 public function deleteAction(int $id): ?bool
129 {
130 $existsResult = $this->exists($id);
131 if (!$existsResult->isSuccess())
132 {
133 $this->addErrors($existsResult->getErrors());
134 return null;
135 }
136
137 $propertyId = $this->get($id)['PROPERTY_ID'];
138 $deleteResult = PropertyEnumerationTable::delete([
139 'ID' => $id,
140 'PROPERTY_ID' => $propertyId,
141 ]);
142
143 if (!$deleteResult)
144 {
145 $this->addErrors($deleteResult->getErrors());
146 return null;
147 }
148
149 return $deleteResult->isSuccess();
150 }
151
152 // endregion
153
157 protected function get($id)
158 {
159 return PropertyEnumerationTable::getRow([
160 'select' => ['*', 'IBLOCK_ID' => 'PROPERTY.IBLOCK_ID'],
161 'filter' => ['=ID' => $id],
162 ]);
163 }
164
168 protected function exists($id)
169 {
170 $result = new Result();
171 $propertyEnum = $this->get($id);
172 if (!$propertyEnum || !$this->isIblockCatalog((int)$propertyEnum['IBLOCK_ID']))
173 {
174 $result->addError(new Error('Property enum does not exist'));
175 }
176
177 return $result;
178 }
179
183 protected function getEntityTable()
184 {
185 return PropertyEnumerationTable::class;
186 }
187}
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])