forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsAppController.php
More file actions
341 lines (301 loc) · 8.43 KB
/
NetCommonsAppController.php
File metadata and controls
341 lines (301 loc) · 8.43 KB
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
<?php
/**
* NetCommonsApp Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Controller', 'Controller');
App::uses('Utility', 'Inflector');
App::uses('Current', 'NetCommons.Utility');
App::uses('NetCommonsUrl', 'NetCommons.Utility');
App::uses('PermissionComponent', 'NetCommons.Controller/Component');
App::uses('SiteSettingUtil', 'SiteManager.Utility');
/**
* NetCommonsApp Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Controller
*/
class NetCommonsAppController extends Controller {
/**
* use layout
*
* @var string
*/
public $layout = 'NetCommons.default';
/**
* use theme
*
* @var string
*/
public $theme = 'default';
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.AccessCtrl',
'Auth' => array(
'loginAction' => array(
'plugin' => 'auth',
'controller' => 'auth',
'action' => 'login',
),
'loginRedirect' => array(
'plugin' => 'pages',
'controller' => 'pages',
'action' => 'index',
),
'logoutRedirect' => array(
'plugin' => 'pages',
'controller' => 'pages',
'action' => 'index',
)
),
'DebugKit.Toolbar',
'Flash',
'MobileDetect.MobileDetect',
'NetCommons.Asset',
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'index' => null,
'view' => null,
),
),
'NetCommons.NetCommons',
'NetCommons.NetCommonsTime',
'RequestHandler',
'Session',
'Workflow.Workflow',
);
/**
* use model
*
* @var array
*/
public $uses = [
'M17n.Language',
];
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Html' => array(
'className' => 'NetCommons.SingletonViewBlockHtml'
),
'M17n.M17n',
'NetCommons.BackTo',
'NetCommons.Button',
'NetCommons.LinkButton',
'NetCommons.Date',
'NetCommons.MessageFlash',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* Constructor.
*
* @param CakeRequest $request Request object for this controller. Can be null for testing,
* but expect that features that use the request parameters will not work.
* @param CakeResponse $response Response object for this controller.
*/
public function __construct($request = null, $response = null) {
parent::__construct($request, $response);
if (in_array('Html', $this->helpers, true) &&
!isset($this->helpers['Html']['className'])) {
$this->helpers['Html']['className'] = 'NetCommons.SingletonViewBlockHtml';
}
//サイトの設定データセット
if (Configure::read('NetCommons.installed')) {
SiteSettingUtil::initialize();
}
}
/**
* 事前準備
*
* @return void
*/
private function __prepare() {
if (Current::read('Block') &&
! $this->Components->loaded('NetCommons.Permission')) {
$this->Components->load('NetCommons.Permission');
}
//現在のテーマを取得
$theme = $this->Asset->getSiteTheme($this);
if ($theme) {
$this->theme = $theme;
}
if ($this->RequestHandler->accepts('json')) {
$this->viewClass = 'Json';
$this->layout = false;
}
if (in_array($this->params['action'], ['emptyRender', 'throwBadRequest', 'emptyFrame'])) {
$this->params['pass'] = array();
}
}
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
if (empty($this->request->params['requested'])) {
$this->request->allowMethod('get', 'post', 'put', 'delete');
}
Security::setHash('sha512');
//言語のセット
$this->_setLanguage();
//カレントデータセット
Current::initialize($this);
if (! $this->AccessCtrl->allowAccess()) {
return;
}
$this->Auth->allow('index', 'view', 'emptyRender', 'download', 'throwBadRequest', 'emptyFrame');
$this->__prepare();
//モバイルかどうかの判定処理
Configure::write('isMobile', $this->MobileDetect->detect('isMobile'));
if ($this->params['plugin'] !== 'frames' &&
Current::read('Frame.id') && ! Current::read('FramePublicLanguage.is_public')) {
return $this->setAction('emptyFrame');
}
}
/**
* Called after the controller action is run and rendered.
*
* @return void
* @link http://book.cakephp.org/2.0/ja/controllers.html#request-life-cycle-callbacks
*/
public function afterFilter() {
//カレントデータセット
Current::terminate($this);
}
/**
* リクエストもしくはSessionから言語をセットする。
*
* @return void
*/
protected function _setLanguage() {
if (isset($this->request->query['lang']) &&
! array_key_exists('search', $this->request->query)) {
Configure::write('Config.language', $this->request->query['lang']);
$this->Session->write('Config.language', $this->request->query['lang']);
} elseif ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
//多言語の切り替えボックス
$languages = $this->Language->getLanguages();
$this->set('switchLanguages', $languages);
$this->set('hasSwitchLang', count($languages) > 1);
}
/**
* beforeRender
*
* @return void
*/
public function beforeRender() {
//theme css指定
$this->set('bootstrapMinCss', $this->Asset->isThemeBootstrapMinCss($this));
$controller = Inflector::camelize($this->params['controller']);
$pluginPath = Hash::get(App::path('View', Inflector::camelize($this->params['plugin'])), '0');
$path = $pluginPath . $controller . DS . 'json' . DS . $this->view . '.ctp';
if ($this->viewClass === 'Json' &&
! isset($this->viewVars['_serialize']) && ! file_exists($path)) {
$this->NetCommons->renderJson();
}
}
/**
* The beforeRedirect method is invoked when the controller's redirect method is called but before any
* further action.
*
* If this method returns false the controller will not continue on to redirect the request.
* The $url, $status and $exit variables have same meaning as for the controller's method. You can also
* return a string which will be interpreted as the URL to redirect to or return associative array with
* key 'url' and optionally 'status' and 'exit'.
*
* @param string|array $url A string or array-based URL pointing to another location within the app,
* or an absolute URL
* @param int $status Optional HTTP status code (eg: 404)
* @param bool $exit If true, exit() will be called after the redirect
* @return mixed
* false to stop redirection event,
* string controllers a new redirection URL or
* array with the keys url, status and exit to be used by the redirect method.
* @throws Exception
* @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function beforeRedirect($url, $status = null, $exit = true) {
if ($url === null && $status >= 400) {
//Auth->allowによるエラーにメッセージが含まれない
$error = $this->response->httpCodes($status);
throw new Exception(__d('net_commons', $error[$status]), $status);
}
return parent::beforeRedirect($url, $status, $exit);
}
/**
* camelizeKeyRecursive
*
* @param array $orig data to camelize
* @return array camelized data
*/
public static function camelizeKeyRecursive($orig) {
$new = [];
$callback = ['Inflector', 'variable'];
foreach ($orig as $key => $value) {
if (is_array($value)) {
$new[call_user_func($callback, $key)] = self::camelizeKeyRecursive($value);
} else {
$new[call_user_func($callback, $key)] = $value;
}
}
return $new;
}
/**
* throw bad request
*
* @param string|null $message メッセージ
* @return void
* @throws BadRequestException
*/
public function throwBadRequest($message = null) {
if (! $message) {
$message = __d('net_commons', 'Bad Request');
}
if ($this->request->is('ajax')) {
$this->NetCommons->setFlashNotification(__d('net_commons', 'Bad Request'), array(
'class' => 'danger',
'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL,
'error' => $message
), 400);
} else {
throw new BadRequestException($message);
}
}
/**
* Empty render
*
* @return void
*/
public function emptyRender() {
$this->autoRender = false;
}
/**
* Empty render
*
* @return void
*/
public function emptyFrame() {
if (Current::isSettingMode() || $this->layout === 'NetCommons.setting') {
$this->view = 'Frames.Frames/emptyRender';
} else {
$this->autoRender = false;
}
}
}