Completed
Push — master ( 49a300...32ae99 )
by Will
9s
created

EditableFileField::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
		if(isset(Config::inst()->get('EditableFileField', 'use_uploadfield')) && Config::inst()->get('EditableFileField', 'use_uploadfield')) {
53
			$field = Upload::create($this->Name, $this->EscapedTitle)
54
		} else {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
55
			$field = FileField::create($this->Name, $this->EscapedTitle)
56
		}
57
	
58
		$field->setFieldHolderTemplate('UserFormsField_holder')
59
				->setTemplate('UserFormsFileField');
60
61
		$field->getValidator()->setAllowedExtensions(
62
			array_diff(
63
				// filter out '' since this would be a regex problem on JS end
64
				array_filter(Config::inst()->get('File', 'allowed_extensions')),
65
				$this->config()->allowed_extensions_blacklist
66
			)
67
		);
68
69
		$folder = $this->Folder();
70
		if($folder && $folder->exists()) {
71
			$field->setFolderName(
72
				preg_replace("/^assets\//","", $folder->Filename)
73
			);
74
		}
75
76
		$this->doUpdateFormField($field);
77
78
		return $field;
79
	}
80
81
82
	/**
83
	 * Return the value for the database, link to the file is stored as a
84
	 * relation so value for the field can be null.
85
	 *
86
	 * @return string
87
	 */
88
	public function getValueFromData() {
89
		return null;
90
	}
91
92
	public function getSubmittedFormField() {
93
		return new SubmittedFileField();
94
	}
95
96
97
	public function migrateSettings($data) {
98
		// Migrate 'Folder' setting to 'FolderID'
99
		if(isset($data['Folder'])) {
100
			$this->FolderID = $data['Folder'];
101
			unset($data['Folder']);
102
		}
103
104
		parent::migrateSettings($data);
105
	}
106
}
107