2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-06-15 15:33:53 +02:00
|
|
|
* Allows a user to add a field that can be used to upload a file
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fieldeditor
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class EditableFileField extends EditableFormField {
|
|
|
|
|
|
|
|
// this needs to be moved.
|
|
|
|
static $has_one = array(
|
|
|
|
"UploadedFile" => "File"
|
|
|
|
);
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* @see {Upload->allowedMaxFileSize}
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $allowed_max_file_size;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see {Upload->allowedExtensions}
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $allowed_extensions = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
static $singular_name = 'File field';
|
|
|
|
static $plural_names = 'File fields';
|
|
|
|
|
|
|
|
function getFormField() {
|
2008-04-06 10:20:13 +02:00
|
|
|
if($field = parent::getFormField())
|
2007-07-19 12:40:28 +02:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
function createSubmittedField($data, $submittedForm, $fieldClass = "SubmittedFileField") {
|
|
|
|
if(!$_FILES[$this->Name])
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
|
|
|
|
|
|
|
$submittedField = new $fieldClass();
|
|
|
|
$submittedField->Title = $this->Title;
|
|
|
|
$submittedField->Name = $this->Name;
|
|
|
|
$submittedField->ParentID = $submittedForm->ID;
|
|
|
|
|
|
|
|
// create the file from post data
|
2008-04-06 10:20:13 +02:00
|
|
|
$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();
|
2007-07-19 12:40:28 +02:00
|
|
|
$submittedField->UploadedFileID = $uploadedFile->ID;
|
|
|
|
$submittedField->write();
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $submittedField;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|