From 5e15a78d0d38934aefdceb6f396c30e6755f3169 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 1 May 2015 22:09:18 -0500
Subject: [PATCH] variable: Add .files to the FileUploadField in templates

---
 include/class.forms.php    | 36 ++++++++++++++++++++++++++++++++++++
 include/class.variable.php | 11 +++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/include/class.forms.php b/include/class.forms.php
index 66eb26b5e..f0ddc5df0 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2184,6 +2184,42 @@ class FileUploadField extends FormField {
             $this->attachments->deleteAll();
         }
     }
+
+    function asVar($value, $id=false) {
+        return new FileFieldAttachments($this->getFiles());
+    }
+    function asVarType() {
+        return 'FileFieldAttachments';
+    }
+}
+
+class FileFieldAttachments {
+    var $files;
+
+    function __construct($files) {
+        $this->files = $files;
+    }
+
+    function __toString() {
+        $files = array();
+        foreach ($this->files as $f) {
+            $files[] = $f->file->name;
+        }
+        return implode(', ', $files);
+    }
+
+    function getVar($tag) {
+        switch ($tag) {
+        case 'files':
+            throw new OOBContent(OOBContent::FILES, $this->files->all());
+        }
+    }
+
+    static function getVarScope() {
+        return array(
+            'files' => __('Attached files'),
+        );
+    }
 }
 
 class InlineFormData extends ArrayObject {
diff --git a/include/class.variable.php b/include/class.variable.php
index a7fc036d1..6401007ae 100644
--- a/include/class.variable.php
+++ b/include/class.variable.php
@@ -127,7 +127,7 @@ class VariableReplacer {
             $type = $content->getType();
             $existing = @$this->extras[$type] ?: array();
             $this->extras[$type] = array_merge($existing, $content->getContent());
-            return '';
+            return $content->asVar();
         }
 
         if ($parts[0] && @isset($this->variables[$parts[0]])) { //root override
@@ -321,18 +321,25 @@ class PlaceholderList
 class OOBContent extends Exception {
     var $type;
     var $content;
+    var $text;
 
     const FILES = 'files';
 
-    function __construct($type, $content) {
+    function __construct($type, $content, $asVar='') {
         $this->type = $type;
         $this->content = $content;
+        $this->text = $asVar;
     }
 
     function getType() { return $this->type; }
     function getContent() { return $this->content; }
+    function asVar() { return $this->text; }
 }
 
+/**
+ * Simple wrapper to represent a rendered or partially rendered template
+ * with extra content such as attachments
+ */
 class TextWithExtras {
     var $text = '';
     var $extras;
-- 
GitLab