Completed
Push — master ( 075472...2198da )
by Daniel
14s
created

ImageFormFactory::getSpecsMarkup()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 11
nc 5
nop 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Control\RequestHandler;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\FormFactory;
11
use SilverStripe\Forms\Tab;
12
use SilverStripe\Forms\TextField;
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
        $width = $record->getWidth();
23
        $height = $record->getHeight();
24
        $dimensions = $width && $height ? sprintf('%dx%dpx', $width, $height) : '';
25
        return sprintf(
26
            '<div class="editor__specs">%s %s %s</div>',
27
            $dimensions,
28
            $record->getSize(),
29
            $this->getStatusFlagMarkup($record)
30
        );
31
    }
32
33
    protected function getFormFieldAttributesTab($record, $context = [])
34
    {
35
        /** @var Tab $tab */
36
        $tab = parent::getFormFieldAttributesTab($record, $context);
37
38
        $alignments = array(
39
            'leftAlone' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AlignmentLeftAlone', 'On the left, on its own.'),
40
            'center' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AlignmentCenter', 'Centered, on its own.'),
41
            'left' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AlignmentLeft', 'On the left, with text wrapping around.'),
42
            'right' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AlignmentRight', 'On the right, with text wrapping around.'),
43
        );
44
45
        $tab->push(
46
            DropdownField::create('Alignment', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Alignment', 'Alignment'), $alignments)
47
        );
48
        $tab->push(
49
            FieldGroup::create(
50
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ImageSpecs', 'Dimensions'),
51
                TextField::create(
52
                    'InsertWidth',
53
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ImageWidth', 'Width')
54
                )
55
                    ->setMaxLength(5)
56
                    ->addExtraClass('flexbox-area-grow'),
57
                TextField::create(
58
                    'InsertHeight',
59
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ImageHeight', 'Height')
60
                )
61
                    ->setMaxLength(5)
62
                    ->addExtraClass('flexbox-area-grow')
63
            )
64
            ->addExtraClass('fieldgroup--fill-width')
65
            ->setName('Dimensions')
66
        );
67
68
        $tab->insertBefore(
69
            'Caption',
70
            TextField::create('AltText', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AltText', 'Alternative text (alt)'))
71
                ->setDescription(_t(
72
                    'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AltTextDescription',
73
                    'Shown to screen readers or if image can\'t be displayed'
74
                ))
75
        );
76
        $tab->insertAfter(
77
            'AltText',
78
            TextField::create('TitleTooltip', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.TitleTooltip', 'Title text (tooltip)'))
79
                ->setDescription(_t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.TitleTooltipDescription', 'For additional information about the image'))
80
                ->setValue($record->Title)
81
        );
82
83
        return $tab;
84
    }
85
86
    /**
87
     * @param RequestHandler $controller
88
     * @param string $name
89
     * @param array $context
90
     * @return Form
91
     */
92
    public function getForm(RequestHandler $controller = null, $name = FormFactory::DEFAULT_NAME, $context = [])
93
    {
94
        $this->beforeExtending('updateForm', function ($form) use ($context) {
95
            $record = null;
96
            if (isset($context['Record'])) {
97
                $record = $context['Record'];
98
            }
99
            
100
            if (!$record) {
101
                return;
102
            }
103
            /** @var FieldList $fields */
104
            $fields = $form->Fields();
105
            
106
            $dimensions = $fields->fieldByName('Editor.Placement.Dimensions');
107
            $width = null;
108
            $height = null;
109
            
110
            if ($dimensions) {
111
                $width = $record->getWidth();
112
                $height = $record->getHeight();
113
            }
114
    
115 View Code Duplication
            if ($width && $height) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                $ratio = $width / $height;
117
        
118
                $dimensions->setSchemaComponent('ProportionConstraintField');
119
                $dimensions->setSchemaState([
120
                    'data' => [
121
                        'ratio' => $ratio
122
                    ]
123
                ]);
124
            }
125
        });
126
127
        return parent::getForm($controller, $name, $context);
128
    }
129
}
130