Fix for re-assign a case to another contact
[civicrm-core.git] / CRM / SMS / Form / Group.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Choose include / exclude groups and mass sms
38 *
39 */
40class CRM_SMS_Form_Group extends CRM_Contact_Form_Task {
41
42 /**
fe482240 43 * Set variables up before form is built.
6a488035
TO
44 *
45 * @return void
6a488035
TO
46 */
47 public function preProcess() {
48 if (!CRM_SMS_BAO_Provider::activeProviderCount()) {
49 CRM_Core_Error::fatal(ts('The <a href="%1">SMS Provider</a> has not been configured or is not active.', array(1 => CRM_Utils_System::url('civicrm/admin/sms/provider', 'reset=1'))));
50 }
51
52 $session = CRM_Core_Session::singleton();
53 $session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1'));
54 }
55
56 /**
c490a46a 57 * Set default values for the form.
6a488035
TO
58 * the default values are retrieved from the database
59 *
6a488035 60 *
355ba699 61 * @return void
6a488035 62 */
00be9182 63 public function setDefaultValues() {
6a488035
TO
64 $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
65 $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL);
66
67 $defaults = array();
68
69 if ($mailingID) {
70 $mailing = new CRM_Mailing_DAO_Mailing();
71 $mailing->id = $mailingID;
72 $mailing->addSelect('name');
73 $mailing->find(TRUE);
74
75 $defaults['name'] = $mailing->name;
76 if (!$continue) {
77 $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name));
78 }
79 else {
80 // CRM-7590, reuse same mailing ID if we are continuing
81 $this->set('mailing_id', $mailingID);
82 }
83
04124b30 84 $dao = new CRM_Mailing_DAO_MailingGroup();
6a488035
TO
85
86 $mailingGroups = array();
87 $dao->mailing_id = $mailingID;
88 $dao->find();
89 while ($dao->fetch()) {
90 $mailingGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id;
91 }
92
93 $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
94 $defaults['excludeGroups'] = CRM_Utils_Array::value('Exclude', $mailingGroups['civicrm_group']);
95
96 $defaults['includeMailings'] = CRM_Utils_Array::value('Include', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups));
97 $defaults['excludeMailings'] = CRM_Utils_Array::value('Exclude', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups));
98 }
99
100 return $defaults;
101 }
102
103 /**
fe482240 104 * Build the form object.
6a488035 105 *
355ba699 106 * @return void
6a488035
TO
107 */
108 public function buildQuickForm() {
109
110 //get the context
111 $context = $this->get('context');
112
113 $this->assign('context', $context);
114
115 $this->add('text', 'name', ts('Name Your SMS'),
116 CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
117 TRUE
118 );
119
120 //get the mailing groups.
24431f7b 121 $groups = CRM_Core_PseudoConstant::nestedGroup('Mailing');
6a488035
TO
122
123 //get the sms mailing list
124 $mailings = CRM_Mailing_PseudoConstant::completed('sms');
125 if (!$mailings) {
126 $mailings = array();
127 }
128
129 // run the groups through a hook so users can trim it if needed
130 CRM_Utils_Hook::mailingGroups($this, $groups, $mailings);
131
b14215fa
CW
132 $select2style = array(
133 'multiple' => TRUE,
134 'style' => 'width: 100%; max-width: 60em;',
135 'class' => 'crm-select2',
136 'placeholder' => ts('- select -'),
6a488035
TO
137 );
138
b14215fa
CW
139 $this->add('select', 'includeGroups',
140 ts('Include Group(s)'),
6a488035 141 $groups,
b14215fa
CW
142 TRUE,
143 $select2style
6a488035
TO
144 );
145
b14215fa
CW
146 $this->add('select', 'excludeGroups',
147 ts('Exclude Group(s)'),
148 $groups,
149 FALSE,
150 $select2style
151 );
6a488035 152
b14215fa
CW
153 $this->add('select', 'includeMailings',
154 ts('INCLUDE Recipients of These Message(s)'),
6a488035 155 $mailings,
b14215fa
CW
156 FALSE,
157 $select2style
6a488035 158 );
b14215fa
CW
159 $this->add('select', 'excludeMailings',
160 ts('EXCLUDE Recipients of These Message(s)'),
6a488035 161 $mailings,
b14215fa
CW
162 FALSE,
163 $select2style
6a488035
TO
164 );
165
6a488035
TO
166 $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule'));
167
168 $buttons = array(
6ea503d4
TO
169 array(
170 'type' => 'next',
f212d37d 171 'name' => ts('Next'),
6a488035
TO
172 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
173 'isDefault' => TRUE,
174 ),
175 array(
176 'type' => 'cancel',
177 'name' => ts('Cancel'),
178 ),
179 );
180
181 $this->addButtons($buttons);
182
183 $this->assign('groupCount', count($groups));
184 $this->assign('mailingCount', count($mailings));
185 }
186
187 public function postProcess() {
188 $values = $this->controller->exportValues($this->_name);
189
190 $groups = array();
191
192 foreach (array(
353ffa53
TO
193 'name',
194 'group_id',
389bcebf 195 'is_sms',
353ffa53 196 ) as $n) {
a7488080 197 if (!empty($values[$n])) {
6a488035
TO
198 $params[$n] = $values[$n];
199 }
200 }
201
202 $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
203 $this->set('name', $params['name']);
204
353ffa53
TO
205 $inGroups = $values['includeGroups'];
206 $outGroups = $values['excludeGroups'];
207 $inMailings = $values['includeMailings'];
6a488035
TO
208 $outMailings = $values['excludeMailings'];
209
210 if (is_array($inGroups)) {
211 foreach ($inGroups as $key => $id) {
212 if ($id) {
213 $groups['include'][] = $id;
214 }
215 }
216 }
217 if (is_array($outGroups)) {
218 foreach ($outGroups as $key => $id) {
219 if ($id) {
220 $groups['exclude'][] = $id;
221 }
222 }
223 }
224
225 $mailings = array();
226 if (is_array($inMailings)) {
227 foreach ($inMailings as $key => $id) {
228 if ($id) {
229 $mailings['include'][] = $id;
230 }
231 }
232 }
233 if (is_array($outMailings)) {
234 foreach ($outMailings as $key => $id) {
235 if ($id) {
236 $mailings['exclude'][] = $id;
237 }
238 }
239 }
240
353ffa53
TO
241 $session = CRM_Core_Session::singleton();
242 $params['groups'] = $groups;
6a488035 243 $params['mailings'] = $mailings;
353ffa53 244 $ids = array();
6a488035
TO
245 if ($this->get('mailing_id')) {
246
247 // don't create a new mass sms if already exists
248 $ids['mailing_id'] = $this->get('mailing_id');
249
250 $groupTableName = CRM_Contact_BAO_Group::getTableName();
251 $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
252
253 // delete previous includes/excludes, if mailing already existed
254 foreach (array(
353ffa53 255 'groups',
389bcebf 256 'mailings',
353ffa53
TO
257 ) as $entity) {
258 $mg = new CRM_Mailing_DAO_MailingGroup();
259 $mg->mailing_id = $ids['mailing_id'];
6a488035
TO
260 $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName;
261 $mg->find();
262 while ($mg->fetch()) {
263 $mg->delete();
264 }
265 }
266 }
267 else {
268 // new mailing, so lets set the created_id
269 $session = CRM_Core_Session::singleton();
270 $params['created_id'] = $session->get('userID');
271 $params['created_date'] = date('YmdHis');
272 }
273
274 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
275
276 $this->set('mailing_id', $mailing->id);
277
278 // also compute the recipients and store them in the mailing recipients table
279 CRM_Mailing_BAO_Mailing::getRecipients($mailing->id,
280 $mailing->id,
281 NULL,
282 NULL,
283 TRUE,
284 FALSE,
285 'sms'
286 );
287
288 $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
289 $this->set('count', $count);
290 $this->assign('count', $count);
291 $this->set('groups', $groups);
292 $this->set('mailings', $mailings);
293
294 if ($qf_Group_submit) {
295 $status = ts("Your Mass SMS has been saved.");
296 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
297 $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1');
298 return $this->controller->setDestination($url);
299 }
300 }
301
302 /**
fe482240 303 * Display Name of the form.
6a488035 304 *
6a488035
TO
305 *
306 * @return string
307 */
308 public function getTitle() {
309 return ts('Select Recipients');
310 }
311
312 /**
fe482240 313 * Global validation rules for the form.
6a488035 314 *
901b501a
TO
315 * @param array $fields
316 * Posted values of the form.
6a488035 317 *
a6c01b45
CW
318 * @return array
319 * list of errors to be posted back to the form
6a488035 320 */
00be9182 321 public static function formRule($fields) {
6a488035
TO
322 $errors = array();
323 if (isset($fields['includeGroups']) &&
324 is_array($fields['includeGroups']) &&
325 isset($fields['excludeGroups']) &&
326 is_array($fields['excludeGroups'])
327 ) {
328 $checkGroups = array();
329 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
330 if (!empty($checkGroups)) {
331 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
332 }
333 }
334
335 if (isset($fields['includeMailings']) &&
336 is_array($fields['includeMailings']) &&
337 isset($fields['excludeMailings']) &&
338 is_array($fields['excludeMailings'])
339 ) {
340 $checkMailings = array();
341 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
342 if (!empty($checkMailings)) {
343 $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
344 }
345 }
346
347 return empty($errors) ? TRUE : $errors;
348 }
96025800 349
6a488035 350}