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