Skip to content
Snippets Groups Projects
Commit bd8eeb55 authored by Jared Hancock's avatar Jared Hancock
Browse files

variables: Implement %{message.files}

This allows for specifying that attachments from something like a new
message should be brokered with autoresponses and alerts.
parent 7c0799ea
Branches
Tags
No related merge requests found
...@@ -386,6 +386,17 @@ class Mailer { ...@@ -386,6 +386,17 @@ class Mailer {
} }
$mime = new Mail_mime($eol); $mime = new Mail_mime($eol);
// Add in extra attachments, if any from template variables
if ($message instanceof TextWithExtras
&& ($files = $message->getFiles())
) {
foreach ($files as $F) {
$file = $F->getFile();
$mime->addAttachment($file->getData(),
$file->getType(), $file->getName(), false);
}
}
// If the message is not explicitly declared to be a text message, // If the message is not explicitly declared to be a text message,
// then assume that it needs html processing to create a valid text // then assume that it needs html processing to create a valid text
// body // body
......
...@@ -793,17 +793,6 @@ implements TemplateVariable { ...@@ -793,17 +793,6 @@ implements TemplateVariable {
return $str; return $str;
} }
/* Returns file names with id as key */
function getFiles() {
$files = array();
foreach($this->attachments as $attachment)
$files[$attachment->file_id] = $attachment->file->name;
return $files;
}
/* save email info /* save email info
* TODO: Refactor it to include outgoing emails on responses. * TODO: Refactor it to include outgoing emails on responses.
*/ */
...@@ -851,17 +840,16 @@ implements TemplateVariable { ...@@ -851,17 +840,16 @@ implements TemplateVariable {
} }
function getVar($tag) { function getVar($tag) {
global $cfg; if ($tag && is_callable(array($this, 'get'.ucfirst($tag))))
if($tag && is_callable(array($this, 'get'.ucfirst($tag))))
return call_user_func(array($this, 'get'.ucfirst($tag))); return call_user_func(array($this, 'get'.ucfirst($tag)));
switch(strtolower($tag)) { switch(strtolower($tag)) {
case 'create_date': case 'create_date':
// XXX: Consider preferences of receiving user return new FormattedDate($this->getCreateDate());
return Format::datetime($this->getCreateDate(), true, 'UTC');
case 'update_date': case 'update_date':
return Format::datetime($this->getUpdateDate(), true, 'UTC'); return new FormattedDate($this->getUpdateDate());
case 'files':
throw new OOBContent(OOBContent::FILES, $this->attachments->all());
} }
return false; return false;
......
...@@ -21,8 +21,9 @@ class VariableReplacer { ...@@ -21,8 +21,9 @@ class VariableReplacer {
var $start_delim; var $start_delim;
var $end_delim; var $end_delim;
var $objects; var $objects = array();
var $variables; var $variables = array();
var $extras = array();
var $errors; var $errors;
...@@ -30,9 +31,6 @@ class VariableReplacer { ...@@ -30,9 +31,6 @@ class VariableReplacer {
$this->start_delim = $start_delim; $this->start_delim = $start_delim;
$this->end_delim = $end_delim; $this->end_delim = $end_delim;
$this->objects = array();
$this->variables = array();
} }
function setError($error) { function setError($error) {
...@@ -97,13 +95,21 @@ class VariableReplacer { ...@@ -97,13 +95,21 @@ class VariableReplacer {
function replaceVars($input) { function replaceVars($input) {
// Preserve existing extras
if ($input instanceof TextWithExtras)
$this->extras = $input->extras;
if($input && is_array($input)) if($input && is_array($input))
return array_map(array($this, 'replaceVars'), $input); return array_map(array($this, 'replaceVars'), $input);
if(!($vars=$this->_parse($input))) if(!($vars=$this->_parse($input)))
return $input; return $input;
return str_replace(array_keys($vars), array_values($vars), $input); $text = str_replace(array_keys($vars), array_values($vars), $input);
if ($this->extras) {
return new TextWithExtras($text, $this->extras);
}
return $text;
} }
function _resolveVar($var) { function _resolveVar($var) {
...@@ -113,9 +119,18 @@ class VariableReplacer { ...@@ -113,9 +119,18 @@ class VariableReplacer {
return $this->variables[$var]; return $this->variables[$var];
$parts = explode('.', $var, 2); $parts = explode('.', $var, 2);
if($parts && ($obj=$this->getObj($parts[0]))) try {
return $this->getVar($obj, $parts[1]); if ($parts && ($obj=$this->getObj($parts[0])))
elseif($parts[0] && @isset($this->variables[$parts[0]])) { //root override return $this->getVar($obj, $parts[1]);
}
catch (OOBContent $content) {
$type = $content->getType();
$existing = @$this->extras[$type] ?: array();
$this->extras[$type] = array_merge($existing, $content->getContent());
return '';
}
if ($parts[0] && @isset($this->variables[$parts[0]])) { //root override
if (is_array($this->variables[$parts[0]]) if (is_array($this->variables[$parts[0]])
&& isset($this->variables[$parts[0]][$parts[1]])) && isset($this->variables[$parts[0]][$parts[1]]))
return $this->variables[$parts[0]][$parts[1]]; return $this->variables[$parts[0]][$parts[1]];
...@@ -299,6 +314,52 @@ class PlaceholderList ...@@ -299,6 +314,52 @@ class PlaceholderList
} }
} }
/**
* Exception used in the variable replacement process to indicate non text
* content (such as attachments)
*/
class OOBContent extends Exception {
var $type;
var $content;
const FILES = 'files';
function __construct($type, $content) {
$this->type = $type;
$this->content = $content;
}
function getType() { return $this->type; }
function getContent() { return $this->content; }
}
class TextWithExtras {
var $text = '';
var $extras;
function __construct($text, array $extras) {
$this->setText($text);
$this->extras = $extras;
}
function setText($text) {
try {
$this->text = (string) $text;
}
catch (Exception $e) {
throw new InvalidArgumentException('String type is required', 0, $e);
}
}
function __toString() {
return $this->text;
}
function getFiles() {
return $this->extras[OOBContent::FILES];
}
}
interface TemplateVariable { interface TemplateVariable {
// function asVar(); — not absolutely required // function asVar(); — not absolutely required
// function getVar($name); — not absolutely required // function getVar($name); — not absolutely required
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment