FlexSlider::getSlideshowSpeed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FlexSlider\ORM;
4
5
use Dynamic\FlexSlider\Model\SlideImage;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\GridField\GridField;
13
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
14
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
15
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
16
use SilverStripe\Forms\NumericField;
17
use SilverStripe\Forms\ToggleCompositeField;
18
use SilverStripe\ORM\DataExtension;
19
use SilverStripe\ORM\DataObject;
20
use SilverStripe\View\Requirements;
21
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
22
23
/**
24
 * Class FlexSlider
25
 * @package Dynamic\FlexSlider\ORM
26
 * @property string $Animation
27
 * @property bool $Loop
28
 * @property bool $Animate
29
 * @property bool $ThumbnailNav
30
 * @property bool $SliderControlNav
31
 * @property bool $CarouselControlNav
32
 * @property bool $CarouselDirectionNav
33
 * @property int $CarouselThumbnailCt
34
 * @property double $FlexSliderSpeed
35
 *
36
 * @property-read DataObject|FlexSlider $owner
37
 */
38
class FlexSlider extends DataExtension
39
{
40
    use Configurable;
41
42
    /**
43
     * @var array
44
     */
45
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
46
        'Animation' => "Enum('slide, fade', 'slide')",
47
        'Loop' => 'Boolean',
48
        'Animate' => 'Boolean',
49
        'ThumbnailNav' => 'Boolean',
50
        'SliderControlNav' => 'Boolean',
51
        'SliderDirectionNav' => 'Boolean',
52
        'CarouselControlNav' => 'Boolean',
53
        'CarouselDirectionNav' => 'Boolean',
54
        'CarouselThumbnailCt' => 'Int',
55
        'FlexSliderSpeed' => 'Double',
56
    ];
57
58
    /**
59
     * @var array
60
     */
61
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
62
        'Slides' => SlideImage::class,
63
    ];
64
65
    /**
66
     *
67
     */
68
    public function populateDefaults()
69
    {
70
        $this->owner->Loop = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $Loop was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
71
        $this->owner->Animate = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $Animate was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
72
        $this->owner->SliderControlNav = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $SliderControlNav was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
73
        $this->owner->SliderDirectionNav = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property SliderDirectionNav does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        $this->owner->CarouselControlNav = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $CarouselControlNav was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
        $this->owner->CarouselDirectionNav = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $CarouselDirectionNav was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
76
        $this->owner->CarouselThumbnailCt = 6;
77
        $this->owner->FlexSliderSpeed = $this->getDefaultSpeed();
78
79
        return parent::populateDefaults();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::populateDefaults() targeting SilverStripe\ORM\DataExtension::populateDefaults() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
    }
81
82
    /**
83
     * @param FieldList $fields
84
     */
85
    public function updateCMSFields(FieldList $fields)
