1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Model\Submission; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A file uploaded on a {@link UserDefinedForm} and attached to a single |
10
|
|
|
* {@link SubmittedForm}. |
11
|
|
|
* |
12
|
|
|
* @package userforms |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class SubmittedFileField extends SubmittedFormField |
16
|
|
|
{ |
17
|
|
|
private static $has_one = [ |
|
|
|
|
18
|
|
|
'UploadedFile' => File::class |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private static $table_name = 'SubmittedFileField'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return the value of this field for inclusion into things such as |
25
|
|
|
* reports. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function getCleanValue() |
30
|
|
|
{ |
31
|
|
|
$name = $this->getFileName(); |
32
|
|
|
$link = $this->getLink(); |
33
|
|
|
$title = _t(__CLASS__.'.DOWNLOADFILE', 'Download File'); |
34
|
|
|
|
35
|
|
|
if ($link) { |
36
|
|
|
return DBField::create_field('HTMLText', sprintf( |
37
|
|
|
'%s - <a href="%s" target="_blank">%s</a>', |
38
|
|
|
$name, |
39
|
|
|
$link, |
40
|
|
|
$title |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the value for this field in the CSV export. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getExportValue() |
53
|
|
|
{ |
54
|
|
|
return ($link = $this->getLink()) ? $link : ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the link for the file attached to this submitted form field. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getLink() |
63
|
|
|
{ |
64
|
|
|
if ($file = $this->UploadedFile()) { |
|
|
|
|
65
|
|
|
if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) { |
66
|
|
|
return $this->UploadedFile()->AbsoluteLink(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the name of the file, if present |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getFileName() |
77
|
|
|
{ |
78
|
|
|
if ($this->UploadedFile()) { |
79
|
|
|
return $this->UploadedFile()->Name; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|