Passed
Pull Request — master (#778)
by Mark
03:00
created

SubmittedFileField::getFormattedValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\Submission;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\ORM\FieldType\DBField;
7
8
/**
9
 * A file uploaded on a {@link UserDefinedForm} and attached to a single
10
 * {@link SubmittedForm}.
11
 *
12
 * @package userforms
13
 */
14
15
class SubmittedFileField extends SubmittedFormField
16
{
17
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
18
        'UploadedFile' => File::class
19
    ];
20
21
    private static $table_name = 'SubmittedFileField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    /**
24
     * Return the value of this field for inclusion into things such as
25
     * reports.
26
     *
27
     * @return string
28
     */
29
    public function getCleanValue()
30
    {
31
        $name = $this->getFileName();
32
        $link = $this->getLink();
33
        $title = _t(__CLASS__.'.DOWNLOADFILE', 'Download File');
34
35
        if ($link) {
36
            return DBField::create_field('HTMLText', sprintf(
37
                '%s - <a href="%s" target="_blank">%s</a>',
38
                $name,
39
                $link,
40
                $title
41
            ));
42
        }
43
44
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
45
    }
46
47
    /**
48
     * Return the value for this field in the CSV export.
49
     *
50
     * @return string
51
     */
52
    public function getExportValue()
53
    {
54
        return ($link = $this->getLink()) ? $link : '';
55
    }
56
57
    /**
58
     * Return the link for the file attached to this submitted form field.
59
     *
60
     * @return string
61
     */
62
    public function getLink()
63
    {
64
        if ($file = $this->UploadedFile()) {
0 ignored issues
show
Bug introduced by
The method UploadedFile() does not exist on SilverStripe\UserForms\M...sion\SubmittedFileField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        if ($file = $this->/** @scrutinizer ignore-call */ UploadedFile()) {
Loading history...
65
            if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) {
66
                return $this->UploadedFile()->AbsoluteLink();
67
            }
68
        }
69
    }
70
71
    /**
72
     * Return the name of the file, if present
73
     *
74
     * @return string
75
     */
76
    public function getFileName()
77
    {
78
        if ($this->UploadedFile()) {
79
            return $this->UploadedFile()->Name;
80
        }
81
    }
82
}
83