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