Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | * Function to set variables up before form is built | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | public function preProcess() { | |
49 | if (!CRM_SMS_BAO_Provider::activeProviderCount()) { | |
50 | 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')))); | |
51 | } | |
52 | ||
53 | $session = CRM_Core_Session::singleton(); | |
54 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1')); | |
55 | } | |
56 | ||
57 | /** | |
58 | * This function sets the default values for the form. | |
59 | * the default values are retrieved from the database | |
60 | * | |
61 | * @access public | |
62 | * | |
355ba699 | 63 | * @return void |
6a488035 TO |
64 | */ |
65 | function setDefaultValues() { | |
66 | $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL); | |
67 | $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL); | |
68 | ||
69 | $defaults = array(); | |
70 | ||
71 | if ($mailingID) { | |
72 | $mailing = new CRM_Mailing_DAO_Mailing(); | |
73 | $mailing->id = $mailingID; | |
74 | $mailing->addSelect('name'); | |
75 | $mailing->find(TRUE); | |
76 | ||
77 | $defaults['name'] = $mailing->name; | |
78 | if (!$continue) { | |
79 | $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name)); | |
80 | } | |
81 | else { | |
82 | // CRM-7590, reuse same mailing ID if we are continuing | |
83 | $this->set('mailing_id', $mailingID); | |
84 | } | |
85 | ||
04124b30 | 86 | $dao = new CRM_Mailing_DAO_MailingGroup(); |
6a488035 TO |
87 | |
88 | $mailingGroups = array(); | |
89 | $dao->mailing_id = $mailingID; | |
90 | $dao->find(); | |
91 | while ($dao->fetch()) { | |
92 | $mailingGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id; | |
93 | } | |
94 | ||
95 | $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include']; | |
96 | $defaults['excludeGroups'] = CRM_Utils_Array::value('Exclude', $mailingGroups['civicrm_group']); | |
97 | ||
98 | $defaults['includeMailings'] = CRM_Utils_Array::value('Include', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); | |
99 | $defaults['excludeMailings'] = CRM_Utils_Array::value('Exclude', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); | |
100 | } | |
101 | ||
102 | return $defaults; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Function to actually build the form | |
107 | * | |
355ba699 | 108 | * @return void |
6a488035 TO |
109 | * @access public |
110 | */ | |
111 | public function buildQuickForm() { | |
112 | ||
113 | //get the context | |
114 | $context = $this->get('context'); | |
115 | ||
116 | $this->assign('context', $context); | |
117 | ||
118 | $this->add('text', 'name', ts('Name Your SMS'), | |
119 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'name'), | |
120 | TRUE | |
121 | ); | |
122 | ||
123 | //get the mailing groups. | |
24431f7b | 124 | $groups = CRM_Core_PseudoConstant::nestedGroup('Mailing'); |
6a488035 TO |
125 | |
126 | //get the sms mailing list | |
127 | $mailings = CRM_Mailing_PseudoConstant::completed('sms'); | |
128 | if (!$mailings) { | |
129 | $mailings = array(); | |
130 | } | |
131 | ||
132 | // run the groups through a hook so users can trim it if needed | |
133 | CRM_Utils_Hook::mailingGroups($this, $groups, $mailings); | |
134 | ||
b14215fa CW |
135 | $select2style = array( |
136 | 'multiple' => TRUE, | |
137 | 'style' => 'width: 100%; max-width: 60em;', | |
138 | 'class' => 'crm-select2', | |
139 | 'placeholder' => ts('- select -'), | |
6a488035 TO |
140 | ); |
141 | ||
b14215fa CW |
142 | $this->add('select', 'includeGroups', |
143 | ts('Include Group(s)'), | |
6a488035 | 144 | $groups, |
b14215fa CW |
145 | TRUE, |
146 | $select2style | |
6a488035 TO |
147 | ); |
148 | ||
b14215fa CW |
149 | $this->add('select', 'excludeGroups', |
150 | ts('Exclude Group(s)'), | |
151 | $groups, | |
152 | FALSE, | |
153 | $select2style | |
154 | ); | |
6a488035 | 155 | |
b14215fa CW |
156 | $this->add('select', 'includeMailings', |
157 | ts('INCLUDE Recipients of These Message(s)'), | |
6a488035 | 158 | $mailings, |
b14215fa CW |
159 | FALSE, |
160 | $select2style | |
6a488035 | 161 | ); |
b14215fa CW |
162 | $this->add('select', 'excludeMailings', |
163 | ts('EXCLUDE Recipients of These Message(s)'), | |
6a488035 | 164 | $mailings, |
b14215fa CW |
165 | FALSE, |
166 | $select2style | |
6a488035 TO |
167 | ); |
168 | ||
6a488035 TO |
169 | $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule')); |
170 | ||
171 | $buttons = array( | |
172 | array('type' => 'next', | |
173 | 'name' => ts('Next >>'), | |
174 | 'spacing' => ' ', | |
175 | 'isDefault' => TRUE, | |
176 | ), | |
177 | array( | |
178 | 'type' => 'cancel', | |
179 | 'name' => ts('Cancel'), | |
180 | ), | |
181 | ); | |
182 | ||
183 | $this->addButtons($buttons); | |
184 | ||
185 | $this->assign('groupCount', count($groups)); | |
186 | $this->assign('mailingCount', count($mailings)); | |
187 | } | |
188 | ||
189 | public function postProcess() { | |
190 | $values = $this->controller->exportValues($this->_name); | |
191 | ||
192 | $groups = array(); | |
193 | ||
194 | foreach (array( | |
195 | 'name', 'group_id', 'is_sms') as $n) { | |
a7488080 | 196 | if (!empty($values[$n])) { |
6a488035 TO |
197 | $params[$n] = $values[$n]; |
198 | } | |
199 | } | |
200 | ||
201 | $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit'); | |
202 | $this->set('name', $params['name']); | |
203 | ||
204 | $inGroups = $values['includeGroups']; | |
205 | $outGroups = $values['excludeGroups']; | |
206 | $inMailings = $values['includeMailings']; | |
207 | $outMailings = $values['excludeMailings']; | |
208 | ||
209 | if (is_array($inGroups)) { | |
210 | foreach ($inGroups as $key => $id) { | |
211 | if ($id) { | |
212 | $groups['include'][] = $id; | |
213 | } | |
214 | } | |
215 | } | |
216 | if (is_array($outGroups)) { | |
217 | foreach ($outGroups as $key => $id) { | |
218 | if ($id) { | |
219 | $groups['exclude'][] = $id; | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | $mailings = array(); | |
225 | if (is_array($inMailings)) { | |
226 | foreach ($inMailings as $key => $id) { | |
227 | if ($id) { | |
228 | $mailings['include'][] = $id; | |
229 | } | |
230 | } | |
231 | } | |
232 | if (is_array($outMailings)) { | |
233 | foreach ($outMailings as $key => $id) { | |
234 | if ($id) { | |
235 | $mailings['exclude'][] = $id; | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | $session = CRM_Core_Session::singleton(); | |
241 | $params['groups'] = $groups; | |
242 | $params['mailings'] = $mailings; | |
243 | $ids = array(); | |
244 | if ($this->get('mailing_id')) { | |
245 | ||
246 | // don't create a new mass sms if already exists | |
247 | $ids['mailing_id'] = $this->get('mailing_id'); | |
248 | ||
249 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); | |
250 | $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); | |
251 | ||
252 | // delete previous includes/excludes, if mailing already existed | |
253 | foreach (array( | |
254 | 'groups', 'mailings') as $entity) { | |
04124b30 | 255 | $mg = new CRM_Mailing_DAO_MailingGroup(); |
6a488035 TO |
256 | $mg->mailing_id = $ids['mailing_id']; |
257 | $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName; | |
258 | $mg->find(); | |
259 | while ($mg->fetch()) { | |
260 | $mg->delete(); | |
261 | } | |
262 | } | |
263 | } | |
264 | else { | |
265 | // new mailing, so lets set the created_id | |
266 | $session = CRM_Core_Session::singleton(); | |
267 | $params['created_id'] = $session->get('userID'); | |
268 | $params['created_date'] = date('YmdHis'); | |
269 | } | |
270 | ||
271 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); | |
272 | ||
273 | $this->set('mailing_id', $mailing->id); | |
274 | ||
275 | // also compute the recipients and store them in the mailing recipients table | |
276 | CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, | |
277 | $mailing->id, | |
278 | NULL, | |
279 | NULL, | |
280 | TRUE, | |
281 | FALSE, | |
282 | 'sms' | |
283 | ); | |
284 | ||
285 | $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id); | |
286 | $this->set('count', $count); | |
287 | $this->assign('count', $count); | |
288 | $this->set('groups', $groups); | |
289 | $this->set('mailings', $mailings); | |
290 | ||
291 | if ($qf_Group_submit) { | |
292 | $status = ts("Your Mass SMS has been saved."); | |
293 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
294 | $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1'); | |
295 | return $this->controller->setDestination($url); | |
296 | } | |
297 | } | |
298 | ||
299 | /** | |
300 | * Display Name of the form | |
301 | * | |
302 | * @access public | |
303 | * | |
304 | * @return string | |
305 | */ | |
306 | public function getTitle() { | |
307 | return ts('Select Recipients'); | |
308 | } | |
309 | ||
310 | /** | |
311 | * global validation rules for the form | |
312 | * | |
313 | * @param array $fields posted values of the form | |
314 | * | |
315 | * @return array list of errors to be posted back to the form | |
316 | * @static | |
317 | * @access public | |
318 | */ | |
319 | static function formRule($fields) { | |
320 | $errors = array(); | |
321 | if (isset($fields['includeGroups']) && | |
322 | is_array($fields['includeGroups']) && | |
323 | isset($fields['excludeGroups']) && | |
324 | is_array($fields['excludeGroups']) | |
325 | ) { | |
326 | $checkGroups = array(); | |
327 | $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']); | |
328 | if (!empty($checkGroups)) { | |
329 | $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).'); | |
330 | } | |
331 | } | |
332 | ||
333 | if (isset($fields['includeMailings']) && | |
334 | is_array($fields['includeMailings']) && | |
335 | isset($fields['excludeMailings']) && | |
336 | is_array($fields['excludeMailings']) | |
337 | ) { | |
338 | $checkMailings = array(); | |
339 | $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']); | |
340 | if (!empty($checkMailings)) { | |
341 | $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).'); | |
342 | } | |
343 | } | |
344 | ||
345 | return empty($errors) ? TRUE : $errors; | |
346 | } | |
347 | } | |
348 |