-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpace.php
More file actions
256 lines (229 loc) · 5.36 KB
/
Space.php
File metadata and controls
256 lines (229 loc) · 5.36 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
<?php
/**
* Space Model
*
* @property Space $ParentSpace
* @property Room $Room
* @property Space $ChildSpace
* @property Language $Language
*
* @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('RoomsAppModel', 'Rooms.Model');
/**
* Space Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Model
*/
class Space extends RoomsAppModel {
/**
* インスタンス
*
* @var array
*/
public static $instanceRoom = null;
/**
* インスタンス
*
* @var array
*/
public static $instanceSpace = null;
/**
* スペースデータ
* ※publicにしているのは、UnitTestで使用するため
*
* @var array
*/
public static $spaces;
/**
* Table name
*
* @var string
*/
public $useTable = 'spaces';
/**
* Space id
*
* @var const
*/
const
WHOLE_SITE_ID = '1',
PUBLIC_SPACE_ID = '2',
PRIVATE_SPACE_ID = '3',
COMMUNITY_SPACE_ID = '4';
/**
* SpaceIdのリスト
*
* @var array
*/
public static $spaceIds = array();
/**
* DefaultParticipationFixed
*
* @var bool
*/
public $participationFixed = false;
/**
* Behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.NetCommonsCache',
);
/**
* RoomSpaceルームのデフォルト値
*
* @param array $data 初期値データ配列
* @return array RoomSpaceルームのデフォルト値配列
*/
public function createRoom($data = array()) {
$this->loadModels([
'Language' => 'M17n.Language',
'Room' => 'Rooms.Room',
'RoomsLanguage' => 'Rooms.RoomsLanguage',
]);
$result = $this->Room->create(array_merge(array(
'id' => null,
'active' => true,
), $data));
$languages = $this->Language->getLanguages();
foreach ($languages as $i => $language) {
$roomsLanguage = $this->RoomsLanguage->create(array(
'id' => null,
'language_id' => $language['Language']['id'],
'room_id' => null,
'name' => '',
));
$result['RoomsLanguage'][$i] = $roomsLanguage['RoomsLanguage'];
}
return $result;
}
/**
* インスタンスの取得
*
* @param string $spaceModel モデル名(Migrationで使用)
* @param array $options ClassRegistryオプション
* @return object RoomSpaceルームのデフォルト値配列
*/
public static function getInstance($spaceModel = 'Space', $options = []) {
$options['class'] = 'Rooms.' . $spaceModel;
if ($spaceModel === 'Space') {
if (! self::$instanceSpace) {
self::$instanceSpace = ClassRegistry::init($options, true);
}
return self::$instanceSpace;
} else {
if (! self::$instanceRoom) {
self::$instanceRoom = ClassRegistry::init($options, true);
}
return self::$instanceRoom;
}
}
/**
* SpaceのルームIDを取得
*
* @param int $spaceId スペースID
* @param string $spaceModel モデル名(Migrationで使用)
* @param array $options ClassRegistryオプション
* @return int
*/
public static function getRoomIdRoot($spaceId, $spaceModel = 'Space', $options = []) {
$Space = self::getInstance($spaceModel, $options);
if ($spaceModel === 'Space') {
if (! isset(self::$spaceIds['Space'])) {
$spaces = $Space->cacheFindQuery('list', array(
'recursive' => -1,
'fields' => array('id', 'room_id_root'),
));
if ($spaces) {
self::$spaceIds['Space'] = $spaces;
}
}
$spaceIds = [];
if (isset(self::$spaceIds['Space'])) {
$spaceIds = self::$spaceIds['Space'];
}
} else {
if (! isset(self::$spaceIds['Room'])) {
$spaceIds = $Space->find('list', array(
'recursive' => -1,
'fields' => array('space_id', 'id'),
'conditions' => array(
'space_id' => self::WHOLE_SITE_ID
),
));
$result = $Space->find('list', array(
'recursive' => -1,
'fields' => array('space_id', 'id'),
'conditions' => array(
'parent_id' => $spaceIds[self::WHOLE_SITE_ID]
),
));
foreach ($result as $key => $item) {
$spaceIds[$key] = $item;
}
self::$spaceIds['Room'] = $spaceIds;
}
$spaceIds = [];
if (isset(self::$spaceIds['Room'])) {
$spaceIds = self::$spaceIds['Room'];
}
}
if (isset($spaceIds[$spaceId])) {
return $spaceIds[$spaceId];
}
return 0;
}
/**
* SpaceのページIDを取得
*
* @param int $spaceId スペースID
* @return int
*/
public static function getPageIdSpace($spaceId) {
$Space = ClassRegistry::init('Rooms.Space', true);
if (! isset(self::$spaceIds['Page'])) {
$spaces = $Space->cacheFindQuery('list', array(
'recursive' => -1,
'fields' => array('id', 'page_id_top'),
));
self::$spaceIds['Page'] = $spaces;
}
return (string)self::$spaceIds['Page'][$spaceId];
}
/**
* スペースデータ取得
*
* @param int $spaceId スペースID
* @return array スペースデータ配列
*/
public function getSpace($spaceId) {
$spaces = $this->getSpaces();
foreach ($spaces as $space) {
if ($spaceId == $space[$this->alias]['id']) {
return $space[$this->alias];
}
}
return [];
}
/**
* スペースデータ取得
*
* @return array スペースデータ配列
*/
public function getSpaces() {
if (self::$spaces) {
return self::$spaces;
}
self::$spaces = $this->cacheFindQuery('all', array(
'recursive' => -1
));
return self::$spaces;
}
}