Source of file SubmittedFileField.php
Size: 2,524 Bytes - Last Modified: 2021-12-23T10:35:40+00:00
/var/www/docs.ssmods.com/process/src/code/Model/Submission/SubmittedFileField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 | <?php namespace SilverStripe\UserForms\Model\Submission; use SilverStripe\Assets\File; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Versioned\Versioned; /** * A file uploaded on a {@link UserDefinedForm} and attached to a single * {@link SubmittedForm}. * * @package userforms * @property int $UploadedFileID * @method File UploadedFile() */ class SubmittedFileField extends SubmittedFormField { private static $has_one = [ 'UploadedFile' => File::class ]; private static $table_name = 'SubmittedFileField'; private static $owns = [ 'UploadedFile' ]; private static $cascade_deletes = [ 'UploadedFile' ]; /** * 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'); if ($link) { return DBField::create_field('HTMLText', sprintf( '%s - <a href="%s" target="_blank">%s</a>', $name, $link, $title )); } return false; } /** * Return the value for this field in the CSV export. * * @return string */ public function getExportValue() { return ($link = $this->getLink()) ? $link : ''; } /** * Return the link for the file attached to this submitted form field. * * @return string */ public function getLink() { if ($file = $this->getUploadedFileFromDraft()) { if ($file->exists()) { return $file->getAbsoluteURL(); } } } /** * As uploaded files are stored in draft by default, this retrieves the * uploaded file from draft mode rather than using the current stage. * * @return File */ public function getUploadedFileFromDraft() { $fileId = $this->UploadedFileID; return Versioned::withVersionedMode(function () use ($fileId) { Versioned::set_stage(Versioned::DRAFT); return File::get()->byID($fileId); }); } /** * Return the name of the file, if present * * @return string */ public function getFileName() { if ($file = $this->getUploadedFileFromDraft()) { return $file->Name; } } } |