-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPageLayoutHelper.php
More file actions
348 lines (307 loc) · 9.07 KB
/
PageLayoutHelper.php
File metadata and controls
348 lines (307 loc) · 9.07 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
<?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
*
*/
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()) {
parent::__construct($View, $settings);
$isTestMock = (substr(get_class($this->_View->request), 0, 4) === 'Mock');
if (! self::$_containers || $isTestMock) {
self::$_containers = Hash::combine(
Hash::get($settings, 'page.PageContainer', array()), '{n}.container_type', '{n}'
);
}
$this->containers = self::$_containers;
if (! self::$_plugins || $isTestMock) {
self::$_plugins = Hash::combine(
Current::read('PluginsRoom', array()), '{n}.Plugin.key', '{n}.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;
}
}