Completed
Push — master ( d17b85...9e6eca )
by Silvan
02:31
created

SlickBlock::responsiveImages()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 8.8571
cc 6
eloc 10
nc 17
nop 1
crap 42
1
<?php
2
/**
3
 * SliderBlock.
4
 *
5
 * @author Silvan Hahn <[email protected]>
6
 */
7
8
namespace dev7ch\slick\blocks;
9
10
use dev7ch\slick\BaseSlickBlock;
11
use dev7ch\slick\Module;
12
use luya\cms\frontend\blockgroups\ProjectGroup;
13
use luya\cms\helpers\BlockHelper;
14
15
class SlickBlock extends BaseSlickBlock
16
{
17
    /**
18
     * @var bool Choose whether a block can be cached trough the caching component. Be carefull with caching container blocks.
19
     */
20
    public $cacheEnabled = false;
21
    /**
22
     * @var int The cache lifetime for this block in seconds (3600 = 1 hour), only affects when cacheEnabled is true
23
     */
24
    public $cacheExpiration = 3600;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function blockGroup()
30
    {
31 1
        return ProjectGroup::class;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function name()
38
    {
39 1
        return 'Slider Block';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function icon()
46
    {
47 1
        return 'burst_mode'; // see the list of icons on: https://design.google.com/icons/
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function config()
54
    {
55
        return [
56 1
            'vars' => [
57 1
                ['var' => 'images', 'label' => Module::t('block_slick_item'), 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
58 1
                    ['var' => 'image', 'label' => Module::t('block_slick_image'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => false]],
59 1
                    ['var' => 'alt', 'label' => Module::t('block_slick_image_alt'), 'type' => self::TYPE_TEXT],
60 1
                    ['var' => 'title', 'label' => Module::t('block_slick_image_title'), 'type' => self::TYPE_TEXT],
61 1
                    ['var' => 'link', 'label' => Module::t('block_slick_image_link'), 'type' => self::TYPE_LINK],
62 1
                    ['var' => 'responsive_images', 'label' => Module::t('block_slick_adaptive'), 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
63 1
                        ['var' => 'image', 'label' => Module::t('block_slick_adaptive_image'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => true]],
64 1
                        ['var' => 'breakpoint', 'label' => Module::t('block_slick_adaptive_breakpoint'), 'type' => self::TYPE_TEXT, 'options' => ['placeholder' => Module::t('block_slick_adaptive_breakpoint_info')]],
65 1
                        ['var' => 'image_hd', 'label' => Module::t('block_slick_adaptive_image_hd'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => true]],
66 1
                        ['var' => 'orientation', 'label' => Module::t('block_slick_adaptive_orientation'), 'type' => self::TYPE_SELECT, 'options' => BlockHelper::selectArrayOption(
67
                            [
68 1
                                'landscape' => Module::t('block_slick_adaptive_orientation_landscape'),
69 1
                                'portrait'  => Module::t('block_slick_adaptive_orientation_portrait'),
70
                            ]
71
                        )],
72
                    ]],
73
                ]],
74
            ],
75
            'cfgs' => [
76 1
                ['var' => 'cssBackground', 'label' => 'Bild via CSS darstellen', 'type' => self::TYPE_CHECKBOX],
77 1
                ['var' => 'positionTop', 'label' => 'Position vertikal', 'type' => self::TYPE_TEXT, 'placeholder' => '50'],
78 1
                ['var' => 'positionLeft', 'label' => 'Position horizontal', 'type' => self::TYPE_TEXT, 'placeholder' => '50'],
79 1
                ['var' => 'sliderConfig', 'label' => 'Slider options', 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
80 1
                    ['var' => 'option', 'label' => 'Option', 'type' => self::TYPE_TEXT],
81 1
                    ['var' => 'config', 'label' => 'Config', 'type' => self::TYPE_TEXT, 'options' => ['initValue' => 'true']],
82
                ]],
83
            ],
84
        ];
85
    }
86
87
    protected function responsiveImages($parent)
88
    {
89
        $respImagesInput = $parent['responsive_images'];
90
        $respImages = [];
91
        foreach ($respImagesInput as $item) {
92
           $respImages[] = [
93
                'breakpoint'  => isset($item['breakpoint']) ? $item['breakpoint'] : '0',
94
                'orientation' => isset($item['orientation']) ? $item['orientation'] : 'portrait',
95
                'image'       => isset($item['image']) ? BlockHelper::imageUpload($item['image'], false, true) : null,
96
                'imageHD'     => isset($item['image_hd']) ? BlockHelper::imageUpload($item['image'], false, true) : null,
97
            ];
98
        }
99
100
        return $respImages;
101
    }
102
103 1
    protected function images()
104
    {
105 1
        $imagesInput = $this->getVarValue('images', []);
106 1
        $images = [];
107 1
        foreach ($imagesInput as $item) {
108
            $images[] = [
109
                'image'             => isset($item['image']) ? BlockHelper::imageUpload($item['image'], false, true) : null,
110
                'alt'               => isset($item['alt']) ? $item['alt'] : 'no-alt-text-set',
111
                'title'             => isset($item['title']) ? $item['title'] : '',
112
                'link'              => isset($item['link']) ? BlockHelper::linkObject($item['link']) : null,
113
                'isPublished'       => isset($item['isPublished']) ? true : false,
114
                'responsive_images' => $this->responsiveImages($item),
115
116
            ];
117
        }
118
119 1
        return $images;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1
    public function extraVars()
126
    {
127
        return [
128 1
            'images' => $this->images(),
129
        ];
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @param {{extras.image}}
136
     * @param {{vars.image}}
137
     * @param {{vars.isPublished}}
138
     * @param {{vars.link}}
139
     * @param {{vars.title}}
140
     */
141 1
    public function admin()
142
    {
143 1
        return 'Slider Admin View';
144
    }
145
}
146