/
CalendarMailBehavior.php
205 lines (184 loc) · 6.96 KB
/
CalendarMailBehavior.php
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
<?php
/**
* CalendarMail 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('WorkflowComponent', 'Workflow.Controller/Component');
App::uses('CalendarPermissiveRooms', 'Calendars.Utility');
App::uses('CalendarPlan', 'Calendars.Helper');
App::uses('CalendarPlanRrule', 'Calendars.Helper');
/**
* CalendarMailBehavior
*
* @author Allcreator <info@allcreator.net>
* @package NetCommons\Calendars\Model\Behavior
*/
class CalendarMailBehavior extends CalendarAppBehavior {
/**
* sendWorkflowAndNoticeMail
*
* 承認依頼メールや公開通知メールを送る処理
* カレンダーは「カレント」のルームIDじゃない情報を作ったりするのでカレントのすり替え処理が必要
*
* @param Model $model モデル
* @param int $eventId イベントID(繰り返しの場合は先頭のイベント)
* @param bool $isMyPrivateRoom (プライベートルームの情報かどうか)
* @return void
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function sendWorkflowAndNoticeMail(Model $model, $eventId, $isMyPrivateRoom) {
$model->loadModels([
'Block' => 'Blocks.Block',
'CalendarEvent' => 'Calendars.CalendarEvent'
]);
$model->CalendarEvent->Behaviors->load('Mails.MailQueue');
// 指定されたイベント情報を取得
$data = $model->CalendarEvent->getEventById($eventId);
if (! $data) {
return;
}
$model->CalendarEvent->set($data);
$this->_setDateTags($model, $data);
$this->_setRruleTags($model, $data);
$this->_setUrlTags($model, $data);
$this->_setRoomTags($model, $data);
$model->CalendarEvent->setAddEmbedTagValue('X-SUBJECT', $data['CalendarEvent']['title']);
$model->CalendarEvent->setAddEmbedTagValue('X-CONTACT', $data['CalendarEvent']['contact']);
$model->CalendarEvent->setAddEmbedTagValue('X-LOCATION', $data['CalendarEvent']['location']);
$model->CalendarEvent->setAddEmbedTagValue('X-BODY', $data['CalendarEvent']['description']);
// すり替え前にオリジナルルームID,オリジナルのBlockID,オリジナルのBlockKeyを確保
$originalRoomId = Current::read('Room.id');
$originalBlockId = Current::read('Block.id');
$originalBlockKey = Current::read('Block.key');
// 予定のルームID
$eventRoomId = $data['CalendarEvent']['room_id'];
$eventBlockId = $originalBlockId;
$eventBlockKey = $originalBlockKey;
$block = $model->Block->find('first', array(
'conditions' => array(
'plugin_key' => 'calendars',
'room_id' => $eventRoomId
)
));
if ($block) {
$eventBlockId = $block['Block']['id'];
$eventBlockKey = $block['Block']['key'];
}
// パーミッション情報をターゲットルームのものにすり替え
CalendarPermissiveRooms::setCurrentPermission($eventRoomId);
// カレントのルームIDなどをすり替え
Current::$current['Room']['id'] = $eventRoomId;
Current::$current['Block']['id'] = $eventBlockId;
Current::$current['Block']['key'] = $eventBlockKey;
// プライベートのものの場合は自分と共有者に
if ($isMyPrivateRoom) {
$userIds = [Current::read('User.id')];
foreach ($data['CalendarEventShareUser'] as $shareUser) {
$userIds[] = $shareUser['share_user'];
}
$model->CalendarEvent->setSetting(MailQueueBehavior::MAIL_QUEUE_SETTING_USER_IDS, $userIds);
}
$model->CalendarEvent->Behaviors->load('Mails.IsMailSend',
array(
'keyField' => 'key',
MailQueueBehavior::MAIL_QUEUE_SETTING_IS_MAIL_SEND_POST => true,
));
$isMailSend = $model->CalendarEvent->isMailSend(
MailSettingFixedPhrase::DEFAULT_TYPE, $data['CalendarEvent']['key'], 'calendars');
if ($isMailSend) {
//ワークフローコメントがある場合、saveQueue()で使用しているため、セットする
if (!empty($model->data['WorkflowComment']['comment'])) {
$model->CalendarEvent->data['WorkflowComment']['comment'] =
$model->data['WorkflowComment']['comment'];
}
// メールキュー作成
$model->CalendarEvent->saveQueue();
// キューからメール送信
MailSend::send();
}
$model->CalendarEvent->Behaviors->unload('Mails.IsMailSend');
$model->CalendarEvent->Behaviors->unload('Mails.MailQueue');
// すり替えものをリカバー
Current::$current['Room']['id'] = $originalRoomId;
Current::$current['Block']['id'] = $originalBlockId;
Current::$current['Block']['key'] = $originalBlockKey;
CalendarPermissiveRooms::recoverCurrentPermission();
}
/**
* _setDateTags
*
* @param Model $model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setDateTags(Model $model, $data) {
$view = new View();
$planHelper = $view->loadHelper('Calendars.CalendarPlan');
$startDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['CalendarEvent']['dtstart'], $data['CalendarEvent']['is_allday']);
$model->CalendarEvent->setAddEmbedTagValue('X-START_TIME', $startDate);
if ($data['CalendarEvent']['is_allday']) {
$endDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['CalendarEvent']['dtstart'], $data['CalendarEvent']['is_allday']);
} else {
$endDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['CalendarEvent']['dtend'], $data['CalendarEvent']['is_allday']);
}
$model->CalendarEvent->setAddEmbedTagValue('X-END_TIME', $endDate);
}
/**
* _setRruleTags
*
* @param Model $model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setRruleTags(Model $model, $data) {
$view = new View();
$rruleHelper = $view->loadHelper('Calendars.CalendarPlanRrule');
$rrule = $rruleHelper->getStringRrule($data['CalendarRrule']['rrule']);
if ($rrule != '') {
$rrule = str_replace(' ', ' ', $rrule);
$model->CalendarEvent->setAddEmbedTagValue('X-RRULE', htmlspecialchars_decode($rrule));
} else {
$model->CalendarEvent->setAddEmbedTagValue('X-RRULE', __d('calendars', 'nothing'));
}
}
/**
* _setUrlTags
*
* @param Model $model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setUrlTags(Model $model, $data) {
$url = NetCommonsUrl::actionUrl(array(
'plugin' => Current::read('Plugin.key'),
'controller' => 'calendar_plans',
'action' => 'view',
'block_id' => '',
'frame_id' => Current::read('Frame.id'),
'key' => $data['CalendarEvent']['key']
));
$url = NetCommonsUrl::url($url, true);
$model->CalendarEvent->setAddEmbedTagValue('X-URL', $url);
}
/**
* _setRoomTags
*
* @param Model $model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setRoomTags(Model $model, $data) {
if ($data['CalendarEvent']['room_id'] == Space::getRoomIdRoot(Space::COMMUNITY_SPACE_ID)) {
$model->CalendarEvent->setAddEmbedTagValue('X-ROOM', __d('calendars', 'All the members'));
}
}
}