1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class DraftNotificationsService |
7
|
|
|
*/ |
8
|
|
|
class DraftNotificationsService extends BaseApplicationComponent |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param EntryModel $draft |
13
|
|
|
* @param $status |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
6 |
|
public function sendNotifications(EntryModel $draft, $status) |
17
|
|
|
{ |
18
|
6 |
|
$recipients = $this->getNotificationRecipients(); |
19
|
|
|
|
20
|
6 |
|
if (!$this->isDraft($draft) || empty($recipients)) { |
21
|
2 |
|
return true; |
22
|
|
|
} |
23
|
|
|
|
24
|
4 |
|
$mailSubject = $this->getNotificationSubject($status); |
25
|
4 |
|
$mailBody = $this->getNotificationBody($draft, $status, $mailSubject); |
26
|
4 |
|
$mailSubject .= ' (' . $draft->getSection()->name . ' - ' . $draft->title . ')'; |
27
|
|
|
|
28
|
4 |
|
$email = new EmailModel(); |
29
|
4 |
|
$email->replyTo = craft()->userSession->getUser()->email; |
30
|
4 |
|
$email->subject = $mailSubject; |
31
|
4 |
|
$email->body = $mailBody; |
32
|
4 |
|
$email->htmlBody = nl2br($mailBody); |
33
|
|
|
|
34
|
4 |
|
foreach ($recipients as $recipient) { |
35
|
4 |
|
$email->toEmail = $recipient->email; |
36
|
4 |
|
craft()->email->sendEmail($email); |
37
|
4 |
|
} |
38
|
|
|
|
39
|
4 |
|
return $email; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param EntryModel $draft |
44
|
|
|
* @param string $status |
45
|
|
|
* @param string $subject |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
4 |
|
private function getNotificationBody(EntryModel $draft, $status, $subject) |
49
|
|
|
{ |
50
|
4 |
|
return craft()->templates->render('draftNotifications/_email.text.twig', array( |
51
|
4 |
|
'draft' => $draft, |
52
|
4 |
|
'status' => $status, |
53
|
4 |
|
'subject' => $subject, |
54
|
4 |
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $status |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
4 |
|
private function getNotificationSubject($status) |
62
|
|
|
{ |
63
|
|
|
switch ($status) { |
64
|
4 |
|
case DraftNotifications_EventCallbackService::STATUS_NEW_DRAFT: |
65
|
1 |
|
$message = Craft::t('A new draft has been saved'); |
66
|
1 |
|
break; |
67
|
3 |
|
case DraftNotifications_EventCallbackService::STATUS_EXISTING_DRAFT: |
68
|
1 |
|
$message = Craft::t('An existing draft has been saved'); |
69
|
1 |
|
break; |
70
|
2 |
|
case DraftNotifications_EventCallbackService::STATUS_DELETED_DRAFT: |
71
|
1 |
|
$message = Craft::t('A draft has been deleted'); |
72
|
1 |
|
break; |
73
|
1 |
|
default: |
74
|
1 |
|
$message = craft::t('A draft has been saved'); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
4 |
|
return $message; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* When users are not allowed to publish live changes any new entries they create will be disabled entries |
82
|
|
|
* which are not technically drafts |
83
|
|
|
* |
84
|
|
|
* @param EntryModel $entry |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
6 |
|
private function isDraft(EntryModel $entry) |
88
|
|
|
{ |
89
|
6 |
|
return $entry instanceof EntryDraftModel || $entry->getStatus() == 'disabled'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get all users from chiefEditor group except current user |
94
|
|
|
* @return UserModel[] |
95
|
|
|
*/ |
96
|
6 |
|
private function getNotificationRecipients() |
97
|
|
|
{ |
98
|
6 |
|
$userGroupHandles = craft()->plugins->getPlugin('draftNotifications')->getSettings()->userGroups; |
99
|
6 |
|
if (empty($userGroupHandles)) { |
100
|
|
|
return []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @var ElementCriteriaModel $userCriteria */ |
104
|
6 |
|
$userCriteria = craft()->elements->getCriteria('User'); |
105
|
6 |
|
$userCriteria->group = $userGroupHandles; |
106
|
6 |
|
$userCriteria->id = 'not ' . craft()->userSession->getUser()->id; |
107
|
6 |
|
return $userCriteria->find(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|