added communication tokens (ie all emails, all phone numbers)

This commit is contained in:
sgladstone 2016-09-05 15:08:57 -04:00
parent 32bfadcb66
commit 4333b6ca8e
3 changed files with 238 additions and 1 deletions

View File

@ -146,6 +146,24 @@ function fancytokens_civicrm_tokens( &$tokens ){
); );
$tok_category_label = " :: Dates";
$tokens['dates'] = array(
'dates.today' => 'Today (m/d/yyyy)'.$tok_category_label,
'dates.today___j_F_yyyy' => 'Today (d monthname yyyy)'.$tok_category_label,
'dates.today___F_j_yyyy' => 'Today (monthname d, yyyy)'.$tok_category_label,
'dates.birth_date___F_j' => 'Birth Date (monthname d)'.$tok_category_label,
);
$tok_category_label = " :: Communication";
$tokens['communication'] = array(
'communication.phone_all' => 'All Phone Numbers'.$tok_category_label,
'communication.email_all' => 'All Email Addresses'.$tok_category_label,
);
} }
@ -173,6 +191,77 @@ function fancytokens_civicrm_tokens( &$tokens ){
function fancytokens_civicrm_tokenValues( &$values, &$contactIDs, $job = null, $tokens = array(), $context = null) { function fancytokens_civicrm_tokenValues( &$values, &$contactIDs, $job = null, $tokens = array(), $context = null) {
if(!empty($tokens['dates'])){
// deal with tokens for 'today', birth_date', and similar.
$today = date_create();
foreach ( $contactIDs as $cid ) {
$values[$cid]['dates.today'] = date("n/j/Y");
$values[$cid]['dates.today___j_F_yyyy'] = date("j F Y");
$values[$cid]['dates.today___F_j_yyyy'] = date("F j, Y");
}
$birthday_token = 'dates.birth_date___F_j' ;
while( $cur_token_raw = current( $tokens['dates'] )){
$tmp_key = key($tokens['dates']);
$cur_token = '';
if( is_numeric( $tmp_key)){
$cur_token = $cur_token_raw;
}else{
// Its being used by CiviMail.
$cur_token = $tmp_key;
}
$token_to_fill = 'dates.'.$cur_token;
if($token_to_fill == $birthday_token ){
$sql_cids = implode( ",", $contactIDs);
if( strlen( $sql_cids) > 0 ){
$sql = "select c.id as contact_id, date_format( c.birth_date, '%M %e') as birth_date
FROM civicrm_contact c
WHERE c.id IN ( ".$sql_cids." )
ORDER BY c.id" ;
$dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ) ;
while($dao->fetch()){
$cid = $dao->contact_id ;
$birth_date_formatted = $dao->birth_date;
$values[$cid][$birthday_token] = $birth_date_formatted ;
}
$dao->free();
}
}
next($tokens['dates']);
}
}
if(!empty($tokens['communication'] )){
$phone_token = 'communication.phone_all' ;
$email_token = 'communication.email_all' ;
require_once( 'utils/CommunicationTokenHelper.php');
$commUtils = new CommunicationTokenHelper();
$commUtils->getTableOfPhones($contactIDs, $values, $phone_token );
$commUtils->getTableOfEmails($contactIDs, $values, $email_token );
}
if (!empty($tokens['greetings'])) { if (!empty($tokens['greetings'])) {
$greetings_token_names = array( $greetings_token_names = array(

View File

@ -15,7 +15,7 @@
<email>info@fountaintribe.com</email> <email>info@fountaintribe.com</email>
</maintainer> </maintainer>
<releaseDate>2016-08-28</releaseDate> <releaseDate>2016-08-28</releaseDate>
<version>3.9</version> <version>4.0</version>
<develStage>stable</develStage> <develStage>stable</develStage>
<compatibility> <compatibility>
<ver>4.4</ver> <ver>4.4</ver>

View File

@ -0,0 +1,148 @@
<?php
class CommunicationTokenHelper{
function getTableOfPhones(&$contactIDs, &$values, &$token_needed ){
require_once('utils/CustomSearchTools.php');
$tmp = new CustomSearchTools();
$sql_cids = $tmp->convertArrayToSqlString($contactIDs);
$sql = "select ph.contact_id, ph.phone , ph.is_primary,
lt.display_name as location_type_label,
ov.label as phone_type_label
FROM civicrm_phone ph
LEFT JOIN civicrm_location_type lt ON ph.location_type_id = lt.id ,
civicrm_option_value ov ,
civicrm_option_group og
WHERE ph.contact_id IN ( ".$sql_cids." )
AND ph.phone_type_id = ov.value
AND ov.option_group_id = og.id AND og.name = 'phone_type'
ORDER BY ph.contact_id" ;
// print "\n\n<br><br>sql: ".$sql;
$dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ) ;
$first = true;
$prev_cid = "";
while($dao->fetch()){
//print "\n\n<br>Inside while loop";
$cid = $dao->contact_id ;
$phone = $dao->phone;
$is_primary = $dao->is_primary;
$location_type_label = $dao->location_type_label;
$phone_type_label = $dao->phone_type_label;
if(strcmp( $is_primary, '1') == 0 ){
$is_primary_formatted = 'Yes';
}else{
$is_primary_formatted = 'No';
}
if( strcmp($cid, $prev_cid ) <> 0 ){
// First phone for this contact
$values[$cid][$token_needed] = "<table border=0 width='100%'><tr><th align=left>Number</th>
<th align=left>Is Primary?</th><th align=left>Location</th><Th align=left>Phone Type</th></tr>";
// wrap up token for prev. cid
if( strcmp( $prev_cid, "") <> 0){
$values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."</table>";
}
}
// add the data for this child to this parent's token
$values[$cid][$token_needed] = $values[$cid][$token_needed]."<tr><td>$phone</td>".
"<td>$is_primary_formatted</td><td>$location_type_label</td><td>$phone_type_label</td></tr>";
$prev_cid = $cid;
}
$dao->free();
if( strcmp( $prev_cid, "") <> 0){
$values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."</table>";
}
}
function getTableOfEmails(&$contactIDs, &$values, &$token_needed){
require_once('utils/CustomSearchTools.php');
$tmp = new CustomSearchTools();
$sql_cids = $tmp->convertArrayToSqlString($contactIDs);
$sql = "select em.contact_id, em.email , em.is_primary,
lt.display_name as location_type_label
FROM civicrm_email em
LEFT JOIN civicrm_location_type lt ON em.location_type_id = lt.id
WHERE em.contact_id IN ( ".$sql_cids." )
ORDER BY em.contact_id" ;
// print "\n\n<br><br>sql: ".$sql;
$dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ) ;
$first = true;
$prev_cid = "";
while($dao->fetch()){
//print "\n\n<br>Inside while loop";
$cid = $dao->contact_id ;
$email = $dao->email;
$is_primary = $dao->is_primary;
$location_type_label = $dao->location_type_label;
if(strcmp( $is_primary, '1') == 0 ){
$is_primary_formatted = 'Yes';
}else{
$is_primary_formatted = 'No';
}
if( strcmp($cid, $prev_cid ) <> 0 ){
// First phone for this contact
$values[$cid][$token_needed] = "<table border=0 width='100%'><tr><th align=left>Email</th>
<th align=left>Is Primary?</th><th align=left>Location</th></tr>";
// wrap up token for prev. cid
if( strcmp( $prev_cid, "") <> 0){
$values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."</table>";
}
}
// add the data for this child to this parent's token
$values[$cid][$token_needed] = $values[$cid][$token_needed]."<tr><td>$email</td>".
"<td>$is_primary_formatted</td><td>$location_type_label</td></tr>";
$prev_cid = $cid;
}
$dao->free();
if( strcmp( $prev_cid, "") <> 0){
$values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."</table>";
}
}
}