/** * Replace all the contact-level tokens in $str with information from * $contact. * * @param string $str * The string with tokens to be replaced. * @param array $contact * Associative array of contact properties. * @param bool $html * Replace tokens with HTML or plain text. * @param array $knownTokens * A list of tokens that are known to exist in the email body. * @param bool $returnBlankToken * Return unevaluated token if value is null. * * @param bool $escapeSmarty * * @return string * The processed string */ public static function &replaceContactTokens($str, &$contact, $html = FALSE, $knownTokens = NULL, $returnBlankToken = FALSE, $escapeSmarty = FALSE) { $key = 'contact'; if (self::$_tokens[$key] == NULL) { // This should come from UF self::$_tokens[$key] = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields('All')), array('checksum', 'contact_id')); } // here we intersect with the list of pre-configured valid tokens // so that we remove anything we do not recognize // I hope to move this step out of here soon and // then we will just iterate on a list of tokens that are passed to us if (!$knownTokens || empty($knownTokens[$key])) { return $str; } $str = preg_replace_callback(self::tokenRegex($key), function ($matches) use(&$contact, $html, $returnBlankToken, $escapeSmarty) { return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty); }, $str); $str = preg_replace('/\\\\|\\{(\\s*)?\\}/', ' ', $str); return $str; }
/** * * getTokenData receives a token from an email * and returns the appropriate data for the token * */ private function getTokenData(&$token_a, $html = false, &$contact, &$verp, &$urls, $event_queue_id) { $type = $token_a['type']; $token = $token_a['token']; $data = $token; $escapeSmarty = defined('CIVICRM_MAIL_SMARTY') ? true : false; if ($type == 'embedded_url') { $embed_data = array(); foreach ($token as $t) { $embed_data[] = $this->getTokenData($t, $html = false, $contact, $verp, $urls, $event_queue_id); } $numSlices = count($embed_data); $url = ''; for ($i = 0; $i < $numSlices; $i++) { $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}"; } if (isset($token_a['embed_parts'][$numSlices])) { $url .= $token_a['embed_parts'][$numSlices]; } // add trailing quote since we've gobbled it up in a previous regex // function getPatterns, line 431 if (preg_match('/^href[ ]*=[ ]*\'/', $url)) { $url .= "'"; } else { if (preg_match('/^href[ ]*=[ ]*\\"/', $url)) { $url .= '"'; } } $data = $url; } else { if ($type == 'url') { if ($this->url_tracking) { $data = CRM_Mailing_BAO_TrackableURL::getTrackerURL($token, $this->id, $event_queue_id); } else { $data = $token; } } else { if ($type == 'contact') { $data = CRM_Utils_Token::getContactTokenReplacement($token, $contact, false, false, $escapeSmarty); } else { if ($type == 'action') { $data = CRM_Utils_Token::getActionTokenReplacement($token, $verp, $urls, $html); } else { if ($type == 'domain') { require_once 'CRM/Core/BAO/Domain.php'; $domain =& CRM_Core_BAO_Domain::getDomain(); $data = CRM_Utils_Token::getDomainTokenReplacement($token, $domain, $html); } else { if ($type == 'mailing') { require_once 'CRM/Mailing/BAO/Mailing.php'; $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->find(true); if ($token == 'name') { $data = $mailing->name; } else { if ($token == 'group') { $groups = $mailing->getGroupNames(); $data = implode(', ', $groups); } } } else { $data = CRM_Utils_Array::value("{$type}.{$token}", $contact); } } } } } } return $data; }
/** * * getTokenData receives a token from an email * and returns the appropriate data for the token */ private function getTokenData(&$token_a, $html = FALSE, &$contact, &$verp, &$urls, $event_queue_id) { $type = $token_a['type']; $token = $token_a['token']; $data = $token; $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE; if ($type == 'embedded_url') { $embed_data = array(); foreach ($token as $t) { $embed_data[] = $this->getTokenData($t, $html = FALSE, $contact, $verp, $urls, $event_queue_id); } $numSlices = count($embed_data); $url = ''; for ($i = 0; $i < $numSlices; $i++) { $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}"; } if (isset($token_a['embed_parts'][$numSlices])) { $url .= $token_a['embed_parts'][$numSlices]; } // add trailing quote since we've gobbled it up in a previous regex // function getPatterns, line 431 if (preg_match('/^href[ ]*=[ ]*\'/', $url)) { $url .= "'"; } elseif (preg_match('/^href[ ]*=[ ]*\\"/', $url)) { $url .= '"'; } $data = $url; } elseif ($type == 'url') { if ($this->url_tracking) { $data = CRM_Mailing_BAO_TrackableURL::getTrackerURL($token, $this->id, $event_queue_id); if (!empty($html)) { $data = htmlentities($data, ENT_NOQUOTES); } } else { $data = $token; } } elseif ($type == 'contact') { $data = CRM_Utils_Token::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty); } elseif ($type == 'action') { $data = CRM_Utils_Token::getActionTokenReplacement($token, $verp, $urls, $html); } elseif ($type == 'domain') { $domain = CRM_Core_BAO_Domain::getDomain(); $data = CRM_Utils_Token::getDomainTokenReplacement($token, $domain, $html); } elseif ($type == 'mailing') { if ($token == 'name') { $data = $this->name; } elseif ($token == 'group') { $groups = $this->getGroupNames(); $data = implode(', ', $groups); } } else { $data = CRM_Utils_Array::value("{$type}.{$token}", $contact); } return $data; }
function postProcess() { $values = $this->_contactIds; $config = CRM_Core_Config::singleton(); $msg_id = $this->_submitValues['message_template']; if (!empty($msg_id)) { $mysql = " SELECT * FROM veda_civicrm_wordmailmerge WHERE msg_template_id = %1"; $params = array(1 => array($msg_id, 'Integer')); $dao = CRM_Core_DAO::executeQuery($mysql, $params); //$dao = CRM_Core_DAO::executeQuery($mysql); while ($dao->fetch()) { $fileId = $dao->file_id; } $sql = "SELECT * FROM civicrm_file WHERE id = %1"; $params = array(1 => array($fileId, 'Integer')); $dao = CRM_Core_DAO::executeQuery($sql, $params); //$dao = CRM_Core_DAO::executeQuery($sql); while ($dao->fetch()) { $default['fileID'] = $dao->id; $default['mime_type'] = $dao->mime_type; $default['fileName'] = $dao->uri; $default['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri); $default['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri; $default['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs('civicrm_file', $msg_id, $dao->id); } $defaults[$dao->id] = $default; $this->assign('defaults', $defaults); $noofContact = count($this->_contactIds); require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong/tbs_class.php'; require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong-opentbs/tbs_plugin_opentbs.php'; $TBS = new clsTinyButStrong(); // new instance of TBS $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load the OpenTBS plugin $template = $default['fullPath']; // contactrows to check for duplicate address $contactrows = array(); foreach ($values as $key => $value) { $SelectedcontactID = $values[$key]; // get the details for all selected contacts list($contactDetails) = CRM_Utils_Token::getTokenDetails(array($SelectedcontactID), $this->_returnProperties, NULL, NULL, FALSE, $this->_allTokens); // populate contactrows array to check dupliacte address $contactrows[$SelectedcontactID] = $contactDetails[$SelectedcontactID]; } // if merge_letter_for_same_address selected check for duplicate address if (isset($this->_submitValues['merge_letter_for_same_address']) && $this->_submitValues['merge_letter_for_same_address']) { CRM_Core_BAO_Address::mergeSameAddress($contactrows); } foreach ($values as $key => $value) { if ($key < $noofContact) { $selectedCID = $values[$key]; $contactFormatted = array(); // if contact_id found in filtered contactrows array get contact details from contactrows if (array_key_exists($selectedCID, $contactrows)) { $contactFormatted[$selectedCID] = $contactrows[$selectedCID]; $membershipFormatted = array(); if ($this->_searchFrom == 'member' && isset($contactFormatted[$selectedCID]['membership_id'])) { $membershipFormatted = CRM_Utils_Token::getMembershipTokenDetails($contactFormatted[$selectedCID]['membership_id']); } foreach ($this->_tokenMerge as $atKey => $atValue) { // Replace hook tokens $explodedTokenName = explode('.', $atValue['token_name']); // this is fixed by assigning 'address_block' token into 'contact' token array // gopi@vedaconsulting.co.uk //need to do proper fix seems token named as contact.address_block // $atValue['token_name'] = ($atValue['token_name'] == 'address_block') ? 'contact.'.$atValue['token_name'] : $atValue['token_name']; if (array_key_exists($atValue['token_name'], $contactFormatted[$selectedCID])) { if (!empty($explodedTokenName[1]) && $explodedTokenName[0] != 'contact') { $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = $contactFormatted[$selectedCID][$atValue['token_name']]; } else { $vars[$key][$atValue['token_name']] = $contactFormatted[$selectedCID][$atValue['token_name']]; } } else { if ($explodedTokenName[0] == 'membership') { $explodedTokenName[1] = $explodedTokenName[1] == 'membership_id' ? 'id' : $explodedTokenName[1]; $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = CRM_Utils_Token::getMembershipTokenReplacement($explodedTokenName[1], $membershipFormatted[$contactFormatted[$selectedCID]['membership_id']]); } else { $vars[$key][$atValue['token_name']] = CRM_Utils_Token::getContactTokenReplacement($atValue['token_name'], $contactFormatted[$selectedCID], FALSE, FALSE); } } //need to do proper fix, token_name.date seems not returning null value if not found if ($explodedTokenName[0] == 'token_name' && !is_array($vars[$key]['token_name'])) { $vars[$key][$atValue['token_name']] = ''; } } //to skip error, if by chance using membership token in 'find contact' search if ($this->_searchFrom != 'member') { foreach (CRM_Core_SelectValues::membershipTokens() as $token => $label) { $token = str_replace(array('{', '}'), "", $token); $tokenNames = explode('.', $token); $vars[$key]['membership'][$tokenNames[1]] = $label; } } foreach ($vars[$key] as $varKey => $varValue) { $explodeValues = explode('.', $varKey); if (isset($explodeValues[1]) && !empty($explodeValues[1])) { $vars[$key][$explodeValues[0]][$explodeValues[1]] = $vars[$key][$varKey]; unset($vars[$key][$varKey]); } } // blank lines removed while creating the address_block - gopi@vedaconsulting.co.uk /*if (!empty($vars[$key]['contact']['address_block'])) { $vars[$key]['contact']['address_block'] = str_replace('<br />', "", $vars[$key]['contact']['address_block']); }*/ $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8); $TBS->MergeBlock(self::TOKEN_VAR_NAME, $vars); } } } $output_file_name = 'CiviCRMWordExport.docx'; $TBS->Show(OPENTBS_DOWNLOAD, $output_file_name); CRM_Utils_System::civiExit(); } parent::postProcess(); }