silverstripe-userforms/code/Model/Submission/SubmittedFileField.php

84 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\Submission;
use SilverStripe\Assets\File;
use SilverStripe\ORM\FieldType\DBField;
/**
2015-09-11 00:20:06 +02:00
* A file uploaded on a {@link UserDefinedForm} and attached to a single
* {@link SubmittedForm}.
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class SubmittedFileField extends SubmittedFormField
{
private static $has_one = [
'UploadedFile' => File::class
];
2015-09-11 00:20:06 +02:00
private static $table_name = 'SubmittedFileField';
2015-09-11 00:20:06 +02:00
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();
$link = $this->getLink();
$title = _t(__CLASS__.'.DOWNLOADFILE', 'Download File');
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
if ($link) {
return DBField::create_field('HTMLText', sprintf(
'%s - <a href="%s" target="_blank">%s</a>',
2017-08-11 02:37:03 +02:00
$name,
$link,
$title
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()
{
return ($link = $this->getLink()) ? $link : '';
2016-07-21 07:53:59 +02:00
}
2016-07-21 07:53:59 +02:00
/**
* Return the link for the file attached to this submitted form field.
*
* @return string
*/
public function getLink()
{
$file = $this->UploadedFile();
if ($file && $file->exists()) {
2016-07-21 07:53:59 +02:00
if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) {
return $this->UploadedFile()->AbsoluteLink();
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 the name of the file, if present
*
* @return string
*/
public function getFileName()
{
if ($this->UploadedFile()) {
return $this->UploadedFile()->Name;
}
}
2015-07-24 04:37:48 +02:00
}