Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
3
5
19
20class Base extends Controller
21{
22 protected $viewManager;
23
29 protected function create($actionName)
30 {
31 $action = parent::create($actionName);
32
33 $this->viewManager = $this->createViewManager($action);
34
35 return $action;
36 }
37
42 protected function createViewManager(Action $action)
43 {
44 throw new NotImplementedException('The method createViewManager is not implemented.');
45 }
46
47
51 public function getViewManager()
52 {
53 return $this->viewManager;
54 }
55
62 protected function processBeforeAction(Engine\Action $action)
63 {
64 $r = $this->checkPermission($action->getName(), $action->getArguments());
65
66 if($r->isSuccess())
67 {
68 $internalizer = new Internalizer(
69 $this->getViewManager()
70 );
71
72 $r = $internalizer->process();
73
74 if($r->isSuccess())
75 {
76 $action->setArguments($r->getData()['data']);
77 return parent::processBeforeAction($action);
78 }
79 else
80 {
81 $this->addErrors($r->getErrors());
82 return null;
83 }
84 }
85 else
86 {
87 $this->addErrors($r->getErrors());
88 return null;
89 }
90 }
91
92 protected function processAfterAction(Engine\Action $action, $result)
93 {
94 $externalizer = null;
95 if($this->errorCollection->count()==0)
96 {
97 if($result instanceof Engine\Response\DataType\Page || is_array($result))
98 {
99 $data = $result instanceof Engine\Response\DataType\Page ?
100 $result->toArray():$result;
101
102 $externalizer = new Externalizer(
103 $this->getViewManager(),
104 $data
105 );
106 }
107
108 if($externalizer instanceof ModificationFieldsBase)
109 {
110 if($this->getScope() == Engine\Controller::SCOPE_REST)
111 {
112 return $result instanceof Engine\Response\DataType\Page ?
113 $externalizer->getPage($result):$externalizer;
114 }
115 else if($this->getScope() == Engine\Controller::SCOPE_AJAX)
116 {
117 return $externalizer;
118 }
119 }
120 }
121
122 return parent::processAfterAction($action, $result);
123 }
124
131 private function checkPermission($name, $arguments=[])
132 {
133 if($name == 'add')
134 {
135 $r = $this->checkCreatePermissionEntity();
136 }
137 elseif ($name == 'update')
138 {
139 $r = $this->checkUpdatePermissionEntity();
140 }
141 elseif ($name == 'list')
142 {
143 $r = $this->checkReadPermissionEntity();
144 }
145 elseif ($name == 'getfields')
146 {
147 $r = $this->checkGetFieldsPermissionEntity();
148 }
149 elseif ($name == 'get')
150 {
151 $r = $this->checkReadPermissionEntity();
152 }
153 elseif ($name == 'delete')
154 {
155 $r = $this->checkDeletePermissionEntity();
156 }
157 else
158 {
159 $r = $this->checkPermissionEntity($name, $arguments);
160 }
161
162 return $r;
163 }
164
169 {
170 return $this->checkReadPermissionEntity();
171 }
172
176 protected function checkReadPermissionEntity()
177 {
178 throw new NotImplementedException('Check read permission. The method checkReadPermissionEntity is not implemented.');
179 }
180
185 protected function checkModifyPermissionEntity()
186 {
187 throw new NotImplementedException('Check modify permission. The method checkModifyPermissionEntity is not implemented.');
188 }
189
194 protected function checkCreatePermissionEntity()
195 {
196 return $this->checkModifyPermissionEntity();
197 }
198
203 protected function checkUpdatePermissionEntity()
204 {
205 return $this->checkModifyPermissionEntity();
206 }
207
212 protected function checkDeletePermissionEntity()
213 {
214 return $this->checkModifyPermissionEntity();
215 }
216
223 protected function checkPermissionEntity($name, $arguments=[])
224 {
225 throw new NotImplementedException('Check permission entity. The method '.$name.' is not implemented.');
226 }
227
232 protected function getEntityTable()
233 {
234 throw new NotImplementedException('The method getEntityTable is not implemented.');
235 }
236
248 protected function getList(array $select, array $filter, array $order, PageNavigation $pageNavigation = null): array
249 {
250 $entityTable = $this->getEntityTable();
251
252 $select = empty($select) ? ['*'] : $select;
253 $order = empty($order) ? ['ID' => 'ASC'] : $order;
254 $params = [
255 'select' => $select,
256 'filter' => $filter,
257 'order' => $order,
258 ];
259 if ($pageNavigation)
260 {
261 $params['offset'] = $pageNavigation->getOffset();
262 $params['limit'] = $pageNavigation->getLimit();
263 }
264
265 return $entityTable::getList($params)->fetchAll();
266 }
267
275 protected function count($filter)
276 {
277 return function() use ($filter)
278 {
279 $entityTable = $this->getEntityTable();
280 return $entityTable::getCount([$filter]);
281 };
282 }
283
292 protected function get($id)
293 {
294 $entityTable = $this->getEntityTable();
295
296 return $entityTable::getById($id)->fetch();
297 }
298
304 public function add(array $fields)
305 {
306 $entityTable = $this->getEntityTable();
307 return $entityTable::add($fields);
308 }
309
319 protected function update($id, array $fields)
320 {
321 $entityTable = $this->getEntityTable();
322
324 $r = $this->exists($id);
325 if($r->isSuccess())
326 {
327 return $entityTable::update($id, $fields);
328 }
329 else
330 {
331 $this->addErrors($r->getErrors());
332 return null;
333 }
334 }
335
344 protected function delete($id)
345 {
346 $entityTable = $this->getEntityTable();
347
348 $r = $this->exists($id);
349 if($r->isSuccess())
350 {
351 return $entityTable::delete($id);
352 }
353 else
354 {
355 $this->addErrors($r->getErrors());
356 return null;
357 }
358 }
359
368 protected function exists($id)
369 {
370 $r = new \Bitrix\Main\Result();
371 if(isset($this->get($id)['ID']) == false)
372 $r->addError(new Error('Entity is not exists'));
373
374 return $r;
375 }
376
385 protected function existsByFilter($filter)
386 {
387 $result = new Result();
388 $entityData = $this->getEntityTable()::getList(['filter' => $filter, 'limit' => 1])->fetch();
389
390 if (!$entityData)
391 {
392 $result->addError(new Error('Entity is not exists'));
393 }
394
395 return $result;
396 }
397}
getList(array $select, array $filter, array $order, PageNavigation $pageNavigation=null)
Definition base.php:248
processBeforeAction(Engine\Action $action)
Definition base.php:62
processAfterAction(Engine\Action $action, $result)
Definition base.php:92
checkPermissionEntity($name, $arguments=[])
Definition base.php:223