|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
|
6
|
|
|
use SilverStripe\Assets\File; |
|
7
|
|
|
use SilverStripe\Assets\Folder; |
|
8
|
|
|
use SilverStripe\Forms\FormField; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* For providing schema data to the client side to build a preview field with upload replacement feature |
|
13
|
|
|
*/ |
|
14
|
|
|
class PreviewImageField extends FormField |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Integer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $recordID = null; |
|
20
|
|
|
|
|
21
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM; |
|
22
|
|
|
|
|
23
|
|
|
protected $schemaComponent = 'PreviewImageField'; |
|
24
|
|
|
|
|
25
|
|
|
public function getSchemaDataDefaults() |
|
26
|
|
|
{ |
|
27
|
|
|
$defaults = parent::getSchemaDataDefaults(); |
|
28
|
|
|
$defaults['data']['uploadFileEndpoint'] = [ |
|
29
|
|
|
'url' => AssetAdmin::singleton()->Link('api/uploadFile'), |
|
30
|
|
|
'method' => 'post', |
|
31
|
|
|
'payloadFormat' => 'urlencoded', |
|
32
|
|
|
]; |
|
33
|
|
|
return $defaults; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getSchemaStateDefaults() |
|
37
|
|
|
{ |
|
38
|
|
|
$defaults = parent::getSchemaStateDefaults(); |
|
39
|
|
|
|
|
40
|
|
|
/** @var File $record */ |
|
41
|
|
|
if ($record = $this->getRecord()) { |
|
42
|
|
|
$parent = $record->Parent(); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$defaults['data'] = array_merge_recursive($defaults['data'], [ |
|
45
|
|
|
'parentid' => ($parent) ? $parent->ID : 0, |
|
46
|
|
|
'url' => $record->Link(), |
|
47
|
|
|
'exists' => $record->exists(), |
|
48
|
|
|
'preview' => $record->PreviewLink(), |
|
49
|
|
|
'category' => $record instanceof Folder ? 'folder' : $record->appCategory(), |
|
50
|
|
|
'initialValues' => [ |
|
51
|
|
|
'FileFilename' => $record->FileFilename, |
|
52
|
|
|
'FileHash' => $record->FileHash, |
|
53
|
|
|
'FileVariant' => $record->FileVariant, |
|
54
|
|
|
], |
|
55
|
|
|
'nameField' => 'Name', |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
return $defaults; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function performReadonlyTransformation() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->setReadonly(true); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return DataObject |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getRecord() |
|
72
|
|
|
{ |
|
73
|
|
|
return DataObject::get_by_id(File::class, $this->recordID); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Integer $recordID |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setRecordID($recordID) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->recordID = $recordID; |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.