2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-04-17 04:26:40 +02:00
|
|
|
* Allows a user to add a field that can be used to upload a file
|
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
|
|
|
class EditableFileField extends EditableFormField {
|
|
|
|
|
|
|
|
// this needs to be moved.
|
|
|
|
static $has_one = array(
|
|
|
|
"UploadedFile" => "File"
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2008-10-03 05:43:41 +02:00
|
|
|
* @see Upload->allowedMaxFileSize
|
2008-09-29 05:18:23 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $allowed_max_file_size;
|
|
|
|
|
|
|
|
/**
|
2008-10-03 05:43:41 +02:00
|
|
|
* @see Upload->allowedExtensions
|
2008-09-29 05:18:23 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $allowed_extensions = array();
|
|
|
|
|
|
|
|
static $singular_name = 'File field';
|
|
|
|
static $plural_names = 'File fields';
|
|
|
|
|
|
|
|
function getFormField() {
|
2009-04-14 06:11:05 +02:00
|
|
|
if($field = parent::getFormField()) {
|
2008-09-29 05:18:23 +02:00
|
|
|
return $field;
|
2009-04-14 06:11:05 +02:00
|
|
|
}
|
|
|
|
return new FileField($this->Name, $this->Title, $this->getField('Default'));
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|