-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationsController.php
More file actions
538 lines (490 loc) · 15.4 KB
/
ReservationsController.php
File metadata and controls
538 lines (490 loc) · 15.4 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
/**
* Reservations Controller
*
* @property PaginatorComponent $Paginator
*
* @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('NetCommonsTime', 'NetCommons.Utility');
App::uses('ReservationTime', 'Reservations.Utility');
App::uses('ReservationPermissiveRooms', 'Reservations.Utility');
/**
* ReservationsController
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Reservations\Controller
*/
class ReservationsController extends ReservationsAppController {
/**
* use models
*
* @var array
*/
public $uses = array(
'Reservations.ReservationRrule',
'Reservations.ReservationEvent',
'Reservations.ReservationFrameSetting',
'Reservations.Reservation',
'Reservations.ReservationEventShareUser',
'Reservations.ReservationActionPlan', //予定CRUDaction専用
'Reservations.ReservationLocation',
'Reservations.ReservationLocationsRoom',
'Reservations.ReservationLocationReservable',
'Reservations.ReservationTimeframe',
'Holidays.Holiday',
'Rooms.Room',
'NetCommons.BackTo',
);
/**
* use component
*
* @var array
*/
public $components = array(
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
//indexとviewは祖先基底クラスNetCommonsAppControllerで許可済なので、あえて書かない。
//予定のCRUDはReservationsPlancontrollerが担当。このcontrollerは表示系conroller.とする。
),
),
'Paginator',
'Categories.Categories',
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
//'Workflow.Workflow',
//'NetCommons.Date',
//'NetCommons.DisplayNumber',
//'NetCommons.Button',
'Reservations.ReservationMonthly',
'Reservations.ReservationTurnReservation',
'Reservations.ReservationLegend',
'Reservations.ReservationButton',
'Reservations.ReservationWeeklyTimeline',
'Reservations.ReservationUrl'
);
/**
* beforeRender
*
* @return void
*/
//public function beforeFilter() {
// parent::beforeFilter();
//
// // 以前はここでCurrentのブロックIDをチェックする処理があったが
// // 施設予約はCurrentのブロックID(=現在表示中ページのブロックID)は
// // 表示データ上の意味がないのでチェックは行わない
// // 表示ブロックIDがないときは、パブリックTOPページで仮表示されることに話が決まった
//
// //if (! $locations) {
// // $this->setAction('emptyRender');
// //}
//}
/**
* index
*
* @return void
*/
public function index() {
$vars = array();
$this->setReservationCommonCurrent($vars);
$this->ReservationEvent->initSetting($this->Workflow);
$style = $this->getQueryParam('style');
if (! $style) {
//style未指定の場合、ReservationFrameSettingモデルのdisplay_type情報から表示するctpを決める。
$displayType = (int)Current::read('ReservationFrameSetting.display_type');
$style = $this->__getStyleByDisplayType($displayType);
$this->__setDefaultCategoryWhenDispTypeIsCategory($displayType);
}
$categoryId = Hash::get($this->request->params['named'], 'category_id');
$locations = $this->ReservationLocation->getLocations($categoryId);
if (empty($locations) && $categoryId === null) {
//施設が1つも登録されてない
$this->view = 'location_not_found';
return;
}
if (empty($locations) && in_array($style,
[ReservationsComponent::RESERVATION_STYLE_LACATION_WEEKLY,
ReservationsComponent::RESERVATION_STYLE_LACATION_MONTHLY
])) {
$this->NetCommons->setFlashNotification(
__d('reservations', 'Location not found in select category.'),
array(
'class' => 'info',
)
);
$url = array(
'?' => array(
'frame_id' => Current::read('Frame.id'),
'style' => $this->request->query('style'),
'year' => $this->request->query('year'),
'month' => $this->request->query('month'),
'day' => $this->request->query('day'),
)
);
$this->redirect($url);
}
$this->set('locations', $locations);
$this->_storeRedirectPath($vars);
$roomPermRoles = $this->ReservationEvent->prepareCalRoleAndPerm();
ReservationPermissiveRooms::setRoomPermRoles($roomPermRoles);
$ctpName = $this->_getCtpAndVars($style, $vars);
$frameId = Current::read('Frame.id');
$languageId = Current::read('Language.id');
$this->set(compact('frameId', 'languageId', 'vars'));
$this->render($ctpName);
//$roomId = Current::read('Room.id');
//$userId = Current::read('User.id');
}
/**
* 時間枠セット
*
* @return void
*/
protected function _setTimeframe() {
if (Current::read('ReservationFrameSetting.display_timeframe')) {
$timeframes = $this->ReservationTimeframe->find('all', [
'conditions' => [
'ReservationTimeframe.language_id' => Current::read('Language.id')
]
]);
$this->set('timeframes', $timeframes);
}
}
/**
* カテゴリ別表がデフォルトのとき、初期表示では表示設定で設定されたカテゴリにする。
*
* @return void
*/
protected function _setDefaultCategory() {
$categoryId = Current::read('ReservationFrameSetting.category_id');
//$categoryId = Hash::get($this->params['named'], 'category_id', $categoryId);
$this->request->param('named.category_id', $categoryId);
}
/**
* _getMonthlyVars
*
* 月施設予約用変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars 月(縮小用)データ
*/
protected function _getMonthlyVars($vars) {
$this->setReservationCommonVars($vars);
$vars['selectRooms'] = array(); //マージ前の暫定
return $vars;
}
/**
* _getWeeklyVars
*
* 週単位変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars 週単位データ
*/
protected function _getWeeklyVars($vars) {
$this->setReservationCommonVars($vars);
$vars['selectRooms'] = array(); //マージ前の暫定
$vars['week'] = $this->getQueryParam('week');
return $vars;
}
/**
* getDailyListVars
*
* 日単位(一覧)用変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars 日単位(一覧)データ
*/
public function getDailyListVars($vars) {
$this->setReservationCommonVars($vars);
$vars['tab'] = 'list';
return $vars;
}
/**
* 日単位(タイムライン)用変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars 日単位(タイムライン)データ
*/
protected function _getDailyTimelineVars($vars) {
$this->setReservationCommonVars($vars);
$vars['tab'] = 'timeline';
return $vars;
}
/**
* 日次施設予約変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars 日次施設予約変数
*/
protected function _getDailyVars($vars) {
//$tab = $this->getQueryParam('tab');
//if ($tab === 'timeline') {
$vars = $this->_getDailyTimelineVars($vars);
//} else {
// $vars = $this->getDailyListVars($vars);
//}
$vars['selectRooms'] = array(); //マージ前の暫定
return $vars;
}
/**
* getScheduleVars
*
* スケジュール変数取得
*
* @param array $vars カレンンダー情報
* @return array $vars スケジュール変数
*/
//public function getScheduleVars($vars) {
// //$sort = $this->getQueryParam('sort');
// // スケジュール表示のときだけは直接覗くようにする(正式取得しない)
// // 理由1:スケジュール表示は左カラムから表示されない
// // 理由2:スケジュール表示の種別指定パラメータをデフォルト表示のときもqueryに入れている
// // 理由3:デフォ表示のときrequestedパラメータがないから、まるでよそ様フレーム処理に見える
// // 上記理由から直接見ないと処理できないし、直接見てもよそ様フレームと混同しないから
// $sort = $this->request->query['sort'];
// if ($sort === 'member') {
// $vars = $this->getMemberScheduleVars($vars);
// } else {
// $vars = $this->getTimeScheduleVars($vars);
// }
// return $vars;
//}
/**
* _getCtpAndVars
*
* ctpおよびvars取得
*
* @param string $style 表示タイプ
* @param array &$vars 施設予約共通変数
* @return string ctpNameを格納したstring
*/
protected function _getCtpAndVars($style, &$vars) {
$vars['style'] = $style;
// プライベートルームか?
Current::write('Reservations.accessPrivateRoom',
(Current::read('Room.space_id') == Space::PRIVATE_SPACE_ID));
if (in_array($vars['style'], ReservationsComponent::$stylesByLocation, true)) {
$locationKey = $this->request->query('location_key');
if ($locationKey) {
$vars['location_key'] = $locationKey;
} else {
$vars['location_key'] = Current::read(
'ReservationFrameSetting.location_key',
Hash::get($this->viewVars['locations'], '0.ReservationLocation.key')
);
}
// カテゴリ絞り込みの結果、指定した施設がなかったら1番目の施設を選択する。
$locationList = Hash::combine($this->viewVars['locations'],
'{n}.ReservationLocation.key',
'{n}.ReservationLocation.location_name'
//'{n}.ReservationLocation.category_id'
);
if (!isset($locationList[$vars['location_key']])) {
$vars['location_key'] = Hash::get($this->viewVars['locations'], '0.ReservationLocation.key');
}
// ReservationLocation, ReservationReservableを設定する。
$this->_setupCurrentValues4ByLocation($vars['location_key']);
} else {
// ReservationLocation, ReservationReservableを設定する。
$this->_setupCurrentValues4ByCategory();
}
switch ($style) {
case ReservationsComponent::RESERVATION_STYLE_CATEGORY_WEEKLY:
//カテゴリー別 - 週表示
$vars = $this->_getWeeklyVars($vars);
break;
case ReservationsComponent::RESERVATION_STYLE_CATEGORY_DAILY:
//カテゴリー別 - 日表示
$vars = $this->_getDailyVars($vars);
$this->_setTimeframe();
break;
case ReservationsComponent::RESERVATION_STYLE_LACATION_MONTHLY:
//施設別 - 月表示
$vars = $this->_getMonthlyVars($vars);
break;
case ReservationsComponent::RESERVATION_STYLE_LACATION_WEEKLY:
//施設別 - 週表示
$vars = $this->_getWeeklyVars($vars);
$this->_setTimeframe();
break;
}
//if (in_array($vars['style'], ReservationsComponent::$reservationStylesByLocation, true)) {
// $vars['location_key'] = Hash::get($this->viewVars['locations'], '0.ReservationLocation.key');
//}
$ctpName = $vars['style'];
return $ctpName;
}
/**
* Current にReservationLocation(施設別表示での施設)ReservationReservable(予約可能な施設があるか)を設定する
*
* @param string|null $locationKey 施設別ならReservationLocation.key カテゴリ別ならnull
* @return void
*/
protected function _setupCurrentValues4ByLocation($locationKey) {
// 施設別表時
$currentLocation = $this->ReservationLocation->find(
'first',
[
'conditions' => [
'ReservationLocation.key' => $locationKey,
'ReservationLocation.language_id' => Current::read('Language.id')
]
]
);
Current::write('ReservationLocation', $currentLocation['ReservationLocation']);
if (Current::read('Reservations.accessPrivateRoom')) {
// プライベートルーム
// 個人的な予約OKなら予約可能とする
Current::write(
'ReservationReservable',
$currentLocation['ReservationLocation']['use_private']
);
} else {
//施設への予約権限をセット
Current::write(
'ReservationReservable',
$this->ReservationLocationReservable->isReservableByLocation($currentLocation)
);
}
}
/**
* Current にReservationLocation(施設別表示での施設)ReservationReservable(予約可能な施設があるか)を設定する
*
* @return void
*/
protected function _setupCurrentValues4ByCategory() {
// カテゴリ別表時
// 施設が指定されてないなら、いずれかの施設で予約できれば予約権限ありと判定
$reservable = false;
// あらかじめ全施設の権限をロードしておく
$this->ReservationLocationReservable->loadAll(
$this->ReservationLocationReservable->getReadableRoomIds()
);
foreach ($this->viewVars['locations'] as $location) {
if (Current::read('Reservations.accessPrivateRoom')) {
// プライベートルーム
if ($location['ReservationLocation']['use_private']) {
$reservable = true;
}
} else {
if ($this->ReservationLocationReservable->isReservableByLocation($location)) {
$reservable = true;
}
}
}
Current::write('ReservationReservable', $reservable);
}
/**
* __setDefaultCategoryWhenDispTypeIsCategory
*
* @param int|string $displayType display type
* @return void
*/
private function __setDefaultCategoryWhenDispTypeIsCategory($displayType) {
if (in_array($displayType, [
ReservationsComponent::RESERVATION_DISP_TYPE_CATEGORY_WEEKLY,
ReservationsComponent::RESERVATION_DISP_TYPE_CATEGORY_DAILY
])) {
$this->_setDefaultCategory();
}
}
/**
* __getStyleByDisplayType
*
* @param int|string $displayType display type
* @return string
*/
private function __getStyleByDisplayType($displayType) {
switch ($displayType) {
case ReservationsComponent::RESERVATION_DISP_TYPE_CATEGORY_WEEKLY:
$style = ReservationsComponent::RESERVATION_STYLE_CATEGORY_WEEKLY;
break;
case ReservationsComponent::RESERVATION_DISP_TYPE_CATEGORY_DAILY:
$style = ReservationsComponent::RESERVATION_STYLE_CATEGORY_DAILY;
break;
case ReservationsComponent::RESERVATION_DISP_TYPE_LACATION_MONTHLY:
$style = ReservationsComponent::RESERVATION_STYLE_LACATION_MONTHLY;
break;
case ReservationsComponent::RESERVATION_DISP_TYPE_LACATION_WEEKLY:
$style = ReservationsComponent::RESERVATION_STYLE_LACATION_WEEKLY;
break;
default:
$style = ReservationsComponent::RESERVATION_STYLE_DEFAULT;
}
return $style;
}
/**
* ajaxで施設の公開先ルームを取得する
*
* @return void
*/
public function fetch_rooms_to_publish_reservation() {
// get request params
$locationKey = $this->request->query('location_key');
// guard
if (!$this->__validateLocationKey($locationKey)) {
$this->throwBadRequest();
return;
}
// 公開先ルーム取得
$rooms = $this->__findRoomsToPublishReservation($locationKey);
// jsonで返す
$this->__jsonResponse($rooms);
}
/**
* __validateLocationKey
*
* @param mixed $locationKey 施設キー
* @return bool
*/
private function __validateLocationKey($locationKey) : bool {
if (!is_string($locationKey)) {
return false;
}
return true;
}
/**
* __findRoomsToPublishReservation
*
* @param string $locationKey 施設キー
* @return array
*/
private function __findRoomsToPublishReservation($locationKey) {
$userId = Current::read('User.id');
return $this->ReservationLocationsRoom->getReservableRoomsByLocationKey($locationKey, $userId);
}
/**
* __jsonResponse
*
* @param array $rooms Room data
* @return void
*/
private function __jsonResponse(array $rooms) {
$result = [];
$notSpecified = [
'roomId' => 0,
'name' => __d('reservations', '-- not specified --')
];
$result[] = $notSpecified;
foreach ($rooms as $room) {
$result[] = [
'room_id' => $room['Room']['id'],
'name' => $room['RoomsLanguage'][0]['name']
];
}
$this->NetCommons->renderJson(['rooms' => $result]);
}
}