Completed
Pull Request — master (#433)
by
unknown
02:23
created

ImageFormFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 11
lcom 0
cbo 7
dl 0
loc 114
rs 10
c 3
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpecsMarkup() 0 14 4
A getFormFieldAttributesTab() 0 52 1
B getForm() 0 37 6
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Forms\FieldGroup;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\FormFactory;
10
use SilverStripe\Forms\Tab;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\Control\Controller;
13
14
class ImageFormFactory extends FileFormFactory
15
{
16
    protected function getSpecsMarkup($record)
17
    {
18
        if (!$record || !$record->exists()) {
19
            return null;
20
        }
21
        // Add dimensions to specs
22
        $dimensions = $record->getDimensions() ? $record->getDimensions() . 'px,' : '';
23
        return sprintf(
24
            '<div class="editor__specs">%s %s %s</div>',
25
            $dimensions,
26
            $record->getSize(),
27
            $this->getStatusFlagMarkup($record)
28
        );
29
    }
30
31
    protected function getFormFieldAttributesTab($record, $context = [])
32
    {
33
        /** @var Tab $tab */
34
        $tab = parent::getFormFieldAttributesTab($record, $context);
35
36
        $alignments = array(
37
            'leftAlone' => _t('AssetAdmin.AlignmentLeftAlone', 'On the left, on its own.'),
38
            'center' => _t('AssetAdmin.AlignmentCenter', 'Centered, on its own.'),
39
            'left' => _t('AssetAdmin.AlignmentLeft', 'On the left, with text wrapping around.'),
40
            'right' => _t('AssetAdmin.AlignmentRight', 'On the right, with text wrapping around.'),
41
        );
42
43
        $tab->push(
44
            DropdownField::create('Alignment', _t('AssetAdmin.Alignment', 'Alignment'), $alignments)
45
        );
46
        $tab->push(
47
            FieldGroup::create(
48
                _t('AssetAdmin.ImageSpecs', 'Dimensions'),
49
                TextField::create(
50
                    'InsertWidth',
51
                    _t('AssetAdmin.ImageWidth', 'Width')
52
                )
53
                    ->setMaxLength(5)
54
                    ->addExtraClass('flexbox-area-grow'),
55
                TextField::create(
56
                    'InsertHeight',
57
                    _t('AssetAdmin.ImageHeight', 'Height')
58
                )
59
                    ->setMaxLength(5)
60
                    ->addExtraClass('flexbox-area-grow')
61
            )
62
            ->addExtraClass('fieldgroup--fill-width')
63
            ->setName('Dimensions')
64
        );
65
66
        $tab->insertBefore(
67
            'Caption',
68
            TextField::create('AltText', _t('AssetAdmin.AltText', 'Alternative text (alt)'))
69
                ->setDescription(_t(
70
                    'AssetAdmin.AltTextDescription',
71
                    'Shown to screen readers or if image can\'t be displayed'
72
                ))
73
        );
74
        $tab->insertAfter(
75
            'AltText',
76
            TextField::create('TitleTooltip', _t('AssetAdmin.TitleTooltip', 'Title text (tooltip)'))
77
                ->setDescription(_t('AssetAdmin.TitleTooltipDescription', 'For additional information about the image'))
78
                ->setValue($record->Title)
79
        );
80
81
        return $tab;
82
    }
83
84
    /**
85
     * @param Controller $controller
86
     * @param string $name
87
     * @param array $context
88
     * @return Form
89
     */
90
    public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = [])
91
    {
92
        $this->beforeExtending('updateForm', function ($form, $controller, $name, $context) {
93
            $record = null;
94
            if (isset($context['Record'])) {
95
                $record = $context['Record'];
96
            }
97
            
98
            if (!$record) {
99
                return;
100
            }
101
            /** @var FieldList $fields */
102
            $fields = $form->Fields();
103
            
104
            $dimensions = $fields->fieldByName('Editor.Placement.Dimensions');
105
            $width = null;
106
            $height = null;
107
            
108
            if ($dimensions) {
109
                $width = $record->getWidth();
110
                $height = $record->getHeight();
111
            }
112
    
113
            if ($width && $height) {
114
                $ratio = $width / $height;
115
        
116
                $dimensions->setSchemaComponent('ProportionConstraintField');
117
                $dimensions->setSchemaState([
118
                    'data' => [
119
                        'ratio' => $ratio
120
                    ]
121
                ]);
122
            }
123
        });
124
125
        return parent::getForm($controller, $name, $context);
126
    }
127
}
128