/
abstract.php
390 lines (341 loc) · 10.6 KB
/
abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php defined('SYSPATH') or die('No direct script access allowed.');
/*******************************************************************************
* ExidoEngine Web-sites manager
*
* NOTICE OF LICENSE
*
* This source file is subject to the GNU General Public License (3.0)
* that is bundled with this package in the file license_en.txt
* It is also available through the world-wide-web at this URL:
* http://www.exidoengine.com/license/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@exidoengine.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade ExidoEngine to newer
* versions in the future. If you wish to customize ExidoEngine for your
* needs please refer to http://www.exidoengine.com for more information.
*
* @license http://www.exidoengine.com/license/gpl-3.0.html (GNU General Public License v3)
* @author ExidoTeam
* @copyright Copyright (c) 2009 - 2013, ExidoEngine Solutions
* @link http://www.exidoengine.com/
* @since Version 1.0
* @filesource
*******************************************************************************/
require_once 'controller/interface/abstract.php';
/**
* Base controller class.
* @package core
* @copyright Sharapov A.
* @created 25/12/2009
* @version 1.0
*/
abstract class Controller_Abstract implements Controller_Interface_Abstract
{
/**
* Allow all the controllers to run in production by default
*/
const ALLOW_PRODUCTION = true;
/**
* URI instance
* @var Uri
*/
public $uri;
/**
* Input instance
* @var Input
*/
public $input;
/**
* View instance
* @var View
*/
public $view;
/**
* Prevent after controller
* @var bool
*/
public static $preventAfterController;
/**
* Current system user
* @var string
*/
protected $_system_user;
/**
* Current system user access
* @var string
*/
protected $_system_user_access;
/**
* Installed components
* @var null
*/
protected $_components = null;
/**
* The list of supported environments
* @var array
*/
private $_envs = array('DEVELOPER', 'ADMINISTRATOR', 'FRONTEND', 'PUBLISHER');
/**
* Loaded models registry
* @var bool|object
*/
private $_model;
/**
* Action view object
* @var View
*/
private $_viewAction;
/**
* Layout view object
* @var View
*/
private $_viewLayout;
/**
* Layout view status
* @var bool
*/
private $_disableLayoutView = false;
/**
* Action view status
* @var bool
*/
private $_disableActionView = false;
/**
* Return an action view
* @var bool|string
*/
private $_returnActionView = false;
// ---------------------------------------------------------------------------
/**
* Loads URI and Input objects into the controller.
* Check if the requested environment supports by the system.
* @throws Exception_Exido
*/
public function __construct()
{
// Check environment
if( ! in_array(EXIDO_ENVIRONMENT_NAME, $this->_envs)) {
throw new Exception_Exido('The requested environment %s does not supported by the system', array(EXIDO_ENVIRONMENT_NAME));
}
// URI should always be available
$this->uri = Uri::instance();
// Input should always be available
$this->input = Input::instance();
// Init a model loader object
$this->_model = Registry::factory('Model');
// Input should always be available
$this->view = View::instance();
// Session object
$this->session = Registry::factory('Session');
// Init layout view object
$this->_viewLayout = Registry::factory('View_Layout');
// Init action view object
$this->_viewAction = Registry::factory('View_Action');
// Get active components
$this->_components = $this->model('Model_Component')->getActiveComponents();
// Get session data
$_user_key = $this->_getCurrentUser();
// If current user is unknown so we should start guest session
if(empty($_user_key)) {
// Guest session id
$_user_key = '5627a272bf2563cee5877539bd906e7cc3eb33afcefe2b570a08906f9a34ae48';
}
if($r = $this->model('Model_User')->getUserByUniqueKey($_user_key)) {
$this->_system_user = array(
'user_id' => $r->getUser_id(),
'user_name' => $r->getUser_name(),
'password' => $r->getPassword(),
'user_email' => $r->getUser_email(),
'owner_id' => $r->getOwner_id(),
'owner_name' => $r->getOwner_name(),
'group_id' => $r->getGroup_id(),
'group_name' => $r->getGroup_name(),
'role_name' => $r->getRole_key(),
'permissions' => array(
'owner' => $r->getPermissions_owner(),
'group' => $r->getPermissions_group(),
'other' => $r->getPermissions_other()
),
'is_enabled' => $r->getIs_enabled(),
'is_dropped' => $r->getIs_dropped(),
'is_system' => $r->getIs_system()
);
// User constants
define('@SU.GROUP_ID' , $r->getGroup_id());
define('@SU.GROUP_NAME' , $r->getGroup_name());
define('@SU.USER_ID' , $r->getUser_id());
define('@SU.USER_NAME' , $r->getUser_name());
$this->_system_user_access = $this->model('Model_User')->getUserAccess($r->getUser_id(), EXIDO_ENVIRONMENT_NAME);
$this->session->set('system_user', $_user_key);
}
}
// ---------------------------------------------------------------------------
/**
* Disable all the views for method.
* @return void
*/
protected function disableViews()
{
$this->disableActionView();
$this->disableLayoutView();
}
// ---------------------------------------------------------------------------
/**
* Disable the action view showing.
* @return void
*/
protected function disableActionView()
{
$this->_disableActionView = true;
}
// ---------------------------------------------------------------------------
/**
* Disable the layout view showing.
* @return void
*/
protected function disableLayoutView()
{
$this->_disableLayoutView = true;
}
// ---------------------------------------------------------------------------
/**
* Disable the execution of "After" controller method.
* Can be executed in "Before" or main controller methods.
* @return void
*/
protected function preventAfter()
{
self::$preventAfterController = true;
}
// ---------------------------------------------------------------------------
/**
* Set the special layout view for controller action.
* @param string $path
* @param null $name
* @return void
*/
protected function setForceLayoutView($path, $name = null)
{
$this->_viewLayout->setLayout($path, $name);
}
// ---------------------------------------------------------------------------
/**
* Loads a model.
* @param string $model
* @param null $params
* @return mixed
*/
protected function model($model, $params = null)
{
return $this->_model->load($model, $params);
}
// ---------------------------------------------------------------------------
/**
* Automatically executed before the controller action. Can be used to set
* class properties, do authorization checks, and execute other custom code.
* NOTE: controller method will fail if the method returns FALSE.
* This method can't be executed directly.
* @return bool
*/
public function beforeController()
{
return true;
}
// ---------------------------------------------------------------------------
/**
* Automatically executed after the controller action. Can be used to apply
* transformation to the request response, add extra output, and execute
* other custom code. This method can't be executed directly.
* @return bool
*/
public function afterController()
{
return true;
}
// ---------------------------------------------------------------------------
/**
* Automatically executed after all the controller actions.
* This method can't be executed directly.
* @return void
*/
public function pushLayoutController()
{
$action_content = $this->view->getActionContent();
// If an action view still not was generated, so we need to generate it
if($this->_returnActionView == false and empty($action_content)) {
// If the action view is enabled. Prints it.
if($this->_disableActionView == false) {
// Load an action view to the View object
$this->_viewAction->load(Router::$controller_view, Router::$method);
// Parse action view
$action_content = $this->_viewAction->parse($this->view, new View_Helper);
// Set parsed action view
$this->view->setActionContent($action_content);
} else
$this->view->setActionContent('');
}
// If the layout view is enabled. Prints it.
if($this->_disableLayoutView == false) {
// Load the layout template
$this->_viewLayout->load();
// Print the layout
print $this->_viewLayout->parse($this->view, new View_Helper);
}
}
// ---------------------------------------------------------------------------
/**
* Gets the action view HTML.
* @param bool $print
* @return bool|string
*/
public function getActionView($print = false)
{
$this->_returnActionView = true;
// Load an action view to the View object
$this->_viewAction->load(Router::$controller_view, Router::$method);
// Parse action view
$action_content = $this->_viewAction->parse($this->view, new View_Helper);
// Set parsed action view
$this->view->setActionContent($action_content);
if($print) {
print $this->view->getActionContent();
return true;
}
return $this->view->getActionContent();
}
// ---------------------------------------------------------------------------
/**
* Prints the action view HTML.
* @return string
*/
public function printActionView()
{
$this->getActionView(true);
}
// ---------------------------------------------------------------------------
/**
* Get current user.
* @return mixed
*/
private function _getCurrentUser()
{
return $this->session->get('system_user');
}
// ---------------------------------------------------------------------------
/**
* Handles methods that do not exist.
* @param string $method
* @param array $args
* @return void
* @throws Exception_Exido_404
*/
public function __call($method, array $args)
{
throw new Exception_Exido_404('Undefined method %s(%s)', array($method, implode(', ', $args)));
}
}
?>