diff --git a/include/class.forms.php b/include/class.forms.php index 423dc3c5ec6c6a2c7e2ea83f1a995a94b38245af..4a447cd14bcd0def2ed14a24892983b5023fc62c 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -2688,6 +2688,7 @@ class FileUploadField extends FormField { static $widget = 'FileUploadWidget'; protected $attachments; + protected $files; static function getFileTypes() { static $filetypes; @@ -2859,7 +2860,7 @@ class FileUploadField extends FormField { return ($ext && is_array($allowed) && in_array(".$ext", $allowed)); } - function getFiles() { + function getAttachments() { if (!isset($this->attachments) && ($a = $this->getAnswer()) && ($e = $a->getEntry()) && ($e->get('id')) ) { @@ -2875,6 +2876,18 @@ class FileUploadField extends FormField { $this->attachments = $att; } + function getFiles() { + if (!isset($this->files)) { + $files = array(); + foreach ($this->getAttachments() as $a) { + if ($a && ($f=$a->getFile())) + $files[] = $f; + } + $this->files = $files; + } + return $this->files; + } + function getConfiguration() { $config = parent::getConfiguration(); $_types = self::getFileTypes(); @@ -2932,8 +2945,8 @@ class FileUploadField extends FormField { // array. Then, inspect the difference between the files actually // attached to this field function to_database($value) { - $this->getFiles(); - if (isset($this->attachments)) { + $this->getAttachments(); + if (isset($this->attachments) && $this->attachments) { $this->attachments->keepOnlyFileIds($value); } return JsonDataEncoder::encode($value); @@ -2962,21 +2975,22 @@ class FileUploadField extends FormField { function toString($value) { $files = array(); foreach ($this->getFiles() as $f) { - $files[] = $f->file->name; + $files[] = $f->name; } return implode(', ', $files); } function db_cleanup($field=false) { - // Delete associated attachments from the database, if any - $this->getFiles(); - if (isset($this->attachments)) { + if ($this->getAttachments()) { $this->attachments->deleteAll(); } } function asVar($value, $id=false) { - return new FileFieldAttachments($this->getFiles()); + if (($attachments = $this->getAttachments())) + $attachments = $attachments->all(); + + return new FileFieldAttachments($attachments ?: array()); } function asVarType() { return 'FileFieldAttachments'; @@ -3015,26 +3029,30 @@ class FileUploadField extends FormField { } class FileFieldAttachments { - var $files; + var $attachments; - function __construct($files) { - $this->files = $files; + function __construct($attachments) { + $this->attachments = $attachments; } function __toString() { $files = array(); - foreach ($this->files as $f) { - $files[] = $f->file->name; + foreach ($this->getAttachments() as $a) { + $files[] = $a->getFilename(); } return implode(', ', $files); } + function getAttachments() { + return $this->attachments ?: array(); + } + function getVar($tag) { switch ($tag) { case 'names': return $this->__toString(); case 'files': - throw new OOBContent(OOBContent::FILES, $this->files->all()); + throw new OOBContent(OOBContent::FILES, $this->getAttachments()); } } @@ -3891,7 +3909,7 @@ class FileUploadWidget extends Widget { $config = $this->field->getConfiguration(); $name = $this->field->getFormName(); $id = substr(md5(spl_object_hash($this)), 10); - $attachments = $this->field->getFiles(); + $attachments = $this->field->getAttachments(); $mimetypes = array_filter($config['__mimetypes'], function($t) { return strpos($t, '/') !== false; } ); @@ -4048,21 +4066,22 @@ class FreeTextField extends FormField { } function getAttachments() { - if (!isset($this->attachments)) $this->attachments = GenericAttachments::forIdAndType($this->get('id'), 'I'); - return $this->attachments; + return $this->attachments ?: array(); } function getFiles() { - - if (!($attachments = $this->getAttachments())) - return array(); - - return $attachments->all(); + if (!isset($this->files)) { + $files = array(); + if (($attachments=$this->getAttachments())) + foreach ($attachments->all() as $a) + $files[] = $a->getFile(); + $this->files = $files; + } + return $this->files; } - } class FreeTextWidget extends Widget { @@ -4084,10 +4103,10 @@ class FreeTextWidget extends Widget { echo Format::viewableImages($config['content']); ?></div> </div> <?php - if (($attachments=$this->field->getFiles())) { ?> + if (($attachments = $this->field->getAttachments()) && count($attachments)) { ?> <section class="freetext-files"> <div class="title"><?php echo __('Related Resources'); ?></div> - <?php foreach ($attachments as $attach) { ?> + <?php foreach ($attachments->all() as $attach) { ?> <div class="file"> <a href="<?php echo $attach->file->getDownloadUrl(); ?>" target="_blank" download="<?php echo $attach->file->getDownloadUrl(); diff --git a/include/class.mailer.php b/include/class.mailer.php index a97cf650f1addc3b6537b901b1c46f1c3c08a78a..08de4edc7378bcc9bb91c080652fac97680e28c1 100644 --- a/include/class.mailer.php +++ b/include/class.mailer.php @@ -422,10 +422,10 @@ class Mailer { // Add in extra attachments, if any from template variables if ($message instanceof TextWithExtras - && ($files = $message->getFiles()) + && ($attachments = $message->getAttachments()) ) { - foreach ($files as $F) { - $file = $F->getFile(); + foreach ($attachments as $a) { + $file = $a->getFile(); $mime->addAttachment($file->getData(), $file->getType(), $file->getName(), false); } diff --git a/include/class.variable.php b/include/class.variable.php index 8b346e0cd387483494dc2f245211a16ac75a5784..6a1517b9426a551afe416cb8441f713f37aff421 100644 --- a/include/class.variable.php +++ b/include/class.variable.php @@ -383,8 +383,8 @@ class TextWithExtras { return $this->text; } - function getFiles() { - return $this->extras[OOBContent::FILES]; + function getAttachments() { + return $this->extras[OOBContent::FILES] ?: array(); } }