86
    {
87
        $fields->removeByName([
88
            'Animation',
89
            'Loop',
90
            'Animate',
91
            'ThumbnailNav',
92
            'SliderControlNav',
93
            'SliderDirectionNav',
94
            'CarouselControlNav',
95
            'CarouselDirectionNav',
96
            'CarouselThumbnailCt',
97
            'FlexSliderSpeed',
98
            'Slides',
99
        ]);
100
101
        // Slides
102
        if ($this->owner->ID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on Dynamic\FlexSlider\ORM\FlexSlider. Did you maybe forget to declare it?
Loading history...
103
            $config = GridFieldConfig_RecordEditor::create();
104
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
105
            $config->removeComponentsByType([
106
                GridFieldAddExistingAutocompleter::class,
107
                GridFieldDeleteAction::class,
108
            ]);
109
            $SlidesField = GridField::create(
110
                'Slides',
111
                _t(__CLASS__ . '.SLIDES', 'Slides'),
112
                $this->owner->Slides()->sort('SortOrder'),
0 ignored issues
show
Bug introduced by
The method Slides() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. Did you maybe mean getSlideshowSpeed()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
                $this->owner->/** @scrutinizer ignore-call */ 
113
                              Slides()->sort('SortOrder'),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
                $config
114
            );
115
116
            $slideTitle = $this->owner->stat('slide_tab_title') ?: _t(__CLASS__ . '.SLIDES', 'Slides');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\View\ViewableData::stat() has been deprecated: 5.0 Use ->config()->get() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
            $slideTitle = /** @scrutinizer ignore-deprecated */ $this->owner->stat('slide_tab_title') ?: _t(__CLASS__ . '.SLIDES', 'Slides');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Dynamic\FlexSlider\ORM\FlexSlider::stat() has been deprecated: 5.0 Use ->config()->get() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
            $slideTitle = /** @scrutinizer ignore-deprecated */ $this->owner->stat('slide_tab_title') ?: _t(__CLASS__ . '.SLIDES', 'Slides');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
117
118
            $animations = [];
119
            $animationOptions = $this->owner->dbObject('Animation')->getEnum();
0 ignored issues
show
Bug introduced by
The method dbObject() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            $animationOptions = $this->owner->/** @scrutinizer ignore-call */ dbObject('Animation')->getEnum();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
            foreach ($animationOptions as $value) {
121
                $animations[$value] = _t(__CLASS__ . ".$value", $value);
122
            }
123
124
            $fields->addFieldsToTab("Root.{$slideTitle}", [
125
                $SlidesField,
126
                ToggleCompositeField::create('ConfigHD', _t(__CLASS__ . '.SettingsLabel', 'Slider Settings'), [
127
                    DropdownField::create(
128
                        'Animation',
129
                        _t(__CLASS__ . '.ANIMATION_OPTION', 'Animation option'),
130
                        $animations
131
                    ),
132
                    CheckboxField::create(
133
                        'Animate',
134
                        _t(__CLASS__ . '.ANIMATE', 'Animate automatically')
135
                    ),
136
                    CheckboxField::create(
137
                        'Loop',
138
                        _t(__CLASS__ . '.LOOP', 'Loop the carousel')
139
                    ),
140
                    CheckboxField::create(
141
                        'SliderControlNav',
142
                        _t(__CLASS__ . '.CONTROL_NAV', 'Show ControlNav')
143
                    ),
144
                    CheckboxField::create(
145
                        'SliderDirectionNav',
146
                        _t(__CLASS__ . '.DIRECTION_NAV', 'Show DirectionNav')
147
                    ),
148
                    CheckboxField::create(
149
                        'ThumbnailNav',
150
                        _t(__CLASS__ . '.THUMBNAIL_NAV', 'Thumbnail Navigation')
151
                    ),
152
                    //DisplayLogicWrapper::create(
153
                    CheckboxField::create(
154
                        'CarouselControlNav',
155
                        _t(__CLASS__ . '.CAROUSEL_CONTROL_NAV', 'Show Carousel ControlNav')
156
                    ),
157
                    CheckboxField::create(
158
                        'CarouselDirectionNav',
159
                        _t(__CLASS__ . '.CAROUSEL_DIRECTION_NAV', 'Show Carousel DirectionNav')
160
                    ),
161
                    NumericField::create(
162
                        'CarouselThumbnailCt',
163
                        _t(__CLASS__ . '.CAROUSEL_THUMBNAIL_COUNT', 'Number of thumbnails')
164
                    ),
165
                    NumericField::create(
166
                        'FlexSliderSpeed',
167
                        _t(__CLASS__ . '.SLIDER_SPEED', 'Slider Speed')
168
                    )
169
                        ->setDescription('In Seconds')
170
                        ->setScale(2),
171
                ]),
172
            ]);
173
        }
174
    }
175
176
    /**
177
     * @return mixed
178
     */
179
    public function getSlideShow()
180
    {
181
        $owner = $this->owner;
182
183
        if (!($owner instanceof SiteTree)) {
184
            $this->getCustomScript();
185
        }
186
187
        return $this->owner->Slides()->sort('SortOrder');
188
    }
189
190
    /**
191
     * add requirements to PageController init()
192
     */
193
    public function contentcontrollerInit()
194
    {
195
        // only call custom script if page has Slides and DataExtension
196
        if (DataObject::has_extension($this->owner->Classname, FlexSlider::class)) {
0 ignored issues
show
Bug Best Practice introduced by
The property Classname does not exist on Dynamic\FlexSlider\ORM\FlexSlider. Did you maybe forget to declare it?
Loading history...
197
            if ($this->owner->getSlideShow()->exists()) {
198
                $this->getCustomScript();
199
            }
200
        }
201
    }
202
203
    /**
204
     *
205
     */
206
    public function getCustomScript()
