-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationMailBehavior.php
More file actions
212 lines (188 loc) · 7.31 KB
/
ReservationMailBehavior.php
File metadata and controls
212 lines (188 loc) · 7.31 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
<?php
/**
* ReservationMail 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('ReservationAppBehavior', 'Reservations.Model/Behavior');
App::uses('WorkflowComponent', 'Workflow.Controller/Component');
App::uses('ReservationPermissiveRooms', 'Reservations.Utility');
App::uses('ReservationPlan', 'Reservations.Helper');
App::uses('ReservationPlanRrule', 'Reservations.Helper');
/**
* ReservationMailBehavior
*
* @author Allcreator <info@allcreator.net>
* @package NetCommons\Reservations\Model\Behavior
*/
class ReservationMailBehavior extends ReservationAppBehavior {
/**
* 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',
'ReservationEvent' => 'Reservations.ReservationEvent',
'ReservationLocation' => 'Reservations.ReservationLocation'
]);
$model->ReservationEvent->Behaviors->load('Reservations.ReservationMailQueue');
// 指定されたイベント情報を取得
$data = $model->ReservationEvent->getEventById($eventId);
if (! $data) {
return;
}
$model->ReservationEvent->set($data);
$this->_setDateTags($model, $data);
$this->_setRruleTags($model, $data);
$this->_setUrlTags($model, $data);
$this->_setRoomTags($model, $data);
$reservationEvent = $data['ReservationEvent'];
$model->ReservationEvent->setAddEmbedTagValue('X-SUBJECT', $reservationEvent['title']);
$model->ReservationEvent->setAddEmbedTagValue('X-CONTACT', $reservationEvent['contact']);
// 施設名に置き換え
$location = $model->ReservationLocation->getByKey($data['ReservationEvent']['location_key']);
$model->ReservationEvent->setAddEmbedTagValue('X-LOCATION',
$location['ReservationLocation']['location_name']);
$model->ReservationEvent->setAddEmbedTagValue('X-BODY', $reservationEvent['description']);
// すり替え前にオリジナルルームID,オリジナルのBlockID,オリジナルのBlockKeyを確保
$originalRoomId = Current::read('Room.id');
$originalBlockId = Current::read('Block.id');
$originalBlockKey = Current::read('Block.key');
// 予定のルームID
$eventRoomId = $data['ReservationEvent']['room_id'];
$eventBlockId = $originalBlockId;
$eventBlockKey = $originalBlockKey;
$block = $model->Block->find('first', array(
'conditions' => array(
'plugin_key' => 'reservations',
'room_id' => $eventRoomId
)
));
if ($block) {
$eventBlockId = $block['Block']['id'];
$eventBlockKey = $block['Block']['key'];
}
// パーミッション情報をターゲットルームのものにすり替え
//ReservationPermissiveRooms::setCurrentPermission($eventRoomId);
// カレントのルームIDなどをすり替え
Current::$current['Room']['id'] = $eventRoomId;
Current::$current['Block']['id'] = $eventBlockId;
Current::$current['Block']['key'] = $eventBlockKey;
// プライベートのものの場合は自分と共有者に
if ($isMyPrivateRoom) {
$userIds = Hash::merge(
array(
Current::read('User.id'),
),
Hash::extract($data['ReservationEventShareUser'], '{n}.share_user')
);
$model->ReservationEvent->setSetting(MailQueueBehavior::MAIL_QUEUE_SETTING_USER_IDS, $userIds);
}
$model->ReservationEvent->Behaviors->load('Mails.IsMailSend',
array(
'keyField' => 'key',
MailQueueBehavior::MAIL_QUEUE_SETTING_IS_MAIL_SEND_POST => true,
));
$isMailSend = $model->ReservationEvent->isMailSend(
MailSettingFixedPhrase::DEFAULT_TYPE, $data['ReservationEvent']['key'], 'reservations');
if ($isMailSend) {
// メールキュー作成
$model->ReservationEvent->setSetting('workflowType', 'workflow');
$model->ReservationEvent->saveQueue();
MailSend::send();
//}
}
$model->ReservationEvent->Behaviors->unload('Mails.IsMailSend');
$model->ReservationEvent->Behaviors->unload('Reservations.ReservationMailQueue');
// すり替えものをリカバー
Current::$current['Room']['id'] = $originalRoomId;
Current::$current['Block']['id'] = $originalBlockId;
Current::$current['Block']['key'] = $originalBlockKey;
//ReservationPermissiveRooms::recoverCurrentPermission();
}
/**
* _setDateTags
*
* @param Model &$model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setDateTags(Model &$model, $data) {
$view = new View();
$planHelper = $view->loadHelper('Reservations.ReservationPlan');
$startDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['ReservationEvent']['dtstart'], $data['ReservationEvent']['is_allday']);
$model->ReservationEvent->setAddEmbedTagValue('X-START_TIME', $startDate);
if ($data['ReservationEvent']['is_allday']) {
$endDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['ReservationEvent']['dtstart'], $data['ReservationEvent']['is_allday']);
} else {
$endDate = $planHelper->makeDatetimeWithUserSiteTz(
$data['ReservationEvent']['dtend'], $data['ReservationEvent']['is_allday']);
}
$model->ReservationEvent->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('Reservations.ReservationPlanRrule');
$rrule = $rruleHelper->getStringRrule($data['ReservationRrule']['rrule']);
if ($rrule != '') {
$rrule = str_replace(' ', ' ', $rrule);
$model->ReservationEvent->setAddEmbedTagValue('X-RRULE', htmlspecialchars_decode($rrule));
} else {
$model->ReservationEvent->setAddEmbedTagValue('X-RRULE', __d('reservations', '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' => 'reservation_plans',
'action' => 'view',
'block_id' => '',
// メールを受け取ったユーザが必ずしもこのフレームにアクセスできるとは限らないのでFrameIdは入れない
//'frame_id' => Current::read('Frame.id'),
'key' => $data['ReservationEvent']['key']
));
$url = NetCommonsUrl::url($url, true);
$model->ReservationEvent->setAddEmbedTagValue('X-URL', $url);
}
/**
* _setRoomTags
*
* @param Model &$model モデル
* @param array $data 予定データ
* @return void
*/
protected function _setRoomTags(Model &$model, $data) {
if ($data['ReservationEvent']['room_id'] == Space::getRoomIdRoot(Space::COMMUNITY_SPACE_ID)) {
$model->ReservationEvent->setAddEmbedTagValue('X-ROOM', __d('reservations', 'All the members'));
}
}
}