-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPageLayoutHelper.php
More file actions
569 lines (508 loc) · 16.6 KB
/
PageLayoutHelper.php
File metadata and controls
569 lines (508 loc) · 16.6 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
/**
* LayoutHelper
*
* @copyright Copyright 2014, NetCommons Project
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('AppHelper', 'View/Helper');
App::uses('Container', 'Containers.Model');
App::uses('Box', 'Boxes.Model');
App::uses('Current', 'NetCommons.Utility');
/**
* LayoutHelper
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PageLayoutHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'Html',
'NetCommons.Button',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* Bootstrap col max size
*
* @var int
*/
const COL_MAX_SIZE = 12;
/**
* Bootstrap col-sm default size
*
* @var int
*/
const COL_DEFAULT_SIZE = 3;
/**
* コンテナー変数
*
* 何度も同じ処理をさせないために保持する
*
* @var array
*/
protected static $_containers;
/**
* プラグインデータ
*
* 何度も同じ処理をさせないために保持する
*
* @var array
*/
protected static $_plugins;
/**
* Containers data
*
* @var array
*/
public $containers;
/**
* Plugins data
*
* @var array
*/
public $plugins;
/**
* LayoutがNetCommons.settingかどうか
*
* @var array
*/
public $layoutSetting;
/**
* Default Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
$settings += [
'routingFormat' => ['/:plugin/:controller/:action'],
];
parent::__construct($View, $settings);
$isTestMock = (substr(get_class($this->_View->request), 0, 4) === 'Mock');
if (! self::$_containers || $isTestMock) {
if (isset($settings['page']['PageContainer'])) {
foreach ($settings['page']['PageContainer'] as $container) {
self::$_containers[$container['container_type']] = $container;
}
} else {
self::$_containers = [];
}
}
$this->containers = self::$_containers;
if (! self::$_plugins || $isTestMock) {
self::$_plugins = [];
$pluginRooms = Current::read('PluginsRoom', array());
foreach ($pluginRooms as $plugin) {
self::$_plugins[$plugin['Plugin']['key']] = $plugin['Plugin'];
}
}
$this->plugins = self::$_plugins;
$this->layoutSetting = Hash::get($settings, 'layoutSetting', false);
}
/**
* マジックメソッド。
*
* @param string $method メソッド
* @param array $params パラメータ
* @return string
*/
public function __call($method, $params) {
$boxMethods = array(
'getBox', 'boxTitle', 'displayBoxSetting', 'hasBox',
'hasBoxSetting', 'renderAddPlugin', 'renderFrames', 'renderBoxes',
);
$frameMethods = array(
'frameActionUrl', 'frameSettingLink', 'frameSettingQuitLink',
'frameOrderButton', 'frameDeleteButton',
);
if ($method === 'getBlockStatus') {
$helper = $this->_View->loadHelper('Blocks.Blocks');
return call_user_func_array(array($helper, $method), $params);
} elseif (in_array($method, $boxMethods, true)) {
$helper = $this->_View->loadHelper(
'Boxes.Boxes', array('containers' => $this->containers)
);
return call_user_func_array(array($helper, $method), $params);
} elseif (in_array($method, $frameMethods, true)) {
$helper = $this->_View->loadHelper(
'Frames.Frames', array('plugins' => $this->plugins)
);
return call_user_func_array(array($helper, $method), $params);
}
}
/**
* Before render callback. beforeRender is called before the view file is rendered.
*
* Overridden in subclasses.
*
* @param string $viewFile The view file that is going to be rendered
* @return void
*/
public function beforeRender($viewFile) {
$this->NetCommonsHtml->css('/pages/css/style.css');
$this->NetCommonsHtml->css('/boxes/css/style.css');
$this->NetCommonsHtml->script('/boxes/js/boxes.js');
//メタデータ
$metas = Hash::get($this->_View->viewVars, 'meta', array());
foreach ($metas as $meta) {
$this->NetCommonsHtml->meta($meta, null, ['inline' => false]);
}
parent::beforeRender($viewFile);
}
/**
* Before layout callback. beforeLayout is called before the layout is rendered.
*
* Overridden in subclasses.
*
* @param string $layoutFile The layout about to be rendered.
* @return void
*/
public function beforeLayout($layoutFile) {
if ($this->hasContainer(Container::TYPE_HEADER)) {
$this->_View->viewVars['pageHeader'] = $this->_View->element('Pages.page_header');
} else {
$this->_View->viewVars['pageHeader'] = '';
}
if ($this->hasContainer(Container::TYPE_MAJOR)) {
$this->_View->viewVars['pageMajor'] = $this->_View->element('Pages.page_major');
} else {
$this->_View->viewVars['pageMajor'] = '';
}
if ($this->hasContainer(Container::TYPE_MINOR)) {
$this->_View->viewVars['pageMinor'] = $this->_View->element('Pages.page_minor');
} else {
$this->_View->viewVars['pageMinor'] = '';
}
if ($this->hasContainer(Container::TYPE_FOOTER)) {
$this->_View->viewVars['pageFooter'] = $this->_View->element('Pages.page_footer');
} else {
$this->_View->viewVars['pageFooter'] = '';
}
parent::beforeLayout($layoutFile);
}
/**
* After render callback. afterRender is called after the view file is rendered
* but before the layout has been rendered.
*
* Overridden in subclasses.
*
* @param string $viewFile The view file that was rendered.
* @return void
*/
public function afterRender($viewFile) {
$attributes = array(
'id' => 'container-main',
'role' => 'main'
);
if ($this->layoutSetting && Current::read('Frame')) {
//Frame設定も含めたコンテンツElement
$element = $this->_View->element('Frames.setting_frame', array(
'view' => $this->_View->fetch('content')
));
//属性
$attributes['ng-controller'] = 'FrameSettingsController';
$frameCamelize = NetCommonsAppController::camelizeKeyRecursive(Current::read('Frame'));
$attributes['ng-init'] = 'initialize({frame: ' . json_encode($frameCamelize) . '})';
//セッティングモード
$this->_View->viewVars['isSettingMode'] = true;
} else {
//コンテンツElement
if ($this->_View->request->params['plugin'] === 'pages') {
$element = $this->_View->fetch('content');
} else {
$frame = Hash::merge(
Current::read('FramesLanguage', array(
'name' => Current::read('Plugin.name')
)),
Current::read('Frame', array(
'header_type' => null,
'id' => null,
'plugin_key' => Current::read('Plugin.key'),
))
);
if (isset($this->settings['frameElement'])) {
$frameElement = $this->settings['frameElement'];
} else {
$frameElement = 'Frames.frame';
}
$element = $this->_View->element($frameElement, array(
'frame' => $frame,
'view' => $this->_View->fetch('content'),
'centerContent' => true,
'box' => array(
'Box' => Current::read('Box'),
'BoxesPageContainer' => Current::read('BoxesPageContainer'),
),
));
}
//セッティングモード
$this->_View->viewVars['isSettingMode'] = Current::isSettingMode();
}
//ページコンテンツのセット
$this->_View->viewVars['pageContent'] = $this->_View->element('Pages.page_main', array(
'element' => $element,
'attributes' => $attributes
));
if (Current::read('Page.is_container_fluid')) {
$this->_View->viewVars['pageContainerCss'] = 'container-fluid';
} else {
$this->_View->viewVars['pageContainerCss'] = 'container';
}
}
/**
* Get the container size for layout
*
* @param string $containerType コンテナータイプ
* Container::TYPE_HEADER or Container::TYPE_MAJOR or Container::TYPE_MAIN or
* Container::TYPE_MINOR or Container::TYPE_FOOTER
* @return string Html class attribute
*/
public function containerSize($containerType) {
$result = '';
$mainCol = self::COL_MAX_SIZE;
if ($this->hasContainer(Container::TYPE_MAJOR)) {
$mainCol -= self::COL_DEFAULT_SIZE;
}
if ($this->hasContainer(Container::TYPE_MINOR)) {
$mainCol -= self::COL_DEFAULT_SIZE;
}
switch ($containerType) {
case Container::TYPE_MAJOR:
if ($this->hasContainer($containerType)) {
$result = ' col-md-' . self::COL_DEFAULT_SIZE . ' col-md-pull-' . $mainCol;
}
break;
case Container::TYPE_MINOR:
if ($this->hasContainer($containerType)) {
$result = ' col-md-' . self::COL_DEFAULT_SIZE;
}
break;
default:
$result = ' col-md-' . $mainCol;
if ($this->hasContainer(Container::TYPE_MAJOR)) {
$result .= ' col-md-push-' . self::COL_DEFAULT_SIZE;
}
}
return trim($result);
}
/**
* レイアウトの有無チェック
*
* @param string $containerType コンテナータイプ
* Container::TYPE_HEADER or Container::TYPE_MAJOR or Container::TYPE_MAIN or
* Container::TYPE_MINOR or Container::TYPE_FOOTER
* @param bool $layoutSetting レイアウト変更画面かどうか
* @return bool The layout have container
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function hasContainer($containerType, $layoutSetting = false) {
$result = Hash::get($this->containers, $containerType . '.is_published', false);
if (! $result) {
return false;
}
if (! Current::isSettingMode() && ! $layoutSetting) {
$box = $this->getBox($containerType);
$frames = Hash::combine($box, '{n}.Frame.{n}.id', '{n}.Frame.{n}');
$result = count($frames);
}
return (bool)$result;
}
/**
* requestAction()のオーバライド
*
* $urlは、/:plugin/:controller/:action?xxxx=yyyy&xxxx2=yyyy2 形式とする。
*
* @param string $url $url String or array-based URL. Unlike other URL arrays in CakePHP, this
* URL will not automatically handle passed and named arguments in the $url parameter.
* @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
* also be used to submit GET/POST data, and named/passed arguments.
* @return bool true or false on success/failure, or contents
* of rendered action if 'return' is set in $extra.
*/
public function requestAction($url, $extra = array()) {
if (empty($url)) {
return false;
}
$params = $this->_parseParams($url);
if (! $params) {
return parent::requestAction($url, $extra);
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra += array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1);
$data = isset($extra['data']) ? $extra['data'] : null;
unset($extra['data']);
$request = new CakeRequest($url);
if (isset($data)) {
$request->data = $data;
}
$this->_setParams($request, $params, $extra);
Router::setRequestInfo($request);
$result = $this->_dispatch($request, new CakeResponse(), $extra);
Router::popRequest();
return $result;
}
/**
* Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
* to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
*
* Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
* want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
* For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
* are also not accessible via URL.
*
* If no controller of given name can be found, invoke() will throw an exception.
* If the controller is found, and the action is not found an exception will be thrown.
*
* @param CakeRequest $request Request object to dispatch.
* @param CakeResponse $response Response object to put the results of the dispatch into.
* @return string|null if `$request['return']` is set then it returns response body, null otherwise
* @throws MissingControllerException When the controller is missing.
*/
protected function _dispatch(CakeRequest $request, CakeResponse $response) {
$controller = $this->_getController($request, $response);
if (!($controller instanceof Controller)) {
throw new MissingControllerException(array(
'class' => Inflector::camelize($request->params['controller']) . 'Controller',
'plugin' => empty($request->params['plugin']) ?
null : Inflector::camelize($request->params['plugin'])
));
}
$response = $this->_invoke($controller, $request);
if (isset($request->params['return'])) {
return $response->body();
}
}
/**
* Applies Routing and additionalParameters to the request to be dispatched.
* If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
*
* @param CakeRequest $request Request object to dispatch.
* @param array $params リクエストにセットするパラメータ
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
* @return void
*/
protected function _setParams(CakeRequest $request, $params, $additionalParams = []) {
$request->addParams($params);
if (!empty($additionalParams)) {
$request->addParams($additionalParams);
}
}
/**
* Applies Routing and additionalParameters to the request to be dispatched.
* If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
*
* @param string $url $url String or array-based URL. Unlike other URL arrays in CakePHP, this
* URL will not automatically handle passed and named arguments in the $url parameter.
* @return array|false
*/
protected function _parseParams($url) {
if (strpos($url, '?') !== false) {
list($url, $queryParameters) = explode('?', $url, 2);
parse_str($queryParameters, $queryParameters);
}
if (substr($url, -1, 1) === '/') {
$url = substr($url, 0, -1);
}
if (substr($url, 0, 1) !== '/') {
$url = '/' . $url;
}
$urlArr = explode('/', $url);
$countUrlArr = count($urlArr);
$params = false;
foreach ($this->settings['routingFormat'] as $format) {
$formatArr = explode('/', $format);
if ($countUrlArr === count($formatArr)) {
for ($i = 1; $countUrlArr > $i; $i++) {
$params[substr($formatArr[$i], 1)] = $urlArr[$i];
}
$params += ['named' => [], 'pass' => []];
break;
}
}
return $params;
}
/**
* Get controller to use, either plugin controller or application controller
*
* @param CakeRequest $request Request object
* @param CakeResponse $response Response for the controller.
* @return mixed name of controller if not loaded, or object if loaded
*/
protected function _getController($request, $response) {
$ctrlClass = $this->_loadController($request);
if (!$ctrlClass) {
return false;
}
$reflection = new ReflectionClass($ctrlClass);
if ($reflection->isAbstract() || $reflection->isInterface()) {
return false;
}
return $reflection->newInstance($request, $response);
}
/**
* Load controller and return controller class name
*
* @param CakeRequest $request Request instance.
* @return string|bool Name of controller class name
*/
protected function _loadController($request) {
$pluginName = $pluginPath = $controller = null;
if (!empty($request->params['plugin'])) {
$pluginName = $controller = Inflector::camelize($request->params['plugin']);
$pluginPath = $pluginName . '.';
}
if (!empty($request->params['controller'])) {
$controller = Inflector::camelize($request->params['controller']);
}
if ($pluginPath . $controller) {
$class = $controller . 'Controller';
App::uses('AppController', 'Controller');
App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
App::uses($class, $pluginPath . 'Controller');
if (class_exists($class)) {
return $class;
}
}
return false;
}
/**
* Initializes the components and models a controller will be using.
* Triggers the controller action, and invokes the rendering if Controller::$autoRender
* is true and echo's the output. Otherwise the return value of the controller
* action are returned.
*
* @param Controller $controller Controller to invoke
* @param CakeRequest $request The request object to invoke the controller for.
* @return CakeResponse the resulting response object
*/
protected function _invoke(Controller $controller, CakeRequest $request) {
$controller->constructClasses();
$controller->startupProcess();
$response = $controller->response;
$render = true;
$result = $controller->invokeAction($request);
if ($result instanceof CakeResponse) {
$render = false;
$response = $result;
}
if ($render && $controller->autoRender) {
$response = $controller->render();
} elseif (!($result instanceof CakeResponse) && $response->body() === null) {
$response->body($result);
}
$controller->shutdownProcess();
return $response;
}
}