207
    {
208
        // Flexslider options
209
        $sync = ($this->owner->ThumbnailNav == true) ? "sync: '.fs-carousel:eq('+index+')'," : '';
210
211
        $before = $this->owner->hasMethod('flexSliderBeforeAction')
0 ignored issues
show
Bug introduced by
The method hasMethod() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
        $before = $this->owner->/** @scrutinizer ignore-call */ hasMethod('flexSliderBeforeAction')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
            ? $this->owner->flexSliderBeforeAction()
0 ignored issues
show
Bug introduced by
The method flexSliderBeforeAction() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
            ? $this->owner->/** @scrutinizer ignore-call */ flexSliderBeforeAction()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
            : 'function(){}';
214
215
        $after = $this->owner->hasMethod('flexSliderAfterAction')
216
            ? $this->owner->flexSliderAfterAction()
0 ignored issues
show
Bug introduced by
The method flexSliderAfterAction() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
            ? $this->owner->/** @scrutinizer ignore-call */ flexSliderAfterAction()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
            : 'function(){}';
218
219
        $speed = $this->getSlideshowSpeed();
220
221
        Requirements::customScript(
222
            "(function($) {
223
                $(document).ready(function(){
224
                    jQuery('.flexslider').each(function(index){
225
					 
226
                         if(jQuery('.fs-carousel').eq(index).length) {
227
                             jQuery('.fs-carousel').eq(index).flexslider({
228
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
0 ignored issues
show
Bug introduced by
The method obj() does not exist on Dynamic\FlexSlider\ORM\FlexSlider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

228
                                slideshow: " . $this->owner->/** @scrutinizer ignore-call */ obj('Animate')->NiceAsBoolean() . ",

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
                                animation: 'slide',
230
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
231
                                controlNav: " . $this->owner->obj('CarouselControlNav')->NiceAsBoolean() . ", 
232
                                directionNav: " . $this->owner->obj('CarouselDirectionNav')->NiceAsBoolean() . ",
233
                                prevText: '',
234
                                nextText: '',
235
                                pausePlay: false,
236
                                asNavFor: '.flexslider:eq('+index+')',
237
                                minItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
0 ignored issues
show
Bug introduced by
Are you sure $this->owner->obj('CarouselThumbnailCt') of type SilverStripe\ORM\FieldType\DBField|object can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
                                minItems: " . /** @scrutinizer ignore-type */ $this->owner->obj('CarouselThumbnailCt') . ",
Loading history...
238
                                maxItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
239
                                move: " . $this->owner->obj('CarouselThumbnailCt') . ",
240
                                itemWidth: 100,
241
                                itemMargin: 10
242
                              });
243
                         }
244
 
245
                        if(jQuery('.flexslider').eq(index).length){
246
                            jQuery('.flexslider').eq(index).flexslider({
247
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
248
                                animation: '" . $this->owner->Animation . "',
249
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
250
                                controlNav: " . $this->owner->obj('SliderControlNav')->NiceAsBoolean() . ",
251
                                directionNav: " . $this->owner->obj('SliderDirectionNav')->NiceAsBoolean() . ",
252
                                prevText: '',
253
                                nextText: '',
254
                                pauseOnAction: true,
255
                                pauseOnHover: true,
256
                                " . $sync . "
257
                                start: function(slider){
258
                                  $('body').removeClass('loading');
259
                                },
260
                                before: " . $before . ',
261
                                after: ' . $after . ',
262
                                slideshowSpeed: ' . $speed . ' 
263
                            });
264
                        }
265
                    })
266
                });
267
            }(jQuery));'
268
        );
269
    }
270
271
    /**
272
     * @return int
273
     */
274
    public function getSlideshowSpeed()
275
    {
276
        $speed = $this->owner->FlexSliderSpeed > 0
277
            ? $this->owner->FlexSliderSpeed
278
            : $this->getDefaultSpeed();
279
280
        return $speed * 1000;
281
    }
282
283
    /**
284
     * @return mixed
285
     */
286
    protected function getDefaultSpeed()
287
    {
288
        return $this->owner->config()->get('flex_slider_speed')
289
            ?: Config::inst()->get(FlexSlider::class, 'flex_slider_speed');
290
    }
291
292
    /**
293
     *
294
     */
295
    public function onBeforeWrite()
296
    {
297
        parent::onBeforeWrite();
298
299
        if (!$this->owner->CarouselThumbnailCt) {
300
            $this->owner->CarouselThumbnailCt = 6;
301
        }
302
    }
303
}
304