/** * AllPhones() method - get all Phones for our contact, with primary Phone first */ public function testAllPhones() { $contactParams = array('first_name' => 'Alan', 'last_name' => 'Smith', 'phone-1' => '(415) 222-1011 x 221', 'phone-2' => '(415) 222-5432'); $contactId = Contact::createIndividual($contactParams); $Phones = CRM_Core_BAO_Phone::allPhones($contactId); $this->assertEquals(count($Phones), 2, 'Checking number of returned Phones.'); $firstPhoneValue = array_slice($Phones, 0, 1); // Since we're not passing in a location type to createIndividual above, CRM_Contact_BAO_Contact::createProfileContact uses default location // type for first phone and sets that to primary. $this->assertEquals('(415) 222-1011 x 221', $firstPhoneValue[0]['phone'], "Confirm primary Phone value ( {$firstPhoneValue[0]['phone']} )."); $this->assertEquals(1, $firstPhoneValue[0]['is_primary'], 'Confirm first Phone is primary.'); Contact::delete($contactId); }
static function relatedContactInfo($contactId) { $return = array(); if (empty($contactId)) { return $return; } $phone = CRM_Core_BAO_Phone::allPhones($contactId, TRUE, NULL, array('is_primary' => 1)); $contactSubType = CRM_Contact_BAO_Contact::getContactSubType($contactId); $contactType = CRM_Contact_BAO_ContactType::getLabel($contactSubType[0]); $gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'PCP_Supporter_Profile', 'id', 'name'); $updateURL = CRM_Utils_System::url('civicrm/profile/edit', "reset=1&gid={$gid}&cid={$contactId}"); // $updateURL =CRM_Utils_System::url('civicrm/pcp/support', "action=browse&component=event&id={$params['pcpId']}"); $return = array('name' => CRM_Contact_BAO_Contact::displayName($contactId), 'type' => $contactType, 'email' => CRM_Contact_BAO_Contact::getPrimaryEmail($contactId), 'phone' => !empty($phone) && isset($phone[1]['phone']) ? $phone[1]['phone'] : NULL, 'action' => "<a href={$updateURL}>Update Contact Information</a>"); return $return; }
/** * Build the form object. * * * @param CRM_Core_Form $form * * @return void */ public static function buildQuickForm(&$form) { $toArray = array(); $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc'); $providerSelect = array(); foreach ($providers as $provider) { $providerSelect[$provider['id']] = $provider['title']; } $suppressedSms = 0; //here we are getting logged in user id as array but we need target contact id. CRM-5988 $cid = $form->get('cid'); if ($cid) { $form->_contactIds = array($cid); } $to = $form->add('text', 'to', ts('To'), array('class' => 'huge'), TRUE); $form->add('text', 'activity_subject', ts('Name The SMS'), array('class' => 'huge'), TRUE); $toSetDefault = TRUE; if (property_exists($form, '_context') && $form->_context == 'standalone') { $toSetDefault = FALSE; } // when form is submitted recompute contactIds $allToSMS = array(); if ($to->getValue()) { $allToPhone = explode(',', $to->getValue()); $form->_contactIds = array(); foreach ($allToPhone as $value) { list($contactId, $phone) = explode('::', $value); if ($contactId) { $form->_contactIds[] = $contactId; $form->_toContactPhone[] = $phone; } } $toSetDefault = TRUE; } //get the group of contacts as per selected by user in case of Find Activities if (!empty($form->_activityHolderIds)) { $extendTargetContacts = 0; $invalidActivity = 0; $validActivities = 0; foreach ($form->_activityHolderIds as $key => $id) { //valid activity check if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $id, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) { $invalidActivity++; continue; } $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); //target contacts limit check $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID)); if (count($ids) > 1) { $extendTargetContacts++; continue; } $validActivities++; $form->_contactIds = empty($form->_contactIds) ? $ids : array_unique(array_merge($form->_contactIds, $ids)); } if (!$validActivities) { $errorMess = ""; if ($extendTargetContacts) { $errorMess = ts('One selected activity consists of more than one target contact.', array('count' => $extendTargetContacts, 'plural' => '%count selected activities consist of more than one target contact.')); } if ($invalidActivity) { $errorMess = $errorMess ? ' ' : ''; $errorMess .= ts('The selected activity is invalid.', array('count' => $invalidActivity, 'plural' => '%count selected activities are invalid.')); } CRM_Core_Error::statusBounce(ts("%1: SMS Reply will not be sent.", array(1 => $errorMess))); } } if (is_array($form->_contactIds) && !empty($form->_contactIds) && $toSetDefault) { $returnProperties = array('sort_name' => 1, 'phone' => 1, 'do_not_sms' => 1, 'is_deceased' => 1, 'display_name' => 1); list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_contactIds, $returnProperties, FALSE, FALSE); // make a copy of all contact details $form->_allContactDetails = $form->_contactDetails; foreach ($form->_contactIds as $key => $contactId) { $value = $form->_contactDetails[$contactId]; //to check if the phone type is "Mobile" $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') { //to check for "if the contact id belongs to a specified activity type" $actDetails = CRM_Activity_BAO_Activity::getContactActivity($contactId); if (self::RECIEVED_SMS_ACTIVITY_SUBJECT != CRM_Utils_Array::retrieveValueRecursive($actDetails, 'subject')) { $suppressedSms++; unset($form->_contactDetails[$contactId]); continue; } } 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'])) { //if phone is not primary check if non-primary phone is "Mobile" if (!empty($value['phone']) && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes) && empty($value['is_deceased'])) { $filter = array('do_not_sms' => 0); $contactPhones = CRM_Core_BAO_Phone::allPhones($contactId, FALSE, 'Mobile', $filter); if (count($contactPhones) > 0) { $mobilePhone = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'phone'); $form->_contactDetails[$contactId]['phone_id'] = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'id'); $form->_contactDetails[$contactId]['phone'] = $mobilePhone; $form->_contactDetails[$contactId]['phone_type_id'] = CRM_Utils_Array::value('Mobile', $phoneTypes); } else { $suppressedSms++; unset($form->_contactDetails[$contactId]); continue; } } else { $suppressedSms++; unset($form->_contactDetails[$contactId]); continue; } } if (isset($mobilePhone)) { $phone = $mobilePhone; } elseif (empty($form->_toContactPhone)) { $phone = $value['phone']; } else { $phone = CRM_Utils_Array::value($key, $form->_toContactPhone); } if ($phone) { $toArray[] = array('text' => '"' . $value['sort_name'] . '" (' . $phone . ')', 'id' => "{$contactId}::{$phone}"); } } if (empty($toArray)) { 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')); } } //activity related variables if (isset($invalidActivity)) { $form->assign('invalidActivity', $invalidActivity); } if (isset($extendTargetContacts)) { $form->assign('extendTargetContacts', $extendTargetContacts); } $form->assign('toContact', json_encode($toArray)); $form->assign('suppressedSms', $suppressedSms); $form->assign('totalSelectedContacts', count($form->_contactIds)); $form->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE); CRM_Mailing_BAO_Mailing::commonCompose($form); if ($form->_single) { // also fix the user context stack if ($form->_context) { $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); } else { $url = CRM_Utils_System::url('civicrm/contact/view', "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity"); } $session = CRM_Core_Session::singleton(); $session->replaceUserContext($url); $form->addDefaultButtons(ts('Send SMS'), 'upload', 'cancel'); } else { $form->addDefaultButtons(ts('Send SMS'), 'upload'); } $form->addFormRule(array('CRM_Contact_Form_Task_SMSCommon', 'formRule'), $form); }
/** * Send the sms message to a specific contact. * * @param int $toID * The contact id of the recipient. * @param $tokenText * @param array $smsParams * The params used for sending sms. * @param int $activityID * The activity ID that tracks the message. * @param int $userID * * @return bool|PEAR_Error * true on success or PEAR_Error object */ public static function sendSMSMessage($toID, &$tokenText, $smsParams = array(), $activityID, $userID = NULL) { $toDoNotSms = ""; $toPhoneNumber = ""; if ($smsParams['To']) { $toPhoneNumber = trim($smsParams['To']); } elseif ($toID) { $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0); $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters); // To get primary mobile phonenumber,if not get the first mobile phonenumber if (!empty($toPhoneNumbers)) { $toPhoneNumerDetails = reset($toPhoneNumbers); $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumerDetails); // Contact allows to send sms $toDoNotSms = 0; } } // make sure both phone are valid // and that the recipient wants to receive sms if (empty($toPhoneNumber) or $toDoNotSms) { return PEAR::raiseError('Recipient phone number is invalid or recipient does not want to receive SMS', NULL, PEAR_ERROR_RETURN); } $recipient = $smsParams['To']; $smsParams['contact_id'] = $toID; $smsParams['parent_activity_id'] = $activityID; $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id'])); $sendResult = $providerObj->send($recipient, $smsParams, $tokenText, NULL, $userID); if (PEAR::isError($sendResult)) { return $sendResult; } $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); // add activity target record for every sms that is send $activityTargetParams = array('activity_id' => $activityID, 'contact_id' => $toID, 'record_type_id' => $targetID); CRM_Activity_BAO_ActivityContact::create($activityTargetParams); return TRUE; }
/** * @param $mappingID * @param $now * * @throws CRM_Core_Exception */ static function sendMailings($mappingID, $now) { $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); $fromEmailAddress = "{$domainValues['0']} <{$domainValues['1']}>"; $mapping = new CRM_Core_DAO_ActionMapping(); $mapping->id = $mappingID; $mapping->find(TRUE); $actionSchedule = new CRM_Core_DAO_ActionSchedule(); $actionSchedule->mapping_id = $mappingID; $actionSchedule->is_active = 1; $actionSchedule->find(FALSE); $tokenFields = array(); $session = CRM_Core_Session::singleton(); while ($actionSchedule->fetch()) { $extraSelect = $extraJoin = $extraWhere = $extraOn = ''; if ($actionSchedule->from_email) { $fromEmailAddress = "{$actionSchedule->from_name} <{$actionSchedule->from_email}>"; } if ($actionSchedule->record_activity) { if ($mapping->entity == 'civicrm_membership') { $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Membership Renewal Reminder', 'name'); } else { $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Reminder Sent', 'name'); } $activityStatusID = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'); } if ($mapping->entity == 'civicrm_activity') { $tokenEntity = 'activity'; $tokenFields = array('activity_id', 'activity_type', 'subject', 'details', 'activity_date_time'); $extraSelect = ', ov.label as activity_type, e.id as activity_id'; $extraJoin = "\nINNER JOIN civicrm_option_group og ON og.name = 'activity_type'\nINNER JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id"; $extraOn = ' AND e.is_current_revision = 1 AND e.is_deleted = 0 '; if ($actionSchedule->limit_to == 0) { $extraJoin = "\nLEFT JOIN civicrm_option_group og ON og.name = 'activity_type'\nLEFT JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id"; } } if ($mapping->entity == 'civicrm_participant') { $tokenEntity = 'event'; $tokenFields = array('event_type', 'title', 'event_id', 'start_date', 'end_date', 'summary', 'description', 'location', 'info_url', 'registration_url', 'fee_amount', 'contact_email', 'contact_phone', 'balance'); $extraSelect = ', ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone '; $extraJoin = "\nINNER JOIN civicrm_event ev ON e.event_id = ev.id\nINNER JOIN civicrm_option_group og ON og.name = 'event_type'\nINNER JOIN civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id\nLEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id\nLEFT JOIN civicrm_address address ON address.id = lb.address_id\nLEFT JOIN civicrm_email email ON email.id = lb.email_id\nLEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id\n"; if ($actionSchedule->limit_to == 0) { $extraJoin = "\nLEFT JOIN civicrm_event ev ON e.event_id = ev.id\nLEFT JOIN civicrm_option_group og ON og.name = 'event_type'\nLEFT JOIN civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id\nLEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id\nLEFT JOIN civicrm_address address ON address.id = lb.address_id\nLEFT JOIN civicrm_email email ON email.id = lb.email_id\nLEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id\n"; } } if ($mapping->entity == 'civicrm_membership') { $tokenEntity = 'membership'; $tokenFields = array('fee', 'id', 'join_date', 'start_date', 'end_date', 'status', 'type'); $extraSelect = ', mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, ms.name as status, mt.name as type'; $extraJoin = ' INNER JOIN civicrm_membership_type mt ON e.membership_type_id = mt.id INNER JOIN civicrm_membership_status ms ON e.status_id = ms.id'; if ($actionSchedule->limit_to == 0) { $extraJoin = ' LEFT JOIN civicrm_membership_type mt ON e.membership_type_id = mt.id LEFT JOIN civicrm_membership_status ms ON e.status_id = ms.id'; } } if ($mapping->entity == 'civicrm_contact') { $tokenEntity = 'contact'; //TODO: get full list somewhere! $tokenFields = array('birth_date', 'last_name'); //TODO: is there anything to add here? } $entityJoinClause = "INNER JOIN {$mapping->entity} e ON e.id = reminder.entity_id"; if ($actionSchedule->limit_to == 0) { $entityJoinClause = "LEFT JOIN {$mapping->entity} e ON e.id = reminder.entity_id"; $extraWhere .= " AND (e.id = reminder.entity_id OR reminder.entity_table = 'civicrm_contact')"; } $entityJoinClause .= $extraOn; $query = "\nSELECT reminder.id as reminderID, reminder.contact_id as contactID, reminder.*, e.id as entityID, e.* {$extraSelect}\nFROM civicrm_action_log reminder\n{$entityJoinClause}\n{$extraJoin}\nWHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL\n{$extraWhere}"; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer'))); while ($dao->fetch()) { $entityTokenParams = array(); foreach ($tokenFields as $field) { if ($field == 'location') { $loc = array(); $stateProvince = CRM_Core_PseudoConstant::stateProvince(); $loc['street_address'] = $dao->street_address; $loc['city'] = $dao->city; $loc['state_province'] = CRM_Utils_Array::value($dao->state_province_id, $stateProvince); $loc['postal_code'] = $dao->postal_code; $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Address::format($loc); } elseif ($field == 'info_url') { $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); } elseif ($field == 'registration_url') { $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); } elseif (in_array($field, array('start_date', 'end_date', 'join_date', 'activity_date_time'))) { $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Date::customFormat($dao->{$field}); } elseif ($field == 'balance') { $info = CRM_Contribute_BAO_Contribution::getPaymentInfo($dao->entityID, 'event'); $balancePay = CRM_Utils_Array::value('balance', $info); $balancePay = CRM_Utils_Money::format($balancePay); $entityTokenParams["{$tokenEntity}." . $field] = $balancePay; } elseif ($field == 'fee_amount') { $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Money::format($dao->{$field}); } else { $entityTokenParams["{$tokenEntity}." . $field] = $dao->{$field}; } } $isError = 0; $errorMsg = $toEmail = $toPhoneNumber = ''; if ($actionSchedule->mode == 'SMS' or $actionSchedule->mode == 'User_Preference') { $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0); $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($dao->contactID, FALSE, 'Mobile', $filters); //to get primary mobile ph,if not get a first mobile phONE if (!empty($toPhoneNumbers)) { $toPhoneNumberDetails = reset($toPhoneNumbers); $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumberDetails); //contact allows to send sms $toDoNotSms = 0; } } if ($actionSchedule->mode == 'Email' or $actionSchedule->mode == 'User_Preference') { $toEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($dao->contactID); } if ($toEmail || !(empty($toPhoneNumber) or $toDoNotSms)) { $to['email'] = $toEmail; $to['phone'] = $toPhoneNumber; $result = CRM_Core_BAO_ActionSchedule::sendReminder($dao->contactID, $to, $actionSchedule->id, $fromEmailAddress, $entityTokenParams); if (!$result || is_a($result, 'PEAR_Error')) { // we could not send an email, for now we ignore, CRM-3406 $isError = 1; } } else { $isError = 1; $errorMsg = "Couldn\\'t find recipient\\'s email address."; } // update action log record $logParams = array('id' => $dao->reminderID, 'is_error' => $isError, 'message' => $errorMsg ? $errorMsg : "null", 'action_date_time' => $now); CRM_Core_BAO_ActionLog::create($logParams); // insert activity log record if needed if ($actionSchedule->record_activity) { $activityParams = array('subject' => $actionSchedule->title, 'details' => $actionSchedule->body_html, 'source_contact_id' => $session->get('userID') ? $session->get('userID') : $dao->contactID, 'target_contact_id' => $dao->contactID, 'activity_date_time' => date('YmdHis'), 'status_id' => $activityStatusID, 'activity_type_id' => $activityTypeID, 'source_record_id' => $dao->entityID); $activity = CRM_Activity_BAO_Activity::create($activityParams); } } $dao->free(); } }
/** * Pick SMS phone number. * * @param int $smsToContactId * * @return NULL|string */ protected static function pickSmsPhoneNumber($smsToContactId) { $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($smsToContactId, FALSE, 'Mobile', array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0)); //to get primary mobile ph,if not get a first mobile phONE if (!empty($toPhoneNumbers)) { $toPhoneNumberDetails = reset($toPhoneNumbers); $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumberDetails); return $toPhoneNumber; } return NULL; }
/** * send the sms message to a specific contact * * @param int $toID the contact id of the recipient * @param int $activityID the activity ID that tracks the message * @param array $smsParams the params used for sending sms * * @return boolean true if successfull else false. * @access public * @static */ static function sendSMSMessage($toID, &$tokenText, &$tokenHtml, $smsParams = array(), $activityID) { $toDoNotSms = ""; $toPhoneNumber = ""; if ($smsParams['To']) { $toPhoneNumber = trim($smsParams['To']); } elseif ($toID) { $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0); $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters); //to get primary mobile ph,if not get a first mobile ph if (!empty($toPhoneNumbers)) { $toPhoneNumerDetails = reset($toPhoneNumbers); $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumerDetails); //contact allows to send sms $toDoNotSms = 0; } } // make sure both phone are valid // and that the recipient wants to receive sms if (empty($toPhoneNumber) or $toDoNotSms) { return FALSE; } $message = $tokenHtml ? $tokenHtml : $tokenText; $recipient = $smsParams['To']; $smsParams['contact_id'] = $toID; $smsParams['parent_activity_id'] = $activityID; $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id'])); if (!$providerObj->send($recipient, $smsParams, $message, NULL)) { return FALSE; } // add activity target record for every sms that is send $activityTargetParams = array('activity_id' => $activityID, 'target_contact_id' => $toID); self::createActivityTarget($activityTargetParams); return TRUE; }