-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalendarInsertPlanBehavior.php
More file actions
196 lines (173 loc) · 7.8 KB
/
CalendarInsertPlanBehavior.php
File metadata and controls
196 lines (173 loc) · 7.8 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
<?php
/**
* CalendarInsertPlan 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');
/**
* CalendarInsertPlanBehavior
*
* @property array $calendarWdayArray calendar weekday array カレンダー曜日配列
* @property array $editRrules editRules 編集ルール配列
* @author Allcreator <info@allcreator.net>
* @package NetCommons\Calendars\Model\Behavior
*/
class CalendarInsertPlanBehavior 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 bool $isMyPrivateRoom isMyPrivateRoom 予定の公開対象が自分のプライベートルームかどうか
* @return int 追加成功時 $eventId(calendarEvent.id)を返す。追加失敗時 InternalErrorExceptionを投げる。
* @throws InternalErrorException
*/
public function insertPlan(Model $model, $planParams, $isMyPrivateRoom) {
if (!$model->Behaviors->hasMethod('doArrangeData')) {
$model->Behaviors->load('Calendars.CalendarCrudPlanCommon');
}
$planParams = $model->doArrangeData($planParams);
$rruleData = $this->insertRruleData($model, $planParams); //rruleDataの1件登録
$eventData = $this->insertEventData($model, $planParams, $rruleData); //eventDataの1件登録
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);
}
return $eventId;
}
/**
* RruleDataへのデータ登録
*
* @param Model $model モデル
* @param array $planParams 予定パラメータ
* @param string $icalUidPart icalUidPart
* @param int $createdUserWhenUpd createdUserWhenUpd
* @return array $rruleDataを返す
* @throws InternalErrorException
*/
public function insertRruleData(Model $model, $planParams,
$icalUidPart = '', $createdUserWhenUpd = null) {
if (!(isset($model->CalendarRrule) && is_callable($model->CalendarRrule->create))) {
$model->loadModels([
'CalendarRrule' => 'Calendars.CalendarRrule',
]);
}
//rruleData保存のためにモデルをリセット(insert用)
$rruleData = $model->CalendarRrule->create();
//rruleDataにplanParamデータを詰め、それをモデルにセット
$this->setRruleData($model, $planParams, $rruleData);
//icalUidパーツの指定があれば、それをセットしておく。
//「この予定以降の変更」で1つのCalenarRruleが2つにスプリットする
//ケースを想定している。
//
if ($icalUidPart !== '') {
$rruleData['CalendarRrule']['icalendar_uid'] = $icalUidPart;
}
if (!$model->Behaviors->hasMethod('saveRruleData')) {
$model->Behaviors->load('Calendars.CalendarCrudPlanCommon');
}
$rruleData = $model->saveRruleData($rruleData, $createdUserWhenUpd);
return $rruleData;
}
/**
* EventDataへのデータ登録
*
* @param Model $model モデル
* @param array $planParams 予定パラメータ
* @param array $rruleData rruleデータ
* @param int $createdUserWhenUpd created_userを明示指定する時にnull以外を指定。updatePlanで主に利用されている。
* @return array $eventDataを返す
* @throws InternalErrorException
* @SuppressWarnings(PHPMD)
*/
public function insertEventData(Model $model, $planParams, $rruleData,
$createdUserWhenUpd = null) {
if (!(isset($model->CalendarEvent) && is_callable($model->CalendarEvent->create))) {
$model->loadModels([
'CalendarEvent' => 'Calendars.CalendarEvent',
]);
}
//eventData保存のためにモデルをリセット(insert用)
$eventData = $model->CalendarEvent->create();
//eventDataにplanParamデータを詰め、それをモデルにセット
$this->setEventData($planParams, $rruleData, $eventData);
$model->CalendarEvent->set($eventData);
if (!$model->CalendarEvent->validates()) { //eventDataをチェック
//CakeLog::debug("DBG: validationErrors[ " . print_r($model->CalendarEvent->validationErrors, true) . "}");
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if (!$model->CalendarEvent->save(null, false)) { //保存のみ
$model->validationErrors = array_merge(
$model->validationErrors, $model->CalendarEvent->validationErrors);
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//カレンダー独自の例外追加1)
//変更後の公開ルームidが、「元予定生成者の*ルーム」から「編集者・承認者(=ログイン者)の
//プライベート」に変化していた場合、created_userを、元予定生成者「から」編集者・承認者(=ログイン者)
//「へ」に変更すること。=>これを考慮したcreatedUserWhenUpdを使えばよい。
//
//尚、saveの中で $createdUserWhenUpd を直接セットせず、以下のsaveField(=UPDATE文)を使ったのは
//WFのbeforeSaveによりセットしたcreatedUserWhenUpd以外の値の書き換えられる可能性があるため。
//
if ($model->CalendarEvent->id > 0 && $createdUserWhenUpd !== null) {
//saveが成功し、かつ、createdUserWhenUpd がnull以外なら、created_userを更新しておく。
//modifiedも更新されるが、saveの直後なので誤差の範囲として了とする。
$model->CalendarEvent->saveField('created_user', $createdUserWhenUpd, ['callbacks' => false]);
//UPDATEでセットしたcreatedUserWhenUpdの値をeventDataに記録しておく
$eventData['CalendarEvent']['created_user'] = $createdUserWhenUpd;
}
//採番されたidをeventDataにセットしておく
$eventData['CalendarEvent']['id'] = $model->CalendarEvent->id;
//ShareUsersとContentは、calendar_event_id単位に登録するので、ここにもってきた。
//
if (!$model->Behaviors->hasMethod('insertShareUsers')) {
$model->Behaviors->load('Calendars.CalendarShareUserEntry');
}
//カレンダ共有ユーザ登録
$model->insertShareUsers($planParams['share_users'], $eventData['CalendarEvent']['id'],
$createdUserWhenUpd);
//注: 他のモデルの組み込みBehaviorをcallする場合、第一引数に$modelの指定はいらない。
//関連コンテンツの登録
if ($eventData['CalendarEventContent']['model'] !== '') {
if (!$model->Behaviors->hasMethod('saveLinkMdlCkey')) {
$model->Behaviors->load('Calendars.CalendarLinkEntry');
}
$model->saveLinkMdlCkey($eventData, $createdUserWhenUpd);
}
return $eventData;
}
}