Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
status.php
1<?php
2
3
5
6
19use phpDocumentor\Reflection\Types\This;
20
22{
23 //region Actions
24 public function getFieldsAction()
25 {
26 $view = $this->getViewManager()
27 ->getView($this);
28
29 return ['STATUS'=>$view->prepareFieldInfos(
30 $view->getFields()
31 )];
32 }
33
34 protected function getEntityTable(): StatusTable
35 {
36 return new StatusTable();
37 }
38
39 public function addAction($fields)
40 {
41 $r = new Result();
42
43 $res = $this->exists($fields['ID']);
44 if($res->isSuccess() == false)
45 {
46 $r = $this->validate($fields);
47 if($r->isSuccess())
48 {
49 $fields = $this->prepareFields($fields);
50 $r = $this->getEntityTable()::add($fields);
51 }
52 }
53 else
54 {
55 $r->addError(new Error('Duplicate entry for key [id]',201350000001));
56 }
57
58 if(!$r->isSuccess())
59 {
60 $this->addErrors($r->getErrors());
61 return null;
62 }
63 else
64 {
65 return ['STATUS'=>$this->get($fields['ID'])];
66 }
67 }
68
69 public function updateAction($id, array $fields)
70 {
71 $r = $this->exists($id);
72 if($r->isSuccess())
73 {
74 $fields['ID'] = $id;
75
76 $r = $this->validate($fields);
77 if($r->isSuccess())
78 {
79 $r = $this->getEntityTable()::update($id, $fields);
80 }
81 }
82
83 if($r->isSuccess())
84 {
85 return ['STATUS'=>$this->get($id)];
86 }
87 else
88 {
89 $this->addErrors($r->getErrors());
90 return null;
91 }
92 }
93
94 public function deleteAction($id)
95 {
96 $r = $this->exists($id);
97 if($r->isSuccess())
98 {
99 if (in_array($id, [
104 {
105 $r->addError(new Error('delete status type loced',201350000002));
106 }
107
108 if($r->isSuccess())
109 {
110 $r = $this->getEntityTable()::delete($id);
111 }
112 }
113
114 if($r->isSuccess())
115 {
116 return true;
117 }
118 else
119 {
120 $this->addErrors($r->getErrors());
121 return null;
122 }
123 }
124
125 public function getAction($id)
126 {
127 $r = $this->exists($id);
128 if($r->isSuccess())
129 {
130 return ['STATUS'=>$this->get($id)];
131 }
132 else
133 {
134 $this->addErrors($r->getErrors());
135 return null;
136 }
137 }
138
139 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
140 {
141 $select = empty($select) ? ['*'] : $select;
142 $order = empty($order) ? ['ID' => 'ASC'] : $order;
143
144 $items = $this->getEntityTable()::getList(
145 [
146 'select' => $select,
147 'filter' => $filter,
148 'order' => $order,
149 'offset' => $pageNavigation->getOffset(),
150 'limit' => $pageNavigation->getLimit(),
151 ]
152 )->fetchAll();
153
154 return new Page('STATUSES', $items, function() use ($filter)
155 {
156 return $this->getEntityTable()::getCount([$filter]);
157 });
158 }
159 //endregion
160
161 protected function prepareFields($fields)
162 {
163 if(!isset($fields['XML_ID']) && $fields['XML_ID'] == '')
164 {
165 $fields['XML_ID'] = $this->getEntityTable()::generateXmlId();
166 }
167
168 $fields['COLOR'] = isset($fields['COLOR']) ? $fields['COLOR']:'';
169 $fields['NOTIFY'] = isset($fields['NOTIFY']) && $fields['NOTIFY']=='Y' ? 'Y':'N';
170 $fields['SORT'] = isset($fields['SORT']) ? $fields['SORT']:0;
171
172 return $fields;
173 }
174
175 protected function validate(array $fields)
176 {
177 $r = new Result();
178
179 if(!in_array($fields['TYPE'], [
182 ]))
183 {
184 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_TYPE_OUT_OF_RANGE'), 201350000003));
185 }
186
187 if(trim($fields['ID'])=='')
188 {
189 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_TYPE_ID_EMPTY'), 201350000004));
190 }
191 elseif(mb_strlen($fields['ID']) > 2)
192 {
193 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_TYPE_STRLEN'), 201350000005));
194 }
195
196 /* TODO: check is_latin()
197 * if(!is_latin($fields['TYPE']))
198 {
199 $r->addError(new Error('', 'ERROR_STATUS_TYPE_LATIN_ONLY'));
200 }*/
201
202 if($r->isSuccess())
203 {
204 if($status = $this->get($fields['ID']))
205 {
206 $lockedType = $this->getLockedStatusType($fields['ID']);
207 if($lockedType<>'' && $lockedType!=$fields['TYPE'])
208 {
209 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_WRONG_TYPE'),201350000006));
210 }
211
212 if ($status['TYPE'] != $fields['TYPE'])
213 {
214 if ($status['TYPE'] == \Bitrix\Sale\OrderStatus::TYPE)
215 {
216 if(\Bitrix\Sale\Internals\OrderTable::getList([
217 'select'=>['ID'],
218 'filter'=>['STATUS_ID'=>$status['ID']],
219 'limit'=>1
220 ])->fetch())
221 {
222 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_TYPE_ORDER_EXISTS'),201350000007));
223 }
224 }
225 else
226 {
227 if(\Bitrix\Sale\Internals\ShipmentTable::getList([
228 'select'=>['ID'],
229 'filter'=>['STATUS_ID'=>$status['ID']],
230 'limit'=>1
231 ])->fetch())
232 {
233 $r->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_STATUS_TYPE_SHIPMENT_EXISTS'),201350000008));
234 }
235 }
236 }
237 }
238 }
239
240 return $r;
241 }
242
243 protected function getLockedStatusType($statusId)
244 {
245 $lockedStatusList = [
249 ],
253 ]
254 ];
255
256 foreach ($lockedStatusList as $lockStatusType=>$lockStatusIdList)
257 {
258 foreach ($lockStatusIdList as $lockStatusId)
259 {
260 if ($lockStatusId == $statusId)
261 {
262 return $lockStatusType;
263 }
264 }
265 }
266 return '';
267 }
268
269 protected function get($id)
270 {
271 return $this->getEntityTable()::getById($id)->fetch();
272 }
273
274 protected function exists($id)
275 {
276 $r = new Result();
277 if(isset($this->get($id)['ID']) == false)
278 $r->addError(new Error('status is not exists', 201340400001));
279
280 return $r;
281 }
282
284 {
285 $r = new Result();
286
287 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
288 if ($saleModulePermissions < "W")
289 {
290 $r->addError(new Error('Access Denied', 200040300020));
291 }
292 return $r;
293 }
294
295 protected function checkReadPermissionEntity(): Result
296 {
297 $r = new Result();
298
299 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
300 if ($saleModulePermissions == "D")
301 {
302 $r->addError(new Error('Access Denied', 200040300010));
303 }
304 return $r;
305 }
306}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
updateAction($id, array $fields)
Definition status.php:69
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition status.php:139