Completed
Push — master ( 653568...4bb1b9 )
by Silvan
01:57
created

SlickBlock::responsiveImages()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 9
nc 17
nop 1
crap 6
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 2
    public function blockGroup()
30
    {
31 2
        return ProjectGroup::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return luya\cms\frontend...ups\ProjectGroup::class returns the type string which is incompatible with the return type mandated by luya\cms\base\BlockInterface::blockGroup() of luya\cms\base\BlockGroup.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function name()
38
    {
39 2
        return 'Slider Block';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function icon()
46
    {
47 2
        return 'burst_mode'; // see the list of icons on: https://design.google.com/icons/
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function config()
54
    {
55
        return [
56 2
            'vars' => [
57 2
                ['var' => 'images', 'label' => Module::t('block_slick_item'), 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
58 2
                    ['var' => 'image', 'label' => Module::t('block_slick_image'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => false]],
59 2
                    ['var' => 'alt', 'label' => Module::t('block_slick_image_alt'), 'type' => self::TYPE_TEXT],
60 2
                    ['var' => 'title', 'label' => Module::t('block_slick_image_title'), 'type' => self::TYPE_TEXT],
61 2
                    ['var' => 'link', 'label' => Module::t('block_slick_image_link'), 'type' => self::TYPE_LINK],
62 2
                    ['var' => 'responsive_images', 'label' => Module::t('block_slick_adaptive'), 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
63 2
                        ['var' => 'image', 'label' => Module::t('block_slick_adaptive_image'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => true]],
64 2
                        ['var' => 'breakpoint', 'label' => Module::t('block_slick_adaptive_breakpoint'), 'type' => self::TYPE_TEXT, 'options' => ['placeholder' => Module::t('block_slick_adaptive_breakpoint_info')]],
65 2
                        ['var' => 'image_hd', 'label' => Module::t('block_slick_adaptive_image_hd'), 'type' => self::TYPE_IMAGEUPLOAD, 'options' => ['no_filter' => true]],
66 2
                        ['var' => 'orientation', 'label' => Module::t('block_slick_adaptive_orientation'), 'type' => self::TYPE_SELECT, 'options' => BlockHelper::selectArrayOption(
67
                            [
68 2
                                'landscape' => Module::t('block_slick_adaptive_orientation_landscape'),
69 2
                                'portrait'  => Module::t('block_slick_adaptive_orientation_portrait'),
70
                            ]
71
                        )],
72
                    ]],
73
                ]],
74
            ],
75
            'cfgs' => [
76
                // in progress
77 2
                ['var' => 'cssBackground', 'label' => 'Bild via CSS darstellen', 'type' => self::TYPE_CHECKBOX],
78 2
                ['var' => 'positionTop', 'label' => 'Position vertikal', 'type' => self::TYPE_TEXT, 'placeholder' => '50'],
79 2
                ['var' => 'positionLeft', 'label' => 'Position horizontal', 'type' => self::TYPE_TEXT, 'placeholder' => '50'],
80 2
                ['var' => 'sliderConfig', 'label' => 'Slider options', 'type' => self::TYPE_MULTIPLE_INPUTS, 'options' => [
81 2
                    ['var' => 'option', 'label' => 'Option', 'type' => self::TYPE_TEXT],
82 2
                    ['var' => 'config', 'label' => 'Config', 'type' => self::TYPE_TEXT, 'options' => ['initValue' => 'true']],
83
                ]],
84
            ],
85
        ];
86
    }
87
88 1
    public function responsiveImages($parent)
89
    {
90 1
        $respImagesInput = $parent['responsive_images'];
91 1
        $respImages = [];
92 1
        foreach ($respImagesInput as $item) {
93 1
            $respImages[] = [
94 1
                'breakpoint'  => isset($item['breakpoint']) ? $item['breakpoint'] : '0',
95 1
                'orientation' => isset($item['orientation']) ? $item['orientation'] : 'portrait',
96 1
                'image'       => isset($item['image']) ? BlockHelper::imageUpload($item['image'], false, true) : null,
97 1
                'imageHD'     => isset($item['image_hd']) ? BlockHelper::imageUpload($item['image_hd'], false, true) : null,
98
            ];
99
        }
100
101 1
        return $respImages;
102
    }
103
104 5
    public function images($image = null)
105
    {
106 5
        $imagesInput = $image != null ? $image : $this->getVarValue('images', []);
107 5
        $images = [];
108 5
        foreach ($imagesInput as $item) {
109
            $images[] = [
110
                'image'             => isset($item['image']) ? BlockHelper::imageUpload($item['image'], false, true) : null,
111
                'alt'               => isset($item['alt']) ? $item['alt'] : 'no-alt-text-set',
112
                'title'             => isset($item['title']) ? $item['title'] : '',
113
                'link'              => isset($item['link']) ? BlockHelper::linkObject($item['link']) : null,
114
                'isPublished'       => isset($item['isPublished']) ? true : false,
115
                'responsive_images' => $this->responsiveImages($item),
116
117
            ];
118
        }
119
120 5
        return $images;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 4
    public function extraVars()
127
    {
128
        return [
129 4
            'images' => $this->images(),
130
        ];
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     *
136
     * @param {{extras.image}}
137
     * @param {{vars.image}}
138
     * @param {{vars.isPublished}}
139
     * @param {{vars.link}}
140
     * @param {{vars.title}}
141
     */
142 2
    public function admin()
143
    {
144 2
        return 'Slider Admin View';
145
    }
146
}
147