Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
componentcontroller.php
1<?php
9
14
15Loc::loadMessages(__FILE__);
16
17abstract class ComponentController
18{
19 protected $errors = array();
20 protected $action = null;
21 protected $responseData = array();
22 protected $requestData = array();
23
25 protected $request = array();
26
27 abstract protected function getActions();
28 abstract protected function checkPermissions();
29
30 protected function prepareRequestData()
31 {
32
33 }
34
35 protected function giveResponse()
36 {
37 global $APPLICATION;
38 $APPLICATION->restartBuffer();
39
40 header('Content-Type:application/json; charset=UTF-8');
41 echo Json::encode(
42 $this->responseData + array(
43 'error' => $this->hasErrors(),
44 'text' => implode('<br>', $this->errors),
45 )
46 );
47
48 \CMain::finalActions();
49 exit;
50 }
51
52 protected function getActionCall()
53 {
54 return array($this, $this->action);
55 }
56
57 protected function hasErrors()
58 {
59 return !empty($this->errors);
60 }
61
62 protected function check()
63 {
64 if(!$this->checkPermissions())
65 {
66 $this->errors[] = Loc::getMessage('MAIN_PERMISSION_DENIED');
67 }
68 if(!in_array($this->action, $this->getActions()))
69 {
70 $this->errors[] = 'Action "' . $this->action . '" not found.';
71 }
72 elseif(!check_bitrix_sessid() || !$this->request->isPost())
73 {
74 $this->errors[] = 'Security error.';
75 }
76 elseif(!is_callable($this->getActionCall()))
77 {
78 $this->errors[] = 'Action method "' . $this->action . '" not found.';
79 }
80
81 return !$this->hasErrors();
82 }
83
89 public function exec()
90 {
91 $this->request = Context::getCurrent()->getRequest();
92 $this->action = $this->request->get('action');
93
94 $this->prepareRequestData();
95
96 if($this->check())
97 {
98 call_user_func_array($this->getActionCall(), array($this->requestData));
99 }
100 $this->giveResponse();
101 }
102}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29