1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 class FileField extends FormField {
16
17 18 19 20 21 22 23 24
25 public $allowedMaxFileSize = array();
26
27 28 29 30 31 32 33 34 35
36 public $allowedExtensions = array();
37
38 39 40 41 42 43 44 45
46 public $relationAutoSetting = true;
47
48 49 50 51 52 53 54
55 protected $upload;
56
57 58 59 60 61 62
63 protected $folderName = 'Uploads';
64
65 66 67 68 69 70 71 72 73 74
75 function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null) {
76 if(isset($folderName)) $this->folderName = $folderName;
77 $this->upload = new Upload();
78
79 parent::__construct($name, $title, $value, $form, $rightTitle);
80 }
81
82 public function Field() {
83 if ($this->isReadonly()) {
84 if ($this->form->getRecord()) {
85 $file = $this->form->getRecord()->{$this->Name()}();
86 if ($file->ID) return $file->CMSThumbnail();
87 }
88 return '';
89 }
90
91 return $this->createTag(
92 'input',
93 array(
94 "type" => "file",
95 "name" => $this->name,
96 "id" => $this->id(),
97 "tabindex" => $this->getTabIndex()
98 )
99 ) .
100 $this->createTag(
101 'input',
102 array(
103 "type" => "hidden",
104 "name" => "MAX_FILE_SIZE",
105 "value" => $this->getValidator()->getAllowedMaxFileSize(),
106 "tabindex" => $this->getTabIndex()
107 )
108 );
109 }
110
111 public function saveInto(DataObject $record) {
112 if(!isset($_FILES[$this->name])) return false;
113
114 if($this->relationAutoSetting) {
115
116 $hasOnes = $record->has_one($this->name);
117
118 $file = (is_string($hasOnes)) ? Object::create($hasOnes) : new File();
119 } else {
120 $file = new File();
121 }
122
123 $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
124 if($this->upload->isError()) return false;
125
126 $file = $this->upload->getFile();
127
128 if($this->relationAutoSetting) {
129 if(!$hasOnes) return false;
130
131
132 $record->{$this->name . 'ID'} = $file->ID;
133 }
134 }
135
136 public function Value() {
137 return $_FILES[$this->Name()];
138 }
139
140 141 142 143 144
145 public function getValidator() {
146 return $this->upload->getValidator();
147 }
148
149 150 151 152 153
154 public function setValidator($validator) {
155 $this->upload->setValidator($validator);
156 }
157
158 159 160 161 162 163 164 165 166
167 public function getAllowedMaxFileSize($ext = null) {
168 user_error('Upload::getAllowedMaxFileSize() is deprecated. Please use Upload_Validator::getAllowedMaxFileSize() instead', E_USER_NOTICE);
169 $this->getValidator()->getAllowedMaxFileSize($ext);
170 }
171
172 173 174 175 176 177 178 179 180 181 182 183 184
185 public function setAllowedMaxFileSize($rules) {
186 user_error('Upload::setAllowedMaxFileSize() is deprecated. Please use Upload_Validator::setAllowedMaxFileSize() instead', E_USER_NOTICE);
187 $this->getValidator()->setAllowedMaxFileSize($rules);
188 }
189
190 191 192 193
194 public function getAllowedExtensions() {
195 user_error('Upload::getAllowedExtensions() is deprecated. Please use Upload_Validator::getAllowedExtensions() instead', E_USER_NOTICE);
196 return $this->getValidator()->getAllowedExtensions();
197 }
198
199 200 201 202
203 public function setAllowedExtensions($rules) {
204 user_error('Upload::setAllowedExtensions() is deprecated. Please use Upload_Validator::setAllowedExtensions() instead', E_USER_NOTICE);
205 $this->getValidator()->setAllowedExtensions($rules);
206 }
207
208 209 210
211 public function setFolderName($folderName) {
212 $this->folderName = $folderName;
213 }
214
215 216 217
218 public function getFolderName() {
219 return $this->folderName;
220 }
221
222 public function validate($validator) {
223 if(!isset($_FILES[$this->name])) return true;
224
225 $tmpFile = $_FILES[$this->name];
226
227 $valid = $this->upload->validate($tmpFile);
228 if(!$valid) {
229 $errors = $this->upload->getErrors();
230 if($errors) foreach($errors as $error) {
231 $validator->validationError($this->name, $error, "validation", false);
232 }
233 return false;
234 }
235
236 return true;
237 }
238 239 240
241 function performReadonlyTransformation() {
242 $clone = clone $this;
243 $clone->setReadonly(true);
244 return $clone;
245 }
246 }
247 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-