From 4333b6ca8e6ba3afccefc787d8da6817f67f903d Mon Sep 17 00:00:00 2001 From: sgladstone Date: Mon, 5 Sep 2016 15:08:57 -0400 Subject: [PATCH] added communication tokens (ie all emails, all phone numbers) --- fancytokens.php | 89 +++++++++++++++++ info.xml | 2 +- utils/CommunicationTokenHelper.php | 148 +++++++++++++++++++++++++++++ 3 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 utils/CommunicationTokenHelper.php diff --git a/fancytokens.php b/fancytokens.php index 1909660..9d5e1b5 100644 --- a/fancytokens.php +++ b/fancytokens.php @@ -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) { + + 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'])) { $greetings_token_names = array( diff --git a/info.xml b/info.xml index ad3c8bf..5e219bf 100644 --- a/info.xml +++ b/info.xml @@ -15,7 +15,7 @@ info@fountaintribe.com 2016-08-28 - 3.9 + 4.0 stable 4.4 diff --git a/utils/CommunicationTokenHelper.php b/utils/CommunicationTokenHelper.php new file mode 100644 index 0000000..fb7015c --- /dev/null +++ b/utils/CommunicationTokenHelper.php @@ -0,0 +1,148 @@ +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

sql: ".$sql; + $dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ) ; + + $first = true; + $prev_cid = ""; + + while($dao->fetch()){ + //print "\n\n
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] = " + "; + // wrap up token for prev. cid + if( strcmp( $prev_cid, "") <> 0){ + + $values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."
NumberIs Primary?LocationPhone Type
"; + + } + } + + // add the data for this child to this parent's token + $values[$cid][$token_needed] = $values[$cid][$token_needed]."$phone". + "$is_primary_formatted$location_type_label$phone_type_label"; + + + + + $prev_cid = $cid; + } + $dao->free(); + if( strcmp( $prev_cid, "") <> 0){ + + $values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed].""; + + } + + + + + } + + 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

sql: ".$sql; + $dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray ) ; + + $first = true; + $prev_cid = ""; + + while($dao->fetch()){ + //print "\n\n
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] = " + "; + // wrap up token for prev. cid + if( strcmp( $prev_cid, "") <> 0){ + + $values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed]."
EmailIs Primary?Location
"; + + } + } + + // add the data for this child to this parent's token + $values[$cid][$token_needed] = $values[$cid][$token_needed]."$email". + "$is_primary_formatted$location_type_label"; + + + + + $prev_cid = $cid; + } + $dao->free(); + + if( strcmp( $prev_cid, "") <> 0){ + + $values[$prev_cid][$token_needed] = $values[$prev_cid][$token_needed].""; + + } + + } + + + + +}