|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Allows a user to add a field that can be used to upload a file. |
|
5
|
|
|
* |
|
6
|
|
|
* @package userforms |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
class EditableFileField extends EditableFormField { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
private static $singular_name = 'File Upload Field'; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
private static $plural_names = 'File Fields'; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
16
|
|
|
'Folder' => 'Folder' // From CustomFields |
|
17
|
|
|
); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Further limit uploadable file extensions in addition to the restrictions |
|
21
|
|
|
* imposed by the File.allowed_extensions global configuration. |
|
22
|
|
|
* @config |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $allowed_extensions_blacklist = array( |
|
|
|
|
|
|
25
|
|
|
'htm', 'html', 'xhtml', 'swf', 'xml' |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return FieldList |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getCMSFields() { |
|
32
|
|
|
$fields = parent::getCMSFields(); |
|
33
|
|
|
|
|
34
|
|
|
$fields->addFieldToTab( |
|
35
|
|
|
'Root.Main', |
|
36
|
|
|
TreeDropdownField::create( |
|
37
|
|
|
'FolderID', |
|
38
|
|
|
_t('EditableUploadField.SELECTUPLOADFOLDER', 'Select upload folder'), |
|
39
|
|
|
'Folder' |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("FileUploadWarning", |
|
44
|
|
|
"<p class=\"message notice\">" . _t("UserDefinedForm.FileUploadWarning", |
|
45
|
|
|
"Files uploaded through this field could be publicly accessible if the exact URL is known") |
|
46
|
|
|
. "</p>"), "Type"); |
|
47
|
|
|
|
|
48
|
|
|
return $fields; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getFormField() { |
|
52
|
|
|
$field = FileField::create($this->Name, $this->EscapedTitle) |
|
|
|
|
|
|
53
|
|
|
->setFieldHolderTemplate('UserFormsField_holder') |
|
54
|
|
|
->setTemplate('UserFormsFileField'); |
|
55
|
|
|
|
|
56
|
|
|
$field->setFieldHolderTemplate('UserFormsField_holder') |
|
57
|
|
|
->setTemplate('UserFormsFileField'); |
|
58
|
|
|
|
|
59
|
|
|
$field->getValidator()->setAllowedExtensions( |
|
60
|
|
|
array_diff( |
|
61
|
|
|
// filter out '' since this would be a regex problem on JS end |
|
62
|
|
|
array_filter(Config::inst()->get('File', 'allowed_extensions')), |
|
63
|
|
|
$this->config()->allowed_extensions_blacklist |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$folder = $this->Folder(); |
|
|
|
|
|
|
68
|
|
|
if($folder && $folder->exists()) { |
|
69
|
|
|
$field->setFolderName( |
|
70
|
|
|
preg_replace("/^assets\//","", $folder->Filename) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->doUpdateFormField($field); |
|
75
|
|
|
|
|
76
|
|
|
return $field; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return the value for the database, link to the file is stored as a |
|
82
|
|
|
* relation so value for the field can be null. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getValueFromData() { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getSubmittedFormField() { |
|
91
|
|
|
return new SubmittedFileField(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
public function migrateSettings($data) { |
|
96
|
|
|
// Migrate 'Folder' setting to 'FolderID' |
|
97
|
|
|
if(isset($data['Folder'])) { |
|
98
|
|
|
$this->FolderID = $data['Folder']; |
|
|
|
|
|
|
99
|
|
|
unset($data['Folder']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
parent::migrateSettings($data); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.