-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationLocationsController.php
More file actions
396 lines (345 loc) · 10.7 KB
/
ReservationLocationsController.php
File metadata and controls
396 lines (345 loc) · 10.7 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
<?php
/**
* 施設設定 Controller
*
* @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('ReservationsAppController', 'Reservations.Controller');
App::uses('ReservationLocationReservable', 'Reservations.Model');
/**
* 施設設定 Controller
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Reservations\Controller
*/
class ReservationLocationsController extends ReservationsAppController {
/**
* @var int 施設編集画面で表示する最大ルーム数
*/
const MAX_ROOMS = 300;
/**
* layout
*
* @var array
*/
public $layout = 'NetCommons.setting';
/**
* @var array use models
*/
public $uses = array(
'Reservations.ReservationLocation',
'Reservations.ReservationLocationChangeWeight',
'Reservations.ReservationLocationsRoom',
'Categories.Category',
'Roles.Role',
'Rooms.RoomRole',
'Reservations.ReservationLocationReservable',
'Reservations.ReservationLocationsApprovalUser',
'Users.User',
);
/**
* Components
*
* @var array
*/
public $components = array(
'Categories.Categories',
'NetCommons.NetCommonsTime',
'Paginator',
'Rooms.RoomsForm',
'Reservations.ReservationSettings', //NetCommons.Permissionは使わず、独自でやる
);
/**
* @var array helpers
*/
public $helpers = array(
'Blocks.BlockTabs', // 設定内容はReservationSettingsComponentにまとめた
'Rooms.RoomsForm',
'Reservations.ReservationLocation',
'Groups.GroupUserList',
'Users.UserSearch'
);
/**
* index
*
* @return void
*/
public function index() {
//条件
$conditions = array(
'ReservationLocation.language_id' => Current::read('Language.id'),
);
if (isset($this->params['named']['category_id'])) {
$conditions['ReservationLocation.category_id'] = $this->params['named']['category_id'];
}
$query = [
'conditions' => $conditions,
'recursive' => 0,
'order' => 'ReservationLocation.weight ASC'
];
$this->Paginator->settings = [
'ReservationLocation' => $query
];
$reservationLocations = $this->Paginator->paginate('ReservationLocation');
// openTextの追加
$openText = new ReservationLocationOpenText();
$reservationLocations = $openText->openTextAdd($reservationLocations);
$this->set('reservationLocations', $reservationLocations);
}
/**
* add method
*
* @return void
*/
public function add() {
$this->set('isEdit', false);
$this->_processPermission();
// 施設管理者保持
if ($this->request->is('post')) {
$result = $this->ReservationLocation->saveLocation($this->request->data);
if ($result) {
$url = NetCommonsUrl::actionUrl(
array(
'controller' => 'reservation_locations',
'action' => 'index',
'frame_id' => Current::read('Frame.id'),
)
);
return $this->redirect($url);
}
$this->NetCommons->handleValidationError($this->ReservationLocation->validationErrors);
//未選択の場合、文字列の空値が入ってくる
if (! is_array($this->request->data['ReservationLocationsRoom']['room_id'])) {
$this->request->data['ReservationLocationsRoom']['room_id'] = [];
}
$isMyUser = false;
} else {
$newLocation = $this->ReservationLocation->createLocation();
$this->request->data['ReservationLocation'] = $newLocation['ReservationLocation'];
$isMyUser = true;
}
//施設管理者のセット
$this->_setSelectUsers($isMyUser);
// プライベートルームは除外する
$roomConditions = [
//'Room.space_id !=' => Space::PRIVATE_SPACE_ID,
];
$this->RoomsForm->setRoomsForCheckbox(
$roomConditions,
['limit' => self::MAX_ROOMS, 'maxLimit' => self::MAX_ROOMS]
);
$this->view = 'form';
}
/**
* 予約できる権限の処理
*
* @param null $key locationKey
* @return void
*/
protected function _processPermission($key = null) {
$reservables = $this->ReservationLocationReservable->getPermissions($key);
$this->request->data['ReservationLocationReservable'] = Hash::merge(
$reservables,
Hash::get($this->request->data, 'ReservationLocationReservable', array())
);
//Role取得
$roles = $this->Role->find('all', array(
'recursive' => -1,
'conditions' => array(
'Role.type' => Role::ROLE_TYPE_ROOM,
'Role.language_id' => Current::read('Language.id'),
),
));
$roles = Hash::combine($roles, '{n}.Role.key', '{n}.Role');
//RoomRole取得
$roomRoles = $this->RoomRole->find('all', array(
'recursive' => -1,
));
$roomRoles = Hash::combine($roomRoles, '{n}.RoomRole.role_key', '{n}.RoomRole');
$roomRoles = Hash::remove($roomRoles, '{n}.RoomRole.id');
$this->set('roles', Hash::merge($roomRoles, $roles));
}
/**
* 担当者ユーザを設定
*
* @param bool $isMyUser 作成者ユーザー取得フラグ
* @return void
*/
protected function _setSelectUsers($isMyUser) {
if ($isMyUser) {
$this->request->data['ReservationLocationsApprovalUser'][] = array(
'user_id' => Current::read('User.id')
);
}
$this->request->data['selectUsers'] = array();
if (isset($this->request->data['ReservationLocationsApprovalUser'])) {
$selectUsers = Hash::extract(
$this->request->data['ReservationLocationsApprovalUser'],
'{n}.user_id'
);
foreach ($selectUsers as $userId) {
$user = $this->User->getUser($userId);
$this->request->data['selectUsers'][] = $user;
}
}
}
/**
* edit method
*
* @return void
*/
public function edit() {
$this->set('isEdit', true);
$key = $this->params['key'];
$this->_processPermission($key);
//施設データの取得
$reservationLocation = $this->ReservationLocation->find('first', [
'recursive' => 0,
'conditions' => [
'ReservationLocation.key' => $key,
'ReservationLocation.language_id' => Current::read('Language.id')
]
]);
if (empty($reservationLocation)) {
return $this->throwBadRequest();
}
$timeTable = explode('|', $reservationLocation['ReservationLocation']['time_table']);
$reservationLocation['ReservationLocation']['time_table'] = $timeTable;
if ($this->request->is(array('post', 'put'))) {
// リクエストデータから施設管理者保持
$this->_setSelectUsers(false);
$data = $this->request->data;
if ($this->ReservationLocation->saveLocation($data)) {
$url = NetCommonsUrl::actionUrl(
array(
'controller' => 'reservation_locations',
'action' => 'index',
'frame_id' => Current::read('Frame.id'),
)
);
return $this->redirect($url);
}
$this->NetCommons->handleValidationError($this->ReservationLocation->validationErrors);
//未選択の場合、文字列の空値が入ってくる
if (! is_array($this->request->data['ReservationLocationsRoom']['room_id'])) {
$this->request->data['ReservationLocationsRoom']['room_id'] = [];
}
} else {
// start_time, end_timeを施設のタイムゾーンに変換してH:i形式へ
$locationTimeZone = new DateTimeZone($reservationLocation['ReservationLocation']['timezone']);
$startDate = new DateTime(
$reservationLocation['ReservationLocation']['start_time'], new DateTimeZone('UTC')
);
$startDate->setTimezone($locationTimeZone);
$reservationLocation['ReservationLocation']['start_time'] = $startDate->format('H:i');
$endDate = new DateTime(
$reservationLocation['ReservationLocation']['end_time'], new DateTimeZone('UTC')
);
$endDate->setTimezone($locationTimeZone);
$reservationLocation['ReservationLocation']['end_time'] = $endDate->format('H:i');
$this->request->data['ReservationLocation'] = $reservationLocation['ReservationLocation'];
//施設管理者のデータセット
$approvalUsers = $this->ReservationLocationsApprovalUser->find('all', [
'recursive' => 0,
'conditions' => [
'location_key' => $key
]
]);
foreach ($approvalUsers as $approvalUser) {
$this->request->data['selectUsers'][] = ['User' => $approvalUser['User']];
}
//予約を受け付けるルームを取得
$result = $this->ReservationLocationsRoom->find('list', array(
'recursive' => -1,
'fields' => array('id', 'room_id'),
'conditions' => ['reservation_location_key' =>
$this->request->data['ReservationLocation']['key']],
));
$roomIds = array_unique(array_values($result));
$this->request->data['ReservationLocationsRoom']['room_id'] = $roomIds;
}
$this->set('reservationLocation', $reservationLocation);
$this->set('isDeletable', true);
// プライベートルームは除外する
$roomConditions = [
//'Room.space_id !=' => Space::PRIVATE_SPACE_ID,
];
$this->RoomsForm->setRoomsForCheckbox(
$roomConditions,
['limit' => self::MAX_ROOMS, 'maxLimit' => self::MAX_ROOMS]
);
$this->view = 'form';
}
/**
* delete method
*
* @throws InternalErrorException
* @return void
*/
public function delete() {
if (! $this->request->is('delete')) {
return $this->throwBadRequest();
}
$key = $this->request->data['ReservationLocation']['key'];
if (! $this->ReservationLocation->deleteLocation($key)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$url = NetCommonsUrl::actionUrl(
array(
'controller' => 'reservation_locations',
'action' => 'index',
'frame_id' => Current::read('Frame.id'),
)
);
return $this->redirect($url);
}
/**
* 施設並び替え
*
* @return void
*/
public function sort() {
if ($this->request->is('post')) {
if ($this->ReservationLocation->saveWeights($this->data)) {
$url = NetCommonsUrl::actionUrl(
array(
'controller' => 'reservation_locations',
'action' => 'index',
'frame_id' => Current::read('Frame.id'),
)
);
return $this->redirect($url);
}
$this->NetCommons->handleValidationError($this->ReservationLocation->validationErrors);
} else {
//条件
$conditions = array(
'ReservationLocation.language_id' => Current::read('Language.id'),
);
if (isset($this->params['named']['category_id'])) {
$conditions['ReservationLocation.category_id'] = $this->params['named']['category_id'];
}
$query = [
'conditions' => $conditions,
'recursive' => 0,
'order' => 'ReservationLocation.weight ASC',
'limit' => PHP_INT_MAX,
'maxLimit' => PHP_INT_MAX
];
$this->Paginator->settings = [
'ReservationLocation' => $query
];
$reservationLocations = $this->Paginator->paginate('ReservationLocation');
// openTextの追加
$openText = new ReservationLocationOpenText();
$reservationLocations = $openText->openTextAdd($reservationLocations);
$this->set('reservationLocations', $reservationLocations);
$this->request->data['ReservationLocations'] = $reservationLocations;
$this->request->data['Frame'] = Current::read('Frame');
}
}
}