mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-03 00:07:58 +02:00
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@56213 467b73ca-7a2a-4603-9d3b-597d59a354a9
65 lines
1.7 KiB
PHP
Executable File
65 lines
1.7 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Allows a user to add a field that can be used to upload a file
|
|
* @package forms
|
|
* @subpackage fieldeditor
|
|
*/
|
|
class EditableFileField extends EditableFormField {
|
|
|
|
// this needs to be moved.
|
|
static $has_one = array(
|
|
"UploadedFile" => "File"
|
|
);
|
|
|
|
/**
|
|
* @see {Upload->allowedMaxFileSize}
|
|
* @var int
|
|
*/
|
|
public static $allowed_max_file_size;
|
|
|
|
/**
|
|
* @see {Upload->allowedExtensions}
|
|
* @var array
|
|
*/
|
|
public static $allowed_extensions = array();
|
|
|
|
static $singular_name = 'File field';
|
|
static $plural_names = 'File fields';
|
|
|
|
function getFormField() {
|
|
if($field = parent::getFormField())
|
|
return $field;
|
|
return new FileField($this->Name, $this->Title, $this->getField('Default'));
|
|
// TODO We can't use the preview feature because FileIFrameField also shows the "From the file store" functionality
|
|
//return new FileIFrameField( $this->Name, $this->Title, $this->getField('Default') );
|
|
}
|
|
|
|
function getSimpleFormField(){
|
|
return new FileField($this->Name, $this->Title, $this->getField('Default'));
|
|
}
|
|
|
|
function createSubmittedField($data, $submittedForm, $fieldClass = "SubmittedFileField") {
|
|
if(!$_FILES[$this->Name])
|
|
return null;
|
|
|
|
$submittedField = new $fieldClass();
|
|
$submittedField->Title = $this->Title;
|
|
$submittedField->Name = $this->Name;
|
|
$submittedField->ParentID = $submittedForm->ID;
|
|
|
|
// create the file from post data
|
|
$upload = new Upload();
|
|
$upload->setAllowedExtensions(self::$allowed_extensions);
|
|
$upload->setAllowedMaxFileSize(self::$allowed_max_file_size);
|
|
|
|
// upload file
|
|
$upload->load($_FILES[$this->Name]);
|
|
|
|
$uploadedFile = $upload->getFile();
|
|
$submittedField->UploadedFileID = $uploadedFile->ID;
|
|
$submittedField->write();
|
|
|
|
return $submittedField;
|
|
}
|
|
}
|
|
?>
|