-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIsMailSendBehavior.php
More file actions
367 lines (321 loc) · 12.6 KB
/
IsMailSendBehavior.php
File metadata and controls
367 lines (321 loc) · 12.6 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
<?php
/**
* メール送信する・しない Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('ModelBehavior', 'Model');
App::uses('MailQueueBehavior', 'Mails.Model/Behavior');
App::uses('MailSend', 'Mails.Utility');
App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior');
/**
* メール送信する・しない Behavior
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Mails\Model\Behavior
*/
class IsMailSendBehavior extends ModelBehavior {
/**
* setup
*
* @param Model $model モデル
* @param array $settings 設定値
* @return void
* @link http://book.cakephp.org/2.0/ja/models/behaviors.html#ModelBehavior::setup
*/
public function setup(Model $model, $settings = array()) {
$this->settings[$model->alias] = $settings;
$model->loadModels([
'MailSetting' => 'Mails.MailSetting',
'MailQueue' => 'Mails.MailQueue',
'MailQueueUser' => 'Mails.MailQueueUser',
'SiteSetting' => 'SiteManager.SiteSetting',
'BlockSetting' => 'Blocks.BlockSetting',
]);
}
/**
* 通常の投稿メールを送るかどうか
*
* @param Model $model モデル
* @param string $typeKey メールの種類
* @param string $contentKey コンテンツキー
* @param string $settingPluginKey 設定を取得するプラグインキー
* @return bool
*/
public function isMailSend(Model $model,
$typeKey = MailSettingFixedPhrase::DEFAULT_TYPE,
$contentKey = null,
$settingPluginKey = null) {
if (! $this->isMailSendCommon($model, $typeKey, $settingPluginKey)) {
return false;
}
// 投稿メールOFFなら、メール送らない
$isMailSendPost = Hash::get($this->settings,
$model->alias . '.' . MailQueueBehavior::MAIL_QUEUE_SETTING_IS_MAIL_SEND_POST);
if (isset($isMailSendPost) && $isMailSendPost == '0') {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// 承認コメントありなら、編集であっても通知メールを送る
$comment = Hash::get($model->data, 'WorkflowComment.comment');
if ($comment) {
return true;
}
// 公開許可あり(承認者、承認OFF時の一般)の編集 and 投稿メールフラグが未設定の場合、メール送らない
// 公開記事 編集フラグ
$isPublishableEdit = $this->isPublishableEdit($model, $contentKey);
if ($isPublishableEdit && $isMailSendPost === null) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
return true;
}
/**
* リマインダーメールを送るかどうか
*
* @param Model $model モデル
* @param string $typeKey メールの種類
* @param string $settingPluginKey 設定を取得するプラグインキー
* @return bool
*/
public function isMailSendReminder(Model $model,
$typeKey = MailSettingFixedPhrase::DEFAULT_TYPE,
$settingPluginKey = null) {
$useReminder = $this->settings[$model->alias]['reminder']['useReminder'];
if (! $useReminder) {
return false;
}
if (! $this->isMailSendCommon($model, $typeKey, $settingPluginKey)) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// リマインダーの公開以外はメール送らない
$status = Hash::get($model->data, $model->alias . '.status');
if ($status != WorkflowComponent::STATUS_PUBLISHED) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// リマインダーが複数日あって、全て日時が過ぎてたら、メール送らない
$isMailSendReminder = false;
$sendTimeReminders = $this->settings[$model->alias]['reminder']['sendTimes'];
foreach ($sendTimeReminders as $sendTime) {
if ($this->isMailSendTime($model, $sendTime)) {
$isMailSendReminder = true;
}
}
if (! $isMailSendReminder) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
return true;
}
/**
* 公開許可あり(承認者、承認OFF時の一般)の編集か ゲット
*
* @param Model $model モデル
* @param string $contentKey コンテンツキー
* @return bool
*/
public function isPublishableEdit(Model $model, $contentKey) {
$workflowType = Hash::get($this->settings, $model->alias . '.workflowType');
// --- コンテンツコメント
if ($workflowType == MailQueueBehavior::MAIL_QUEUE_WORKFLOW_TYPE_COMMENT) {
if (!Current::permission('content_comment_publishable')) {
// 公開権限なし
return false;
}
// 登録日時
$created = Hash::get($model->data, $model->alias . '.created');
$isApproveAction = Hash::get($this->settings, $model->alias . '.isCommentApproveAction');
if (isset($created)) {
// 新規登録
return false;
}
if ($isApproveAction) {
// 承認時
return false;
}
return true;
}
// --- 通常
if (!Current::permission('content_publishable')) {
// 公開権限なし
return false;
}
//$contentKey = $this->__getContentKey($model);
$keyField = $this->settings[$model->alias]['keyField'];
$conditions = array($model->alias . '.' . $keyField => $contentKey);
$result = $model->find('all', array(
'recursive' => -1,
'conditions' => $conditions,
'order' => array($model->alias . '.modified DESC'),
'callbacks' => false,
'limit' => 2,
));
if (count($result) <= 1) {
// 新規登録
return false;
}
// keyに対して2件以上記事がある = 編集
// 1つ前のコンテンツのステータス
$beforeStatus = $result[1][$model->alias]['status'];
$status = $result[0][$model->alias]['status'];
// 承認ONでもOFFでも、公開中の記事を編集して、公開だったら、公開の編集
// ・承認ONで、承認者が公開中の記事を編集しても、公開許可ありの編集で、メール送らない
// ・承認OFFで、公開中の記事を編集しても、公開許可ありの編集で、メール送らない
// ・・公開中の記事(1つ前の記事のstatus=1)
// ・・編集した記事が公開(status=1)
// ※承認ONで公開中の記事を編集して、編集した記事が公開なのは、承認者だけ
if ($beforeStatus == WorkflowComponent::STATUS_PUBLISHED &&
$status == WorkflowComponent::STATUS_PUBLISHED) {
// 公開の編集
return true;
}
// 公開以外の編集
return false;
}
/**
* メールを送るかどうか - 共通処理
*
* @param Model $model モデル
* @param string $typeKey メールの種類
* @param string $settingPluginKey 設定を取得するプラグインキー
* @return bool
*/
public function isMailSendCommon(Model $model,
$typeKey = MailSettingFixedPhrase::DEFAULT_TYPE,
$settingPluginKey = null) {
if ($settingPluginKey === null) {
$settingPluginKey = Current::read('Plugin.key');
}
$from = SiteSettingUtil::read('Mail.from');
// Fromが空ならメール未設定のため、メール送らない
if (empty($from)) {
//CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// cakeコマンドに実行権限なければ、メール送らない
if (!MailSend::isExecutableCake()) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
/** @see MailSetting::getMailSettingPlugin() */
$mailSettingPlugin = $model->MailSetting->getMailSettingPlugin(null, $typeKey, $settingPluginKey);
$isMailSend = Hash::get($mailSettingPlugin, 'MailSetting.is_mail_send');
$isMailSendApproval = Hash::get($mailSettingPlugin, 'MailSetting.is_mail_send_approval');
// プラグイン設定でメール通知 and 承認メール通知をどちらも使わないなら、メール送らない
if (!$isMailSend && !$isMailSendApproval) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
$status = Hash::get($model->data, $model->alias . '.status');
// 一時保存はメール送らない
if ($status == WorkflowComponent::STATUS_IN_DRAFT) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
$block = Current::read('Block');
// ブロック非公開、期間外はメール送らない
if (!$model->MailQueue->isSendBlockType($block, '')) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
return true;
}
/**
* メール送信日時で送るかどうか
*
* @param Model $model モデル
* @param date $sendTime メール送信日時
* @return bool
*/
public function isMailSendTime(Model $model, $sendTime) {
$useReminder = $this->settings[$model->alias]['reminder']['useReminder'];
/** @see MailQueue::isMailSendTime() */
return $model->MailQueue->isMailSendTime($sendTime, $useReminder);
}
/**
* 承認通知メールを送るかどうか
*
* @param Model $model モデル
* @param int $isMailSendApproval 承認メール通知機能を使うフラグ
* @param int $modifiedUserId 更新ユーザID
* @param string $pluginKey プラグインキー
* @return bool
*/
public function isSendMailQueueNotice(Model $model, $isMailSendApproval, $modifiedUserId,
$pluginKey) {
$workflowType = Hash::get($this->settings, $model->alias . '.workflowType');
if ($workflowType == MailQueueBehavior::MAIL_QUEUE_WORKFLOW_TYPE_WORKFLOW) {
// --- ワークフロー
// 承認しないなら、通知メール送らない
$fieldNameApproval = BlockSettingBehavior::FIELD_USE_WORKFLOW;
} elseif ($workflowType == MailQueueBehavior::MAIL_QUEUE_WORKFLOW_TYPE_COMMENT) {
// --- コンテンツコメント
// コメント承認しないなら、通知メール送らない
$fieldNameApproval = BlockSettingBehavior::FIELD_USE_COMMENT_APPROVAL;
}
/** @see BlockSetting::getBlockSettingValue() */
$useApproval = $model->BlockSetting->getBlockSettingValue($fieldNameApproval, $pluginKey);
if (! $useApproval) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// 承認メール使わないなら、通知メール送らない
if (! $isMailSendApproval) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// 承認コメントありなら、承認者であっても通知メールを送る
$comment = Hash::get($model->data, 'WorkflowComment.comment');
if ($comment) {
return true;
}
$permissionKey = $this->settings[$model->alias]['publishablePermissionKey'];
// 投稿者がルーム内の承認者だったら、通知メール送らない
/** @see MailQueueUser::getRolesRoomsUsersByPermission() */
$rolesRoomsUsers = $model->MailQueueUser->getRolesRoomsUsersByPermission($permissionKey);
$rolesRoomsUserIds = Hash::extract($rolesRoomsUsers, '{n}.RolesRoomsUser.user_id');
if (in_array($modifiedUserId, $rolesRoomsUserIds)) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
return true;
}
/**
* 公開メールを送るかどうか
*
* @param Model $model モデル
* @param string $isMailSend メール通知機能を使うフラグ
* @param string $contentKey コンテンツキー
* @return bool
*/
public function isSendMailQueuePublish(Model $model, $isMailSend, $contentKey) {
// メール送らないなら、公開メール送らない
if (! $isMailSend) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// 公開以外はメール送らない
$status = Hash::get($model->data, $model->alias . '.status');
if ($status != WorkflowComponent::STATUS_PUBLISHED) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
$isMailSendPost = Hash::get($this->settings,
$model->alias . '.' . MailQueueBehavior::MAIL_QUEUE_SETTING_IS_MAIL_SEND_POST);
// 公開メールだけども、編集時にもここを通るようになったので、編集チェックを追加
// ・公開許可あり(承認者、承認OFF時の一般)の編集 and 投稿メールフラグが未設定の場合、メール送らない
// ・公開記事 編集フラグ
$isPublishableEdit = $this->isPublishableEdit($model, $contentKey);
if ($isPublishableEdit && $isMailSendPost === null) {
CakeLog::debug('[' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
return true;
}
}