Merge pull request #4893 from colemanw/INFRA-132
[civicrm-core.git] / CRM / SMS / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Choose include / exclude groups and mass sms
38 *
39 */
40 class CRM_SMS_Form_Group extends CRM_Contact_Form_Task {
41
42 /**
43 * Set variables up before form is built
44 *
45 * @return void
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 /**
57 * Set default values for the form.
58 * the default values are retrieved from the database
59 *
60 *
61 * @return void
62 */
63 public function setDefaultValues() {
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
84 $dao = new CRM_Mailing_DAO_MailingGroup();
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 /**
104 * Build the form object
105 *
106 * @return void
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.
121 $groups = CRM_Core_PseudoConstant::nestedGroup('Mailing');
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
132 $select2style = array(
133 'multiple' => TRUE,
134 'style' => 'width: 100%; max-width: 60em;',
135 'class' => 'crm-select2',
136 'placeholder' => ts('- select -'),
137 );
138
139 $this->add('select', 'includeGroups',
140 ts('Include Group(s)'),
141 $groups,
142 TRUE,
143 $select2style
144 );
145
146 $this->add('select', 'excludeGroups',
147 ts('Exclude Group(s)'),
148 $groups,
149 FALSE,
150 $select2style
151 );
152
153 $this->add('select', 'includeMailings',
154 ts('INCLUDE Recipients of These Message(s)'),
155 $mailings,
156 FALSE,
157 $select2style
158 );
159 $this->add('select', 'excludeMailings',
160 ts('EXCLUDE Recipients of These Message(s)'),
161 $mailings,
162 FALSE,
163 $select2style
164 );
165
166 $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule'));
167
168 $buttons = array(
169 array('type' => 'next',
170 'name' => ts('Next'),
171 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
172 'isDefault' => TRUE,
173 ),
174 array(
175 'type' => 'cancel',
176 'name' => ts('Cancel'),
177 ),
178 );
179
180 $this->addButtons($buttons);
181
182 $this->assign('groupCount', count($groups));
183 $this->assign('mailingCount', count($mailings));
184 }
185
186 public function postProcess() {
187 $values = $this->controller->exportValues($this->_name);
188
189 $groups = array();
190
191 foreach (array(
192 'name', 'group_id', 'is_sms') as $n) {
193 if (!empty($values[$n])) {
194 $params[$n] = $values[$n];
195 }
196 }
197
198 $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
199 $this->set('name', $params['name']);
200
201 $inGroups = $values['includeGroups'];
202 $outGroups = $values['excludeGroups'];
203 $inMailings = $values['includeMailings'];
204 $outMailings = $values['excludeMailings'];
205
206 if (is_array($inGroups)) {
207 foreach ($inGroups as $key => $id) {
208 if ($id) {
209 $groups['include'][] = $id;
210 }
211 }
212 }
213 if (is_array($outGroups)) {
214 foreach ($outGroups as $key => $id) {
215 if ($id) {
216 $groups['exclude'][] = $id;
217 }
218 }
219 }
220
221 $mailings = array();
222 if (is_array($inMailings)) {
223 foreach ($inMailings as $key => $id) {
224 if ($id) {
225 $mailings['include'][] = $id;
226 }
227 }
228 }
229 if (is_array($outMailings)) {
230 foreach ($outMailings as $key => $id) {
231 if ($id) {
232 $mailings['exclude'][] = $id;
233 }
234 }
235 }
236
237 $session = CRM_Core_Session::singleton();
238 $params['groups'] = $groups;
239 $params['mailings'] = $mailings;
240 $ids = array();
241 if ($this->get('mailing_id')) {
242
243 // don't create a new mass sms if already exists
244 $ids['mailing_id'] = $this->get('mailing_id');
245
246 $groupTableName = CRM_Contact_BAO_Group::getTableName();
247 $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
248
249 // delete previous includes/excludes, if mailing already existed
250 foreach (array(
251 'groups', 'mailings') as $entity) {
252 $mg = new CRM_Mailing_DAO_MailingGroup();
253 $mg->mailing_id = $ids['mailing_id'];
254 $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName;
255 $mg->find();
256 while ($mg->fetch()) {
257 $mg->delete();
258 }
259 }
260 }
261 else {
262 // new mailing, so lets set the created_id
263 $session = CRM_Core_Session::singleton();
264 $params['created_id'] = $session->get('userID');
265 $params['created_date'] = date('YmdHis');
266 }
267
268 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
269
270 $this->set('mailing_id', $mailing->id);
271
272 // also compute the recipients and store them in the mailing recipients table
273 CRM_Mailing_BAO_Mailing::getRecipients($mailing->id,
274 $mailing->id,
275 NULL,
276 NULL,
277 TRUE,
278 FALSE,
279 'sms'
280 );
281
282 $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
283 $this->set('count', $count);
284 $this->assign('count', $count);
285 $this->set('groups', $groups);
286 $this->set('mailings', $mailings);
287
288 if ($qf_Group_submit) {
289 $status = ts("Your Mass SMS has been saved.");
290 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
291 $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1');
292 return $this->controller->setDestination($url);
293 }
294 }
295
296 /**
297 * Display Name of the form
298 *
299 *
300 * @return string
301 */
302 public function getTitle() {
303 return ts('Select Recipients');
304 }
305
306 /**
307 * Global validation rules for the form
308 *
309 * @param array $fields
310 * Posted values of the form.
311 *
312 * @return array
313 * list of errors to be posted back to the form
314 * @static
315 */
316 public static function formRule($fields) {
317 $errors = array();
318 if (isset($fields['includeGroups']) &&
319 is_array($fields['includeGroups']) &&
320 isset($fields['excludeGroups']) &&
321 is_array($fields['excludeGroups'])
322 ) {
323 $checkGroups = array();
324 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
325 if (!empty($checkGroups)) {
326 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
327 }
328 }
329
330 if (isset($fields['includeMailings']) &&
331 is_array($fields['includeMailings']) &&
332 isset($fields['excludeMailings']) &&
333 is_array($fields['excludeMailings'])
334 ) {
335 $checkMailings = array();
336 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
337 if (!empty($checkMailings)) {
338 $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
339 }
340 }
341
342 return empty($errors) ? TRUE : $errors;
343 }
344 }