Completed
Pull Request — master (#307)
by Damian
12:34
created

ImageFormFactory::getInsertHeight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldGroup;
9
use SilverStripe\Forms\Tab;
10
use SilverStripe\Forms\TextField;
11
12
class ImageFormFactory extends FileFormFactory
13
{
14
    use Configurable;
15
    
16
    /**
17
     * Default insertion width for Images and Media
18
     *
19
     * @config
20
     * @var int
21
     */
22
    private static $insert_width = 600;
0 ignored issues
show
Unused Code introduced by
The property $insert_width is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
    
24
    /**
25
     * Default insert height for images and media
26
     *
27
     * @config
28
     * @var int
29
     */
30
    private static $insert_height = 360;
0 ignored issues
show
Unused Code introduced by
The property $insert_height is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
    
32
    protected function getSpecsMarkup($record)
33
    {
34
        if (!$record || !$record->exists()) {
35
            return null;
36
        }
37
        // Add dimensions to specs
38
        $dimensions = $record->getDimensions() ? $record->getDimensions() . 'px,' : '';
39
        return sprintf(
40
            '<div class="editor__specs">%s %s %s</div>',
41
            $dimensions,
42
            $record->getSize(),
43
            $this->getStatusFlagMarkup($record)
44
        );
45
    }
46
    
47
    protected function getFormFieldAttributesTab($record)
48
    {
49
        /** @var Tab $tab */
50
        $tab = parent::getFormFieldAttributesTab($record);
51
    
52
        $alignments = array(
53
            'leftAlone' => _t('AssetAdmin.AlignmentLeftAlone', 'On the left, on its own.'),
54
            'center' => _t('AssetAdmin.AlignmentCenter', 'Centered, on its own.'),
55
            'left' => _t('AssetAdmin.AlignmentLeft', 'On the left, with text wrapping around.'),
56
            'right' => _t('AssetAdmin.AlignmentRight', 'On the right, with text wrapping around.'),
57
        );
58
    
59
        $tab->push(
60
            DropdownField::create('Alignment', _t('AssetAdmin.Alignment', 'Alignment'), $alignments)
61
        );
62
        $tab->push(
63
            FieldGroup::create(_t('AssetAdmin.ImageSpecs', 'Dimensions'),
64
                TextField::create(
65
                    'Width',
66
                    _t('AssetAdmin.ImageWidth', 'Width'),
67
                    $this->getInsertWidth($record)
68
                )
69
                    ->setMaxLength(5)
70
                    ->addExtraClass('flexbox-area-grow'),
71
                TextField::create(
72
                    'Height',
73
                    _t('AssetAdmin.ImageHeight', 'Height'),
74
                    $this->getInsertHeight($record)
75
                )
76
                    ->setMaxLength(5)
77
                    ->addExtraClass('flexbox-area-grow')
78
            )->addExtraClass('fill-width')
79
        );
80
        
81
        $tab->insertBefore(
82
            'Caption',
83
            TextField::create('AltText', _t('AssetAdmin.AltText', 'Alternative Text (alt)'))
84
                ->setDescription(_t('AssetAdmin.AltTextDescription', 'Shown to screen readers or if image can\'t be displayed'))
85
        );
86
        $tab->insertAfter(
87
            'AltText',
88
            TextField::create('TitleTooltip', _t('AssetAdmin.TitleTooltip', 'Title text (tooltip)'))
89
                ->setDescription(_t('AssetAdmin.TitleTooltipDescription', 'For additional information about the image'))
90
        );
91
        
92
        return $tab;
93
    }
94
    
95
    /**
96
     * Provide an initial width for inserted media, restricted based on $embed_width
97
     *
98
     * @param File $file
99
     * @return int
100
     */
101
    protected function getInsertWidth($file)
102
    {
103
        $maxWidth = $this->config()->insert_width;
104
        
105
        $width = max($file->getWidth(), $maxWidth);
106
        
107
        return min($width, $maxWidth);
108
    }
109
    
110
    /**
111
     * Provide an initial height for inserted media, scaled proportionally to the initial width
112
     *
113
     * @param File $file
114
     * @return int
115
     */
116
    protected function getInsertHeight($file)
117
    {
118
        $maxWidth = $this->config()->insert_width;
119
        $maxHeight = $this->config()->insert_height;
120
        
121
        $width = max($file->getWidth(), $maxWidth);
122
        $height = max($file->getHeight(), $maxHeight);
123
        
124
        return ($width <= $maxWidth) ? $height : round($height * ($maxWidth / $width));
125
    }
126
    
127
}
128