2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Represents a file type which can be added to a form.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class FileField extends FormField {
|
2008-01-29 21:18:06 +01:00
|
|
|
/**
|
|
|
|
* Create a new file field.
|
|
|
|
* @param string $name The internal field name, passed to forms.
|
|
|
|
* @param string $title The field label.
|
|
|
|
* @param int $value The value of the field.
|
|
|
|
* @param Form $form Reference to the container form
|
|
|
|
* @param string $rightTitle Used in SmallFieldHolder() to force a right-aligned label
|
|
|
|
* @param string $folderName Folder to upload files to
|
|
|
|
*/
|
|
|
|
function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = 'Uploads') {
|
|
|
|
$this->folderName = $folderName;
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value, $form, $rightTitle);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Field() {
|
|
|
|
return
|
|
|
|
$this->createTag("input", array("type" => "file", "name" => $this->name, "id" => $this->id())) .
|
|
|
|
$this->createTag("input", array("type" => "hidden", "name" => "MAX_FILE_SIZE", "value" => 30*1024*1024));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveInto(DataObject $record) {
|
|
|
|
$fieldName = $this->name . 'ID';
|
2007-07-25 02:26:00 +02:00
|
|
|
$hasOnes = $record->has_one(/*$fieldName*/$this->name);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// assume that the file is connected via a has-one
|
|
|
|
if( !$hasOnes )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$file = new File();
|
2008-01-29 21:18:06 +01:00
|
|
|
$file->loadUploaded($_FILES[$this->name], $this->folderName);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$record->$fieldName = $file->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Value() {
|
|
|
|
return $_FILES[$this->Name()];
|
|
|
|
}
|
|
|
|
}
|
2008-01-29 21:18:06 +01:00
|
|
|
?>
|