Completed
Pull Request — master (#6325)
by Damian
08:47
created

UploadReceiver::setUpload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SilverStripe\Forms;
5
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Upload;
8
use SilverStripe\Assets\Upload_Validator;
9
10
/**
11
 * Represents a form field which has an Upload() instance and can upload to a folder
12
 *
13
 * Note that this trait expects to be applied to a {@see FormField} class
14
 *
15
 * @mixin FormField
16
 */
17
trait UploadReceiver
18
{
19
    /**
20
     * Upload object (needed for validation
21
     * and actually moving the temporary file
22
     * created by PHP).
23
     *
24
     * @var Upload
25
     */
26
    protected $upload;
27
28
    /**
29
     * Partial filesystem path relative to /assets directory.
30
     * Defaults to Upload::$uploads_folder.
31
     *
32
     * @var string
33
     */
34
    protected $folderName = false;
35
36
    /**
37
     * Bootstrap Uploadable field
38
     */
39
    protected function constructUploadReceiver()
40
    {
41
        // Set Upload instance
42
        $this->setUpload(Upload::create());
43
44
        // filter out '' since this would be a regex problem on JS end
45
        $this->getValidator()->setAllowedExtensions(
46
            array_filter(File::config()->allowed_extensions)
47
        );
48
49
        // get the lower max size
50
        $maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
51
        $maxPost = File::ini2bytes(ini_get('post_max_size'));
52
        $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost));
53
    }
54
55
    /**
56
     * Retrieves the Upload handler
57
     *
58
     * @return Upload
59
     */
60
    public function getUpload()
61
    {
62
        return $this->upload;
63
    }
64
65
    /**
66
     * Sets the upload handler
67
     *
68
     * @param Upload $upload
69
     * @return $this Self reference
70
     */
71
    public function setUpload(Upload $upload)
72
    {
73
        $this->upload = $upload;
74
        return $this;
75
    }
76
77
    /**
78
     * Limit allowed file extensions. Empty by default, allowing all extensions.
79
     * To allow files without an extension, use an empty string.
80
     * See {@link File::$allowed_extensions} to get a good standard set of
81
     * extensions that are typically not harmful in a webserver context.
82
     * See {@link setAllowedMaxFileSize()} to limit file size by extension.
83
     *
84
     * @param array $rules List of extensions
85
     * @return $this
86
     */
87
    public function setAllowedExtensions($rules)
88
    {
89
        $this->getValidator()->setAllowedExtensions($rules);
90
        return $this;
91
    }
92
93
    /**
94
     * Limit allowed file extensions by specifying categories of file types.
95
     * These may be 'image', 'image/supported', 'audio', 'video', 'archive', 'flash', or 'document'
96
     * See {@link File::$allowed_extensions} for details of allowed extensions
97
     * for each of these categories
98
     *
99
     * @param string $category Category name
100
     * @param string,... $categories Additional category names
0 ignored issues
show
Documentation introduced by
The doc-type string,... could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $categories. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     * @return $this
102
     */
103
    public function setAllowedFileCategories($category)
0 ignored issues
show
Unused Code introduced by
The parameter $category is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        $extensions = File::get_category_extensions(func_get_args());
106
        return $this->setAllowedExtensions($extensions);
107
    }
108
109
    /**
110
     * Returns list of extensions allowed by this field, or an empty array
111
     * if there is no restriction
112
     *
113
     * @return array
114
     */
115
    public function getAllowedExtensions()
116
    {
117
        return $this->getValidator()->getAllowedExtensions();
118
    }
119
120
    /**
121
     * Get custom validator for this field
122
     *
123
     * @return Upload_Validator
124
     */
125
    public function getValidator()
126
    {
127
        return $this->getUpload()->getValidator();
128
    }
129
130
    /**
131
     * Set custom validator for this field
132
     *
133
     * @param Upload_Validator $validator
134
     * @return $this Self reference
135
     */
136
    public function setValidator(Upload_Validator $validator)
137
    {
138
        $this->getUpload()->setValidator($validator);
139
        return $this;
140
    }
141
142
    /**
143
     * Sets the upload folder name
144
     *
145
     * @param string $folderName
146
     * @return $this Self reference
147
     */
148
    public function setFolderName($folderName)
149
    {
150
        $this->folderName = $folderName;
151
        return $this;
152
    }
153
154
    /**
155
     * Gets the upload folder name
156
     *
157
     * @return string
158
     */
159
    public function getFolderName()
160
    {
161
        return ($this->folderName !== false)
162
            ? $this->folderName
163
            : Upload::config()->uploads_folder;
164
    }
165
}
166