-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationImportController.php
More file actions
473 lines (419 loc) · 15.5 KB
/
ReservationImportController.php
File metadata and controls
473 lines (419 loc) · 15.5 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
<?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('CsvFileReader', 'Files.Utility');
/**
* 予約のインポート Controller
*
* @property ReservationSelectRoomComponent $ReservationSelectRoom
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Reservations\Controller
*/
class ReservationImportController extends ReservationsAppController {
/**
* reservation information
*
* @var array
*/
protected $_vars = array();
/**
* layout
*
* @var array
*/
public $layout = 'NetCommons.setting'; //PageLayoutHelperのafterRender()の中で利用。
//
//$layoutに'NetCommons.setting'があると
//「Frame設定も含めたコンテンツElement」として
//ng-controller='FrameSettingsController'属性
//ng-init=initialize(Frame情報)属性が付与される。
//
//'NetCommons.setting'がないと、普通の
//「コンテンツElement」として扱われる。
//
//ちなみに、使用されるLayoutは、Pages.default
//
/**
* @var array use models
*/
public $uses = array(
'Reservations.ReservationLocation',
'Reservations.ReservationLocationsRoom',
'Categories.Category',
'Reservations.ReservationImport',
'Reservations.ReservationActionPlan',
'Reservations.ReservationCsvRecord',
'Reservations.ReservationEvent',
'Reservations.ReservationLocationsRoom',
//'Workflow.WorkflowComment',
);
/**
* Components
*
* @var array
*/
public $components = array(
'Categories.Categories',
//'Blogs.ReservationLocationPermission',
'NetCommons.NetCommonsTime',
'Paginator',
'Rooms.RoomsForm',
'Reservations.ReservationSettings', //NetCommons.Permissionは使わず、独自でやる
'Reservations.ReservationWorks',
'Reservations.ReservationSelectRoom',
);
/**
* @var array helpers
*/
public $helpers = array(
'NetCommons.BackTo',
'NetCommons.NetCommonsForm',
'Workflow.Workflow',
'NetCommons.NetCommonsTime',
'NetCommons.TitleIcon',
//'Blocks.BlockForm',
'Blocks.BlockTabs', // 設定内容はReservationSettingsComponentにまとめた
'Rooms.RoomsForm',
'Reservations.ReservationLocation',
'Reservations.ReservationWorkflow',
'Reservations.ReservationPlan',
'NetCommons.Date',
'NetCommons.DisplayNumber',
'NetCommons.Button',
'Reservations.ReservationUrl',
'Reservations.ReservationCommon',
'Reservations.ReservationMonthly',
'Reservations.ReservationCategory',
'Reservations.ReservationShareUsers',
'Reservations.ReservationEditDatetime',
//'Reservations.ReservationExposeTarget',
'Reservations.ReservationPlanRrule',
'Reservations.ReservationPlanEditRepeatOption',
'Groups.GroupUserList',
'Users.UserSearch',
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
}
/**
* edit method
*
* @return void
*/
public function edit() {
// 施設情報
$locations = $this->ReservationLocation->getReservableLocations();
// openTextの追加
$openText = new ReservationLocationOpenText();
$locations = $openText->openTextAdd($locations);
$this->set('locations', $locations);
$frameId = Current::read('Frame.id');
if (! $frameId) {
$this->setAction('can_not_edit');
return;
}
$this->set('isEdit', true);
if ($this->request->is(array('post', 'put'))) {
$this->_reservationPost();
} else {
$this->request->data['ReservationActionPlan']['timezone'] = 'Asia/Tokyo';
}
$this->_reservationGet(ReservationsComponent::PLAN_ADD);
$userId = Current::read('User.id');
$defaultLocationKey = $this->request->data['ReservationActionPlan']['location_key'] ?? null;
$defaultRooms = $this->ReservationSelectRoom->getDefaultPublishableRooms(
$locations,
$defaultLocationKey,
$userId
);
$this->set('defaultPublishableRooms', json_encode($defaultRooms));
// 選択済みルーム
$selectedRoomId = $this->request->data['ReservationActionPlan']['plan_room_id'] ?? 0;
$selectedRoom = $this->ReservationSelectRoom->getSelectedRoom(
$defaultRooms,
$selectedRoomId
);
$this->set('selectedRoom', json_encode($selectedRoom));
}
/**
* _reservationPost
*
* @return void
* @SuppressWarnings(PHPMD)
*/
protected function _reservationPost() {
//CakeLog::debug("DBG: request_data[" . print_r($this->request->data, true) . "]");
//CalenarActionPlanモデルの繰返し回数超過フラグをoffにしておく。
$this->ReservationActionPlan->isOverMaxRruleIndex = false;
//Xdebugがインストールされている環境だと、xdebug.max_nesting_levelの値(100とか200とか256とか)
//の制限を受けてしまうので、再帰callを多用する施設予約登録では一時的に閾値を引き上げておく。
$xdebugMaxNestingLvl = ini_get('xdebug.max_nesting_level');
if ($xdebugMaxNestingLvl) {
//Xdebugが入っている環境
$xdebugMaxNestingLvl = ini_set('xdebug.max_nesting_level',
ReservationsComponent::CALENDAR_XDEBUG_MAX_NESTING_LEVEL);
}
// ステータスは…
$locationKey = $this->request->data['ReservationActionPlan']['location_key'];
$location = $this->ReservationLocation->getByKey($locationKey);
$userId = Current::read('User.id');
// 1. 選択した施設が承認不要 → 公開
if ($location['ReservationLocation']['use_workflow']) {
if (in_array($userId, $location['approvalUserIds'])) {
// 2. 承認者 → 公開
$status = WorkflowComponent::STATUS_PUBLISHED;
} else {
// 3. それ以外 公開申請
$status = WorkflowComponent::STATUS_APPROVAL_WAITING;
}
} else {
//選択した施設が承認不要 → 公開
$status = WorkflowComponent::STATUS_PUBLISHED;
}
//$status = $this->ReservationActionPlan->getStatus($this->request->data);
$this->request->data['ReservationActionPlan']['status'] = $status;
$this->ReservationActionPlan->set($this->request->data);
//公開対象のルームが、ログイン者(編集者・承認者)のプライベートルームかどうかを判断しておく。
$isMyPrivateRoom =
($this->request->data['ReservationActionPlan']['plan_room_id'] == $this->_myself);
if (! $isMyPrivateRoom) {
//CakeLog::debug("DBG: 予定のルームが、ログインの者のプライベートルーム以外の時");
if (isset($this->request->data['GroupsUser'])) {
//CakeLog::debug("DBG: 予定を共有する人情報は存在してはならないので、stripする。");
unset($this->request->data['GroupsUser']);
}
}
// CSVファイルとフォーム項目のバリデーション
$this->ReservationImport->set($this->request->data);
if (!$this->ReservationImport->validates()) {
$this->ReservationActionPlan->validationErrors = Hash::merge(
$this->ReservationActionPlan->validationErrors,
$this->ReservationImport->validationErrors
);
$this->NetCommons->handleValidationError($this->ReservationActionPlan->validationErrors);
return;
}
$path = $this->request->data['ReservationActionPlan']['csv_file']['tmp_name'];
$csvFile = new CsvFileReader($path);
// ここから登録開始
$this->ReservationActionPlan->begin();
if ($this->request->data['ReservationActionPlan']['delete_room_events']) {
// 公開先の全予約を削除してからインポート
$roomId = $this->request->data['ReservationActionPlan']['plan_room_id'];
if (!$this->ReservationEvent->deleteEventByRoomId($roomId)) {
$this->ReservationActionPlan->validationErrors['delete_room_events'] =
__d('reservations', 'Delete failed.');
$this->NetCommons->handleValidationError(
$this->ReservationActionPlan->validationErrors,
__d('reservations', 'Delete failed.')
);
return;
}
}
$errors = [];
$result = true;
foreach ($csvFile as $index => $item) {
if ($index == 0) {
// 1行目は読み飛ばす
continue;
}
// CSVデータのバリデーション "件名","利用時間の制限なし","予約日","開始時間","終了時間","連絡先","詳細"の順
$csvRecord = $this->ReservationCsvRecord->getCsvRecordByRow($item, $location);
$this->ReservationCsvRecord->create();
$this->ReservationCsvRecord->set($csvRecord);
if (!$this->ReservationCsvRecord->validates()) {
// csvデータのバリデーションエラー
foreach ($this->ReservationCsvRecord->validationErrors as $error) {
$errorMessage = implode('', $error);
//$errors[] = __d('reservations', '%d行目:%s', $index, $errorMessage);
//$this->ReservationActionPlan->validationErrors['csv_file'][] =
// __d('reservations', '%d行目:%s', $index, $errorMessage);
$errors['csv_file'][] =
__d('reservations', 'Line %d : %s', $index, $errorMessage);
}
$result = false;
continue; // 次の行へ
}
$this->ReservationActionPlan->create();
$this->request->data['ReservationActionPlan']['timezone'] =
Current::read('User.timezone');
$this->request->data['ReservationActionPlan']['enable_time'] = 1;
$this->request->data['ReservationActionPlan']['is_detail'] = 1;
$this->request->data['ReservationActionPlan']['is_repeat'] = 0;
$this->request->data['ReservationActionPlan']['WorkflowComment'] = '';
$this->request->data['ReservationActionPlan']['title_icon'] = '';
$this->request->data['ReservationActionPlan']['location'] = '';
$this->request->data['ReservationActionPlan']['enable_email'] = 0;
$this->request->data['ReservationActionPlan']['email_send_timing'] = 5;
$this->request->data['ReservationActionPlan'] = Hash::merge(
$this->request->data['ReservationActionPlan'],
$this->ReservationCsvRecord->convertActionPlanData($csvRecord)
);
if ($this->request->data['ReservationActionPlan']['skip_duplicate_events']) {
// 同じ予約を無視する設定なら→同じ予約があるか探す
// 同じタイトル、日時の予約があったら、このCSVデータは保存しないでスキップ
if ($this->_existDuplicateEvent($this->request->data)) {
//$errorMessage = __d('reservations', '件名と予約日時が一致する予約がありました')
//$errors['csv_file'][] =
// __d('reservations', '%d行目:%s', $index, $errorMessage);
// 特にメッセージいらないかな?
continue;
}
}
$this->ReservationActionPlan->set($this->request->data);
//校正用配列の準備
$this->ReservationActionPlan->reservationProofreadValidationErrors = array();
if (! $this->ReservationActionPlan->validates()) {
$error = $this->ReservationActionPlan->validationErrors;
foreach ($error as $field => $err) {
if (in_array($field, ['location_key', 'room_id'])) {
$errors[$field] = $err;
} else {
// location_key, room_id以外のバリデーションエラーはCSVレコード単位のエラー
$errorMessage = implode('', $err);
$errors['csv_file'][] =
__d('reservations', 'Line %d : %s', $index, $errorMessage);
}
}
$result = false;
//$this->NetCommons->handleValidationError($this->ReservationActionPlan->validationErrors);
continue;
}
$eventId = $this->ReservationActionPlan->saveImportRecord(
$this->request->data,
$this->_myself);
if (!$eventId) {
$errors['csv_file'][] =
__d('reservations', 'Line %d : %s', $index,
__d('reservations', 'Registration failed.'));
$result = false;
}
}
if (!$result) {
$this->ReservationActionPlan->rollback();
$this->ReservationActionPlan->validationErrors = $errors;
$this->NetCommons->handleValidationError(
$this->ReservationActionPlan->validationErrors
);
return;
}
$this->ReservationActionPlan->commit();
$url = NetCommonsUrl::backToPageUrl();
$this->redirect($url);
}
/**
* 件名、予約日時、施設が同じ予約が存在するか?
* CSVインポートで「件名と予約日時が一致するデータは無視する」のときに利用。
*
* @param array $data ReservationActionPlan
* @return bool
*/
protected function _existDuplicateEvent($data) {
$ncTime = new NetCommonsTime();
$start = $ncTime->toServerDatetime($data['ReservationActionPlan']['detail_start_datetime']);
$start = date('YmdHis', strtotime($start));
$end = $ncTime->toServerDatetime($data['ReservationActionPlan']['detail_end_datetime']);
$end = date('YmdHis', strtotime($end));
$conditions = [
'ReservationEvent.title' => $data['ReservationActionPlan']['title'],
'ReservationEvent.dtstart' => $start,
'ReservationEvent.dtend' => $end,
'ReservationEvent.location_key' => $data['ReservationActionPlan']['location_key'],
];
$conditions = $this->ReservationEvent->getWorkflowConditions($conditions);
$count = $this->ReservationEvent->find('count', ['conditions' => $conditions]);
return ($count > 0);
}
/**
* _reservationGet
*
* @param string $planViewMode アクション
* @return void
*/
protected function _reservationGet($planViewMode) {
//eventが空の場合、初期値でFILLした表示用配列を取得する。
list(
$year, $month, $day, $hour, $minute, $second, $enableTime
) =
$this->ReservationWorks->getDateTimeParam($this->request->query);
$capForView = (new ReservationSupport())->getInitialReservationActionPlanForView(
$year,
$month,
$day,
$hour,
$minute,
$second,
$enableTime,
$this->_exposeRoomOptions
);
//0件を意味する空配列を入れておく。
$eventSiblings = array();
//eventが空なので、1stSibも初期値でFILLしておく
$capForViewOf1stSib = $capForView;
$firstSibEventId = 0; //新規だからidは未設定をあらわす0
$firstSibEventKey = '';
$startDatetime = $capForViewOf1stSib['ReservationActionPlan']['detail_start_datetime'];
$year1stSib = substr($startDatetime, 0, 4);
$month1stSib = substr($startDatetime, 5, 2);
$day1stSib = substr($startDatetime, 8, 2);
$firstSib = array(
'ReservationActionPlan' => array(
'first_sib_event_id' => $firstSibEventId,
'first_sib_event_key' => $firstSibEventKey,
'first_sib_year' => intval($year1stSib),
'first_sib_month' => intval($month1stSib),
'first_sib_day' => intval($day1stSib),
),
);
//capForViewのrequest->data反映
$this->request->data = $this->ReservationWorks->setCapForView2RequestData(
$capForView,
$this->request->data
);
$mailSettingInfo = $this->getMailSettingInfo();
//reuqest->data['GroupUser']にある各共有ユーザの情報取得しセット
$shareUsers = array();
foreach ($this->request->data['GroupsUser'] as $user) {
$shareUsers[] = $this->User->getUser($user['user_id'], Current::read('Language.id'));
}
//キャンセル時のURLセット
//testセッション方式
//$url = $this->__getSessionStoredRedirectUrl();
//$url = []; //
//$this->_vars['returnUrl'] = $url;
$this->set(
compact(
'capForView',
'mailSettingInfo',
'shareUsers',
'eventSiblings',
'planViewMode',
'firstSib'
)
);
$this->set('vars', $this->_vars);
$this->set('event', $this->eventData);
$this->set('frameSetting', $this->_frameSetting);
$this->set('exposeRoomOptions', $this->_exposeRoomOptions);
$this->set('myself', $this->_myself);
$this->set('emailOptions', $this->_emailOptions);
$this->set('frameId', Current::read('Frame.id', 0));
$this->set('languageId', Current::read('Language.id'));
//$this->request->data['ReservationFrameSettingSelectRoom'] =
// $this->ReservationFrameSetting->getSelectRooms($this->_frameSetting['ReservationFrameSetting']['id']);
}
}