Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pricetype.php
1<?php
2
3
5
6
14
15final class PriceType extends Controller implements EventBindInterface
16{
17 use PriceTypeRights;
18
19 const EVENT_ON_ADD = 'OnGroupAdd';
20 const EVENT_ON_UPDATE = 'OnGroupUpdate';
21 const EVENT_ON_DELETE = 'OnGroupDelete';
22
23 private const USER_GROUP_ADMINS = 1;
24 private const USER_GROUP_ALL_USERS = 2;
25
26 //region Actions
27
32 public function addAction(array $fields): ?array
33 {
34 $application = self::getApplication();
35 $application->ResetException();
36
37 $fields['USER_GROUP'] = $fields['USER_GROUP_BUY'] = [self::USER_GROUP_ADMINS, self::USER_GROUP_ALL_USERS];
38
39 $addResult = \CCatalogGroup::Add($fields);
40 if (!$addResult)
41 {
42 if ($application->GetException())
43 {
44 $this->addError(new Error($application->GetException()->GetString()));
45 }
46 else
47 {
48 $this->addError(new Error('Error adding price type'));
49 }
50 return null;
51 }
52
53 return ['PRICE_TYPE' => $this->get($addResult)];
54 }
55
61 public function updateAction(int $id, array $fields): ?array
62 {
63 $existsResult = $this->exists($id);
64 if (!$existsResult->isSuccess())
65 {
66 $this->addErrors($existsResult->getErrors());
67 return null;
68 }
69
70 $application = self::getApplication();
71 $application->ResetException();
72
73 $updateResult = \CCatalogGroup::Update($id, $fields);
74 if (!$updateResult)
75 {
76 if ($application->GetException())
77 {
78 $this->addError(new Error($application->GetException()->GetString()));
79 }
80 else
81 {
82 $this->addError(new Error('Error updating price type'));
83 }
84 return null;
85 }
86
87 return ['PRICE_TYPE' => $this->get($id)];
88 }
89
94 public function deleteAction(int $id): ?bool
95 {
96 $existsResult = $this->exists($id);
97 if (!$existsResult->isSuccess())
98 {
99 $this->addErrors($existsResult->getErrors());
100 return null;
101 }
102
103 $application = self::getApplication();
104 $application->ResetException();
105
106 $deleteResult = \CCatalogGroup::Delete($id);
107 if (!$deleteResult)
108 {
109 if ($application->GetException())
110 {
111 $this->addError(new Error($application->GetException()->GetString()));
112 }
113 else
114 {
115 $this->addError(new Error('Error deleting price type'));
116 }
117 return null;
118 }
119
120 return true;
121 }
122
126 public function getFieldsAction(): array
127 {
128 return ['PRICE_TYPE' => $this->getViewFields()];
129 }
130
138 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
139 {
140 return new Page(
141 'PRICE_TYPES',
142 $this->getList($select, $filter, $order, $pageNavigation),
143 $this->count($filter)
144 );
145 }
146
151 public function getAction($id): ?array
152 {
153 $r = $this->exists($id);
154 if ($r->isSuccess())
155 {
156 return ['PRICE_TYPE' => $this->get($id)];
157 }
158 else
159 {
160 $this->addErrors($r->getErrors());
161 return null;
162 }
163 }
164 //endregion
165
169 protected function getEntityTable()
170 {
171 return new \Bitrix\Catalog\GroupTable();
172 }
173
177 protected function exists($id)
178 {
179 $r = new Result();
180 if (isset($this->get($id)['ID']) == false)
181 {
182 $r->addError(new Error('Price type is not exists'));
183 }
184
185 return $r;
186 }
187
188 // rest-event region
189 protected static function getBindings(): array
190 {
191 $entity = (new self())->getEntity();
192
193 return [
194 self::EVENT_ON_ADD => $entity->getModule().'.price.type.on.add',
195 self::EVENT_ON_UPDATE => $entity->getModule().'.price.type.on.update',
196 self::EVENT_ON_DELETE => $entity->getModule().'.price.type.on.delete',
197 ];
198 }
199
203 public static function getCallbackRestEvent(): array
204 {
205 return [self::class, 'processItemEvent'];
206 }
207
213 public static function processItemEvent(array $arParams, array $arHandler): array
214 {
215 $id = $arParams[0] ?? null;
216 if (!$id)
217 {
218 throw new RestException('id not found trying to process event');
219 }
220
221 return [
222 'FIELDS' => [
223 'ID' => $id
224 ],
225 ];
226 }
227 // endregion
228}
updateAction(int $id, array $fields)
Definition pricetype.php:61
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
static processItemEvent(array $arParams, array $arHandler)