/** * Load token data. * * @param TokenValueEvent $e * @throws TokenException */ public function onEvaluate(TokenValueEvent $e) { // For reasons unknown, replaceHookTokens requires a pre-computed list of // hook *categories* (aka entities aka namespaces). We'll cache // this in the TokenProcessor's context. $hookTokens = array(); \CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $e->getTokenProcessor()->context['hookTokenCategories'] = $categories; $messageTokens = $e->getTokenProcessor()->getMessageTokens(); foreach ($e->getRows() as $row) { if (empty($row->context['contact'])) { $params = array(array('contact_id', '=', $row->context['contactId'], 0, 0)); list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params); $contact = reset($contact); //CRM-4524 if (!$contact || is_a($contact, 'CRM_Core_Error')) { // FIXME: Need to differentiate errors which kill the batch vs the individual row. throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); } } else { $contact = $row->context['contact']; } if (!empty($row->context['tmpTokenParams'])) { // merge activity tokens with contact array // this is pretty weird. $contact = array_merge($contact, $row->context['tmpTokenParams']); } // Note: This is a small contract change from the past; data should be missing // less randomly. //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']); \CRM_Utils_Hook::tokenValues($contact, $row->context['contactId'], empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id, $messageTokens, $row->context['controller']); $row->context('contact', $contact); } }
/** * Checks wether a contact is a member of a group * * This function is a copy of CRM_Contact_BAO_GroupContact::isContactInGroup but with * a change so that the group contact cache won't be rebuild. Which somehow resulted * in a deadlock * * @param $contact_id * @param $group_id * @return bool */ public static function isContactInGroup($contact_id, $group_id) { if (!CRM_Utils_Rule::positiveInteger($contact_id) || !CRM_Utils_Rule::positiveInteger($group_id)) { return FALSE; } $params = array(array('group', 'IN', array($group_id => 1), 0, 0), array('contact_id', '=', $contact_id, 0, 0)); list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id'), null, null, 0, 1, false, false, true); if (!empty($contacts)) { return TRUE; } return FALSE; }
function _civicrm_api3_get_using_query_object($object_type, $params, $additional_options = array(), $getCount = null) { // Convert id to e.g. contact_id if (empty($params[$object_type . '_id']) && isset($params['id'])) { $params[$object_type . '_id'] = $params['id']; } unset($params['id']); $options = _civicrm_api3_get_options_from_params($params, TRUE); $inputParams = array_merge(CRM_Utils_Array::value('input_params', $options, array()), CRM_Utils_Array::value('input_params', $additional_options, array())); $returnProperties = array_merge(CRM_Utils_Array::value('return', $options, array()), CRM_Utils_Array::value('return', $additional_options, array())); if (empty($returnProperties)) { $returnProperties = null; } $options = array_merge($options, $additional_options); $sort = CRM_Utils_Array::value('sort', $options, NULL); $offset = CRM_Utils_Array::value('offset', $options, NULL); $limit = CRM_Utils_Array::value('limit', $options, NULL); $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params); if ($getCount) { $limit = NULL; $returnProperties = NULL; } $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); list($entities, $options) = CRM_Contact_BAO_Query::apiQuery($newParams, $returnProperties, NULL, $sort, $offset, $limit, $smartGroupCache, $getCount); if ($getCount) { // only return the count of contacts return $entities[0]; } return $entities; }
/** * Send an email from the specified template based on an array of params. * * @param array $params * A string-keyed array of function params, see function body for details. * * @return array * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates */ public static function sendTemplate($params) { $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL); $params = array_merge($defaults, $params); CRM_Utils_Hook::alterMailParams($params, 'messageTemplate'); if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) { CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing.")); } if ($params['messageTemplateID']) { // fetch the three elements from the db based on id $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt WHERE mt.id = %1 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['messageTemplateID'], 'String')); } else { // fetch the three elements from the db based on option_group and option_value names $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt JOIN civicrm_option_value ov ON workflow_id = ov.id JOIN civicrm_option_group og ON ov.option_group_id = og.id WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String')); } $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); $dao->fetch(); if (!$dao->N) { if ($params['messageTemplateID']) { CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID']))); } else { CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName']))); } } $mailContent = array('subject' => $dao->subject, 'text' => $dao->text, 'html' => $dao->html, 'format' => $dao->format); $dao->free(); CRM_Utils_Hook::alterMailContent($mailContent); // add the test banner (if requested) if ($params['isTest']) { $query = "SELECT msg_subject subject, msg_text text, msg_html html\n FROM civicrm_msg_template mt\n JOIN civicrm_option_value ov ON workflow_id = ov.id\n JOIN civicrm_option_group og ON ov.option_group_id = og.id\n WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1"; $testDao = CRM_Core_DAO::executeQuery($query); $testDao->fetch(); $mailContent['subject'] = $testDao->subject . $mailContent['subject']; $mailContent['text'] = $testDao->text . $mailContent['text']; $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']); $testDao->free(); } // replace tokens in the three elements (in subject as if it was the text body) $domain = CRM_Core_BAO_Domain::getDomain(); $hookTokens = array(); $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->subject = $mailContent['subject']; $mailing->body_text = $mailContent['text']; $mailing->body_html = $mailContent['html']; $tokens = $mailing->getTokens(); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $contactID = CRM_Utils_Array::value('contactId', $params); if ($contactID) { $contactParams = array('contact_id' => $contactID); $returnProperties = array(); if (isset($tokens['subject']['contact'])) { foreach ($tokens['subject']['contact'] as $name) { $returnProperties[$name] = 1; } } if (isset($tokens['text']['contact'])) { foreach ($tokens['text']['contact'] as $name) { $returnProperties[$name] = 1; } } if (isset($tokens['html']['contact'])) { foreach ($tokens['html']['contact'] as $name) { $returnProperties[$name] = 1; } } // @todo CRM-17253 don't resolve contact details if there are no tokens // effectively comment out this next (performance-expensive) line // but unfortunately testing is a bit think on the ground to that needs to // be added. list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contact[$contactID]; } $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE); $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE); $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE); if ($contactID) { $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE); $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE); $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE); $contactArray = array($contactID => $contact); CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contactArray[$contactID]; $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE); $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE); $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE); } // strip whitespace from ends and turn into a single line $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; // parse the three elements with Smarty $smarty = CRM_Core_Smarty::singleton(); foreach ($params['tplParams'] as $name => $value) { $smarty->assign($name, $value); } foreach (array('subject', 'text', 'html') as $elem) { $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); } // send the template, honouring the target user’s preferences (if any) $sent = FALSE; // create the params array $params['subject'] = $mailContent['subject']; $params['text'] = $mailContent['text']; $params['html'] = $mailContent['html']; if ($params['toEmail']) { $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams); $prefs = array_pop($contact); if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') { $params['text'] = NULL; } if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') { $params['html'] = NULL; } $config = CRM_Core_Config::singleton(); if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) { $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']); if (empty($params['attachments'])) { $params['attachments'] = array(); } $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']); } $pdf_filename = ''; if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) { if (empty($params['attachments'])) { $params['attachments'] = array(); } $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']); if (isset($params['tplParams']['email_comment'])) { $params['html'] = $params['tplParams']['email_comment']; $params['text'] = strip_tags($params['tplParams']['email_comment']); } } $sent = CRM_Utils_Mail::send($params); if ($pdf_filename) { unlink($pdf_filename); } } return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']); }
protected function replaceTokens($input, $contact_id) { //get contact $params = array(array('contact_id', '=', $contact_id, 0, 0)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { throw new API_Exception('Could not find contact with ID: ' . $params['contact_id']); } $tokens = CRM_Utils_Token::getTokens($input); // get replacement text for these tokens $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1); if (isset($tokens['contact'])) { foreach ($tokens['contact'] as $key => $value) { $returnProperties[$value] = 1; } } list($details) = CRM_Utils_Token::getTokenDetails(array($contact_id), $returnProperties, false, false, null, $tokens); $contact = reset($details); // call token hook $hookTokens = array(); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); CRM_Utils_Token::replaceGreetingTokens($input, NULL, $contact['contact_id']); $input = CRM_Utils_Token::replaceDomainTokens($input, $domain, true, $tokens, true); $input = CRM_Utils_Token::replaceContactTokens($input, $contact, false, $tokens, false, true); $input = CRM_Utils_Token::replaceComponentTokens($input, $contact, $tokens, true); $input = CRM_Utils_Token::replaceHookTokens($input, $contact, $categories, true); return $input; }
/** * Gives required details of contacts in an indexed array format so we * can iterate in a nice loop and do token evaluation * * @param $contactIDs * @param array $returnProperties * Of required properties. * @param bool $skipOnHold Don't return on_hold contact info also. * Don't return on_hold contact info also. * @param bool $skipDeceased Don't return deceased contact info. * Don't return deceased contact info. * @param array $extraParams * Extra params. * @param array $tokens * The list of tokens we've extracted from the content. * @param null $className * @param int $jobID * The mailing list jobID - this is a legacy param. * * @return array */ public static function getTokenDetails($contactIDs, $returnProperties = NULL, $skipOnHold = TRUE, $skipDeceased = TRUE, $extraParams = NULL, $tokens = array(), $className = NULL, $jobID = NULL) { if (empty($contactIDs)) { // putting a fatal here so we can track if/when this happens CRM_Core_Error::fatal(); } // @todo this functions needs unit tests. $params = array(); foreach ($contactIDs as $key => $contactID) { $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0); } // fix for CRM-2613 if ($skipDeceased) { $params[] = array('is_deceased', '=', 0, 0, 0); } //fix for CRM-3798 if ($skipOnHold) { $params[] = array('on_hold', '=', 0, 0, 0); } if ($extraParams) { $params = array_merge($params, $extraParams); } // if return properties are not passed then get all return properties if (empty($returnProperties)) { $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()), array('display_name', 'checksum', 'contact_id')); foreach ($fields as $key => $val) { // The unavailable fields are not available as tokens, do not have a one-2-one relationship // with contacts and are expensive to resolve. // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded // and upstream calls to this should populate return properties. $unavailableFields = array('group', 'tag'); if (!in_array($val, $unavailableFields)) { $returnProperties[$val] = 1; } } } $custom = array(); foreach ($returnProperties as $name => $dontCare) { $cfID = CRM_Core_BAO_CustomField::getKeyID($name); if ($cfID) { $custom[] = $cfID; } } //get the total number of contacts to fetch from database. $numberofContacts = count($contactIDs); $query = new CRM_Contact_BAO_Query($params, $returnProperties); $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $contactDetails =& $details[0]; foreach ($contactIDs as $key => $contactID) { if (array_key_exists($contactID, $contactDetails)) { if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1 && array_key_exists('preferred_communication_method', $contactDetails[$contactID])) { $pcm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); // communication Preference $contactPcm = explode(CRM_Core_DAO::VALUE_SEPARATOR, $contactDetails[$contactID]['preferred_communication_method']); $result = array(); foreach ($contactPcm as $key => $val) { if ($val) { $result[$val] = $pcm[$val]; } } $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result); } foreach ($custom as $cfID) { if (isset($contactDetails[$contactID]["custom_{$cfID}"])) { $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID, $details[1]); } } // special case for greeting replacement foreach (array('email_greeting', 'postal_greeting', 'addressee') as $val) { if (!empty($contactDetails[$contactID][$val])) { $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"]; } } } } // also call a hook and get token details CRM_Utils_Hook::tokenValues($details[0], $contactIDs, $jobID, $tokens, $className); return $details; }
/** * process the form after the input has been submitted and validated * * @access public * * @return void */ public function postProcess() { $fv = $this->controller->exportValues($this->_name); $config = CRM_Core_Config::singleton(); $locName = NULL; //get the address format sequence from the config file $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format'); $sequence = CRM_Utils_Address::sequence($mailingFormat); foreach ($sequence as $v) { $address[$v] = 1; } if (array_key_exists('postal_code', $address)) { $address['postal_code_suffix'] = 1; } //build the returnproperties $returnProperties = array('display_name' => 1, 'contact_type' => 1); $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format'); $mailingFormatProperties = array(); if ($mailingFormat) { $mailingFormatProperties = self::getReturnProperties($mailingFormat); $returnProperties = array_merge($returnProperties, $mailingFormatProperties); } //we should not consider addressee for data exists, CRM-6025 if (array_key_exists('addressee', $mailingFormatProperties)) { unset($mailingFormatProperties['addressee']); } $customFormatProperties = array(); if (stristr($mailingFormat, 'custom_')) { foreach ($mailingFormatProperties as $token => $true) { if (substr($token, 0, 7) == 'custom_') { if (empty($customFormatProperties[$token])) { $customFormatProperties[$token] = $mailingFormatProperties[$token]; } } } } if (!empty($customFormatProperties)) { $returnProperties = array_merge($returnProperties, $customFormatProperties); } if (isset($fv['merge_same_address'])) { // we need first name/last name for summarising to avoid spillage $returnProperties['first_name'] = 1; $returnProperties['last_name'] = 1; } $individualFormat = FALSE; /* * CRM-8338: replace ids of household members with the id of their household * so we can merge labels by household. */ if (isset($fv['merge_same_household'])) { $this->mergeContactIdsByHousehold(); $individualFormat = TRUE; } //get the contacts information $params = array(); if (!empty($fv['location_type_id'])) { $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $locName = $locType[$fv['location_type_id']]; $location = array('location' => array("{$locName}" => $address)); $returnProperties = array_merge($returnProperties, $location); $params[] = array('location_type', '=', array($fv['location_type_id'] => 1), 0, 0); } else { $returnProperties = array_merge($returnProperties, $address); } $rows = array(); foreach ($this->_contactIds as $key => $contactID) { $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0); } // fix for CRM-2651 if (!empty($fv['do_not_mail'])) { $params[] = array('do_not_mail', '=', 0, 0, 0); } // fix for CRM-2613 $params[] = array('is_deceased', '=', 0, 0, 0); $custom = array(); foreach ($returnProperties as $name => $dontCare) { $cfID = CRM_Core_BAO_CustomField::getKeyID($name); if ($cfID) { $custom[] = $cfID; } } //get the total number of contacts to fetch from database. $numberofContacts = count($this->_contactIds); $query = new CRM_Contact_BAO_Query($params, $returnProperties); $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $messageToken = CRM_Utils_Token::getTokens($mailingFormat); // also get all token values CRM_Utils_Hook::tokenValues($details[0], $this->_contactIds, NULL, $messageToken, 'CRM_Contact_Form_Task_Label'); $tokens = array(); CRM_Utils_Hook::tokens($tokens); $tokenFields = array(); foreach ($tokens as $category => $catTokens) { foreach ($catTokens as $token => $tokenName) { $tokenFields[] = $token; } } foreach ($this->_contactIds as $value) { foreach ($custom as $cfID) { if (isset($details[0][$value]["custom_{$cfID}"])) { $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[0][$value]["custom_{$cfID}"], $cfID, $details[1]); } } $contact = CRM_Utils_Array::value($value, $details['0']); if (is_a($contact, 'CRM_Core_Error')) { return NULL; } // we need to remove all the "_id" unset($contact['contact_id']); if ($locName && !empty($contact[$locName])) { // If location type is not primary, $contact contains // one more array as "$contact[$locName] = array( values... )" if (!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } unset($contact[$locName]); if (!empty($contact['county_id'])) { unset($contact['county_id']); } foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } $valuesothers = array(); $paramsothers = array('contact_id' => $value); $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); if (!empty($fv['location_type_id'])) { foreach ($valuesothers as $vals) { if (CRM_Utils_Array::value('location_type_id', $vals) == CRM_Utils_Array::value('location_type_id', $fv)) { foreach ($vals as $k => $v) { if (in_array($k, array('email', 'phone', 'im', 'openid'))) { if ($k == 'im') { $rows[$value][$k] = $v['1']['name']; } else { $rows[$value][$k] = $v['1'][$k]; } $rows[$value][$k . '_id'] = $v['1']['id']; } } } } } } else { if (!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } if (!empty($contact['addressee_display'])) { $contact['addressee_display'] = trim($contact['addressee_display']); } if (!empty($contact['addressee'])) { $contact['addressee'] = $contact['addressee_display']; } // now create the rows for generating mailing labels foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } } } if (isset($fv['merge_same_address'])) { $this->mergeSameAddress($rows); $individualFormat = TRUE; } // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327) foreach ($rows as $id => $row) { if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) { $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods)); $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); $temp = array(); foreach ($val as $vals) { $temp[] = $comm[$vals]; } $row['preferred_communication_method'] = implode(', ', $temp); } $row['id'] = $id; $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields); // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it. // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366 // Due to FriBidi peculiarities, this can't be called on // a multi-line string, hence the explode+implode approach. if (function_exists('fribidi_log2vis')) { $lines = explode("\n", $formatted); foreach ($lines as $i => $line) { $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO, FRIBIDI_CHARSET_UTF8); } $formatted = implode("\n", $lines); } $rows[$id] = array($formatted); } //call function to create labels self::createLabel($rows, $fv['label_name']); CRM_Utils_System::civiExit(1); }
/** * Compose a message. * * @param int $job_id * ID of the Job associated with this message. * @param int $event_queue_id * ID of the EventQueue. * @param string $hash * Hash of the EventQueue. * @param string $contactId * ID of the Contact. * @param string $email * Destination address. * @param string $recipient * To: of the recipient. * @param bool $test * Is this mailing a test?. * @param $contactDetails * @param $attachments * @param bool $isForward * Is this mailing compose for forward?. * @param string $fromEmail * Email address of who is forwardinf it. * * @param null $replyToEmail * * @return Mail_mime The mail object */ public function compose($job_id, $event_queue_id, $hash, $contactId, $email, &$recipient, $test, $contactDetails, &$attachments, $isForward = FALSE, $fromEmail = NULL, $replyToEmail = NULL) { $config = CRM_Core_Config::singleton(); $this->getTokens(); if ($this->_domain == NULL) { $this->_domain = CRM_Core_BAO_Domain::getDomain(); } list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward); //set from email who is forwarding it and not original one. if ($fromEmail) { unset($headers['From']); $headers['From'] = "<{$fromEmail}>"; } if ($replyToEmail && $fromEmail != $replyToEmail) { $headers['Reply-To'] = "{$replyToEmail}"; } if ($contactDetails) { $contact = $contactDetails; } elseif ($contactId === 0) { //anonymous user $contact = array(); CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } else { $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact) = CRM_Contact_BAO_Query::apiQuery($params); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1', array(1 => $contactId))); // setting this because function is called by reference //@todo test not calling function by reference $res = NULL; return $res; } // also call the hook to get contact details CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } $pTemplates = $this->getPreparedTemplates(); $pEmails = array(); foreach ($pTemplates as $type => $pTemplate) { $html = $type == 'html' ? TRUE : FALSE; $pEmails[$type] = array(); $pEmail =& $pEmails[$type]; $template =& $pTemplates[$type]['template']; $tokens =& $pTemplates[$type]['tokens']; $idx = 0; if (!empty($tokens)) { foreach ($tokens as $idx => $token) { $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id); array_push($pEmail, $template[$idx]); array_push($pEmail, $token_data); } } else { array_push($pEmail, $template[$idx]); } if (isset($template[$idx + 1])) { array_push($pEmail, $template[$idx + 1]); } } $html = NULL; if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) { $html =& $pEmails['html']; } $text = NULL; if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) { $text =& $pEmails['text']; } // push the tracking url on to the html email if necessary if ($this->open_tracking && $html) { array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL . "extern/open.php?q={$event_queue_id}\" width='1' height='1' alt='' border='0'>"); } $message = new Mail_mime("\n"); $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE; if ($useSmarty) { $smarty = CRM_Core_Smarty::singleton(); // also add the contact tokens to the template $smarty->assign_by_ref('contact', $contact); } $mailParams = $headers; if ($text && ($test || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both' || $contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))) { $textBody = implode('', $text); if ($useSmarty) { $textBody = $smarty->fetch("string:{$textBody}"); } $mailParams['text'] = $textBody; } if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both'))) { $htmlBody = implode('', $html); if ($useSmarty) { $htmlBody = $smarty->fetch("string:{$htmlBody}"); } $mailParams['html'] = $htmlBody; } if (empty($mailParams['text']) && empty($mailParams['html'])) { // CRM-9833 // something went wrong, lets log it and return null (by reference) CRM_Core_Error::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1', array(1 => $email))); $res = NULL; return $res; } $mailParams['attachments'] = $attachments; $mailingSubject = CRM_Utils_Array::value('subject', $pEmails); if (is_array($mailingSubject)) { $mailingSubject = implode('', $mailingSubject); } $mailParams['Subject'] = $mailingSubject; $mailParams['toName'] = CRM_Utils_Array::value('display_name', $contact); $mailParams['toEmail'] = $email; // Add job ID to mailParams for external email delivery service to utilise $mailParams['job_id'] = $job_id; CRM_Utils_Hook::alterMailParams($mailParams, 'civimail'); // CRM-10699 support custom email headers if (!empty($mailParams['headers'])) { $headers = array_merge($headers, $mailParams['headers']); } //cycle through mailParams and set headers array foreach ($mailParams as $paramKey => $paramValue) { //exclude values not intended for the header if (!in_array($paramKey, array('text', 'html', 'attachments', 'toName', 'toEmail'))) { $headers[$paramKey] = $paramValue; } } if (!empty($mailParams['text'])) { $message->setTxtBody($mailParams['text']); } if (!empty($mailParams['html'])) { $message->setHTMLBody($mailParams['html']); } if (!empty($mailParams['attachments'])) { foreach ($mailParams['attachments'] as $fileID => $attach) { $message->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']); } } //pickup both params from mail params. $toName = trim($mailParams['toName']); $toEmail = trim($mailParams['toEmail']); if ($toName == $toEmail || strpos($toName, '@') !== FALSE) { $toName = NULL; } else { $toName = CRM_Utils_Mail::formatRFC2822Name($toName); } $headers['To'] = "{$toName} <{$toEmail}>"; $headers['Precedence'] = 'bulk'; // Will test in the mail processor if the X-VERP is set in the bounced email. // (As an option to replace real VERP for those that can't set it up) $headers['X-CiviMail-Bounce'] = $verp['bounce']; //CRM-5058 //token replacement of subject $headers['Subject'] = $mailingSubject; CRM_Utils_Mail::setMimeParams($message); $headers = $message->headers($headers); //get formatted recipient $recipient = $headers['To']; // make sure we unset a lot of stuff unset($verp); unset($urls); unset($params); unset($contact); unset($ids); return $message; }
/** * Get the list of member for a group id. * * @param int $groupID * @param bool $useCache * * @return array * this array contains the list of members for this group id */ public static function &getMember($groupID, $useCache = TRUE) { $params = array(array('group', '=', $groupID, 0, 0)); $returnProperties = array('contact_id'); list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, $useCache); $aMembers = array(); foreach ($contacts as $contact) { $aMembers[$contact['contact_id']] = 1; } return $aMembers; }
/** * The API supports 2 types of get requestion. The more complex uses the BAO query object. * This is a generic function for those functions that call it * * At the moment only called by contact we should extend to contribution & * others that use the query object. Note that this function passes permission information in. * The others don't * * * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the * 2 variants call * @param $entity * @param array $params as passed into api get or getcount function * @param array $additional_options array of options (so we can modify the filter) * @param bool $getCount are we just after the count * * @return */ function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL) { // Convert id to e.g. contact_id if (empty($params[$entity . '_id']) && isset($params['id'])) { $params[$entity . '_id'] = $params['id']; } unset($params['id']); $options = _civicrm_api3_get_options_from_params($params, TRUE); $inputParams = array_merge(CRM_Utils_Array::value('input_params', $options, array()), CRM_Utils_Array::value('input_params', $additional_options, array())); $returnProperties = array_merge(CRM_Utils_Array::value('return', $options, array()), CRM_Utils_Array::value('return', $additional_options, array())); if (empty($returnProperties)) { $returnProperties = NULL; } if (!empty($params['check_permissions'])) { // we will filter query object against getfields $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'get')); // we need to add this in as earlier in this function 'id' was unset in favour of $entity_id $fields['values'][$entity . '_id'] = array(); $varsToFilter = array('returnProperties', 'inputParams'); foreach ($varsToFilter as $varToFilter) { if (!is_array(${$varToFilter})) { continue; } //I was going to throw an exception rather than silently filter out - but //would need to diff out of exceptions arr other keys like 'options', 'return', 'api. etcetc //so we are silently ignoring parts of their request //$exceptionsArr = array_diff(array_keys($$varToFilter), array_keys($fields['values'])); ${$varToFilter} = array_intersect_key(${$varToFilter}, $fields['values']); } } $options = array_merge($options, $additional_options); $sort = CRM_Utils_Array::value('sort', $options, NULL); $offset = CRM_Utils_Array::value('offset', $options, NULL); $limit = CRM_Utils_Array::value('limit', $options, NULL); $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params); if ($getCount) { $limit = NULL; $returnProperties = NULL; } $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); foreach ($newParams as &$newParam) { if ($newParam[1] == '=' && is_array($newParam[2])) { // we may be looking at an attempt to use the 'IN' style syntax // @todo at time of writing only 'IN' & 'NOT IN' are supported for the array style syntax $sqlFilter = CRM_Core_DAO::createSqlFilter($newParam[0], $params[$newParam[0]], 'String', NULL, TRUE); if ($sqlFilter) { $newParam[1] = key($newParam[2]); $newParam[2] = $sqlFilter; } } } $skipPermissions = !empty($params['check_permissions']) ? 0 : 1; list($entities, $options) = CRM_Contact_BAO_Query::apiQuery($newParams, $returnProperties, NULL, $sort, $offset, $limit, $smartGroupCache, $getCount, $skipPermissions); if ($getCount) { // only return the count of contacts return $entities; } return $entities; }
static function isContactInGroup($contactID, $groupID) { if (!CRM_Utils_Rule::positiveInteger($contactID) || !CRM_Utils_Rule::positiveInteger($groupID)) { return FALSE; } $params = array(array('group', 'IN', array($groupID => 1), 0, 0), array('contact_id', '=', $contactID, 0, 0)); list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id')); if (!empty($contacts)) { return TRUE; } return FALSE; }
static function sendReminder($contactId, $email, $scheduleID, $from, $tokenParams) { $schedule = new CRM_Core_DAO_ActionSchedule(); $schedule->id = $scheduleID; $domain = CRM_Core_BAO_Domain::getDomain(); $result = NULL; $hookTokens = array(); if ($schedule->find(TRUE)) { $body_text = $schedule->body_text; $body_html = $schedule->body_html; $body_subject = $schedule->subject; if (!$body_text) { $body_text = CRM_Utils_String::htmlToText($body_html); } $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { return NULL; } // merge activity tokens with contact array $contact = array_merge($contact, $tokenParams); //CRM-5734 CRM_Utils_Hook::tokenValues($contact, $contactId); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $type = array('html', 'text'); foreach ($type as $key => $value) { $dummy_mail = new CRM_Mailing_BAO_Mailing(); $bodyType = "body_{$value}"; $dummy_mail->{$bodyType} = ${$bodyType}; $tokens = $dummy_mail->getTokens(); if (${$bodyType}) { CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']); ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens[$value], TRUE); ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens[$value], FALSE, TRUE); ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens[$value], TRUE, FALSE); ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE); } } $html = $body_html; $text = $body_text; $smarty = CRM_Core_Smarty::singleton(); foreach (array('text', 'html') as $elem) { ${$elem} = $smarty->fetch("string:{${$elem}}"); } $matches = array(); preg_match_all('/(?<!\\{|\\\\)\\{(\\w+\\.\\w+)\\}(?!\\})/', $body_subject, $matches, PREG_PATTERN_ORDER); $subjectToken = NULL; if ($matches[1]) { foreach ($matches[1] as $token) { list($type, $name) = preg_split('/\\./', $token, 2); if ($name) { if (!isset($subjectToken['contact'])) { $subjectToken['contact'] = array(); } $subjectToken['contact'][] = $name; } } } $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $subjectToken); $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens[$value]); $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens[$value], TRUE); $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE); $messageSubject = $smarty->fetch("string:{$messageSubject}"); // set up the parameters for CRM_Utils_Mail::send $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject); if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') { // render the & entities in text mode, so that the links work $mailParams['text'] = str_replace('&', '&', $text); } if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) { $mailParams['html'] = $html; } $result = CRM_Utils_Mail::send($mailParams); } $schedule->free(); return $result; }
/** * Send an email from the specified template based on an array of params * * @param array $params a string-keyed array of function params, see function body for details * * @return array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates */ static function sendTemplate($params) { $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL); $params = array_merge($defaults, $params); if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) { CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing.")); } if ($params['messageTemplateID']) { // fetch the three elements from the db based on id $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt WHERE mt.id = %1 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['messageTemplateID'], 'String')); } else { // fetch the three elements from the db based on option_group and option_value names $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format FROM civicrm_msg_template mt JOIN civicrm_option_value ov ON workflow_id = ov.id JOIN civicrm_option_group og ON ov.option_group_id = og.id WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1'; $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String')); } $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); $dao->fetch(); if (!$dao->N) { if ($params['messageTemplateID']) { CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID']))); } else { CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName']))); } } $subject = $dao->subject; $text = $dao->text; $html = $dao->html; $format = $dao->format; $dao->free(); // add the test banner (if requested) if ($params['isTest']) { $query = "SELECT msg_subject subject, msg_text text, msg_html html\n FROM civicrm_msg_template mt\n JOIN civicrm_option_value ov ON workflow_id = ov.id\n JOIN civicrm_option_group og ON ov.option_group_id = og.id\n WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1"; $testDao = CRM_Core_DAO::executeQuery($query); $testDao->fetch(); $subject = $testDao->subject . $subject; $text = $testDao->text . $text; $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html); $testDao->free(); } // replace tokens in the three elements (in subject as if it was the text body) $domain = CRM_Core_BAO_Domain::getDomain(); $hookTokens = array(); $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->body_text = $text; $mailing->body_html = $html; $tokens = $mailing->getTokens(); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $contactID = CRM_Utils_Array::value('contactId', $params); if ($contactID) { $contactParams = array('contact_id' => $contactID); $returnProperties = array(); if (isset($tokens['text']['contact'])) { foreach ($tokens['text']['contact'] as $name) { $returnProperties[$name] = 1; } } if (isset($tokens['html']['contact'])) { foreach ($tokens['html']['contact'] as $name) { $returnProperties[$name] = 1; } } list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contact[$contactID]; } $subject = CRM_Utils_Token::replaceDomainTokens($subject, $domain, TRUE, $tokens['text'], TRUE); $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text'], TRUE); $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE); if ($contactID) { $subject = CRM_Utils_Token::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE); $text = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE); $html = CRM_Utils_Token::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE); $contactArray = array($contactID => $contact); CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate'); $contact = $contactArray[$contactID]; $subject = CRM_Utils_Token::replaceHookTokens($subject, $contact, $categories, TRUE); $text = CRM_Utils_Token::replaceHookTokens($text, $contact, $categories, TRUE); $html = CRM_Utils_Token::replaceHookTokens($html, $contact, $categories, TRUE); } // strip whitespace from ends and turn into a single line $subject = "{strip}{$subject}{/strip}"; // parse the three elements with Smarty $smarty = CRM_Core_Smarty::singleton(); foreach ($params['tplParams'] as $name => $value) { $smarty->assign($name, $value); } foreach (array('subject', 'text', 'html') as $elem) { ${$elem} = $smarty->fetch("string:{${$elem}}"); } // send the template, honouring the target user’s preferences (if any) $sent = FALSE; // create the params array $params['subject'] = $subject; $params['text'] = $text; $params['html'] = $html; if ($params['toEmail']) { $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams); $prefs = array_pop($contact); if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') { $params['text'] = NULL; } if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') { $params['html'] = NULL; } $config = CRM_Core_Config::singleton(); $pdf_filename = ''; if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) { $pdf_filename = $config->templateCompileDir . CRM_Utils_File::makeFileName($params['PDFFilename']); //FIXME : CRM-7894 //xmlns attribute is required in XHTML but it is invalid in HTML, //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, //and will be added to the <html> tag even if you do not include it. $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\\s]+["\'])(.+)?(>)/', '\\1\\3\\4', $params['html']); file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, $params['PDFFilename'], TRUE, $format)); if (empty($params['attachments'])) { $params['attachments'] = array(); } $params['attachments'][] = array('fullPath' => $pdf_filename, 'mime_type' => 'application/pdf', 'cleanName' => $params['PDFFilename']); } $sent = CRM_Utils_Mail::send($params); if ($pdf_filename) { unlink($pdf_filename); } } return array($sent, $subject, $text, $html); }
/** * Get function for query object api. * * The API supports 2 types of get request. The more complex uses the BAO query object. * This is a generic function for those functions that call it * * At the moment only called by contact we should extend to contribution & * others that use the query object. Note that this function passes permission information in. * The others don't * * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the * 2 variants call * * @param $entity * @param array $params * As passed into api get or getcount function. * @param array $additional_options * Array of options (so we can modify the filter). * @param bool $getCount * Are we just after the count. * * @return array */ function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL) { $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); // Convert id to e.g. contact_id if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) { $params[$lowercase_entity . '_id'] = $params['id']; } unset($params['id']); $options = _civicrm_api3_get_options_from_params($params, TRUE); $inputParams = array_merge(CRM_Utils_Array::value('input_params', $options, array()), CRM_Utils_Array::value('input_params', $additional_options, array())); $returnProperties = array_merge(CRM_Utils_Array::value('return', $options, array()), CRM_Utils_Array::value('return', $additional_options, array())); if (empty($returnProperties)) { $returnProperties = NULL; } if (!empty($params['check_permissions'])) { // we will filter query object against getfields $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'get')); // we need to add this in as earlier in this function 'id' was unset in favour of $entity_id $fields['values'][$lowercase_entity . '_id'] = array(); $varsToFilter = array('returnProperties', 'inputParams'); foreach ($varsToFilter as $varToFilter) { if (!is_array(${$varToFilter})) { continue; } //I was going to throw an exception rather than silently filter out - but //would need to diff out of exceptions arr other keys like 'options', 'return', 'api. etcetc //so we are silently ignoring parts of their request //$exceptionsArr = array_diff(array_keys($$varToFilter), array_keys($fields['values'])); ${$varToFilter} = array_intersect_key(${$varToFilter}, $fields['values']); } } $options = array_merge($options, $additional_options); $sort = CRM_Utils_Array::value('sort', $options, NULL); $offset = CRM_Utils_Array::value('offset', $options, NULL); $limit = CRM_Utils_Array::value('limit', $options, NULL); $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params); if ($getCount) { $limit = NULL; $returnProperties = NULL; } if (substr($sort, 0, 2) == 'id') { $sort = $lowercase_entity . "_" . $sort; } $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); $skipPermissions = !empty($params['check_permissions']) ? 0 : 1; list($entities, $options) = CRM_Contact_BAO_Query::apiQuery($newParams, $returnProperties, NULL, $sort, $offset, $limit, $smartGroupCache, $getCount, $skipPermissions); if ($getCount) { // only return the count of contacts return $entities; } return $entities; }
/** * gives required details of contacts * * @param array $contactIds of conatcts * @param array $returnProperties of required properties * @param boolean $skipOnHold don't return on_hold contact info also. * @param boolean $skipDeceased don't return deceased contact info. * @param array $extraParams extra params * * @return array * @access public */ function getDetails($contactIDs, $returnProperties = null, $skipOnHold = true, $skipDeceased = true, $extraParams = null) { $params = array(); foreach ($contactIDs as $key => $contactID) { $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0); } // fix for CRM-2613 if ($skipDeceased) { $params[] = array('is_deceased', '=', 0, 0, 0); } //fix for CRM-3798 if ($skipOnHold) { $params[] = array('on_hold', '=', 0, 0, 0); } if ($extraParams) { $params = array_merge($params, $extraParams); } // if return properties are not passed then get all return properties if (empty($returnProperties)) { require_once 'CRM/Contact/BAO/Contact.php'; $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()), array('display_name', 'checksum', 'contact_id')); foreach ($fields as $key => $val) { $returnProperties[$val] = 1; } } $custom = array(); foreach ($returnProperties as $name => $dontCare) { $cfID = CRM_Core_BAO_CustomField::getKeyID($name); if ($cfID) { $custom[] = $cfID; } } //get the total number of contacts to fetch from database. $numberofContacts = count($contactIDs); require_once 'CRM/Contact/BAO/Query.php'; $query = new CRM_Contact_BAO_Query($params, $returnProperties); $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $contactDetails =& $details[0]; foreach ($contactIDs as $key => $contactID) { if (array_key_exists($contactID, $contactDetails)) { if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1 && array_key_exists('preferred_communication_method', $contactDetails[$contactID])) { require_once 'CRM/Core/PseudoConstant.php'; $pcm = CRM_Core_PseudoConstant::pcm(); // communication Prefferance require_once 'CRM/Core/BAO/CustomOption.php'; $contactPcm = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $contactDetails[$contactID]['preferred_communication_method']); $result = array(); foreach ($contactPcm as $key => $val) { if ($val) { $result[$val] = $pcm[$val]; } } $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result); } foreach ($custom as $cfID) { if (isset($contactDetails[$contactID]["custom_{$cfID}"])) { $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID, $details[1]); } } //special case for greeting replacement foreach (array('email_greeting', 'postal_greeting', 'addressee') as $val) { if (CRM_Utils_Array::value($val, $contactDetails[$contactID])) { $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"]; } } } } // also call a hook and get token details require_once 'CRM/Utils/Hook.php'; CRM_Utils_Hook::tokenValues($details[0], $contactIDs); return $details; }
/** * Retrieve a set of contacts, given a set of input params * * @deprecated deprecated since version 2.2.3 * * @param array $params (reference ) input parameters * @param array $returnProperties Which properties should be included in the * returned Contact object. If NULL, the default * set of properties will be included. * * @return array (reference ) array of contacts, if error an array with an error id and error message * @static void * @access public */ function &civicrm_contact_search(&$params) { _civicrm_initialize(); $inputParams = $returnProperties = array(); $otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache'); $sort = NULL; $offset = 0; $rowCount = 25; $smartGroupCache = FALSE; foreach ($params as $n => $v) { if (substr($n, 0, 6) == 'return') { $returnProperties[substr($n, 7)] = $v; } elseif (in_array($n, $otherVars)) { ${$n} = $v; } else { $inputParams[$n] = $v; } } // explicitly suppress all deleted contacts // this is fixed in api v3 // CRM-8809 $inputParams['contact_is_deleted'] = 0; if (empty($returnProperties)) { $returnProperties = NULL; } require_once 'CRM/Contact/BAO/Query.php'; $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams, $returnProperties, NULL, $sort, $offset, $rowCount, $smartGroupCache); return $contacts; }
static function getGlobalContacts(&$groupInfo) { $globalContacts = array(); $settingsProcessor = new CRM_Case_XMLProcessor_Settings(); $settings = $settingsProcessor->run(); if (!empty($settings)) { $groupInfo['name'] = $settings['groupname']; if ($groupInfo['name']) { $searchParams = array('name' => $groupInfo['name']); $results = array(); CRM_Contact_BAO_Group::retrieve($searchParams, $results); if ($results) { $groupInfo['id'] = $results['id']; $groupInfo['title'] = $results['title']; $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); $return = array('sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); list($globalContacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, $return); } } } return $globalContacts; }
/** * Get the all contact details (Hierarchical). * * @param int $contactId * Contact id. * @param array $fields * Fields array. * * @return array * Contact details */ public static function getHierContactDetails($contactId, &$fields) { $params = array(array('contact_id', '=', $contactId, 0, 0)); $options = array(); $returnProperties = self::makeHierReturnProperties($fields, $contactId); // We don't know the contents of return properties, but we need the lower // level ids of the contact so add a few fields. $returnProperties['first_name'] = 1; $returnProperties['organization_name'] = 1; $returnProperties['household_name'] = 1; $returnProperties['contact_type'] = 1; $returnProperties['contact_sub_type'] = 1; return list($query, $options) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, $options); }
/** * Retrieve a set of contacts, given a set of input params * * @deprecated deprecated since version 2.2.3 * * @param array $params (reference ) input parameters * @param array $returnProperties Which properties should be included in the * returned Contact object. If NULL, the default * set of properties will be included. * * @return array (reference ) array of contacts, if error an array with an error id and error message * @static void * @access public */ function &civicrm_contact_search(&$params) { _civicrm_initialize(); $inputParams = array(); $returnProperties = array(); $otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache'); $sort = null; $offset = 0; $rowCount = 25; $smartGroupCache = false; foreach ($params as $n => $v) { if (substr($n, 0, 6) == 'return') { $returnProperties[substr($n, 7)] = $v; } elseif (in_array($n, $otherVars)) { ${$n} = $v; } else { $inputParams[$n] = $v; } } if (empty($returnProperties)) { $returnProperties = null; } require_once 'CRM/Contact/BAO/Query.php'; $newParams =& CRM_Contact_BAO_Query::convertFormValues($inputParams); list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams, $returnProperties, null, $sort, $offset, $rowCount, $smartGroupCache); return $contacts; }
/** * @param $contactId * @param $to * @param $scheduleID * @param $from * @param $tokenParams * * @return bool|null * @throws CRM_Core_Exception */ static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams) { $email = $to['email']; $phoneNumber = $to['phone']; $schedule = new CRM_Core_DAO_ActionSchedule(); $schedule->id = $scheduleID; $domain = CRM_Core_BAO_Domain::getDomain(); $result = NULL; $hookTokens = array(); if ($schedule->find(TRUE)) { $body_text = $schedule->body_text; $body_html = $schedule->body_html; $sms_body_text = $schedule->sms_body_text; $body_subject = $schedule->subject; if (!$body_text) { $body_text = CRM_Utils_String::htmlToText($body_html); } $params = array(array('contact_id', '=', $contactId, 0, 0)); list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { return NULL; } // merge activity tokens with contact array $contact = array_merge($contact, $tokenParams); //CRM-5734 CRM_Utils_Hook::tokenValues($contact, $contactId); CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $type = array('body_html' => 'html', 'body_text' => 'text', 'sms_body_text' => 'text'); foreach ($type as $bodyType => $value) { $dummy_mail = new CRM_Mailing_BAO_Mailing(); if ($bodyType == 'sms_body_text') { $dummy_mail->body_text = ${$bodyType}; } else { $dummy_mail->{${$bodyType}} = ${$bodyType}; } $tokens = $dummy_mail->getTokens(); if (${$bodyType}) { CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']); ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens[$value], TRUE); ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens[$value], FALSE, TRUE); ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens[$value], TRUE, FALSE); ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE); } } $html = $body_html; $text = $body_text; $sms_text = $sms_body_text; $smarty = CRM_Core_Smarty::singleton(); foreach (array('text', 'html', 'sms_text') as $elem) { ${$elem} = $smarty->fetch("string:{${$elem}}"); } $matches = array(); preg_match_all('/(?<!\\{|\\\\)\\{(\\w+\\.\\w+)\\}(?!\\})/', $body_subject, $matches, PREG_PATTERN_ORDER); $subjectToken = NULL; if ($matches[1]) { foreach ($matches[1] as $token) { list($type, $name) = preg_split('/\\./', $token, 2); if ($name) { if (!isset($subjectToken[$type])) { $subjectToken[$type] = array(); } $subjectToken[$type][] = $name; } } } $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $subjectToken); $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $subjectToken); $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $subjectToken, TRUE); $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE); $messageSubject = $smarty->fetch("string:{$messageSubject}"); if ($schedule->mode == 'SMS' or $schedule->mode == 'User_Preference') { $session = CRM_Core_Session::singleton(); $userID = $session->get('userID') ? $session->get('userID') : $contactId; $smsParams = array('To' => $phoneNumber, 'provider_id' => $schedule->sms_provider_id, 'activity_subject' => $messageSubject); $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name'); $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => $activityTypeID, 'activity_date_time' => date('YmdHis'), 'subject' => $messageSubject, 'details' => $sms_text, 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name')); $activity = CRM_Activity_BAO_Activity::create($activityParams); CRM_Activity_BAO_Activity::sendSMSMessage($contactId, $sms_text, $html, $smsParams, $activity->id, $userID); } if ($schedule->mode == 'Email' or $schedule->mode == 'User_Preference') { // set up the parameters for CRM_Utils_Mail::send $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject, 'entity' => 'action_schedule', 'entity_id' => $scheduleID); if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') { // render the & entities in text mode, so that the links work $mailParams['text'] = str_replace('&', '&', $text); } if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) { $mailParams['html'] = $html; } $result = CRM_Utils_Mail::send($mailParams); } } $schedule->free(); return $result; }
/** * returns a number of contacts from the offset that match the criteria * specified in $params. return_properties are the values that are returned * to the calling function * * @param array $params * @param array $returnProperties * @param object|array $sort object or array describing sort order for sql query. * @param int $offset the row number to start from * @param int $rowCount the number of rows to return * * @return int * @access public */ function crm_contact_search(&$params, $return_properties = null, $sort = null, $offset = 0, $row_count = 25) { $sortString = CRM_Core_DAO::getSortString($sort); return CRM_Contact_BAO_Query::apiQuery($params, $return_properties, null, $sortString, $offset, $row_count); }
/** * Build the smart group cache for a given group. * * @param int $groupID */ public static function add($groupID) { // first delete the current cache self::remove($groupID); if (!is_array($groupID)) { $groupID = array($groupID); } $returnProperties = array('contact_id'); foreach ($groupID as $gid) { $params = array(array('group', 'IN', array($gid), 0, 0)); // the below call updates the cache table as a byproduct of the query CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE); } }
/** * function to get the rows for the labels * * @param array $contactIds Contact IDS to do labels for * @param integer $locationTypeID * @param boolean $respectDoNotMail * @return array of rows for labels * @access public */ static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) { $locName = NULL; //get the address format sequence from the config file $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties(); //build the returnproperties $returnProperties = array('display_name' => 1, 'contact_type' => 1); $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format'); $mailingFormatProperties = array(); if ($mailingFormat) { $mailingFormatProperties = CRM_Contact_Form_Task_LabelCommon::regexReturnProperties($mailingFormat); $returnProperties = array_merge($returnProperties, $mailingFormatProperties); } $customFormatProperties = array(); if (stristr($mailingFormat, 'custom_')) { foreach ($mailingFormatProperties as $token => $true) { if (substr($token, 0, 7) == 'custom_') { if (!CRM_Utils_Array::value($token, $customFormatProperties)) { $customFormatProperties[$token] = $mailingFormatProperties[$token]; } } } } $returnProperties = array_merge($returnProperties, $customFormatProperties); if ($mergeSameAddress) { // we need first name/last name for summarising to avoid spillage $returnProperties['first_name'] = 1; $returnProperties['last_name'] = 1; } //get the contacts information $params = $custom = array(); foreach ($contactIDs as $key => $contactID) { $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0); } // fix for CRM-2651 if (CRM_Utils_Array::value('do_not_mail', $respectDoNotMail)) { $params[] = array('do_not_mail', '=', 0, 0, 0); } // fix for CRM-2613 $params[] = array('is_deceased', '=', 0, 0, 0); if ($locationTypeID) { $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $locName = $locType[$locationTypeID]; $location = array('location' => array("{$locName}" => $addressReturnProperties)); $returnProperties = array_merge($returnProperties, $location); $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0); } else { $returnProperties = array_merge($returnProperties, $addressReturnProperties); } foreach ($returnProperties as $name) { $cfID = CRM_Core_BAO_CustomField::getKeyID($name); if ($cfID) { $custom[] = $cfID; } } //get the total number of contacts to fetch from database. $numberofContacts = count($contactIDs); //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds) // except it also handles multiple locations $query = new CRM_Contact_BAO_Query($params, $returnProperties); $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $messageToken = CRM_Utils_Token::getTokens($mailingFormat); $details = $details[0]; $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details); foreach ($contactIDs as $value) { foreach ($custom as $cfID) { if (isset($details[$value]["custom_{$cfID}"])) { $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]); } } $contact = CRM_Utils_Array::value($value, $details); if (is_a($contact, 'CRM_Core_Error')) { return NULL; } // we need to remove all the "_id" unset($contact['contact_id']); if ($locName && CRM_Utils_Array::value($locName, $contact)) { // If location type is not primary, $contact contains // one more array as "$contact[$locName] = array( values... )" if (!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } unset($contact[$locName]); if (CRM_Utils_Array::value('county_id', $contact)) { unset($contact['county_id']); } foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } $valuesothers = array(); $paramsothers = array('contact_id' => $value); $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); if ($locationTypeID) { foreach ($valuesothers as $vals) { if (CRM_Utils_Array::value('location_type_id', $vals) == $locationTypeID) { foreach ($vals as $k => $v) { if (in_array($k, array('email', 'phone', 'im', 'openid'))) { if ($k == 'im') { $rows[$value][$k] = $v['1']['name']; } else { $rows[$value][$k] = $v['1'][$k]; } $rows[$value][$k . '_id'] = $v['1']['id']; } } } } } } else { if (!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } if (CRM_Utils_Array::value('addressee_display', $contact)) { $contact['addressee_display'] = trim($contact['addressee_display']); } if (CRM_Utils_Array::value('addressee', $contact)) { $contact['addressee'] = $contact['addressee_display']; } // now create the rows for generating mailing labels foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } } } // sigh couldn't extract out tokenfields yet return array($rows, $tokenFields); }
/** * @param $groupInfo * @param null $sort * @param null $showLinks * @param bool $returnOnlyCount * @param int $offset * @param int $rowCount * * @return array */ static function getGlobalContacts(&$groupInfo, $sort = NULL, $showLinks = NULL, $returnOnlyCount = FALSE, $offset = 0, $rowCount = 25) { $globalContacts = array(); $settingsProcessor = new CRM_Case_XMLProcessor_Settings(); $settings = $settingsProcessor->run(); if (!empty($settings)) { $groupInfo['name'] = $settings['groupname']; if ($groupInfo['name']) { $searchParams = array('name' => $groupInfo['name']); $results = array(); CRM_Contact_BAO_Group::retrieve($searchParams, $results); if ($results) { $groupInfo['id'] = $results['id']; $groupInfo['title'] = $results['title']; $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); $return = array('contact_id' => 1, 'sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); list($globalContacts) = CRM_Contact_BAO_Query::apiQuery($params, $return, NULL, $sort, $offset, $rowCount, TRUE, $returnOnlyCount); if ($returnOnlyCount) { return $globalContacts; } if ($showLinks) { foreach ($globalContacts as $idx => $contact) { $globalContacts[$idx]['sort_name'] = '<a href="' . CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$contact['contact_id']}") . '">' . $contact['sort_name'] . '</a>'; } } } } } return $globalContacts; }
/** * Function to get the all contact details(Hierarchical) * * @param int $contactId contact id * @param array $fields fields array * * @return $values array contains the contact details * @static * @access public */ function getHierContactDetails($contactId, &$fields) { $params = array('id' => $contactId); $options = array(); $returnProperties =& CRM_Contact_BAO_Contact::makeHierReturnProperties($fields); return list($query, $options) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, $options); }