function subscriptions_mail_template_preprocess in Subscriptions 6
Same name and namespace in other branches
- 5.2 subscriptions_mail.module \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. true_text and false_text may contain newlines as well as a nested conditional expression (one level of recursion).
1 call to subscriptions_mail_template_preprocess()
- _subscriptions_mail_cron in ./
subscriptions_mail.cron.inc - Implementation of hook_cron().
File
- ./
subscriptions_mail.cron.inc, line 349 - Subscriptions module mail gateway (cron functions).
Code
function subscriptions_mail_template_preprocess($template, $mailvars) {
preg_match_all('/{{(?P<condition>[^?]+?)\\?(?P<true>({{(.|\\n)*?}})|([^:]*?)):(?P<false>({{(.|\\n)*?}})|((.|\\n)*?))}}/', $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 empty($conditions[0]) ? $template : subscriptions_mail_template_preprocess($template, $mailvars);
}