X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FToken%2FTokenProcessor.php;h=5941d0809d548e6e592c87f548126833acf3bf81;hb=668a994a23a05c0fce7eb06734acf0bd80186840;hp=99a0f0f9c43d91e1674c22f853a9aa0a39b5de82;hpb=fa71b5e1ba9b789722b265b758d38c8ecd982bdd;p=civicrm-core.git diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 99a0f0f9c4..5941d0809d 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -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; } @@ -168,6 +182,43 @@ class TokenProcessor { return new TokenRowIterator($this, new \ArrayIterator($this->rowContexts)); } + /** + * Get a list of all unique values for a given context field, + * whether defined at the processor or row level. + * + * @param string $field + * Ex: 'contactId'. + * @return array + * Ex: [12, 34, 56]. + */ + public function getContextValues($field, $subfield = NULL) { + $values = []; + if (isset($this->context[$field])) { + if ($subfield) { + if (isset($this->context[$field]->$subfield)) { + $values[] = $this->context[$field]->$subfield; + } + } + else { + $values[] = $this->context[$field]; + } + } + foreach ($this->getRows() as $row) { + if (isset($row->context[$field])) { + if ($subfield) { + if (isset($row->context[$field]->$subfield)) { + $values[] = $row->context[$field]->$subfield; + } + } + else { + $values[] = $row->context[$field]; + } + } + } + $values = array_unique($values); + return $values; + } + /** * Get the list of available tokens. * @@ -183,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. */