1
|
|
|
<?php |
2
|
|
|
namespace Inji; |
3
|
|
|
/** |
4
|
|
|
* Controller |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
class Controller { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Storage of cur requested controller |
15
|
|
|
* |
16
|
|
|
* @var Controller |
17
|
|
|
*/ |
18
|
|
|
public static $cur = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Requested params for method |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $params = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Path to controller dir |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $path = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Requested action name |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $method = 'index'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Module of this controller |
43
|
|
|
* |
44
|
|
|
* @var Module |
45
|
|
|
*/ |
46
|
|
|
public $module = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This controller name |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $name = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Flag of controller runing |
57
|
|
|
* |
58
|
|
|
* @var boolean |
59
|
|
|
*/ |
60
|
|
|
public $run = false; |
61
|
|
|
|
62
|
|
|
public $methodResolved = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Run controller |
66
|
|
|
*/ |
67
|
|
|
public function run() { |
68
|
|
|
!$this->methodResolved && $this->resolveMethod(); |
69
|
|
|
if (!method_exists($this, $this->method . 'Action')) { |
70
|
|
|
INJI_SYSTEM_ERROR('method not found', true); |
71
|
|
|
} |
72
|
|
|
if (!$this->checkAccess()) { |
73
|
|
|
$msg = !empty($this->module->app->access->config['access']['accessTree'][App::$cur->type]['msg']) ? $this->module->app->access->config['access']['accessTree'][App::$cur->type]['msg'] : \I18n\Text::module('Access', 'noaccess'); |
74
|
|
|
Tools::redirect($this->access->getDeniedRedirect(), $msg); |
75
|
|
|
} |
76
|
|
|
$this->run = true; |
77
|
|
|
call_user_func_array([$this, $this->method . 'Action'], $this->params); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function resolveMethod() { |
81
|
|
|
if ($this->methodResolved) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
if (!empty($this->params[0]) && method_exists($this, $this->params[0] . 'Action')) { |
85
|
|
|
$this->method = $this->params[0]; |
86
|
|
|
$this->params = array_slice($this->params, 1); |
87
|
|
|
$this->methodResolved = true; |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Reference to short access core modules |
95
|
|
|
* |
96
|
|
|
* @param $name |
97
|
|
|
* @return Module|null |
98
|
|
|
*/ |
99
|
|
|
public function __get($name) { |
100
|
|
|
return App::$cur->__get($name); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Reference to short access core modules |
105
|
|
|
* |
106
|
|
|
* @param $name |
107
|
|
|
* @param $params |
108
|
|
|
* @return null|Module |
109
|
|
|
*/ |
110
|
|
|
public function __call($name, $params) { |
111
|
|
|
return App::$cur->__call($name, $params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check access to controller method |
116
|
|
|
* |
117
|
|
|
* @return boolean |
118
|
|
|
*/ |
119
|
|
|
public function checkAccess() { |
120
|
|
|
if ($this->module->app->access) { |
121
|
|
|
return $this->module->app->access->checkAccess($this); |
122
|
|
|
} |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|