2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Represents a file type which can be added to a form.
|
2008-04-06 10:20:13 +02:00
|
|
|
* Automatically tries to save has_one-relations on the saved
|
|
|
|
* record.
|
|
|
|
*
|
|
|
|
* Please set a validator on the form-object to get feedback
|
|
|
|
* about imposed filesize/extension restrictions.
|
|
|
|
*
|
2008-10-31 03:09:04 +01:00
|
|
|
* CAUTION: Doesn't work in the CMS due to ajax submission, please use {@link FileIFrameField} instead.
|
2008-04-06 10:20:13 +02:00
|
|
|
*
|
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-04-06 10:20:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restrict filesize for either all filetypes
|
|
|
|
* or a specific extension, with extension-name
|
|
|
|
* as array-key and the size-restriction in bytes as array-value.
|
2010-03-04 05:42:41 +01:00
|
|
|
*
|
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $allowedMaxFileSize = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Collection of extensions.
|
|
|
|
* Extension-names are treated case-insensitive.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array("jpg","GIF")
|
|
|
|
* </code>
|
2010-03-04 05:42:41 +01:00
|
|
|
*
|
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $allowedExtensions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to automatically determine and save a has_one-relationship
|
|
|
|
* on the saved record (e.g. a "Player" has_one "PlayerImage" would
|
|
|
|
* trigger saving the ID of newly created file into "PlayerImageID"
|
|
|
|
* on the record).
|
|
|
|
*
|
2008-04-09 13:11:03 +02:00
|
|
|
* @var boolean
|
2008-04-06 10:20:13 +02:00
|
|
|
*/
|
|
|
|
public $relationAutoSetting = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload object (needed for validation
|
|
|
|
* and actually moving the temporary file
|
|
|
|
* created by PHP).
|
|
|
|
*
|
|
|
|
* @var Upload
|
|
|
|
*/
|
|
|
|
protected $upload;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partial filesystem path relative to /assets directory.
|
|
|
|
* Defaults to 'Uploads'.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $folderName = 'Uploads';
|
|
|
|
|
2008-01-29 21:18:06 +01:00
|
|
|
/**
|
|
|
|
* Create a new file field.
|
2008-04-06 10:20:13 +02:00
|
|
|
*
|
2008-01-29 21:18:06 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null) {
|
|
|
|
if(isset($folderName)) $this->folderName = $folderName;
|
|
|
|
$this->upload = new Upload();
|
2008-01-29 21:18:06 +01:00
|
|
|
|
|
|
|
parent::__construct($name, $title, $value, $form, $rightTitle);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Field() {
|
2008-09-16 04:14:25 +02:00
|
|
|
return $this->createTag(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
"type" => "file",
|
|
|
|
"name" => $this->name,
|
|
|
|
"id" => $this->id(),
|
|
|
|
"tabindex" => $this->getTabIndex()
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
$this->createTag(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
"type" => "hidden",
|
|
|
|
"name" => "MAX_FILE_SIZE",
|
2010-03-04 05:42:41 +01:00
|
|
|
"value" => $this->getValidator()->getAllowedMaxFileSize(),
|
2008-09-16 04:14:25 +02:00
|
|
|
"tabindex" => $this->getTabIndex()
|
|
|
|
)
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function saveInto(DataObject $record) {
|
2008-04-06 10:20:13 +02:00
|
|
|
if(!isset($_FILES[$this->name])) return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-04-09 13:11:03 +02:00
|
|
|
if($this->relationAutoSetting) {
|
|
|
|
// assume that the file is connected via a has-one
|
|
|
|
$hasOnes = $record->has_one($this->name);
|
|
|
|
// try to create a file matching the relation
|
|
|
|
$file = (is_string($hasOnes)) ? Object::create($hasOnes) : new File();
|
|
|
|
} else {
|
|
|
|
$file = new File();
|
|
|
|
}
|
|
|
|
|
2008-11-05 04:56:22 +01:00
|
|
|
$this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
|
2008-04-06 10:20:13 +02:00
|
|
|
if($this->upload->isError()) return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
$file = $this->upload->getFile();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
if($this->relationAutoSetting) {
|
|
|
|
if(!$hasOnes) return false;
|
|
|
|
|
|
|
|
// save to record
|
2008-04-09 13:11:03 +02:00
|
|
|
$record->{$this->name . 'ID'} = $file->ID;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function Value() {
|
|
|
|
return $_FILES[$this->Name()];
|
|
|
|
}
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2010-03-04 05:42:41 +01:00
|
|
|
/**
|
|
|
|
* Get custom validator for this field
|
|
|
|
*
|
|
|
|
* @param object $validator
|
|
|
|
*/
|
|
|
|
public function getValidator() {
|
|
|
|
return $this->upload->getValidator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set custom validator for this field
|
|
|
|
*
|
|
|
|
* @param object $validator
|
|
|
|
*/
|
|
|
|
public function setValidator($validator) {
|
|
|
|
$this->upload->setValidator($validator);
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Get maximum file size for all or specified file extension.
|
|
|
|
* Falls back to the default filesize restriction ('*')
|
|
|
|
* if the extension was not found.
|
|
|
|
*
|
2010-03-04 05:42:41 +01:00
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @param string $ext
|
|
|
|
* @return int Filesize in bytes (0 means no filesize set)
|
|
|
|
*/
|
|
|
|
public function getAllowedMaxFileSize($ext = null) {
|
2010-03-04 05:42:41 +01:00
|
|
|
user_error('Upload::getAllowedMaxFileSize() is deprecated. Please use Upload_Validator::getAllowedMaxFileSize() instead', E_USER_NOTICE);
|
|
|
|
$this->getValidator()->getAllowedMaxFileSize($ext);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set filesize maximums (in bytes).
|
|
|
|
* Automatically converts extensions to lowercase
|
|
|
|
* for easier matching.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array('*' => 200, 'jpg' => 1000)
|
|
|
|
* </code>
|
|
|
|
*
|
2010-03-04 05:42:41 +01:00
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @param unknown_type $rules
|
|
|
|
*/
|
|
|
|
public function setAllowedMaxFileSize($rules) {
|
2010-03-04 05:42:41 +01:00
|
|
|
user_error('Upload::setAllowedMaxFileSize() is deprecated. Please use Upload_Validator::setAllowedMaxFileSize() instead', E_USER_NOTICE);
|
|
|
|
$this->getValidator()->setAllowedMaxFileSize($rules);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 05:42:41 +01:00
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllowedExtensions() {
|
2010-03-04 05:42:41 +01:00
|
|
|
user_error('Upload::getAllowedExtensions() is deprecated. Please use Upload_Validator::getAllowedExtensions() instead', E_USER_NOTICE);
|
|
|
|
return $this->getValidator()->getAllowedExtensions();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 05:42:41 +01:00
|
|
|
* @deprecated 2.5
|
2008-04-06 10:20:13 +02:00
|
|
|
* @param array $rules
|
|
|
|
*/
|
|
|
|
public function setAllowedExtensions($rules) {
|
2010-03-04 05:42:41 +01:00
|
|
|
user_error('Upload::setAllowedExtensions() is deprecated. Please use Upload_Validator::setAllowedExtensions() instead', E_USER_NOTICE);
|
|
|
|
$this->getValidator()->setAllowedExtensions($rules);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $folderName
|
|
|
|
*/
|
|
|
|
public function setFolderName($folderName) {
|
|
|
|
$this->folderName = $folderName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFolderName() {
|
2009-02-02 00:49:53 +01:00
|
|
|
return $this->folderName;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validate($validator) {
|
2008-04-17 12:11:40 +02:00
|
|
|
if(!isset($_FILES[$this->name])) return true;
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
$tmpFile = $_FILES[$this->name];
|
|
|
|
|
|
|
|
$valid = $this->upload->validate($tmpFile);
|
|
|
|
if(!$valid) {
|
|
|
|
$errors = $this->upload->getErrors();
|
|
|
|
if($errors) foreach($errors as $error) {
|
|
|
|
$validator->validationError($this->name, $error, "validation", false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|