-
Notifications
You must be signed in to change notification settings - Fork 452
/
message.php
158 lines (131 loc) · 4.29 KB
/
message.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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\Users\Groups;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Page\Page;
use Concrete\Core\Routing\Redirect;
use Concrete\Core\User\Group\Group;
use Concrete\Core\User\Group\GroupList;
use Concrete\Core\User\PrivateMessage\Limit;
use Concrete\Core\User\User;
use Concrete\Core\User\UserInfo;
use Concrete\Core\User\UserInfoRepository;
use Concrete\Core\Validation\CSRF\Token;
class Message extends DashboardPageController
{
/**
* ID used to send to all groups.
*/
const ALL_GROUPS_ID = -2;
/**
* @var \Concrete\Core\User\UserInfoRepository
*/
private $repository;
public function __construct(Page $c, UserInfoRepository $repository)
{
parent::__construct($c);
$this->repository = $repository;
}
/**
* Main endpoint for this singlepage.
*/
public function view()
{
// Initialize groups array with an option to send to all groups
$groups = [self::ALL_GROUPS_ID => t('All Groups')];
$groupList = new GroupList();
foreach ($groupList->getResults() as $group) {
/** @var \Concrete\Core\User\Group\Group $group */
$groups[$group->getGroupID()] = $group->getGroupDisplayName();
}
$this->set('groups', $groups);
$this->set('token', new Token());
}
/**
* Handle actually sending out the emails.
*
* @return \Concrete\Core\Routing\RedirectResponse|void
*/
public function process()
{
$subject = trim($this->post('subject'));
$body = trim($this->post('message'));
// Do some quick validation
if (!$this->token->validate('send_message')) {
$this->error->add(t('Invalid CSRF token. Please refresh and try again.'));
return $this->view();
}
if (!$subject) {
$this->error->add(t('Subject cannot be empty.'));
}
if (!$body) {
$this->error->add(t('Message cannot be empty.'));
}
// Resolve the recipients
$recipients = $this->getRecipients();
// Make sure we have at least one recipient
if ($recipients !== null && !$recipients) {
$this->error->add(t('Message not sent. Group has no members.'));
}
// If we have errors, just return
if ($this->error->has()) {
return $this->view();
}
// Send out the message
$this->sendMessage($subject, $body, $recipients, $this->getSender());
$this->flash('success', t('Message Successfully sent.'));
return Redirect::to('/dashboard/users/groups/message');
}
/**
* Send a message to a list of recipients with limit disabled.
*
* @param $subject
* @param $body
* @param \Concrete\Core\User\UserInfo[] $recipients
* @param \Concrete\Core\User\UserInfo $sender
*/
protected function sendMessage($subject, $body, array $recipients, UserInfo $sender)
{
// Ignore limit instead of queueing
// @TODO Queue private messages rather than ignoring the limit
Limit::setEnabled(false);
foreach ($recipients as $member) {
$sender->sendPrivateMessage($member, $subject, $body);
}
// Reenable limit
Limit::setEnabled();
}
/**
* Get the user that should be the sender.
*
* @return \Concrete\Core\User\UserInfo|null
*/
protected function getSender()
{
$u = $this->app->make(User::class);
$sender = $u->getUserInfoObject();
if (!$sender) {
throw new \RuntimeException('User must be logged in.');
}
return $sender;
}
/**
* Get the recipients.
*
* @return UserInfo[]
*/
protected function getRecipients()
{
$groupId = (int) $this->post('group');
// If the groupID is set to the "All" value, just get all active users
if ($groupId === self::ALL_GROUPS_ID) {
return $this->repository->all(true);
}
// Otherwise resolve the users from the group
$group = Group::getByID($groupId);
if (!$group) {
$this->error->add(t('Group not found.'));
return null;
}
return $group->getGroupMembers();
}
}