Completed
Pull Request — master (#312)
by Damian
02:00
created

FileField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getSchemaDataDefaults() 0 13 1
A getFolderID() 0 8 3
A getSchemaStateDefaults() 0 6 1
A getEncodedItems() 0 9 2
A getIsMultiUpload() 0 14 4
A setIsMultiUpload() 0 3 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Forms\FileUploadable;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\SS_List;
11
12
/**
13
 * Represents a file upload field with ReactJS based frontend.
14
 *
15
 * Allows writing to a parent record with the following relation types:
16
 *   - has_one
17
 *   - has_many
18
 *   - many_many
19
 *
20
 * Additionally supports writing directly to the File table not attached
21
 * to any parent record.
22
 */
23
class FileField extends FormField
24
{
25
    use FileUploadable;
26
27
    /**
28
     * @config
29
     * @var int
30
     */
31
    private static $thumbnail_width = 60;
0 ignored issues
show
Unused Code introduced by
The property $thumbnail_width is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
33
    /**
34
     * @config
35
     * @var int
36
     */
37
    private static $thumbnail_height = 60;
0 ignored issues
show
Unused Code introduced by
The property $thumbnail_height is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
38
39
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
40
41
    protected $schemaComponent = 'FileField';
42
43
    /**
44
     * @var bool|null
45
     */
46
    protected $multiUpload = null;
47
48
	/**
49
	 * Create a new file field.
50
	 *
51
	 * @param string $name The internal field name, passed to forms.
52
	 * @param string $title The field label.
53
	 * @param SS_List $items Items assigned to this field
54
	 */
55
	public function __construct($name, $title = null, SS_List $items = null) {
56
		$this->constructFileUploadable();
57
		parent::__construct($name, $title);
58
		if($items) {
59
			$this->setItems($items);
60
		}
61
	}
62
63
	public function getSchemaDataDefaults()
64
    {
65
        $defaults = parent::getSchemaDataDefaults();
66
        $uploadLink = AssetAdmin::singleton()->Link('api/createFile');
67
        $defaults['data']['createFileEndpoint'] = [
68
            'url' => $uploadLink,
69
            'method' => 'post',
70
            'payloadFormat' => 'urlencoded',
71
        ];
72
        $defaults['data']['multi'] = $this->getIsMultiUpload();
73
        $defaults['data']['parentid'] = $this->getFolderID();
74
        return $defaults;
75
    }
76
77
    /**
78
     * Get ID of target parent folder
79
     *
80
     * @return int
81
     */
82
    protected function getFolderID() {
83
        $folderName = $this->getFolderName();
84
        if (!$folderName) {
85
            return 0;
86
        }
87
        $folder = Folder::find_or_make($folderName);
88
        return $folder ? $folder->ID : 0;
89
    }
90
91
    public function getSchemaStateDefaults()
92
    {
93
        $state = parent::getSchemaStateDefaults();
94
        $state['data']['files'] = $this->getEncodedItems();
95
        return $state;
96
    }
97
98
    /**
99
     * Encode selected values for react
100
     *
101
     * @return array
102
     */
103
    protected function getEncodedItems()
104
    {
105
        $assetAdmin = AssetAdmin::singleton();
106
        $fileData = [];
107
        foreach ($this->getItems() as $file) {
108
            $fileData[] = $assetAdmin->getObjectFromData($file);
109
        }
110
        return $fileData;
111
    }
112
113
    /**
114
     * Check if allowed to upload more than one file
115
     *
116
     * @return bool
117
     */
118
    public function getIsMultiUpload() {
119
        if (isset($this->multiUpload)) {
120
            return $this->multiUpload;
121
        }
122
        // Guess from record
123
        $record = $this->getRecord();
124
        $name = $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
125
126
        // Disabled for has_one components
127
        if ($record && DataObject::getSchema()->hasOneComponent(get_class($record), $name)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \SilverStripe\ORM\DataOb..._class($record), $name) of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
128
            return false;
129
        }
130
        return true;
131
    }
132
133
    /**
134
     * Set upload type to multi / single
135
     * @param $multi
136
     */
137
    public function setIsMultiUpload($multi) {
138
        $this->multiUpload = $multi;
139
    }
140
}
141