Merge pull request #13346 from mfb/geocode-job-db-error
[civicrm-core.git] / Civi / Token / TokenProcessor.php
index bfad77926c101512be72a2f085d2472a1280991b..5941d0809d548e6e592c87f548126833acf3bf81 100644 (file)
@@ -25,6 +25,10 @@ class TokenProcessor {
    *     automatically from contactId.)
    *   - actionSchedule: DAO, the rule which triggered the mailing
    *     [for CRM_Core_BAO_ActionScheduler].
+   *   - schema: array, a list of fields that will be provided for each row.
+   *     This is automatically populated with any general context
+   *     keys, but you may need to add extra keys for token-row data.
+   *     ex: ['contactId', 'activityId'].
    */
   public $context;
 
@@ -68,6 +72,13 @@ class TokenProcessor {
    */
   protected $tokens = NULL;
 
+  /**
+   * A list of available tokens formatted for display
+   * @var array
+   *   Array('{' . $dottedName . '}' => 'labelString')
+   */
+  protected $listTokens = NULL;
+
   protected $next = 0;
 
   /**
@@ -75,6 +86,9 @@ class TokenProcessor {
    * @param array $context
    */
   public function __construct($dispatcher, $context) {
+    $context['schema'] = isset($context['schema'])
+      ? array_unique(array_merge($context['schema'], array_keys($context)))
+      : array_keys($context);
     $this->dispatcher = $dispatcher;
     $this->context = $context;
   }
@@ -220,6 +234,22 @@ class TokenProcessor {
     return $this->tokens;
   }
 
+  /**
+   * Get the list of available tokens, formatted for display
+   *
+   * @return array
+   *   Ex: $tokens[ '{token.name}' ] = "Token label"
+   */
+  public function listTokens() {
+    if ($this->listTokens === NULL) {
+      $this->listTokens = array();
+      foreach ($this->getTokens() as $token => $values) {
+        $this->listTokens['{' . $token . '}'] = $values['label'];
+      }
+    }
+    return $this->listTokens;
+  }
+
   /**
    * Compute and store token values.
    */