Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
measure.php
1<?php
2
3
5
6
15
16final class Measure extends Controller implements EventBindInterface
17{
18 //region Actions
22 public function getFieldsAction(): array
23 {
24 return ['MEASURE' => $this->getViewFields()];
25 }
26
35 public function addAction(array $fields): ?array
36 {
37 $r = $this->existsByFilter([
38 '=CODE' => $fields['CODE'],
39 ]);
40 if ($r->isSuccess() === false)
41 {
42 $r = $this->checkDefaultValue($fields);
43 if ($r->isSuccess())
44 {
45 $r = parent::add($fields);
46 if ($r->isSuccess())
47 {
48 return ['MEASURE' => $this->get($r->getPrimary())];
49 }
50 }
51 }
52 else
53 {
54 $r->addError($this->getErrorDublicateFieldCode());
55 }
56
57 $this->addErrors($r->getErrors());
58
59 return null;
60 }
61
71 public function updateAction(int $id, array $fields): ?array
72 {
73 $existsResult = $this->exists($id);
74 if (!$existsResult->isSuccess())
75 {
76 $this->addErrors($existsResult->getErrors());
77
78 return null;
79 }
80
81 $r = $this->checkMeasureBeforeUpdate($id, $fields);
82 if ($r->isSuccess())
83 {
84 $r = parent::update($id, $fields);
85 if ($r->isSuccess())
86 {
87 return ['MEASURE' => $this->get($id)];
88 }
89 }
90
91 $this->addErrors($r->getErrors());
92
93 return null;
94 }
95
104 public function deleteAction(int $id): ?bool
105 {
106 $existsResult = $this->exists($id);
107 if (!$existsResult->isSuccess())
108 {
109 $this->addErrors($existsResult->getErrors());
110 return null;
111 }
112
113 $r = parent::delete($id);
114 if ($r->isSuccess())
115 {
116 return true;
117 }
118 else
119 {
120 $this->addErrors($r->getErrors());
121 return null;
122 }
123 }
124
136 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
137 {
138 return new Page(
139 'MEASURES',
140 $this->getList($select, $filter, $order, $pageNavigation),
141 $this->count($filter)
142 );
143 }
144
153 public function getAction($id)
154 {
155 $r = $this->exists($id);
156 if($r->isSuccess())
157 {
158 return ['MEASURE' => $this->get($id)];
159 }
160 else
161 {
162 $this->addErrors($r->getErrors());
163 return null;
164 }
165 }
166 //endregion
167
168 protected function checkDefaultValue(array $fields): Result
169 {
170 $r = new Result();
171
177 $fields['IS_DEFAULT'] = $fields['IS_DEFAULT'] ?? 'N';
178
179 if ($fields['IS_DEFAULT'] === 'Y')
180 {
181 $exist = $this->existsByFilter([
182 '=IS_DEFAULT' => $fields['IS_DEFAULT'],
183 ]);
184 if ($exist->isSuccess())
185 {
186 $r->addError(new Error('default value can be set once [isDefault]'));
187 }
188 }
189
190 return $r;
191 }
192
193 protected function getEntityTable(): DataManager
194 {
195 return new MeasureTable();
196 }
197
198 protected function checkModifyPermissionEntity()
199 {
200 $r = new Result();
201
202 if (!$this->accessController->check(ActionDictionary::ACTION_STORE_VIEW))
203 {
204 $r->addError(new Error('Access Denied', 200040300020));
205 }
206
207 return $r;
208 }
209
210 protected function checkReadPermissionEntity()
211 {
212 $r = new Result();
213
214 if (
215 !(
216 $this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
217 || $this->accessController->check(ActionDictionary::ACTION_STORE_VIEW)
218 )
219 )
220 {
221 $r->addError(new Error('Access Denied', 200040300010));
222 }
223 return $r;
224 }
225
226 protected function checkMeasureBeforeUpdate(int $id, array $fields): Result
227 {
228 if (isset($fields['CODE']))
229 {
230 $existsResult = $this->existsByFilter([
231 '!=ID' => $id,
232 '=CODE' => $fields['CODE'],
233 ]);
234 if ($existsResult->isSuccess())
235 {
236 $result = new Result();
237 $result->addError($this->getErrorDublicateFieldCode());
238
239 return $result;
240 }
241 }
242
243 return $this->checkDefaultValue($fields);
244 }
245
246 private function getErrorDublicateFieldCode(): Error
247 {
248 return new Error('Duplicate entry for key [code]');
249 }
250}
updateAction(int $id, array $fields)
Definition measure.php:71
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition measure.php:136
checkMeasureBeforeUpdate(int $id, array $fields)
Definition measure.php:226