-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuHelper.php
More file actions
411 lines (362 loc) · 11.2 KB
/
MenuHelper.php
File metadata and controls
411 lines (362 loc) · 11.2 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
<?php
/**
* MenuHelper
*
* @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('Room', 'Rooms.Model');
App::uses('Space', 'Rooms.Model');
App::uses('Page', 'Pages.Model');
/**
* MenuHelper
*
* @package NetCommons\Menus\View\Helper
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class MenuHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $parentPageIds = array();
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* メニューの表示
*
* @param string $displayType 表示タイプ
* @return string HTMLタグ
*/
public function renderMain($displayType = null) {
if (! $displayType) {
$displayType = $this->_View->viewVars['menuFrameSetting']['MenuFrameSetting']['display_type'];
}
//現在選択しているページの親ページIDs
$this->parentPageIds = [Page::PUBLIC_ROOT_PAGE_ID];
foreach ($this->_View->viewVars['parentPages'] as $parentPage) {
$this->parentPageIds[] = $parentPage['Page']['id'];
}
if (! in_array(Current::read('Page.id'), $this->parentPageIds, true)) {
$this->parentPageIds[] = Current::read('Page.id');
}
$this->parentPageIds = array_unique($this->parentPageIds);
//現在選択しているページの下層ページIDs
if (isset($this->_View->viewVars['childPageIds'][Current::read('Page.id')])) {
$this->childPageIds = $this->_View->viewVars['childPageIds'][Current::read('Page.id')];
} else {
$this->childPageIds = [];
}
// メニューのcss, js読み込み、HTML表示
$html = $this->NetCommonsHtml->elementDisplayChange($displayType);
return $html;
}
/**
* メニューの表示(さかのぼり:パンくず用)
*
* @param string $displayType 表示タイプ
* @return string HTMLタグ
*/
public function renderParent($displayType = null) {
$html = '';
$parentPageIds = [];
foreach ($this->_View->viewVars['parentPages'] as $parentPage) {
$parentPageIds[] = $parentPage['Page']['id'];
}
$childPages = $this->_View->viewVars['pages'][1]['ChildPage'];
//$childPages = Hash::sort($childPages, '{n}.lft', 'asc');
$childPages = Hash::sort($childPages, '{n}.sort_key', 'asc');
$parentPageIds = array_merge([$childPages[0]['id']], $parentPageIds);
$parentPageIds = array_unique($parentPageIds);
foreach ($parentPageIds as $parentPageId) {
$parentRoomId = null;
if (isset($this->_View->viewVars['pages'][$parentPageId]['Page']['room_id'])) {
$parentRoomId = $this->_View->viewVars['pages'][$parentPageId]['Page']['room_id'];
}
if (! $parentRoomId) {
continue;
}
$html .= $this->renderPage($parentPageId, $displayType);
}
return $html;
}
/**
* メニューリストの表示
*
* @param array $treePageId TreeページID
* @param string $displayType 表示タイプ
* @return string HTMLタグ
*/
public function renderPage($treePageId, $displayType = null) {
$html = '';
if (! $displayType) {
$displayType = $this->_View->viewVars['menuFrameSetting']['MenuFrameSetting']['display_type'];
}
$pageId = trim($treePageId);
$pages = $this->_View->viewVars['pages'];
$menus = $this->_View->viewVars['menus'];
$page = $pages[$pageId];
$menu = $menus[$page['Room']['id']][$pageId];
$nest = $this->_getIndent($treePageId, $page);
if (! $this->_displayPage(trim($treePageId), $menu)) {
return $html;
}
$html .= $this->_View->element('Menus.Menus/' . $displayType . '/list', array(
'isActive' => $this->isActive($page),
'nest' => $nest,
'options' => $this->link($menu, $displayType),
'menu' => $menu,
'page' => $page,
));
return $html;
}
/**
* リンクの表示
*
* @param array $menu リンクデータ
* @param string $displayType 表示タイプ
* @return array NetCommonsHtml->linkの引数
*/
public function link($menu, $displayType = null) {
if (! $menu) {
return array();
}
if (! $displayType) {
$displayType = $this->_View->viewVars['menuFrameSetting']['MenuFrameSetting']['display_type'];
}
$setting = '';
if (Current::isSettingMode()) {
$setting = Current::SETTING_MODE_WORD . '/';
}
$room = $this->_View->viewVars['menuFrameRooms'][$menu['Page']['room_id']];
$viewPage = $this->_View->viewVars['pages'];
if ($room['Room']['page_id_top'] === $menu['Page']['id'] &&
$room['Room']['id'] === Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID)) {
$url = '';
} elseif ($viewPage[$menu['Page']['id']]['Page']['full_permalink']) {
$url = h($viewPage[$menu['Page']['id']]['Page']['full_permalink']);
} else {
$url = h($menu['Page']['permalink']);
}
$domId = $this->getLinkDomId($displayType, $menu['Page']['id']);
$domIdIcon = $domId . 'Icon';
$options = array('id' => $domId, 'escapeTitle' => false);
$toggle = (int)in_array($menu['Page']['id'], $this->parentPageIds, true);
$hasChildPage = $this->hasChildPage($menu, false);
$title = $this->__getTitle($menu, $displayType, $domId, $hasChildPage);
if ($menu['MenuFramesPage']['folder_type'] && $hasChildPage) {
$childPageIds = array();
$childPageIds = $this->getRecursiveChildPageId(
$menu['Page']['room_id'], $menu['Page']['id'], $childPageIds
);
$childDomIds = array_map(function ($value) use($displayType) {
return $this->getLinkDomId($displayType, $value);
}, $childPageIds);
$options['ng-init'] = $domIdIcon . '=' . $toggle . ';' .
' initialize(\'' . $domId . '\', ' . json_encode($childDomIds) . ', ' . $toggle . ')';
$options['ng-click'] = $domIdIcon . '=!' . $domIdIcon . ';' .
' switchOpenClose($event, \'' . $domId . '\')';
$url = '#';
} else {
$url = '/' . $setting . $url;
}
return array(
'title' => $title['title'],
'icon' => $title['icon'],
'url' => $url,
'options' => $options
);
}
/**
* リンクのタイトル表示
*
* @param array $menu リンクデータ
* @param string $displayType 表示タイプ
* @param string $domId DomのID
* @param bool $hasChildPage 子供ページを持っているか否か
* @return array array(タイトル, アイコン)
*/
private function __getTitle($menu, $displayType, $domId, $hasChildPage) {
$room = $this->_View->viewVars['menuFrameRooms'][$menu['Page']['room_id']];
$title = '';
if ($room &&
$room['Room']['page_id_top'] === $menu['Page']['id'] &&
$room['Room']['id'] !== Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID)) {
$title .= h($room['RoomsLanguage']['name']);
} else {
$title .= h($menu['PagesLanguage']['name']);
}
$domIdIcon = $domId . 'Icon';
$toggle = (int)in_array($menu['Page']['id'], $this->parentPageIds, true);
if ($menu['MenuFramesPage']['folder_type'] && $hasChildPage) {
$icon = '<span class="glyphicon glyphicon-menu-right"' .
' ng-class="{' .
'\'glyphicon-menu-right\': !' . $domIdIcon . ', ' .
'\'glyphicon-menu-down\': ' . $domIdIcon . '' .
'}"> ' .
'</span> ';
} elseif ($hasChildPage) {
if ($toggle) {
$icon = '<span class="glyphicon glyphicon-menu-down"> </span> ';
} else {
$icon = '<span class="glyphicon glyphicon-menu-right"> </span> ';
}
} else {
$icon = null;
}
return array('title' => $title, 'icon' => $icon);
}
/**
* ChildPageのIdを取得する(再帰的に)
*
* @param int $roomId Room.id
* @param int $pageId Page.id
* @param array $result 再帰の結果配列
* @return string HTMLタグ
*/
public function getRecursiveChildPageId($roomId, $pageId, $result) {
if (isset($this->_View->viewVars['childPageIds'][$pageId])) {
$childPageIds = $this->_View->viewVars['childPageIds'][$pageId];
} else {
$childPageIds = [];
}
foreach ($childPageIds as $childPageId) {
$result[] = $childPageId;
$prefixInput = $roomId . '.' . $childPageId . '.MenuFramesPage.folder_type';
if (empty($this->_View->viewVars['menus'][$prefixInput])) {
$result = $this->getRecursiveChildPageId($roomId, $childPageId, $result);
}
}
return $result;
}
/**
* 表示するかどうか
*
* @param string $treePageId Tree(Tabコード付き)のページID
* @return bool
*/
public function displayPage($treePageId) {
$pageId = trim($treePageId);
$pages = $this->_View->viewVars['pages'];
$menus = $this->_View->viewVars['menus'];
$page = $pages[$pageId];
$menu = $menus[$page['Room']['id']][$pageId];
return $this->_displayPage($pageId, $menu);
}
/**
* 表示するかどうか
*
* @param string $pageId ページID
* @param array $menu メニューデータ
* @return bool
*/
protected function _displayPage($pageId, $menu) {
if (! $menu['PagesLanguage']['name'] ||
! $menu ||
! isset($this->_View->viewVars['treeList4Disp'][$pageId])) {
return false;
}
if ($pageId === Page::PUBLIC_ROOT_PAGE_ID) {
return false;
}
return true;
}
/**
* アクティブかどうか
*
* @param array $page ページデータ配列
* @return bool
*/
public function isActive($page) {
return Current::read('Page.id') === (string)$page['Page']['id'];
}
/**
* 子ページを持っているかどうか
*
* @param int $menu メニューデータ
* @param bool $hasTreeList $treeList4Dispに持っているかどうか
* @return bool
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function hasChildPage($menu, $hasTreeList = false) {
if (isset($this->_View->viewVars['childPageIds'][$menu['Page']['id']])) {
$childPage = $this->_View->viewVars['childPageIds'][$menu['Page']['id']];
} else {
$childPage = [];
}
if ($hasTreeList) {
$pageTreeList = $this->_View->viewVars['treeList4Disp'];
} else {
$pageTreeList = $this->_View->viewVars['pageTreeList'];
}
$hasChildPage = false;
foreach ($childPage as $id) {
if (isset($pageTreeList[$id])) {
$hasChildPage = true;
break;
}
}
if ($hasTreeList) {
$result = $hasChildPage;
} else {
if ($menu['MenuFramesPage']['folder_type'] && $hasChildPage) {
$result = true;
} elseif ($hasChildPage) {
$result = true;
} else {
$result = false;
}
}
return $result;
}
/**
* インデント数の取得
*
* @param string $treePageId Tree(Tabコード付き)のページID
* @return int
*/
public function getIndent($treePageId) {
$pageId = trim($treePageId);
$pages = $this->_View->viewVars['pages'];
$page = $pages[$pageId];
return $this->_getIndent($treePageId, $page);
}
/**
* インデント数の取得
*
* @param string $treePageId Tree(Tabコード付き)のページID
* @param array $page ページ情報
* @return int
*/
protected function _getIndent($treePageId, $page) {
$indent = substr_count($treePageId, Page::$treeParser);
if ($page['Page']['root_id'] === Page::PUBLIC_ROOT_PAGE_ID) {
$indent--;
}
return $indent;
}
/**
* リンクのdomIdを取得する
*
* @param string $displayType 表示タイプ
* @param int $pageId ページID
* @return string
*/
public function getLinkDomId($displayType, $pageId) {
$domDisplayType = preg_replace('/-/', '_', $displayType);
return $this->domId(
'MenuFramesPage.' . $domDisplayType . '.' . Current::read('Frame.id') . '.' . $pageId
);
}
}