Completed
Pull Request — master (#339)
by
unknown
01:50
created

PreviewImageField::getSchemaStateDefaults()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 16
nc 2
nop 0
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();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean parentClass()?

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.

Loading history...
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
        $this->setReadonly(true);
63
        
64
        return $this;
65
    }
66
    
67
    /**
68
     * @return DataObject
69
     */
70
    public function getRecord()
71
    {
72
        return DataObject::get_by_id(File::class, $this->recordID);
73
    }
74
    
75
    /**
76
     * @param Integer $recordID
77
     * @return $this
78
     */
79
    public function setRecordID($recordID)
80
    {
81
        $this->recordID = $recordID;
82
        return $this;
83
    }
84
}
85