-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoomBehavior.php
More file actions
272 lines (246 loc) · 7.63 KB
/
RoomBehavior.php
File metadata and controls
272 lines (246 loc) · 7.63 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
<?php
/**
* Room Behavior
*
* @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('ModelBehavior', 'Model');
App::uses('Space', 'Rooms.Model');
/**
* Room Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Model\Behavior
*/
class RoomBehavior extends ModelBehavior {
/**
* スペースデータ
* ※publicにしているのは、UnitTestで使用するため
*
* @var array
*/
public static $spaces;
/**
* 参照できるスペースリストデータ
*
* @var array
*/
public static $readableSpaces = [
Space::PUBLIC_SPACE_ID
];
/**
* Setup this behavior with the specified configuration settings.
*
* @param Model $model Model using this behavior
* @param array $config Configuration settings for $model
* @return void
*/
public function setup(Model $model, $config = array()) {
parent::setup($model, $config);
$model->loadModels([
'RolesRoom' => 'Rooms.RolesRoom',
'RolesRoomsUser' => 'Rooms.RolesRoomsUser',
'Room' => 'Rooms.Room',
'RoomsLanguage' => 'Rooms.RoomsLanguage',
'Space' => 'Rooms.Space',
]);
}
/**
* ルームデータ取得用の条件取得
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param int $spaceId SpaceId
* @param array $options 取得オプション配列
* @return array ルームデータ取得条件
*/
public function getRoomsConditions(Model $model, $spaceId, $options = array()) {
$options = Hash::merge(array(
'recursive' => 1,
'conditions' => array(
$model->Room->alias . '.space_id' => $spaceId,
$model->Room->alias . '.page_id_top NOT' => null,
$model->Room->alias . '.in_draft' => false
),
'order' => 'Room.sort_key',
), $options);
return $options;
}
/**
* ルームデータ取得用の条件取得
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param array $conditions 条件配列
* @param int|null $userId ユーザID。nullの場合、`Current::read('User.id')`。
* @return array ルームデータ取得条件
*/
public function getReadableRoomsConditions(Model $model, $conditions = [], $userId = null) {
if (is_null($userId)) {
$userId = Current::read('User.id');
}
$spaceIds = self::$readableSpaces;
if ($userId) {
$spaceIds[] = Space::COMMUNITY_SPACE_ID;
$joinType = 'INNER';
} else {
$joinType = 'LEFT';
}
if (Current::read('User.UserRoleSetting.use_private_room')) {
$spaceIds[] = Space::PRIVATE_SPACE_ID;
}
if (! Current::allowSystemPlugin('rooms')) {
$conditions = Hash::merge(array('Room.active' => true, 'Room.in_draft' => false), $conditions);
}
$communityRoomId = Space::getRoomIdRoot(Space::COMMUNITY_SPACE_ID);
if (array_key_exists('Room.id', $conditions) && $conditions['Room.id'] === $communityRoomId) {
$conditions = Hash::merge(
array('OR' => array('Room.id' => $communityRoomId)),
$conditions
);
} elseif (isset($conditions[$model->Room->alias . '.page_id_top NOT'])) {
$conditions = Hash::merge(array('OR' => array(
$model->Room->alias . '.page_id_top NOT' => null,
'Room.id' => Space::getRoomIdRoot(Space::COMMUNITY_SPACE_ID),
)), $conditions);
unset($conditions[$model->Room->alias . '.page_id_top NOT']);
} else {
$conditions = Hash::merge(array($model->Room->alias . '.page_id_top NOT' => null), $conditions);
}
$options = Hash::merge(array(
'recursive' => 1,
'fields' => array(
$model->Room->alias . '.*',
$model->RolesRoom->alias . '.*',
$model->RolesRoomsUser->alias . '.*',
//$model->Space->alias . '.*',
),
'conditions' => array(
$model->Room->alias . '.space_id' => $spaceIds,
),
'joins' => array(
array(
'table' => $model->RolesRoomsUser->table,
'alias' => $model->RolesRoomsUser->alias,
'type' => $joinType,
'conditions' => array(
$model->RolesRoomsUser->alias . '.room_id' . ' = ' . $model->Room->alias . ' .id',
$model->RolesRoomsUser->alias . '.user_id' => $userId,
),
),
array(
'table' => $model->RolesRoom->table,
'alias' => $model->RolesRoom->alias,
'type' => $joinType,
'conditions' => array(
$model->RolesRoomsUser->alias . '.roles_room_id' . ' = ' . $model->RolesRoom->alias . ' .id',
$model->RolesRoom->alias . '.room_id' . ' = ' . $model->Room->alias . ' .id',
),
),
),
'order' => 'Room.sort_key',
), array('conditions' => $conditions));
return $options;
}
/**
* スペースデータ取得
*
* @param Model $model ビヘイビア呼び出し元モデル
* @return array スペースデータ配列
*/
public function getSpaces(Model $model) {
if (self::$spaces) {
return self::$spaces;
}
//スペースデータ取得
$bindParentRoom = $model->Room->belongsTo['ParentRoom'];
$bindChildRoom = $model->Room->hasMany['ChildRoom'];
$model->Room->unbindModel(array(
'belongsTo' => array('ParentRoom'),
'hasMany' => array('ChildRoom')
), false);
$spaces = $model->Room->find('all', array(
'recursive' => 1,
'conditions' => array(
$model->Room->alias . '.parent_id' => Space::getRoomIdRoot(Space::WHOLE_SITE_ID, 'Room'),
),
'order' => 'Room.sort_key'
));
// 外したものを戻しておく
$model->Room->bindModel(array(
'belongsTo' => array('ParentRoom' => $bindParentRoom),
'hasMany' => array('ChildRoom' => $bindChildRoom),
), false);
$spaces = Hash::combine($spaces, '{n}.Room.id', '{n}');
$result = $model->RolesRoom->find('all', array(
'recursive' => -1,
'fields' => '*',
'conditions' => array(
$model->RolesRoom->alias . '.room_id' => array(
Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID),
Space::getRoomIdRoot(Space::PRIVATE_SPACE_ID)
),
),
'joins' => array(
array(
'table' => $model->RolesRoomsUser->table,
'alias' => $model->RolesRoomsUser->alias,
'type' => 'INNER',
'conditions' => array(
$model->RolesRoomsUser->alias . '.roles_room_id' . ' = ' . $model->RolesRoom->alias . ' .id',
$model->RolesRoomsUser->alias . '.user_id' => Current::read('User.id'),
),
),
),
));
$result = Hash::combine($result, '{n}.RolesRoom.room_id', '{n}');
self::$spaces = Hash::combine(Hash::merge($spaces, $result), '{n}.Space.id', '{n}');
return self::$spaces;
}
/**
* ロールルームデータの取得
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param array $conditions 条件配列
* @return array
*/
public function getRolesRoomsInDraft(Model $model, $conditions = array()) {
if (! array_key_exists('Room.id', $conditions)) {
$conditions = Hash::merge(array(
'OR' => ['Room.page_id_top NOT' => null, 'Room.in_draft' => true]
), $conditions);
}
$rolesRooms = $model->RolesRoom->find('all', array(
'recursive' => 0,
'fields' => array(
$model->RolesRoom->alias . '.*',
$model->Room->alias . '.*',
),
'conditions' => $conditions,
));
return $rolesRooms;
}
/**
* ロールルームデータの取得
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param array $conditions 条件配列
* @return array
*/
public function getRolesRoomsNotInDraft(Model $model, $conditions = array()) {
$conditions = Hash::merge(array(
'Room.page_id_top NOT' => null, 'Room.in_draft' => false
), $conditions);
$rolesRooms = $model->RolesRoom->find('all', array(
'recursive' => 0,
'fields' => array(
$model->RolesRoom->alias . '.*',
$model->Room->alias . '.*',
),
'conditions' => $conditions,
));
return $rolesRooms;
}
}