Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the common functionality for sending sms to | |
38 | * one or a group of contact ids. | |
39 | */ | |
40 | class CRM_Contact_Form_Task_SMSCommon { | |
41 | CONST RECIEVED_SMS_ACTIVITY_SUBJECT = "SMS Received"; | |
42 | ||
43 | public $_contactDetails = array(); | |
44 | ||
45 | public $_allContactDetails = array(); | |
46 | ||
47 | public $_toContactPhone = array(); | |
48 | ||
49 | ||
50 | static function preProcessProvider(&$form) { | |
51 | $form->_single = FALSE; | |
52 | $className = CRM_Utils_System::getClassName($form); | |
53 | ||
54 | if (property_exists($form, '_context') && | |
55 | $form->_context != 'search' && | |
56 | $className == 'CRM_Contact_Form_Task_SMS' | |
57 | ) { | |
58 | $form->_single = TRUE; | |
59 | } | |
60 | ||
61 | $providersCount = CRM_SMS_BAO_Provider::activeProviderCount(); | |
62 | ||
63 | if (!$providersCount) { | |
64 | CRM_Core_Error::statusBounce(ts('There are no SMS providers configured, or no SMS providers are set active')); | |
65 | } | |
66 | ||
67 | if ($className == 'CRM_Activity_Form_Task_SMS') { | |
68 | $activityCheck = 0; | |
69 | foreach ($form->_activityHolderIds as $value) { | |
70 | if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) { | |
71 | $activityCheck++; | |
72 | } | |
73 | } | |
74 | if ($activityCheck == count($form->_activityHolderIds)) { | |
75 | CRM_Core_Error::statusBounce(ts("The Reply SMS Could only be sent for activities with '%1' subject.", | |
76 | array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT) | |
77 | )); | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | /** | |
83 | * Build the form | |
84 | * | |
85 | * @access public | |
86 | * | |
87 | * @return void | |
88 | */ | |
89 | static function buildQuickForm(&$form) { | |
90 | ||
91 | $toArray = array(); | |
92 | ||
93 | $form->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR); | |
94 | ||
95 | $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc'); | |
96 | ||
97 | $providerSelect = array(); | |
98 | foreach ($providers as $provider) { | |
99 | $providerSelect[$provider['id']] = $provider['title']; | |
100 | } | |
101 | $suppressedSms = 0; | |
102 | //here we are getting logged in user id as array but we need target contact id. CRM-5988 | |
103 | $cid = $form->get('cid'); | |
104 | ||
105 | if ($cid) { | |
106 | $form->_contactIds = array($cid); | |
107 | } | |
108 | ||
109 | $to = $form->add('text', 'to', ts('To'), '', TRUE); | |
110 | $form->add('text', 'activity_subject', ts('Name The SMS'), '', TRUE); | |
111 | ||
112 | $toSetDefault = TRUE; | |
113 | if (property_exists($form, '_context') && $form->_context == 'standalone') { | |
114 | $toSetDefault = FALSE; | |
115 | } | |
116 | ||
117 | // when form is submitted recompute contactIds | |
118 | $allToSMS = array(); | |
119 | if ($to->getValue()) { | |
120 | $allToPhone = explode(',', $to->getValue()); | |
121 | ||
122 | $form->_contactIds = array(); | |
123 | foreach ($allToPhone as $value) { | |
124 | list($contactId, $phone) = explode('::', $value); | |
125 | if ($contactId) { | |
126 | $form->_contactIds[] = $contactId; | |
127 | $form->_toContactPhone[] = $phone; | |
128 | } | |
129 | } | |
130 | $toSetDefault = TRUE; | |
131 | } | |
132 | ||
133 | //get the group of contacts as per selected by user in case of Find Activities | |
134 | if (!empty($form->_activityHolderIds)) { | |
135 | $extendTargetContacts = 0; | |
136 | $invalidActivity = 0; | |
137 | $validActivities = 0; | |
138 | foreach ($form->_activityHolderIds as $key => $id) { | |
139 | //valid activity check | |
140 | if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $id, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) { | |
141 | $invalidActivity++; | |
142 | continue; | |
143 | } | |
144 | ||
e7e657f0 | 145 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 146 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
6a488035 | 147 | //target contacts limit check |
034500d4 | 148 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID)); |
6a488035 TO |
149 | |
150 | if (count($ids) > 1) { | |
151 | $extendTargetContacts++; | |
152 | continue; | |
153 | } | |
154 | $validActivities++; | |
155 | $form->_contactIds = empty($form->_contactIds) ? $ids : array_unique(array_merge($form->_contactIds, $ids)); | |
156 | } | |
157 | ||
158 | if (!$validActivities) { | |
159 | $errorMess = ""; | |
160 | if ($extendTargetContacts) { | |
161 | $errorMess = ts('One selected activity consists of more than one target contact.', array( | |
162 | 'count' => $extendTargetContacts, | |
163 | 'plural' => '%count selected activities consist of more than one target contact.')); | |
164 | } | |
165 | if ($invalidActivity) { | |
166 | $errorMess = ($errorMess ? ' ' : ''); | |
167 | $errorMess .= ts('The selected activity is invalid.', array( | |
168 | 'count' => $invalidActivity, | |
169 | 'plural' => '%count selected activities are invalid.')); | |
170 | } | |
171 | CRM_Core_Error::statusBounce(ts("%1: SMS Reply will not be sent.", array(1 => $errorMess))); | |
172 | } | |
173 | } | |
174 | ||
f53ea1ce | 175 | if (is_array($form->_contactIds) && !empty($form->_contactIds) && $toSetDefault) { |
6a488035 TO |
176 | $returnProperties = array( |
177 | 'sort_name' => 1, | |
178 | 'phone' => 1, | |
179 | 'do_not_sms' => 1, | |
180 | 'is_deceased' => 1, | |
181 | 'display_name' => 1, | |
182 | ); | |
183 | ||
184 | list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_contactIds, | |
185 | $returnProperties, | |
186 | FALSE, | |
187 | FALSE | |
188 | ); | |
189 | ||
190 | // make a copy of all contact details | |
191 | $form->_allContactDetails = $form->_contactDetails; | |
192 | ||
193 | foreach ($form->_contactIds as $key => $contactId) { | |
194 | $value = $form->_contactDetails[$contactId]; | |
195 | ||
196 | //to check if the phone type is "Mobile" | |
197 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); | |
198 | ||
199 | if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') { | |
200 | //to check for "if the contact id belongs to a specified activity type" | |
201 | $actDetails = CRM_Activity_BAO_Activity::getContactActivity($contactId); | |
202 | if (self::RECIEVED_SMS_ACTIVITY_SUBJECT != | |
203 | CRM_Utils_Array::retrieveValueRecursive($actDetails, 'subject') | |
204 | ) { | |
205 | $suppressedSms++; | |
206 | unset($form->_contactDetails[$contactId]); | |
207 | continue; | |
208 | } | |
209 | } | |
210 | ||
211 | if ((isset($value['phone_type_id']) && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes)) || $value['do_not_sms'] || empty($value['phone']) || CRM_Utils_Array::value('is_deceased', $value)) { | |
212 | ||
213 | //if phone is not primary check if non-primary phone is "Mobile" | |
214 | if (!empty($value['phone']) | |
215 | && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes) | |
216 | && !CRM_Utils_Array::value('is_deceased', $value) | |
217 | ) { | |
218 | $filter = array('do_not_sms' => 0); | |
219 | $contactPhones = CRM_Core_BAO_Phone::allPhones($contactId, FALSE, 'Mobile', $filter); | |
220 | if (count($contactPhones) > 0) { | |
221 | $mobilePhone = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'phone'); | |
222 | $form->_contactDetails[$contactId]['phone_id'] = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'id'); | |
223 | $form->_contactDetails[$contactId]['phone'] = $mobilePhone; | |
224 | $form->_contactDetails[$contactId]['phone_type_id'] = CRM_Utils_Array::value('Mobile', $phoneTypes); | |
225 | } | |
226 | else { | |
227 | $suppressedSms++; | |
228 | unset($form->_contactDetails[$contactId]); | |
229 | continue; | |
230 | } | |
231 | } | |
232 | else { | |
233 | $suppressedSms++; | |
234 | unset($form->_contactDetails[$contactId]); | |
235 | continue; | |
236 | } | |
237 | } | |
238 | ||
239 | if (isset($mobilePhone)) { | |
240 | $phone = $mobilePhone; | |
241 | } | |
242 | elseif (empty($form->_toContactPhone)) { | |
243 | $phone = $value['phone']; | |
244 | } | |
245 | else { | |
246 | $phone = CRM_Utils_Array::value($key, $form->_toContactPhone); | |
247 | } | |
248 | ||
249 | if ($phone) { | |
250 | $toArray[] = array( | |
251 | 'name' => '"' . $value['sort_name'] . '" <' . $phone . '>', | |
252 | 'id' => "$contactId::{$phone}", | |
253 | ); | |
254 | } | |
255 | } | |
256 | ||
257 | if (empty($toArray)) { | |
258 | CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid Phone, or communication preferences specify DO NOT SMS, or they are deceased')); | |
259 | } | |
260 | } | |
261 | ||
262 | //activity related variables | |
263 | if (isset($invalidActivity)) { | |
264 | $form->assign('invalidActivity', $invalidActivity); | |
265 | } | |
266 | if (isset($extendTargetContacts)) { | |
267 | $form->assign('extendTargetContacts', $extendTargetContacts); | |
268 | } | |
269 | ||
270 | ||
271 | $form->assign('toContact', json_encode($toArray)); | |
272 | $form->assign('suppressedSms', $suppressedSms); | |
273 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
274 | ||
275 | $form->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE); | |
276 | ||
277 | CRM_Mailing_BAO_Mailing::commonCompose($form); | |
278 | ||
279 | if ($form->_single) { | |
280 | // also fix the user context stack | |
281 | if ($form->_context) { | |
282 | $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
283 | } | |
284 | else { | |
285 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
286 | "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity" | |
287 | ); | |
288 | } | |
289 | ||
290 | $session = CRM_Core_Session::singleton(); | |
291 | $session->replaceUserContext($url); | |
292 | $form->addDefaultButtons(ts('Send SMS'), 'upload', 'cancel'); | |
293 | } | |
294 | else { | |
295 | $form->addDefaultButtons(ts('Send SMS'), 'upload'); | |
296 | } | |
297 | ||
298 | $form->addFormRule(array('CRM_Contact_Form_Task_SMSCommon', 'formRule'), $form); | |
299 | } | |
300 | ||
301 | /** | |
302 | * form rule | |
303 | * | |
304 | * @param array $fields the input form values | |
305 | * @param array $dontCare | |
306 | * @param array $self additional values form 'this' | |
307 | * | |
308 | * @return true if no errors, else array of errors | |
309 | * @access public | |
310 | * | |
311 | */ | |
312 | static function formRule($fields, $dontCare, $self) { | |
313 | $errors = array(); | |
314 | ||
315 | $template = CRM_Core_Smarty::singleton(); | |
316 | ||
a7488080 | 317 | if (empty($fields['text_message'])) { |
6a488035 TO |
318 | $errors['text_message'] = ts('Please provide Text message.'); |
319 | } | |
320 | else { | |
a7488080 | 321 | if (!empty($fields['text_message'])) { |
6a488035 TO |
322 | $messageCheck = CRM_Utils_Array::value('text_message', $fields); |
323 | $messageCheck = str_replace("\r\n", "\n", $messageCheck); | |
324 | if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) { | |
325 | $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)); | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | //Added for CRM-1393 | |
a7488080 | 331 | if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) { |
6a488035 TO |
332 | $errors['saveTemplateName'] = ts("Enter name to save message template"); |
333 | } | |
334 | ||
335 | return empty($errors) ? TRUE : $errors; | |
336 | } | |
337 | ||
338 | /** | |
339 | * process the form after the input has been submitted and validated | |
340 | * | |
341 | * @access public | |
342 | * | |
355ba699 | 343 | * @return void |
6a488035 TO |
344 | */ |
345 | static function postProcess(&$form) { | |
346 | ||
347 | // check and ensure that | |
348 | $thisValues = $form->controller->exportValues($form->getName()); | |
349 | ||
350 | $fromSmsProviderId = $thisValues['sms_provider_id']; | |
351 | ||
352 | // process message template | |
a7488080 | 353 | if (!empty($thisValues['saveTemplate']) || CRM_Utils_Array::value('updateTemplate', $thisValues) |
6a488035 TO |
354 | ) { |
355 | $messageTemplate = array( | |
356 | 'msg_text' => $thisValues['text_message'], | |
357 | 'is_active' => TRUE, | |
358 | ); | |
359 | ||
a7488080 | 360 | if (!empty($thisValues['saveTemplate'])) { |
6a488035 | 361 | $messageTemplate['msg_title'] = $thisValues['saveTemplateName']; |
c6327d7d | 362 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
363 | } |
364 | ||
a7488080 | 365 | if (!empty($thisValues['template']) && |
6a488035 TO |
366 | CRM_Utils_Array::value('updateTemplate', $thisValues) |
367 | ) { | |
368 | $messageTemplate['id'] = $thisValues['template']; | |
369 | unset($messageTemplate['msg_title']); | |
c6327d7d | 370 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
371 | } |
372 | } | |
373 | ||
374 | // format contact details array to handle multiple sms from same contact | |
375 | $formattedContactDetails = array(); | |
376 | $tempPhones = array(); | |
377 | ||
378 | foreach ($form->_contactIds as $key => $contactId) { | |
379 | $phone = $form->_toContactPhone[$key]; | |
380 | ||
381 | if ($phone) { | |
382 | $phoneKey = "{$contactId}::{$phone}"; | |
383 | if (!in_array($phoneKey, $tempPhones)) { | |
384 | $tempPhones[] = $phoneKey; | |
a7488080 | 385 | if (!empty($form->_contactDetails[$contactId])) { |
6a488035 TO |
386 | $formattedContactDetails[] = $form->_contactDetails[$contactId]; |
387 | } | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
392 | // $smsParams carries all the arguments provided on form (or via hooks), to the provider->send() method | |
393 | // this gives flexibity to the users / implementors to add their own args via hooks specific to their sms providers | |
394 | $smsParams = $thisValues; | |
395 | unset($smsParams['text_message']); | |
396 | $smsParams['provider_id'] = $fromSmsProviderId; | |
f53ea1ce DS |
397 | $contactIds = array_keys($form->_contactDetails); |
398 | $allContactIds = array_keys($form->_allContactDetails); | |
6a488035 | 399 | |
f53ea1ce | 400 | list($sent, $activityId, $countSuccess) = CRM_Activity_BAO_Activity::sendSMS($formattedContactDetails, |
6a488035 TO |
401 | $thisValues, |
402 | $smsParams, | |
f53ea1ce | 403 | $contactIds |
6a488035 TO |
404 | ); |
405 | ||
c5a6413b | 406 | if ($countSuccess > 0) { |
f53ea1ce | 407 | CRM_Core_Session::setStatus(ts('One message was sent successfully.', array('plural' => '%count messages were sent successfully.', 'count' => $countSuccess)), ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $countSuccess)), 'success'); |
6a488035 TO |
408 | } |
409 | ||
c5a6413b DS |
410 | if (is_array($sent)) { |
411 | // At least one PEAR_Error object was generated. | |
412 | // Display the error messages to the user. | |
413 | $status = '<ul>'; | |
414 | foreach ($sent as $errMsg) { | |
415 | $status .= '<li>' . $errMsg . '</li>'; | |
6a488035 | 416 | } |
c5a6413b DS |
417 | $status .= '</ul>'; |
418 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($sent), 'plural' => '%count Messages Not Sent')), 'info'); | |
419 | } else { | |
420 | //Display the name and number of contacts for those sms is not sent. | |
421 | $smsNotSent = array_diff_assoc($allContactIds, $contactIds); | |
422 | ||
423 | if (!empty($smsNotSent)) { | |
424 | $not_sent = array(); | |
425 | foreach ($smsNotSent as $index => $contactId) { | |
426 | $displayName = $form->_allContactDetails[$contactId]['display_name']; | |
427 | $phone = $form->_allContactDetails[$contactId]['phone']; | |
428 | $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId"); | |
429 | $not_sent[] = "<a href='$contactViewUrl' title='$phone'>$displayName</a>"; | |
430 | } | |
431 | $status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased'); | |
432 | if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') { | |
433 | $status .= ' ' . ts("or the contact is not part of the activity '%1'", array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT)); | |
434 | } | |
435 | $status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>'; | |
436 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($smsNotSent), 'plural' => '%count Messages Not Sent')), 'info'); | |
6a488035 | 437 | } |
6a488035 | 438 | } |
6a488035 TO |
439 | } |
440 | } | |
441 |