2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Allows a user to add a field that can be used to upload a file.
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
class EditableFileField extends EditableFormField {
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $singular_name = 'File Upload Field';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $plural_names = 'File Fields';
|
2013-12-20 09:07:35 +01:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
private static $has_one = array(
|
|
|
|
'Folder' => 'Folder' // From CustomFields
|
|
|
|
);
|
2013-12-20 09:07:35 +01:00
|
|
|
|
2015-08-28 00:42:32 +02:00
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
)
|
2013-12-20 09:07:35 +01:00
|
|
|
);
|
|
|
|
|
2015-09-11 00:20:06 +02:00
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("FileUploadWarning",
|
|
|
|
"<p class=\"message notice\">" . _t("UserDefinedForm.FileUploadWarning",
|
2015-09-10 14:08:13 +02:00
|
|
|
"Files uploaded through this field could be publicly accessible if the exact URL is known")
|
|
|
|
. "</p>"), "Type");
|
2015-08-27 08:10:13 +02:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
return $fields;
|
2013-12-20 09:07:35 +01:00
|
|
|
}
|
|
|
|
|
2009-04-27 06:32:47 +02:00
|
|
|
public function getFormField() {
|
2016-04-08 01:47:53 +02:00
|
|
|
if(isset(Config::inst()->get('EditableFileField', 'use_uploadfield')) && Config::inst()->get('EditableFileField', 'use_uploadfield')) {
|
2016-04-26 10:39:45 +02:00
|
|
|
$field = Upload::create($this->Name, $this->EscapedTitle);
|
2016-04-08 01:47:53 +02:00
|
|
|
} else {
|
2016-04-26 10:39:45 +02:00
|
|
|
$field = FileField::create($this->Name, $this->EscapedTitle);
|
2016-04-08 01:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$field->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsFileField');
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2015-07-15 23:23:43 +02:00
|
|
|
$field->getValidator()->setAllowedExtensions(
|
2015-08-28 00:42:32 +02:00
|
|
|
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-15 23:23:43 +02:00
|
|
|
);
|
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
$folder = $this->Folder();
|
|
|
|
if($folder && $folder->exists()) {
|
|
|
|
$field->setFolderName(
|
|
|
|
preg_replace("/^assets\//","", $folder->Filename)
|
|
|
|
);
|
2013-12-20 09:07:35 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 07:03:36 +02:00
|
|
|
$this->doUpdateFormField($field);
|
2014-12-22 02:49:33 +01:00
|
|
|
|
2012-04-14 08:36:50 +02:00
|
|
|
return $field;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
|
|
|
|
2014-04-25 15:53:45 +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
|
|
|
|
2010-02-14 08:53:16 +01:00
|
|
|
public function getSubmittedFormField() {
|
|
|
|
return new SubmittedFileField();
|
2012-04-14 08:36:50 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
2013-12-20 09:07:35 +01:00
|
|
|
}
|