Advertisement
freephile

CiviCRM Custom Data Tokens

Jul 28th, 2015
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.35 KB | None | 0 0
  1. <?php
  2. /**
  3.  * A convenience function so that we can map our custom fields and labels
  4.  * in one place, and share them between hooks without having to write to the db
  5.  * with variable_set()
  6.  */
  7. function getHaystack($stack='general') {
  8.     $genKeys = array (
  9.         "wUrl"       => "custom_40",
  10.         "mainpage"   => "custom_41",
  11.         "base"       => "custom_42",
  12.         "sitename"   => "custom_43",
  13.         "logo"       => "custom_44",
  14.         "generator"  => "custom_45",
  15.         "phpversion" => "custom_46",
  16.     // "phpsapi",
  17.         "dbtype"     => "custom_47",
  18.         "dbversion"  => "custom_48",
  19.     // "externalimages",
  20.     // "langconversion",
  21.     // "titleconversion",
  22.     // "linkprefixcharset",
  23.     // "linkprefix",
  24.     // "linktrail",
  25.     // "legaltitlechars",
  26.     // "git-hash",
  27.     // "git-branch",
  28.     // "case",
  29.     // "lang",
  30.     // "fallback",
  31.     // "fallback8bitEncoding",
  32.         "writeapi"    => "custom_49",
  33.         "timezone"    => "custom_50",
  34.         "timeoffset"  => "custom_51",
  35.         "articlepath" => "custom_52",
  36.         "scriptpath"  => "custom_53",
  37.     // "script",
  38.     // "variantarticlepath",
  39.         "server"      => "custom_54",
  40.         "servername"  => "custom_55",
  41.         "wikiid"      => "custom_56",
  42.         "time"        => "custom_57",
  43.         "maxuploadsize" => "custom_58",
  44.     // "thumblimits",
  45.     // "imagelimits",
  46.         "favicon"     => "custom_59",
  47.     );
  48.     $statKeys = array(
  49.         "wUrl"     => "custom_60",
  50.         "pages"    => "custom_61",
  51.         "articles" => "custom_62",
  52.         "edits"    => "custom_63",
  53.         "images"   => "custom_64",
  54.         "users"    => "custom_65",
  55.         "activeusers" => "custom_66",
  56.         "admins"   => "custom_67",
  57.         "jobs"     => "custom_68",
  58.     );
  59.     switch ($stack) {
  60.         case 'general':
  61.             $haystack = $genKeys;
  62.             $addLabels = array (
  63.                 'recorded' => 'custom_69',
  64.             );
  65.             $haystack += $addLabels;
  66.             break;
  67.         case 'stats':
  68.             $haystack = $statKeys;
  69.             $addLabels = array (
  70.                 'recorded' => 'custom_70',
  71.             );
  72.             $haystack += $addLabels;
  73.             break;
  74.         default:
  75.             die('no stack by that name');
  76.     }
  77.     return $haystack;
  78. }
  79. /**
  80.  * implementation of hook_civicrm_tokens()
  81.  * Much appreciation and thanks to Eileen McNaughton who helped me get the logic
  82.  * correct on this. cf.https://github.com/eileenmcnaughton/civicrm_views_token/blob/master/civicrm_views_token.module#L16
  83.  *
  84.  * In the end, we want to populate more $tokens in a format like
  85.  * $token['general.generator'] = 'Generator';
  86.  * $token['general.sitename'] = 'Sitename';
  87.  * where the $token key is the element that needs to be populated
  88.  * by hook_civicrm_tokenValues()
  89.  * The $token value here is used in the UI as a label for the token.
  90.  */
  91. function eqt_civicrm_tokens(&$tokens) {
  92.    
  93.     $labels = getHaystack('general');
  94.     $tokens['general'] = array();
  95.     foreach ($labels as $k => $v) {
  96.         $tokens['general']["general.$k"] = "$k ($v)";
  97.     }
  98.    
  99.     $labels = getHaystack('stats');
  100.     $tokens['stats'] = array();
  101.     foreach ($labels as $k => $v) {
  102.         $tokens['stats']["stats.$k"] = "$k ($v)";
  103.     }
  104. }
  105. /**
  106.  * implementation of hook_civicrm_tokenValues()
  107.  */
  108. function eqt_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  109.     watchdog('eqt',"eqt_civicrm_tokenValues(\$values, \$cids, \$job, \$tokens, \$context)<pre>\n\$values=" . var_export($values,1) . "\n\$cids=" . var_export($cids,1) . "\n\$job=" . $job ."\n\$tokens=" . var_export($tokens,1) . "\n\$context=" . $context . "\n</pre>");
  110.    
  111.     if ( array_key_exists('general', $tokens) ) {
  112.         $haystack = getHaystack('general');
  113.         foreach ($cids as $cid) {
  114.             $params = array(
  115.                 'sequential' => 1,
  116.                 'entity_id' => $cid,
  117.             );
  118.             $result = civicrm_api3('CustomValue', 'get', $params);
  119.             if (!$result['is_error']) {
  120.                 $customdata = ($result['values']);
  121.                 foreach ($customdata as $val) {
  122.                     $needle = 'custom_' . $val['id'];
  123.                     $label = array_search($needle, $haystack);
  124.                     if (!$label) {
  125.                         continue;
  126.                     }
  127.                     $label = 'general.' . $label;
  128.                     $values[$cid][$label] = $val['latest'];
  129.                 }
  130.             }
  131.         }
  132.     }
  133.    
  134.     if ( array_key_exists('stats', $tokens) ) {
  135.         $haystack = getHaystack('stats');
  136.         foreach ($cids as $cid) {
  137.             $params = array(
  138.                 'sequential' => 1,
  139.                 'entity_id' => $cid,
  140.             );
  141.             $result = civicrm_api3('CustomValue', 'get', $params);
  142.             if (!$result['is_error']) {
  143.                 $customdata = ($result['values']);
  144.                 foreach ($customdata as $val) {
  145.                     $needle = 'custom_' . $val['id'];
  146.                     $label = array_search($needle, $haystack);
  147.                     if (!$label) {
  148.                         continue;
  149.                     }
  150.                     $label = 'stats.' . $label;
  151.                     $values[$cid][$label] = $val['latest'];
  152.                 }
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement