forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrentPage.php
More file actions
381 lines (339 loc) · 9.38 KB
/
CurrentPage.php
File metadata and controls
381 lines (339 loc) · 9.38 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
<?php
/**
* CurrentPage Utility
*
* @author Noriko Arai <arai@nii.ac.jp>
* @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('CurrentFrame', 'NetCommons.Utility');
App::uses('Space', 'Rooms.Model');
/**
* CurrentPage Utility
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class CurrentPage {
/**
* Constant default room_role_key
*
* @var string
*/
const DEFAULT_ROOM_ROLE_KEY = 'visitor';
/**
* CurrentFrame Instance object
*
* @var mixed
*/
protected static $_instanceFrame;
/**
* setup current data
*
* @return void
*/
public function initialize() {
if (Current::$request->params['plugin'] === Current::PLUGIN_WYSIWYG) {
if (Hash::get(Current::$request->data, 'Room.id')) {
$roomId = Hash::get(Current::$request->data, 'Room.id');
} else {
$roomId = Hash::get(Current::$request->params, 'pass.0', '');
}
$this->setRoom($roomId);
}
$this->setPage();
$this->setTopPage();
$this->setPageByRoomPageTopId();
$this->setRolesRoomsUser();
$this->setDefaultRolePermissions();
$this->setRoomRolePermissions();
$this->setPluginsRoom();
//$this->setSpace();
}
/**
* Set RolesRoomsUser
*
* @return void
*/
public function setRolesRoomsUser() {
$this->RolesRoomsUser = ClassRegistry::init('Rooms.RolesRoomsUser');
if (isset(Current::$current['User']['id']) &&
isset(Current::$current['Room']['id']) && ! isset(Current::$current['RolesRoomsUser'])) {
$result = $this->RolesRoomsUser->getRolesRoomsUsers(array(
'RolesRoomsUser.user_id' => Current::$current['User']['id'],
'Room.id' => Current::$current['Room']['id']
));
if ($result) {
unset($result[0]['Room']);
Current::setCurrent($result[0], true);
}
}
}
/**
* Set BlockRolePermissions
*
* @param string $roleKey ロールキー
* @param bool $isMerge マージするかどうか
* @return void
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function setDefaultRolePermissions($roleKey = null, $isMerge = false) {
$this->DefaultRolePermission = ClassRegistry::init('Roles.DefaultRolePermission');
if (! isset(Current::$current['DefaultRolePermission'])) {
Current::$current['DefaultRolePermission'] = array();
}
if (! $roleKey && Current::$current['DefaultRolePermission']) {
return;
}
if ($roleKey) {
$roomRoleKey = $roleKey;
} elseif (isset(Current::$current['RolesRoom'])) {
$roomRoleKey = Current::$current['RolesRoom']['role_key'];
} else {
$roomRoleKey = self::DEFAULT_ROOM_ROLE_KEY;
}
$result = $this->DefaultRolePermission->find('all', array(
'recursive' => -1,
'conditions' => array(
'role_key' => $roomRoleKey,
)
));
if ($result) {
$result = Hash::combine(
$result, '{n}.DefaultRolePermission.permission', '{n}.DefaultRolePermission'
);
Current::$current['DefaultRolePermission'] = Hash::merge(
Current::$current['DefaultRolePermission'], $result
);
if ($isMerge) {
if (! isset(Current::$current['Permission'])) {
Current::$current['Permission'] = array();
}
Current::$current['Permission'] = Hash::merge(Current::$current['Permission'], $result);
}
}
}
/**
* Set RoomRolePermissions
*
* @return void
*/
public function setRoomRolePermissions() {
$this->RoomRolePermission = ClassRegistry::init('Rooms.RoomRolePermission');
if (isset(Current::$current['RoomRolePermission']) || ! isset(Current::$current['RolesRoom'])) {
return;
}
$result = $this->RoomRolePermission->find('all', array(
'recursive' => -1,
'conditions' => array(
'roles_room_id' => Current::$current['RolesRoom']['id'],
)
));
if ($result) {
Current::$current['RoomRolePermission'] = Hash::combine(
$result, '{n}.RoomRolePermission.permission', '{n}.RoomRolePermission'
);
}
}
/**
* ページ取得の条件取得
*
* @return array 条件配列
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function __getPageConditions() {
if (Hash::get(Current::$request->data, 'Page.id')) {
$pageId = Current::$request->data['Page']['id'];
$conditions = array('Page.id' => $pageId);
} elseif (!empty(Current::$request->params['pageView'])) {
$value = implode('/', Current::$request->params['pass']);
if ($value === '') {
$value = Page::PUBLIC_ROOT_PAGE_ID;
$conditions = array('Page.root_id' => $value);
} else {
$conditions = array(
'Page.permalink' => $value,
'Room.space_id' => Hash::get(
Current::$request->params, 'spaceId', Space::PUBLIC_SPACE_ID
),
);
}
} elseif (!empty(Current::$request->params['pageEdit'])) {
$value = Hash::get(Current::$request->params, 'pass.1', '');
if (! $value) {
$this->setRoom(Hash::get(Current::$request->params, 'pass.0', ''));
$value = Hash::get(Current::$current, 'Room.page_id_top', '');
}
$conditions = array('Page.id' => $value);
} elseif (Hash::get(Current::$request->query, 'page_id')) {
$pageId = Hash::get(Current::$request->query, 'page_id');
$conditions = array('Page.id' => $pageId);
} elseif (Hash::get(Current::$request->params, 'page_id')) {
$pageId = Hash::get(Current::$request->params, 'page_id');
$conditions = array('Page.id' => $pageId);
} elseif (in_array(Current::$request->params['plugin'],
[Current::PLUGIN_USERS, Current::PLUGIN_GROUPS], true) &&
! Current::$request->is('ajax')) {
$this->Room = ClassRegistry::init('Rooms.Room');
$result = $this->Room->getPrivateRoomByUserId(Current::read('User.id'));
Current::setCurrent($result);
$conditions = array(
'Page.id' => Hash::get($result, 'Room.page_id_top', Page::PUBLIC_ROOT_PAGE_ID)
);
} else {
$conditions = null;
}
return $conditions;
}
/**
* Set TopPage
*
* @return bool
*/
public function setTopPage() {
$this->Page = ClassRegistry::init('Pages.Page');
if (isset(Current::$current['TopPage'])) {
return;
}
$result = $this->__getPage(array(
'recursive' => -1,
'conditions' => array(
'Page.room_id' => Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID),
'Page.parent_id NOT' => null,
),
'order' => array('Page.lft' => 'asc')
));
Current::$current['TopPage'] = Hash::get($result, 'Page');
}
/**
* ページ取得
*
* @param array $query クエリ
* @return array 条件配列
*/
private function __getPage($query) {
$this->Page = ClassRegistry::init('Pages.Page');
if (! empty(Current::$request->params['requested'])) {
$this->Page->unbindModel(array(
'belongsTo' => array('Room'),
), true);
$this->Page->unbindModel(array(
'belongsTo' => array('Space'),
), true);
}
$result = $this->Page->find('first', $query);
return $result;
}
/**
* Set Page
*
* @return bool
*/
public function setPage() {
$this->Page = ClassRegistry::init('Pages.Page');
if (isset(Current::$current['Page'])) {
return;
}
$conditions = $this->__getPageConditions();
if ($conditions) {
$result = $this->__getPage(array(
'recursive' => 0,
'conditions' => $conditions,
'order' => array('Page.lft' => 'asc')
));
Current::setCurrent($result);
if (isset(Current::$current['Page'])) {
return;
}
}
if (isset(Current::$current['Room'])) {
$pageId = Hash::get(Current::$current, 'Room.page_id_top');
} elseif (! $conditions && Current::$request->params['plugin']) {
$pageId = $this->Page->getTopPageId();
} else {
$pageId = null;
}
if ($pageId) {
$result = $this->__getPage(array(
'recursive' => 0,
'conditions' => array('Page.id' => $pageId),
));
Current::setCurrent($result);
}
if (! self::$_instanceFrame) {
self::$_instanceFrame = new CurrentFrame();
}
self::$_instanceFrame->setBoxPageContainer();
}
/**
* Set Page
*
* @return bool
*/
public function setPageByRoomPageTopId() {
if (isset(Current::$current['Page']) || ! isset(Current::$current['Room'])) {
return;
}
$conditions = array(
'Page.id' => Hash::get(Current::$current, 'Room.page_id_top')
);
$result = $this->Page->find('first', array(
'recursive' => 0,
'conditions' => $conditions,
));
Current::setCurrent($result);
}
/**
* Set PluginsRoom
*
* @return bool
*/
public function setPluginsRoom() {
if (isset(Current::$current['PluginsRoom']) || ! isset(Current::$current['Room'])) {
return;
}
$this->PluginsRoom = ClassRegistry::init('PluginManager.PluginsRoom');
$result = $this->PluginsRoom->getPlugins(Current::read('Room.id'));
Current::$current['PluginsRoom'] = $result;
}
/**
* Set Room
*
* @param int $roomId Rooms.id
* @return bool
*/
public function setRoom($roomId) {
$this->Room = ClassRegistry::init('Rooms.Room');
$conditions = array(
'Room.id' => $roomId
);
$result = $this->Room->find('first', array(
'recursive' => 0,
'conditions' => $conditions,
));
Current::setCurrent($result);
}
/**
* set Space
*
* @return void
*/
//public function setSpace() {
// if (! isset(Current::$current['Room'])) {
// return;
// }
//
// $this->Space = ClassRegistry::init('Rooms.Space');
// $conditions = array(
// 'Space.id' => Hash::get(Current::$current, 'Room.space_id')
// );
// $result = $this->Space->find('first', array(
// 'recursive' => 0,
// 'conditions' => $conditions,
// ));
// Current::setCurrent($result, true);
//}
}