silverstripe-userforms/code/model/editableformfields/EditableFileField.php

105 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/**
* Allows a user to add a field that can be used to upload a file.
*
* @package userforms
*/
class EditableFileField extends EditableFormField {
2015-09-11 00:20:06 +02:00
private static $singular_name = 'File Upload Field';
2015-09-11 00:20:06 +02:00
private static $plural_names = 'File Fields';
2015-07-24 04:37:48 +02:00
private static $has_one = array(
'Folder' => 'Folder' // From CustomFields
);
/**
* Further limit uploadable file extensions in addition to the restrictions
* imposed by the File.allowed_extensions global configuration.
* @config
*/
private static $allowed_extensions_blacklist = array(
'htm', 'html', 'xhtml', 'swf', 'xml'
);
2015-07-24 04:37:48 +02:00
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = parent::getCMSFields();
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
$fields->addFieldToTab(
'Root.Main',
TreeDropdownField::create(
'FolderID',
_t('EditableUploadField.SELECTUPLOADFOLDER', 'Select upload folder'),
'Folder'
)
);
2015-09-11 00:20:06 +02:00
$fields->addFieldToTab("Root.Main", new LiteralField("FileUploadWarning",
"<p class=\"message notice\">" . _t("UserDefinedForm.FileUploadWarning",
"Files uploaded through this field could be publicly accessible if the exact URL is known")
. "</p>"), "Type");
2015-07-24 04:37:48 +02:00
return $fields;
}
2009-04-27 06:32:47 +02:00
public function getFormField() {
$field = FileField::create($this->Name, $this->EscapedTitle)
->setFieldHolderTemplate('UserFormsField_holder')
->setTemplate('UserFormsFileField');
$field->setFieldHolderTemplate('UserFormsField_holder')
->setTemplate('UserFormsFileField');
$field->getValidator()->setAllowedExtensions(
array_diff(
// filter out '' since this would be a regex problem on JS end
array_filter(Config::inst()->get('File', 'allowed_extensions')),
$this->config()->allowed_extensions_blacklist
)
);
2015-07-24 04:37:48 +02:00
$folder = $this->Folder();
if($folder && $folder->exists()) {
$field->setFolderName(
preg_replace("/^assets\//","", $folder->Filename)
);
}
$this->doUpdateFormField($field);
return $field;
}
2015-09-11 00:20:06 +02:00
/**
* Return the value for the database, link to the file is stored as a
* relation so value for the field can be null.
*
* @return string
*/
public function getValueFromData() {
return null;
}
2015-09-11 00:20:06 +02:00
public function getSubmittedFormField() {
return new SubmittedFileField();
}
2015-07-24 04:37:48 +02:00
public function migrateSettings($data) {
// Migrate 'Folder' setting to 'FolderID'
if(isset($data['Folder'])) {
$this->FolderID = $data['Folder'];
unset($data['Folder']);
}
parent::migrateSettings($data);
}
}