-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalendarUpdatePlanBehavior.php
More file actions
831 lines (722 loc) · 35.1 KB
/
CalendarUpdatePlanBehavior.php
File metadata and controls
831 lines (722 loc) · 35.1 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
<?php
/**
* CalendarUpdatePlan Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('CalendarAppBehavior', 'Calendars.Model/Behavior');
App::uses('CalendarRruleUtil', 'Calendars.Utility');
/**
* CalendarUpdatePlanBehavior
*
* @property array $calendarWdayArray calendar weekday array カレンダー曜日配列
* @property array $editRrules editRules 編集ルール配列
* @author Allcreator <info@allcreator.net>
* @package NetCommons\Calendars\Model\Behavior
* @SuppressWarnings(PHPMD)
*/
class CalendarUpdatePlanBehavior extends CalendarAppBehavior {
/**
* Default settings
*
* VeventTime(+VeventRRule)の値自動変更
* registered_into to calendar_information
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2015, NetCommons Project
*/
protected $_defaults = array(
'calendarRruleModel' => 'Calendars.CalendarRrule',
'fields' => array(
'registered_into' => 'calendar_information',
),
);
//上記のfields定義は、以下の意味です。
// The (event|todoplugin|journal) was registerd into the calendar information.
// =イベント(または To Doまたは日報)が予定表の情報に登録されました。
/**
* 予定の変更
*
* @param Model $model 実際のモデル名
* @param array $planParams 予定パラメータ
* @param array $newPlan 新世代予定(この新世代予定に対して変更をかけていく)
* @param string $status status(Workflowステータス)
* @param array $isInfoArray (isOriginRepeat、isTimeMod、isRepeatMod、isMyPrivateRoom)を格納した配列
* @param string $editRrule 編集ルール (この予定のみ、この予定以降、全ての予定)
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return 変更成功時 int calendarEventId
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function updatePlan(Model $model, $planParams, $newPlan, $status,
$isInfoArray, $editRrule = self::CALENDAR_PLAN_EDIT_THIS, $createdUserWhenUpd = null) {
$eventId = $newPlan['new_event_id'];
//bool $isOriginRepeat 元予定が繰返しありかなしか
//bool $isTimeMod 元予定に対して時間の変更があったかどうか
//bool $isRepeatMod 元予定に対して繰返しの変更があったかどうか
//bool $isMyPrivateRoom 新しい予定の公開対象のルームがログイン者のプライベートルームかどうか
list($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom) = $isInfoArray;
if (!$model->Behaviors->hasMethod('doArrangeData')) {
$model->Behaviors->load('Calendars.CalendarCrudPlanCommon');
}
$planParams = $model->doArrangeData($planParams);
//CalendarEventの対象データ取得
$this->loadEventAndRruleModels($model);
//対象となるデータを$eventData、$rruleDataそれぞれにセット
$eventData = $rruleData = array();
list($eventData, $rruleData) = $this->setEventDataAndRruleData($model, $newPlan);
//timezone_offsetがなければ、calendar_eventテーブルからセットする。
if (!isset($planParams['timezone_offset'])) {
$planParams['timezone_offset'] = $eventData['CalendarEvent']['timezone_offset'];
}
//「全更新」、「指定以降更新」、「この予定のみ更新or元予定に繰返しなし」
if ($editRrule === self::CALENDAR_PLAN_EDIT_ALL) {
//「この予定ふくめ全て更新」
$isArray = array($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom);
$eventId = $this->updatePlanAll($model, $planParams, $rruleData, $eventData,
$newPlan, $isArray, $status, $editRrule, $createdUserWhenUpd);
return $eventId; //復帰
} elseif ($editRrule === self::CALENDAR_PLAN_EDIT_AFTER) {
//「この予定以降を更新」
$isArray = array($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom);
$eventId = $this->updatePlanByAfter(
$model, $planParams, $rruleData, $eventData, $newPlan, $isArray,
$status, $editRrule, $createdUserWhenUpd);
return $eventId; //復帰
} else {
//「この予定のみ更新or元予定に繰返しなし」
if ($isOriginRepeat) {
//元予定に繰返しあり、の「この予定のみ更新」ケース
if ($isRepeatMod) {
//繰返し条件が変更になった場合、(b)
CakeLog::notice(
"「この予定のみ更新」の場合、" .
"繰返し予定の変更は許可していない。" .
"Googleカレンダー仕様に準拠し、" .
"繰返し予定の変更は無視し、" .
"現繰返しルールをそのままつかう。");
}
//すでにnewPlanを作成する時rruleDataは生成されているので、
//rruleDataの上書き(updateRruleData()発行)は無駄なのでしない。
//
//補足)newPlanを生成するとき、createdUserWhenUpdを考慮してrruleをcopyしています。
} else {
//「元予定に繰返しなし」=元予定は単一予定
//
//すでにnewPlanを作成する時rruleDataは生成されている。
//
//変更後、繰返し指定になっている可能性もあるので、
//rruleデータを入力データに従い更新しておく。
//
$rruleData = $this->updateRruleData($model, $planParams, $rruleData, $createdUserWhenUpd);
}
//選択したeventデータを更新 (a). keyは踏襲されている。
//
$this->setEventData($planParams, $rruleData, $eventData); //eventDataに値セット
$isArrays = array($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom);
$eventData = $this->updateDtstartData($model, $planParams, $rruleData, $eventData,
$isArrays, 1, $editRrule, $createdUserWhenUpd);
$eventId = $eventData['CalendarEvent']['id'];
//「この予定のみ更新or元予定に繰返しなし」
if ($isOriginRepeat) {
//元予定に繰返しありのケース
//兄弟eventの情報を書き換える必要はないので、ここではなにもしない。
} else {
//「元予定に繰返しなし」=元予定は単一予定
//元予定に兄弟eventは存在しないので、
//前出の「選択したeventデータを更新 (a)」を最初のeventとして扱えばよい。
//(もし繰返し指定があれば、2件目以降のevent生成を行う)
//
if ($rruleData['CalendarRrule']['rrule'] !== '') { //Rruleの登録
if (!$model->Behaviors->hasMethod('insertRrule')) {
$model->Behaviors->load('Calendars.CalendarRruleEntry');
}
$model->insertRrule($planParams, $rruleData, $eventData, $createdUserWhenUpd);
}
}
return $eventId; //復帰
}
}
/**
* CalendarEventの対象データ取得
*
* @param Model $model 実際のモデル名
* @param int $eventId CalendarEvent.id
* @param string $editRrule editRrule デフォルト値 self::CALENDAR_PLAN_EDIT_THIS
* @return 成功時 array 条件にマッチするCalendarEventDataとそのbelongsTo,hasOne関係のデータ(実際には、CalendarRruleData), 失敗時 空配列
*/
public function getCalendarEventAndRrule(Model $model, $eventId, $editRrule) {
$params = array(
'conditions' => array('CalendarEvent.id' => $eventId),
'recursive' => 0, //belongTo, hasOneの1跨ぎの関係までとってくる。
'fields' => array('CalendarEvent.*', 'CalendarRrule.*'),
'callbacks' => false
);
return $model->CalendarEvent->find('first', $params);
}
/**
* RruleDataへのデータをdateへセット
*
* @param array $rruleData rruleData
* @param array &$data data
* @return void
*/
public function setRruleData2Data($rruleData, &$data) {
//$data['CalendarRrule']['location'] = $rruleData['CalendarRrule']['location'];
//$data['CalendarRrule']['contact'] = $rruleData['CalendarRrule']['contact'];
//$data['CalendarRrule']['description'] = $rruleData['CalendarRrule']['description'];
$data['CalendarRrule']['rrule'] = $rruleData['CalendarRrule']['rrule'];
$data['CalendarRrule']['room_id'] = $rruleData['CalendarRrule']['room_id'];
//$data['CalendarRrule']['status'] = $rruleData['CalendarRrule']['status'];
//$data['CalendarRrule']['language_id'] = $rruleData['CalendarRrule']['language_id'];
}
/**
* 予定データの全更新
*
* @param Model $model モデル
* @param array $planParams 予定パラメータ
* @param array $rruleData rruleData
* @param array $eventData eventData(編集画面のevent)
* @param array $newPlan 新世代予定データ
* @param array $isArray ($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom)をまとめた配列
* @param string $status status(Workflowステータス)
* @param int $editRrule editRrule
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return int eventIdを返す
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function updatePlanAll(Model $model, $planParams, $rruleData, $eventData,
$newPlan, $isArray, $status, $editRrule, $createdUserWhenUpd) {
$isOriginRepeat = $isArray[0];
$isTimeMod = $isArray[1];
$isRepeatMod = $isArray[2];
$isMyPrivateRoom = $isArray[3];
if (!(isset($model->CalendarRrule))) {
$model->loadModels([
'CalendarRrule' => 'Calendars.CalendarRrule',
]);
}
//繰返し情報が更新されている時は、rruleDataをplanParasを使って書き換える
if ($isRepeatMod) {
//setRruleDataはsave()を呼んでいないフィールドセットだけのmethodなので、
//setRruleData()+save()のupdateRruleData()の変更する。
////$this->setRruleData($model, $planParams, $rruleData, self::CALENDAR_UPDATE_MODE);
$this->updateRruleData($model, $planParams, $rruleData, $createdUserWhenUpd);
}
$eventId = null;
if ($isTimeMod || $isRepeatMod) {
//時間・繰返し系が変更されたので、
////////////////////////
//(0)編集画面のplanParamsをもとに、eventDataを生成する。
$this->setEventData($planParams, $rruleData, $eventData);
////////////////////////
//(1)現在のrrule配下の全eventDataを消す
//まずnewPlanより、消す対象のeventのidをすべて抽出する。
//$eventIds = Hash::extract($newPlan,
// 'CalendarEvent.{n}[language_id=' . Current::read('Langugage.id') . '].id');
//eventのidではなく、keyで消すこと。
//そうしないと、直近のidだけ消しても、過去世代の同一keyの別idの
//eventデータが拾われてしますから。
////$eventIds = Hash::extract($newPlan, 'CalendarEvent.{n}.id');
$eventKeys = [];
foreach ($newPlan['CalendarEvent'] as $item) {
$eventKeys[] = $item['key'];
}
$this->__deleteOrUpdateAllEvents($model, $status, $eventData, $eventKeys);
/////////////////
//(2)新たな時間・繰返し系情報をもとに、eventDataを生成し直す。(keyはすべて新規)
//*vcalendarでは日付時刻がキーになっているので、繰返し系に変更がなくとも、
// 時間系が変われば、vcalendar的にはキーがかわるので、eventデータのkeyも取り直すこととする。
//
//(以下で行うのは、insertPlan()のサブセット処理)
if (!$model->Behaviors->hasMethod('insertEventData')) {
$model->Behaviors->load('Calendars.CalendarInsertPlan');
}
//先頭のeventDataの1件登録
$eventData = $model->insertEventData($planParams, $rruleData, $createdUserWhenUpd);
if (!isset($eventData['CalendarEvent']['id'])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$eventId = $eventData['CalendarEvent']['id'];
if ($rruleData['CalendarRrule']['rrule'] !== '') { //Rruleの登録
if (!$model->Behaviors->hasMethod('insertRrule')) {
$model->Behaviors->load('Calendars.CalendarRruleEntry');
}
$model->insertRrule($planParams, $rruleData, $eventData, $createdUserWhenUpd);
////uddateRruleData()は、$isRepeatModがtrueの時だけ発行する関数なので、
////ここではなく、前出の if($isRepeatMod){...}へ移動した。
////$this->updateRruleData($model, $planParams, $rruleData);//FUJI
}
} else {
//時間・繰返し系が変更されていない(vcalendar的なキーが変わらない)ので、eventのkeyはそのままに
//現在の全eventDataの時間以外の値を書き換える。
//選択されたデータを編集画面のデータ(planParams)をもとに書き換える
//書き換え後のデータは、以下の全書き換えの雛形eventとする。
//
$this->setEventData($planParams, $rruleData, $eventData);
$index = 0;
foreach ($newPlan['CalendarEvent'] as $fields) {
++$index;
$event = array();
$event['CalendarEvent'] = $fields; //$eventは元のeventを指す。
$eventDataForAllUpd = $this->__getEventDataForUpdateAllOrAfter($event,
$eventData, $status);
if ($eventId === null) {
//繰返しの最初のeventIdを記録しておく。
$eventId = $eventDataForAllUpd['CalendarEvent']['id'];
}
$isArrays = array($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom);
$eventDataForAllUpd = $this->updateDtstartData(
$model, $planParams, $rruleData, $eventDataForAllUpd,
$isArrays, $index, $editRrule, $createdUserWhenUpd);
}
}
return $eventId;
}
/**
* EventDataのデータ更新
*
* @param Model $model モデル
* @param array $planParams 予定パラメータ
* @param array $rruleData rruleデータ
* @param array $eventData eventデータ
* @param array $isArrays isArrays (isOriginRepeat,isTimeMod,isRepeatMod,isMyPrivateRoom)を格納した配列
* @param int $index index 何回目のupdateのindex(1はじまり)
* @param string $editRrule editRrule
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return array $eventData 変更後の$eventDataを返す
* @throws InternalErrorException
*/
public function updateDtstartData(Model $model, $planParams, $rruleData, $eventData,
$isArrays, $index, $editRrule, $createdUserWhenUpd = null) {
//bool $isOriginRepeat isOriginRepeat
//bool $isTimeMod isTimeMod
//bool $isRepeatMod isRepeatMod
list($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom) = $isArrays;
if (!(isset($model->CalendarEvent) && is_callable($model->CalendarEvent->create))) {
$model->loadModels([
'CalendarEvent' => 'Calendars.CalendarEvent',
]);
}
if ($editRrule === self::CALENDAR_PLAN_EDIT_ALL) {
//「この予定ふくめ全て更新」
//繰返し・時間系の変更がない場合のEDIT_ALLの場合、
//単一の更新と同じ処理にながせばよい。
//なお、「この予定のみ更新」ではないので、
//recurrenceにはなにもしない
} elseif ($editRrule === self::CALENDAR_PLAN_EDIT_AFTER) {
//「この予定以降を更新」
//繰返し・時間系の変更がない場合のEDIT_AFTERの場合、
//単一の更新と同じ処理にながせばよい。
//なお、「この予定のみ更新」ではないので、
//recurrenceにはなにもしない
} else {
//「この予定のみ更新」
if ($isOriginRepeat) {
//元予定が繰返しあり
//置換イベントidとして1を立てておく。
$eventData['CalendarEvent']['recurrence_event_id'] = 1; //暫定1
}
}
$eventId = $eventData['CalendarEvent']['id']; //update対象のststartendIdを退避
//カレンダー独自の例外追加1)
//変更後の公開ルームidが、「元予定生成者の*ルーム」から「編集者・承認者(=ログイン者)の
//プライベート」に変化していた場合、created_userを、元予定生成者「から」編集者・承認者(=ログイン者)
//「へ」に変更すること。=>これを考慮したcreatedUserWhenUpdを使えばよい。
//尚、ここのsaveはUPDATなので、save前に、create_user項目へセットして問題なし。
if ($createdUserWhenUpd !== null) {
$eventData['CalendarEvent']['created_user'] = $createdUserWhenUpd;
}
$model->CalendarEvent->set($eventData);
if (!$model->CalendarEvent->validates()) { //eventDataをチェック
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//copyEventData()のINSERTsaveでは、WFのbeforeSaveのis_active調整処理を抑止し、
//代わりに、prepareLatestCreatedForInsを発行し、is_latest,created調整処理および
//is_activeのoff暫定セットをした。
//(WFのbeforeSaveはUPDATEsaveでは発動されないことが分かっているので)
//よって、「ここ」UPDATEsaveで、prepareActiveForUpdを事前実行し、INSERTsaveでdelayさせた
//is_active調整処理を行う。(eventDataの値が一部変更されます)
$model->CalendarEvent->prepareActiveForUpd($model->CalendarEvent->data);
if (!$model->CalendarEvent->save(null,
array(
'validate' => false,
'callbacks' => true,
))) { //保存のみ
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($eventId !== $model->CalendarEvent->id) {
//insertではなくupdateでなくてはならないのに、insertになってしまった。(つまりid値が新しくなってしまった)
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//採番されたidをeventDataにセットしておく
$eventData['CalendarEvent']['id'] = $model->CalendarEvent->id;
//カレンダー共有ユーザ更新
if (!$model->Behaviors->hasMethod('updateShareUsers')) {
$model->Behaviors->load('Calendars.CalendarShareUserEntry');
}
$model->updateShareUsers(
$planParams['share_users'],
$eventId,
isset($eventData['CalendarEvent']['CalendarEventShareUser'])
? $eventData['CalendarEvent']['CalendarEventShareUser']
: null,
$createdUserWhenUpd
);
//関連コンテンツ(calendar_event_contents)の更新
//
if (!empty($eventData['CalendarEvent']['CalendarEventContent']['model'])) {
if (!(isset($model->CalendarEventContent))) {
$model->loadModels(['CalendarEventContent' => 'Calendars.CalendarEventContent']);
}
//saveLinkedData()は、内部で
//modelとcontent_key一致データなし=> insert
//modelとcontent_key一致データあり=> update
//と登録・変更を適宜区別して実行する関数である。
$model->CalendarEventContent->saveLinkedData($eventData, $createdUserWhenUpd);
}
return $eventData;
}
/**
* 指定eventデータ以降の予定の変更
*
* @param Model $model 実際のモデル名
* @param array $planParams 予定パラメータ
* @param array $rruleData rruleData
* @param array $eventData eventData
* @param array $newPlan 新世代予定データ
* @param array $isArray ($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom)をまとめた配列
* @param string $status status(Workflowステータス)
* @param int $editRrule editRrule
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return int $eventIdを返す
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function updatePlanByAfter(Model $model, $planParams, $rruleData, $eventData,
$newPlan, $isArray, $status, $editRrule, $createdUserWhenUpd) {
$eventId = $newPlan['new_event_id'];
////$rruleKey = $rruleData['CalendarRrule']['key'];
$isOriginRepeat = $isArray[0];
$isTimeMod = $isArray[1];
$isRepeatMod = $isArray[2];
$isMyPrivateRoom = $isArray[3];
if (!(isset($model->CalendarRrule))) {
$model->loadModels([
'CalendarRrule' => 'Calendars.CalendarRrule',
]);
}
$eventId = null;
if ($isTimeMod || $isRepeatMod) {
//時間・繰返し系が変更されたので、
////////////////////////
//(1)指定eventのdtstart以降の全eventDataを消す
//まずnewPlanより、基準日時以後の消す対象eventのidをすべて抽出する。
//注)ここに来る前に、setEventDataAndRruleData()で、
//rruleData, eventDataには、newPlanより指定したものが抽出・セットされているので、
//それを使う。
//
//CakeLog::debug("DBG: before setEventData()。eventData[" . print_r($eventData, true) . "]");
//ここでは、指定された元予定の時刻をつかわないといけない。
//誤って、$planParamsからsetEventData()実行でeventDataを上書きすると、
//時間系が変更になっているための別の日時になってしまい、
//つかえないdtstartになります。要注意。
//
//CakeLog::debug("DBG: after setEventData()。eventData[" . print_r($eventData, true) . "]");
//画面より入力された開始の日付時刻を、$baseDtstartにする。
$baseDtstart = $eventData['CalendarEvent']['dtstart']; //基準日時
//eventのidではなく、keyで消さないといけない。(なぜなら同一キーをもつ過去世代が複数あり
//1つのidをけしても、同一keyの他のidのデータが拾われて表示されてしまうため。
////$eventIds = Hash::extract($newPlan['CalendarEvent'], '{n}[dtstart>=' . $baseDtstart . '].id');
$eventKeys = [];
foreach ($newPlan['CalendarEvent'] as $item) {
if ($item['dtstart'] >= $baseDtstart) {
$eventKeys[] = $item['key'];
}
}
$this->__deleteOrUpdateAllEvents($model, $status, $eventData, $eventKeys);
//////////////////////////////
//(2) eventsを消した後、rruleIdを親にもつeventDataの件数を調べる。
//(2)-1. eventData件数==0、つまり、今の親rruleDataは、子を一切持たなくなった。
// 自分の新しい親rruleDataをこの後つくるので)現在の親rruleDataは浮きリソースになるので消す。
//(2)-2. eventData件数!=0、つまり、今の親rruleDataは自分(eventData)以外の子(時間軸では自分より前の時間)
// を持っている。
// なので、今の親rruleDataのrruleのUNTIL値を「自分の直前まで」に書き換える。
// 自分を今の親rruleDataの管理下から切り離す。(自分の新しい親rruleDataはこのあと作る)
//
// =>これらの(2)の一連処理を実行する関数 auditEventOrRewriteUntil() をcallする。
//
if (!$model->Behaviors->hasMethod('auditEventOrRewriteUntil')) {
$model->Behaviors->load('Calendars.CalendarCrudPlanCommon');
}
$model->auditEventOrRewriteUntil($eventData, $rruleData, $baseDtstart);
/////////////////
//(3) 新たな時間・繰返し系情報をもとに、rruleDataと、eventData群を生成し直す。(keyはすべて新規)
//*rruleDataは新しく発行する。icalendar_uidに分割された元rruleDataのkeyの一部を保持する。
//*vcalendarでは日付時刻がキーになっているので、繰返し系に変更がなくとも、
// 時間系が変われば、vcalendar的にはキーがかわるので、eventデータのkeyも取り直すこととする。
//
//(以下で行うのは、insertPlan()のサブセット処理)
//あとで、2つのrruleDataが分割されたものであることが分かるよう、
//新rruleDataのicalendar_uidを、元のicalendar_uid + 元keyにしておく。
//
$newIcalUid = CalendarRruleUtil::addKeyToIcalUid(
$rruleData['CalendarRrule']['icalendar_uid'], $rruleData['CalendarRrule']['key']);
//(以下は、insertPlan()のサブセット処理
if (!$model->Behaviors->hasMethod('insertRruleData')) {
$model->Behaviors->load('Calendars.CalendarInsertPlan');
}
//rruleDataの新規1件登録
$rruleData = $model->insertRruleData($planParams, $newIcalUid, $createdUserWhenUpd);
//先頭のeventDataの1件登録
$eventData = $model->insertEventData($planParams, $rruleData, $createdUserWhenUpd);
if (!isset($eventData['CalendarEvent']['id'])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$eventId = $eventData['CalendarEvent']['id'];
if ($rruleData['CalendarRrule']['rrule'] !== '') { //Rruleの登録
if (!$model->Behaviors->hasMethod('insertRrule')) {
$model->Behaviors->load('Calendars.CalendarRruleEntry');
}
$model->insertRrule($planParams, $rruleData, $eventData, $createdUserWhenUpd);
}
} else {
//時間・繰返し系が変更されていない(vcalendar的なキーが変わらない)ので、eventのkeyはそのままに
//指定されたeventIdより日付時刻が後になるeventDataすべての時間以外の値を書き換える。
//選択されたデータを編集画面のデータ(planParams)をもとに書き換える
//書き換え後のデータは、以下の全書き換えの雛形eventとする。
//
//$planParamsの値(画面の入力値)より、$eventDataを作り出す。
//*時間系が変更されていないことが保証されているので、
//setEventData()を発行して、$eventDataを更新しても、
//dtstartは元のままです。
$this->setEventData($planParams, $rruleData, $eventData);
//画面より入力された開始の日付時刻を、$baseDtstartにする。
$baseDtstart = $eventData['CalendarEvent']['dtstart'];
$eventsAfterBase = [];
foreach ($newPlan['CalendarEvent'] as $item) {
if ($item['dtstart'] >= $baseDtstart) {
$eventsAfterBase[] = $item;
}
}
$index = 0;
foreach ($eventsAfterBase as $fields) {
++$index;
$event = array();
$event['CalendarEvent'] = $fields; //$eventは元のeventを指す。
$eventDataForAfterUpd = $this->__getEventDataForUpdateAllOrAfter($event,
$eventData, $status);
if ($eventId === null) {
//繰返しの最初のeventIdを記録しておく。
$eventId = $eventDataForAfterUpd['CalendarEvent']['id'];
}
$isArrays = array($isOriginRepeat, $isTimeMod, $isRepeatMod, $isMyPrivateRoom);
$eventDataForAfterUpd = $this->updateDtstartData(
$model, $planParams, $rruleData, $eventDataForAfterUpd,
$isArrays, $index, $editRrule, $createdUserWhenUpd);
}
}
return $eventId;
}
/**
* resutlsよりeventDataとrruleDataに値セット
*
* @param Model $model モデル
* @param array $newPlan 新世代予定
* @return array array($eventData, $rruleData)を返す
* @throws InternalErrorException
*/
public function setEventDataAndRruleData(Model $model, $newPlan) {
//この時点で、$newPlan['CalendarRrule']、$newPlan['CalendarEvent']のcreated_userは、
//createdUserWhenUpd考慮済になっている。
$rruleData['CalendarRrule'] = $newPlan['CalendarRrule'];
$calendarEvent = [];
foreach ($newPlan['CalendarEvent'] as $item) {
if ($item['id'] === $newPlan['new_event_id']) {
$calendarEvent = $item;
break;
}
}
$eventData['CalendarEvent'] = $calendarEvent;
return array($eventData, $rruleData);
}
/**
* getEditRruleForUpdate
*
* request->data情報より、editRruleモードを決定し返す。
*
* @param Model $model モデル
* @param array $data data
* @return string 成功時editRruleモード(0/1/2)を返す。失敗時 例外をthrowする
* @throws InternalErrorException
*/
public function getEditRruleForUpdate(Model $model, $data) {
if (empty($data['CalendarActionPlan']['edit_rrule'])) {
//edit_rruleが存在しないか'0'ならば、「この予定のみ変更」
return self::CALENDAR_PLAN_EDIT_THIS;
}
if ($data['CalendarActionPlan']['edit_rrule'] == self::CALENDAR_PLAN_EDIT_AFTER) {
return self::CALENDAR_PLAN_EDIT_AFTER;
}
if ($data['CalendarActionPlan']['edit_rrule'] == self::CALENDAR_PLAN_EDIT_ALL) {
return self::CALENDAR_PLAN_EDIT_ALL;
}
//ここに流れてくる時は、モードの値がおかしいので、例外throw
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
/**
* RruleDataのデータ更新
*
* @param Model $model モデル
* @param array $planParams 予定パラメータ
* @param array $rruleData 更新対象となるrruleData
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return array $rruleDataを返す
* @throws InternalErrorException
*/
public function updateRruleData(Model $model, $planParams, $rruleData,
$createdUserWhenUpd = null) {
if (!(isset($model->CalendarRrule) && is_callable($model->CalendarRrule->create))) {
$model->loadModels([
'CalendarRrule' => 'Calendars.CalendarRrule',
]);
}
//現rruleDataにplanParamデータを詰め、それをモデルにセット
$this->setRruleData($model, $planParams, $rruleData, self::CALENDAR_UPDATE_MODE,
$rruleData['CalendarRrule']['key'], $rruleData['CalendarRrule']['id']);
if (!$model->Behaviors->hasMethod('saveRruleData')) {
$model->Behaviors->load('Calendars.CalendarCrudPlanCommon');
}
$rruleData = $model->saveRruleData($rruleData, $createdUserWhenUpd);
return $rruleData;
}
/**
* __getEventDataForUpdateAllOrAfter
*
* 時間系・繰返し系に変更がない時の全変更・以後変更兼用イベントデータ生成
*
* @param array $event newPlanの各繰返しeventデータ。keyにCalendarEventを持つように整形してある。
* @param array $eventData 編集画面のデータに基づいて作成されたeventData
* @param string $status status
* @return array 全変更用に適宜編集された繰返しeventデータ
*/
private function __getEventDataForUpdateAllOrAfter($event, $eventData, $status) {
//id,key,rrule_idはnewPlanのまま
//$event['CalendarEvent']['id'] = 108
//$event['CalendarEvent']['calendar_rrule_id'] = 83
//$event['CalendarEvent']['key'] = d5612115c24c86ea8987eddd021aff5b
//room_idは編集画面の値を使う
$event['CalendarEvent']['room_id'] = $eventData['CalendarEvent']['room_id'];
//langauge_id,target_userは編集画面にないのでnewPlanのまま
//$event['CalendarEvent']['language_id'] = 2
//$event['CalendarEvent']['target_user'] = 1
//タイトル、場所、連絡先、詳細は編集画面の値を使う
$event['CalendarEvent']['title'] = $eventData['CalendarEvent']['title'];
$event['CalendarEvent']['title_icon'] = $eventData['CalendarEvent']['title_icon'];
$event['CalendarEvent']['location'] = $eventData['CalendarEvent']['location'];
$event['CalendarEvent']['contact'] = $eventData['CalendarEvent']['contact'];
$event['CalendarEvent']['description'] = $eventData['CalendarEvent']['description'];
//終日指定、開始終了日時、TZは「全て変更」の場合、newPlanの値を使う
//$event['CalendarEvent']['is_allday'] =
//$event['CalendarEvent']['start_date'] = 20160616
//$event['CalendarEvent']['start_time'] = 080000
//$event['CalendarEvent']['dtstart'] = 20160616080000
//$event['CalendarEvent']['end_date'] = 20160616
//$event['CalendarEvent']['end_time'] = 090000
//$event['CalendarEvent']['dtend'] = 20160616090000
//$event['CalendarEvent']['timezone_offset'] = 9.0
//statusは、編集画面のsave_Nを元にカレンダー拡張新statusになっているので、
//それを代入する。
$event['CalendarEvent']['status'] = $status;
//is_active, is_latestは、statusの値変化の有無で、処理が変わるのでここではスルーする。
//$event['CalendarEvent']['is_active'] = $eventData['CalendarEvent']['is_active'];
//$event['CalendarEvent']['is_latest'] = $eventData['CalendarEvent']['is_latest'];
//「この予定のみ」変更した記録(置換)は残しておく(newPlanの値のまま)
//$event['CalendarEvent']['recurrence_event_id'] = 0
//「除外」記録は残しておく(newPlanの値のまま)
//$event['CalendarEvent']['exception_event_id'] = 0
//メール通知関連は編集画面の値を使う
$event['CalendarEvent']['is_enable_mail'] = $eventData['CalendarEvent']['is_enable_mail'];
$event['CalendarEvent']['email_send_timing'] = $eventData['CalendarEvent']['email_send_timing'];
//作成日、作成者情報はnewPlanの値のまま
//$event['CalendarEvent']['created_user'] = 1
//$event['CalendarEvent']['created'] = 2016-06-17 07:38:27
//更新日、更新者情報は変更する
$event['CalendarEvent']['modified_user'] = $eventData['CalendarEvent']['modified_user'];
$event['CalendarEvent']['modified'] = $eventData['CalendarEvent']['modified'];
//CalendarEventShareUserは、あとで、planParamsのShareUserを
//つかって書き換えるので、元のままとしておく。
//$event['CalendarEvent']['CalendarEventShareUser'] = Array
// (
// )
//CalendarEventContentは、あとで、書き換えるので、
//元のままとしておく。
//$event['CalendarEvent']['CalendarEventContent'] = Array
// (
// )
return $event;
}
/**
* __deleteOrUpdateAllEvents
*
* 指定した全イベントデータの削除または更新処理
*
* @param Model $model 実際のモデル名
* @param string $status status
* @param array $eventData 元となるeventData
* @param array $eventKeys 対象とするeventデータ群のkey集合
* @return void
* @throws InternalErrorException
*/
private function __deleteOrUpdateAllEvents(Model $model, $status, $eventData, $eventKeys) {
if ($status == WorkflowComponent::STATUS_PUBLISHED) {
// (1)-1 statusが発行済の場合、実際に削除する。
$conditions = array(
array(
$model->CalendarEvent->alias . '.key' => $eventKeys,
)
);
//第2引数cascade==trueで、関連する子 CalendarEventShareUsers, CalendarEventContentを
//ここで消す。
//第3引数callbacks==trueで、メール関連のafterDeleteを動かす? FIXME: 要確認
//
if (!$model->CalendarEvent->deleteAll($conditions, true, true)) {
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
} else {
// (1)-2 statusが一時保存、承認待ち、差し戻しの場合、現在のrrule配下の全eventDataの
// excepted(除去)を立てて、無効化しておく。
// なお、表示に引っかからないよう、is_xxxxもoffしておくこと。
$fields = array(
$model->CalendarEvent->alias . '.exception_event_id' => 1,
$model->CalendarEvent->alias . '.modified_user' =>
$eventData['CalendarEvent']['modified_user'],
$model->CalendarEvent->alias . '.modified' =>
"'" . $eventData['CalendarEvent']['modified'] . "'", //クオートに注意
//update,updateAllの時はWFのbeforeSaveによるis_xxxx変更処理は動かない.
//よってCAL自体でis_xxxxを変更(off)しておく。
$model->CalendarEvent->alias . '.is_active' => false, //aaaaaaaaa
$model->CalendarEvent->alias . '.is_latest' => false, //aaaaaaaaa
);
$conditions = array($model->CalendarEvent->alias . '.key' => $eventKeys);
if (!$model->CalendarEvent->updateAll($fields, $conditions)) {
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
}