2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Forms\FileField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedFileField;
|
|
|
|
|
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
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableFileField extends EditableFormField
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $singular_name = 'File Upload Field';
|
|
|
|
|
|
|
|
private static $plural_names = 'File Fields';
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2016-08-12 00:51:47 +02:00
|
|
|
'MaxFileSizeMB' => 'Float',
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-08-12 00:51:47 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
2017-08-09 01:55:09 +02:00
|
|
|
'Folder' => Folder::class // From CustomFields
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableFileField';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Further limit uploadable file extensions in addition to the restrictions
|
|
|
|
* imposed by the File.allowed_extensions global configuration.
|
|
|
|
* @config
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $allowed_extensions_blacklist = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'htm', 'html', 'xhtml', 'swf', 'xml'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TreeDropdownField::create(
|
|
|
|
'FolderID',
|
|
|
|
_t('EditableUploadField.SELECTUPLOADFOLDER', 'Select upload folder'),
|
2017-08-09 01:55:09 +02:00
|
|
|
Folder::class
|
2016-07-21 07:53:59 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
$fields->addFieldToTab("Root.Main", LiteralField::create(
|
|
|
|
'FileUploadWarning',
|
|
|
|
'<p class="message notice">' . _t(
|
|
|
|
'SilverStripe\\UserForms\\Model\\UserDefinedForm.FileUploadWarning',
|
|
|
|
'Files uploaded through this field could be publicly accessible if the exact URL is known'
|
|
|
|
)
|
|
|
|
. '</p>'
|
|
|
|
), 'Type');
|
2016-08-12 00:51:47 +02:00
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
NumericField::create('MaxFileSizeMB')
|
|
|
|
->setTitle('Max File Size MB')
|
2016-10-13 06:34:23 +02:00
|
|
|
->setDescription("Note: Maximum php allowed size is {$this->getPHPMaxFileSizeMB()} MB")
|
2016-08-12 00:51:47 +02:00
|
|
|
);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-01 00:05:07 +02:00
|
|
|
$fields->removeByName('Default');
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2016-08-12 00:51:47 +02:00
|
|
|
/**
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
|
|
|
|
$max = static::get_php_max_file_size();
|
|
|
|
if ($this->MaxFileSizeMB * 1024 > $max) {
|
2017-08-13 23:26:53 +02:00
|
|
|
$result->addError("Your max file size limit can't be larger than the server's limit of {$this->getPHPMaxFileSizeMB()}.");
|
2016-08-12 00:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function getFormField()
|
|
|
|
{
|
2016-04-28 08:05:19 +02:00
|
|
|
$field = FileField::create($this->Name, $this->EscapedTitle)
|
2016-07-21 07:53:59 +02:00
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
2017-08-14 02:29:57 +02:00
|
|
|
->setTemplate(__CLASS__);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$field->setFieldHolderTemplate('UserFormsField_holder')
|
2017-08-14 02:29:57 +02:00
|
|
|
->setTemplate(__CLASS__);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$field->getValidator()->setAllowedExtensions(
|
|
|
|
array_diff(
|
2017-08-11 02:37:03 +02:00
|
|
|
// filter out '' since this would be a regex problem on JS end
|
2017-08-09 01:55:09 +02:00
|
|
|
array_filter(Config::inst()->get(File::class, 'allowed_extensions')),
|
2017-08-11 01:33:06 +02:00
|
|
|
$this->config()->get('allowed_extensions_blacklist')
|
2016-07-21 07:53:59 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-08-12 00:51:47 +02:00
|
|
|
if ($this->MaxFileSizeMB > 0) {
|
2017-05-24 08:31:25 +02:00
|
|
|
$field->getValidator()->setAllowedMaxFileSize($this->MaxFileSizeMB * 1024 * 1024);
|
2016-08-12 00:51:47 +02:00
|
|
|
} else {
|
|
|
|
$field->getValidator()->setAllowedMaxFileSize(static::get_php_max_file_size());
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$folder = $this->Folder();
|
|
|
|
if ($folder && $folder->exists()) {
|
|
|
|
$field->setFolderName(
|
|
|
|
preg_replace("/^assets\//", "", $folder->Filename)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubmittedFormField()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
return SubmittedFileField::create();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 00:51:47 +02:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public static function get_php_max_file_size()
|
|
|
|
{
|
|
|
|
$maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
|
|
|
|
$maxPost = File::ini2bytes(ini_get('post_max_size'));
|
|
|
|
return min($maxUpload, $maxPost);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPHPMaxFileSizeMB()
|
|
|
|
{
|
|
|
|
return round(static::get_php_max_file_size() / 1024.0, 1);
|
|
|
|
}
|
2013-12-20 09:07:35 +01:00
|
|
|
}
|