-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationLocationReservable.php
More file actions
332 lines (308 loc) · 9.27 KB
/
ReservationLocationReservable.php
File metadata and controls
332 lines (308 loc) · 9.27 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
<?php
/**
* ReservationLocationReservable Model
*
* @property Room $Room
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('ReservationsAppModel', 'Reservations.Model');
/**
* Summary for ReservationLocationReservable Model
*/
class ReservationLocationReservable extends ReservationsAppModel {
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'reservation_location_reservable';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Room' => array(
'className' => 'Rooms.Room',
'foreignKey' => 'room_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
'location_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'role_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
));
return parent::beforeValidate($options);
}
/**
* 予約可能な施設か?(権限判定のみ)
*
* @param array $location ReservationLocation data
* @return bool
*/
public function isReservableByLocation($location) {
$this->loadModels([
'RolesRoomsUser' => 'Rooms.RolesRoomsUser',
//'RolesRoom' => 'Rooms.RolesRoom',
//'Room' => 'Rooms.Room'
]);
$roomIds = $this->getReadableRoomIdsWithOutPrivate();
$userId = Current::read('User.id');
if (!$userId) {
return [];
}
// 個人的な予約OKな施設
if ($location['ReservationLocation']['use_private']) {
// マイルームが使えるならOK
$this->loadModels(['UserRoleSetting' => 'UserRoles.UserRoleSetting']);
$userRole = Current::read('User.role_key');
$userRoleSetting = $this->UserRoleSetting->find('first', [
'conditions' => [
'UserRoleSetting.role_key' => $userRole
]
]);
$usePrivateRoom = $userRoleSetting['UserRoleSetting']['use_private_room'];
return $usePrivateRoom;
}
if ($location['ReservationLocation']['use_all_rooms']) {
// 全てのルームで予約Ok
// アクセスできる全ルーム(プライベートのぞく)でのロール取得
$rolesRoomsUsers = $this->RolesRoomsUser->find('all', array(
'recursive' => 0,
'conditions' => array(
'RolesRoomsUser.user_id' => $userId,
'RolesRoomsUser.room_id' => $roomIds,
),
));
$roleKeys = Hash::combine($rolesRoomsUsers, '{n}.RolesRoom.role_key', '{n}.RolesRoom.role_key');
$conditions = [
'ReservationLocationReservable.location_key' => $location['ReservationLocation']['key'],
'ReservationLocationReservable.role_key' => $roleKeys,
'room_id' => null,
];
$reservables = $this->find('all', ['conditions' => $conditions]);
foreach ($reservables as $reservable) {
if (Hash::get($reservable, 'ReservationLocationReservable.value')) {
// いずれかのロールで予約権限ついてれば予約OK
return true;
}
}
return false;
} else {
// 選択されたルームのみ予約OK
$reservable = false;
foreach ($roomIds as $roomId) {
// ルームでのロール取得
$rolesRoomsUsers = $this->RolesRoomsUser->find('first', array(
'recursive' => 0,
'conditions' => array(
'RolesRoomsUser.user_id' => $userId,
'RolesRoomsUser.room_id' => $roomId,
),
));
$roleKeys = Hash::get($rolesRoomsUsers, 'RolesRoom.role_key');
// ロールに対する予約権限取得
$conditions = [
'ReservationLocationReservable.location_key' => $location['ReservationLocation']['key'],
'ReservationLocationReservable.role_key' => $roleKeys,
'room_id' => $roomId,
];
$reservable = $this->find('first', ['conditions' => $conditions]);
// ユーザがアクセス可能なルーム(プライベートのぞく)のいずれかで予約OKなら予約できる施設
if (Hash::get($reservable, 'ReservationLocationReservable.value', false)) {
$reservable = true;
}
}
return $reservable;
}
}
/**
* 施設に対する予約できる権限データを取得する
*
* @param string $locationKey 施設キー
* @param int| $roomId ルームID
* @return array
*/
public function getPermissions($locationKey, $roomId = null) {
// reseravableデータ取得
if ($locationKey !== null) {
if (! $roomId) {
$result = $this->find('first', [
'recursive' => -1,
'fields' => ['location_key', 'room_id'],
'conditions' => [
'location_key' => $locationKey
]
]);
$roomId = Hash::get($result, 'ReservationLocationReservable.room_id');
}
$result = $this->find('all', [
'recursive' => -1,
'conditions' => [
'location_key' => $locationKey,
'room_id' => $roomId
]
]);
$reservables = Hash::combine(
$result,
'{n}.ReservationLocationReservable.role_key',
'{n}.ReservationLocationReservable.value'
);
} else {
// default
$reservables = [];
}
//DefaultRolePermission取得
$this->loadModels([
'DefaultRolePermission' => 'Roles.DefaultRolePermission',
]);
$defaultPermissions = $this->DefaultRolePermission->find('all', array(
'recursive' => -1,
'fields' => array('DefaultRolePermission.*', 'DefaultRolePermission.value AS default'),
'conditions' => array(
'DefaultRolePermission.type' => 'location_role',
'DefaultRolePermission.permission' => 'location_reservable',
),
));
$defaultPermissions = Hash::remove($defaultPermissions, '{n}.DefaultRolePermission.id');
$defaults = Hash::combine(
$defaultPermissions, '{n}.DefaultRolePermission.role_key', '{n}.DefaultRolePermission'
);
foreach ($defaults as $roleKey => $default) {
$default['value'] = Hash::get($reservables, $roleKey, $default['value']);
$defaults[$roleKey] = $default;
}
return $defaults;
}
/**
* 予約できる権限の保存
*
* #### $data のサンプル
* $data => array(
* 'ReservationLocationReservable' => array(
* 'room_administrator' => array(
* 'role_key' => 'room_administrator',
* 'type' => 'location_role',
* 'permission' => 'location_reservable',
* 'value' => true,
* 'fixed' => true,
* 'default' => true,
* ),
* 'chief_editor' => array(
* 'role_key' => 'chief_editor',
* 'type' => 'location_role',
* 'permission' => 'location_reservable',
* 'value' => '1',
* 'fixed' => false,
* 'default' => true,
* 'id' => '',
* ),
* 'editor' => array(
* 'role_key' => 'editor',
* 'type' => 'location_role',
* 'permission' => 'location_reservable',
* 'value' => '1',
* 'fixed' => false,
* 'default' => true,
* 'id' => '',
* ),
* 'general_user' => array(
* 'role_key' => 'general_user',
* 'type' => 'location_role',
* 'permission' => 'location_reservable',
* 'value' => '0',
* 'fixed' => false,
* 'default' => false,
* 'id' => '',
* ),
* 'visitor' => array(
* 'role_key' => 'visitor',
* 'type' => 'location_role',
* 'permission' => 'location_reservable',
* 'value' => false,
* 'fixed' => true,
* 'default' => false,
* ),
* ),
* )
*
* @param string $locationKey location_key
* @param array $data save data
* @throws InternalErrorException
* @return bool
*/
public function saveReservable($locationKey, $data) {
$roles = $data[$this->alias];
// 同じ施設のreservableデータをあらかじめ削除しておく
if (! $this->deleteAll(['location_key' => $locationKey])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
foreach ($roles as $roleKey => $role) {
$value = $role['value'];
// ルーム毎に保存
if ($data['ReservationLocation']['use_all_rooms']) {
// 全てのルームから予約を受けつける
$this->create();
$reservableData = [
'location_key' => $locationKey,
'role_key' => $roleKey,
'room_id' => null,
'value' => $value
];
if (! $this->save($reservableData)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
};
} else {
// 個別のルームから予約を受け付ける
foreach ($data['ReservationLocationsRoom']['room_id'] as $roomId) {
$this->create();
$reservableData = [
'location_key' => $locationKey,
'role_key' => $roleKey,
'room_id' => $roomId,
'value' => $value
];
if (! $this->save($reservableData)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
}
return true;
}
}