2012-04-14 08:36:50 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\Submission;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\File;
|
2023-08-08 04:53:14 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2021-03-31 04:26:38 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2012-04-14 08:36:50 +02:00
|
|
|
/**
|
2015-09-11 00:20:06 +02:00
|
|
|
* A file uploaded on a {@link UserDefinedForm} and attached to a single
|
2013-01-29 09:44:00 +01:00
|
|
|
* {@link SubmittedForm}.
|
2012-04-14 08:36:50 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property int $UploadedFileID
|
|
|
|
* @method File UploadedFile()
|
2012-04-14 08:36:50 +02:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class SubmittedFileField extends SubmittedFormField
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
|
|
|
'UploadedFile' => File::class
|
|
|
|
];
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $table_name = 'SubmittedFileField';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2021-03-31 04:26:38 +02:00
|
|
|
private static $owns = [
|
|
|
|
'UploadedFile'
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $cascade_deletes = [
|
|
|
|
'UploadedFile'
|
|
|
|
];
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return the value of this field for inclusion into things such as
|
|
|
|
* reports.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormattedValue()
|
|
|
|
{
|
|
|
|
$name = $this->getFileName();
|
2022-08-11 05:35:39 +02:00
|
|
|
$link = $this->getLink(false);
|
2021-03-31 04:26:38 +02:00
|
|
|
$title = _t(__CLASS__ . '.DOWNLOADFILE', 'Download File');
|
2022-08-11 05:35:39 +02:00
|
|
|
$message = _t(__CLASS__ . '.INSUFFICIENTRIGHTS', 'You don\'t have the right permissions to download this file');
|
|
|
|
$file = $this->getUploadedFileFromDraft();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($link) {
|
2022-08-11 05:35:39 +02:00
|
|
|
if ($file->canView()) {
|
|
|
|
return DBField::create_field('HTMLText', sprintf(
|
|
|
|
'%s - <a href="%s" target="_blank">%s</a>',
|
|
|
|
htmlspecialchars($name, ENT_QUOTES),
|
|
|
|
htmlspecialchars($link, ENT_QUOTES),
|
|
|
|
htmlspecialchars($title, ENT_QUOTES)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return DBField::create_field('HTMLText', sprintf(
|
|
|
|
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
|
|
|
|
htmlspecialchars($name, ENT_QUOTES),
|
|
|
|
htmlspecialchars($message, ENT_QUOTES)
|
|
|
|
));
|
|
|
|
}
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return the value for this field in the CSV export.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getExportValue()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
return ($link = $this->getLink()) ? $link : '';
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return the link for the file attached to this submitted form field.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-08-11 05:35:39 +02:00
|
|
|
public function getLink($grant = true)
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2021-03-22 09:30:14 +01:00
|
|
|
if ($file = $this->getUploadedFileFromDraft()) {
|
|
|
|
if ($file->exists()) {
|
2023-08-08 04:53:14 +02:00
|
|
|
$url = $file->getURL($grant);
|
|
|
|
if ($url) {
|
|
|
|
return Director::absoluteURL($url);
|
|
|
|
}
|
|
|
|
return null;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2021-03-22 09:30:14 +01:00
|
|
|
/**
|
|
|
|
* As uploaded files are stored in draft by default, this retrieves the
|
|
|
|
* uploaded file from draft mode rather than using the current stage.
|
2021-03-31 04:26:38 +02:00
|
|
|
*
|
2021-03-22 09:30:14 +01:00
|
|
|
* @return File
|
|
|
|
*/
|
2021-03-31 04:26:38 +02:00
|
|
|
public function getUploadedFileFromDraft()
|
2021-03-22 09:30:14 +01:00
|
|
|
{
|
|
|
|
$fileId = $this->UploadedFileID;
|
|
|
|
|
2021-03-31 04:26:38 +02:00
|
|
|
return Versioned::withVersionedMode(function () use ($fileId) {
|
2021-03-22 09:30:14 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
|
|
|
|
|
|
|
return File::get()->byID($fileId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return the name of the file, if present
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2021-03-22 09:30:14 +01:00
|
|
|
if ($file = $this->getUploadedFileFromDraft()) {
|
|
|
|
return $file->Name;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
}
|