Completed
Pull Request — master (#339)
by Damian
02:03
created

PreviewImageField::performReadonlyTransformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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
    {
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