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
265 lines (240 loc) · 6.51 KB
/
NetCommonsAppController.php
File metadata and controls
265 lines (240 loc) · 6.51 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
<?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');
/**
* 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(
'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',
'NetCommons.Asset',
'NetCommons.NetCommons',
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'index' => null,
'view' => null,
),
),
'RequestHandler',
'Session',
'Workflow.Workflow',
'NetCommons.NetCommonsTime',
);
/**
* use model
*
* @var array
*/
public $uses = [
'M17n.Language',
'SiteManager.SiteSetting', //後で使う予定
];
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Html' => array(
'className' => 'NetCommons.SingletonViewBlockHtml'
),
'NetCommons.BackTo',
'NetCommons.Button',
'NetCommons.LinkButton',
'NetCommons.Date',
'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';
}
}
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
Security::setHash('sha512');
if (! Configure::read('NetCommons.installed') && $this->params['plugin'] === 'install') {
return;
}
//言語の取得
if (isset($this->request->query['language'])) {
Configure::write('Config.language', $this->request->query['language']);
$this->Session->write('Config.language', $this->request->query['language']);
} elseif ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
//カレントデータセット
Current::initialize($this->request);
if (Current::read('Block') &&
! $this->Components->loaded('NetCommons.Permission')) {
$this->Components->load('NetCommons.Permission');
}
//現在のテーマを取得
$theme = $this->Asset->getSiteTheme($this);
if ($theme) {
$this->theme = $theme;
}
$this->Auth->allow('index', 'view', 'emptyRender', 'download');
if ($this->RequestHandler->accepts('json')) {
$this->NetCommons->renderJson();
}
}
/**
* beforeRender
*
* @return void
*/
public function beforeRender() {
//theme css指定
$this->set('bootstrapMinCss', $this->Asset->isThemeBootstrapMinCss($this));
}
/**
* 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);
}
/**
* Keep connection alive
*
* @author Jun Nishikawa <topaz2@m0n0m0n0.com>
* @return void
**/
public function ping() {
$this->set('result', array('message' => 'OK'));
$this->set('_serialize', array('result'));
}
/**
* 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;
}
}