You are here

function subscriptions_mail_template_preprocess in Subscriptions 5.2

Same name and namespace in other branches
  1. 6 subscriptions_mail.cron.inc \subscriptions_mail_template_preprocess()

Preprocess a mail template (subject or body), detecting conditional clauses that conform to a prescribed syntax

Parameters

string $template: the template for preprocessing

array $mailvars: an associatvie array of currently existing variables that are to be interpolated into the template later , and which can be used by this function for preprocessing

This function allows the administrator to specify ternary-type conditions to determine what text is used in a mail in a particular situation, using the variables that are currently available for that mail for reference. The syntax is standard PHP/C-style ternary syntax, but only allows the "==" and "!=": {{!variable_name==sometext?text for true condition:text for false condition}}

sometext must not contain a question mark, and the true text no colon.

2 calls to subscriptions_mail_template_preprocess()
subscriptions_mail_cron in ./subscriptions_mail.module
Implementation of hook_cron().
subscriptions_template_preprocess in ./subscriptions_mail.module

File

./subscriptions_mail.module, line 392
Subscriptions module mail gateway.

Code

function subscriptions_mail_template_preprocess($template, $mailvars) {
  preg_match_all('/{{(?P<condition>[^?]+?)\\?(?P<true>[^:]*?):(?P<false>[^\\]]*?)}}/', $template, $conditions);

  // locate the actual operators/operand for each
  $replacement = '';
  foreach ($conditions[0] as $k => $v) {
    preg_match('/(?P<operand_1>!.+)\\s*(?P<operator>==|!=)\\s*(?P<operand_2>.+)/', $conditions['condition'][$k], $matches);
    $operand1 = isset($mailvars[$matches['operand_1']]) ? $mailvars[$matches['operand_1']] : $matches['operand_1'];
    if ($matches['operator'] == '==') {
      $replacement = $operand1 == $matches['operand_2'] ? $conditions['true'][$k] : $conditions['false'][$k];
    }
    elseif ($matches['operator'] == '!=') {
      $replacement = $operand1 != $matches['operand_2'] ? $conditions['true'][$k] : $conditions['false'][$k];
    }
    else {
      continue;
    }

    // replace the condition with the result of its evalutation
    $template = str_replace($v, $replacement, $template);
  }
  return $template;
}