Completed
Pull Request — master (#109)
by Jason
06:12
created

FlexSlider::getCustomScript()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 66
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 0
cts 0
cp 0
rs 8.6045
c 0
b 0
f 0
cc 5
eloc 29
nc 16
nop 0
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dynamic\FlexSlider;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Core\Object;
7
use SilverStripe\Dev\Debug;
8
use SilverStripe\Forms\CheckboxField;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\GridField\GridField;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
13
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14
use SilverStripe\Forms\NumericField;
15
use SilverStripe\Forms\ToggleCompositeField;
16
use SilverStripe\GridFieldExtensions\GridFieldOrderableRows;
17
use SilverStripe\ORM\DataExtension;
18
use SilverStripe\View\Requirements;
19
20
class FlexSlider extends DataExtension
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $db = array(
26
        'Animation' => "Enum('slide, fade', 'slide')",
27
        'Loop' => 'Boolean',
28
        'Animate' => 'Boolean',
29
        'ThumbnailNav' => 'Boolean',
30
        'SliderControlNav' => 'Boolean',
31
        'SliderDirectionNav' => 'Boolean',
32
        'CarouselControlNav' => 'Boolean',
33
        'CarouselDirectionNav' => 'Boolean',
34
        'CarouselThumbnailCt' => 'Int',
35
    );
36
37
    /**
38
     * @var array
39
     */
40
    private static $has_many = array(
41
        'Slides' => SlideImage::class,
42
    );
43
44
    /**
45
     *
46
     */
47 3
    public function populateDefaults()
48
    {
49 3
        parent::populateDefaults();
50 3
        $this->owner->Loop = 1;
51 3
        $this->owner->Animate = 1;
52 3
        $this->owner->SliderControlNav = 1;
53 3
        $this->owner->SliderDirectionNav = 1;
54 3
        $this->owner->CarouselControlNav = 0;
55 3
        $this->owner->CarouselDirectionNav = 1;
56 3
        $this->owner->CarouselThumbnailCt = 6;
57 3
    }
58
59
    /**
60
     * @param FieldList $fields
61
     */
62 2
    public function updateCMSFields(FieldList $fields)
63
    {
64 2
        $fields->removeByName(array(
65 2
            'Animation',
66
            'Loop',
67
            'Animate',
68
            'ThumbnailNav',
69
            'SliderControlNav',
70
            'SliderDirectionNav',
71
            'CarouselControlNav',
72
            'CarouselDirectionNav',
73
            'CarouselThumbnailCt'
74
        ));
75
76
        // Slides
77 2
        if ($this->owner->ID) {
78 2
            $config = GridFieldConfig_RecordEditor::create();
79
            //$config->addComponent(new GridFieldOrderableRows('SortOrder'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80 2
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
81 2
            $config->removeComponentsByType('GridFieldDeleteAction');
82 2
            $config->addComponent(new GridFieldDeleteAction(false));
83 2
            $SlidesField = GridField::create('Slides', 'Slides', $this->owner->Slides()->sort('SortOrder'), $config);
84
85 2
            $slideTitle = $this->owner->stat('slide_tab_title') ? $this->owner->stat('slide_tab_title') : 'Slides';
86
87 2
            $fields->addFieldsToTab("Root.{$slideTitle}", array(
88 2
                $SlidesField,
89 2
                ToggleCompositeField::create('ConfigHD', 'Slider Settings', array(
90 2
                    DropdownField::create('Animation', 'Animation option', $this->owner->dbObject('Animation')->enumValues()),
91 2
                    CheckboxField::create('Animate', 'Animate automatically'),
92 2
                    CheckboxField::create('Loop', 'Loop the carousel'),
93 2
                    CheckboxField::create('SliderControlNav', 'Show ControlNav'),
94 2
                    CheckboxField::create('SliderDirectionNav', 'Show DirectionNav'),
95 2
                    CheckboxField::create('ThumbnailNav', 'Thumbnail Navigation'),
96
                    //DisplayLogicWrapper::create(
97 2
                        CheckboxField::create('CarouselControlNav', 'Show Carousel ControlNav'),
98 2
                        CheckboxField::create('CarouselDirectionNav', 'Show Carousel DirectionNav'),
99 2
                        NumericField::create('CarouselThumbnailCt', 'Number of thumbnails')
100
                    //)->displayIf('ThumbnailNav')->isChecked()->end()
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
                )),
102
            ));
103
        }
104 2
    }
105
106
    /**
107
     * @return DataList
108
     */
109 1
    public function SlideShow()
110
    {
111 1
        $owner = $this->owner;
112
113 1
        if (!($owner instanceof SiteTree)) {
114
            $this->getCustomScript();
115
        }
116
117 1
        return $this->owner->Slides()->sort('SortOrder');
118
    }
119
120
    /**
121
     * add requirements to Page_Controller init()
122
     */
123
    public function contentcontrollerInit()
124
    {
125
        // only call custom script if page has Slides and DataExtension
126
        if (Object::has_extension($this->owner->ClassName, 'FlexSlider')) {
127
            if ($this->owner->SlideShow()->exists()) {
128
                $this->getCustomScript();
129
            }
130
        }
131
    }
132
133
    /**
134
     *
135
     */
136
    public function getCustomScript()
137
    {
138
        // Flexslider options
139
        $sync = ($this->owner->ThumbnailNav == true) ? "sync: '.carousel:eq('+index+')'," : '';
140
141
        $before = (method_exists($this->owner->ClassName, 'flexSliderBeforeAction'))
142
            ? $this->owner->flexSliderBeforeAction()
143
            : 'function(){}';
144
145
        $after = (method_exists($this->owner->ClassName, 'flexSliderAfterAction'))
146
            ? $this->owner->flexSliderAfterAction()
147
            : 'function(){}';
148
149
        $speed = (method_exists($this->owner->ClassName, 'setFlexSliderSpeed'))
150
            ? $this->owner->setFlexSliderSpeed()
151
            : 7000;
152
153
        Requirements::customScript("
154
            (function($) {
155
                $(document).ready(function(){
156
                    jQuery('.flexslider').each(function(index){
157
					 
158
                         if(jQuery('.carousel').eq(index).length) {
159
                             jQuery('.carousel').eq(index).flexslider({
160
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
161
                                animation: '" . $this->owner->Animation . "',
162
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
163
                                controlNav: " . $this->owner->obj('CarouselControlNav')->NiceAsBoolean() . ", 
164
                                directionNav: " . $this->owner->obj('CarouselDirectionNav')->NiceAsBoolean() . ",
165
                                prevText: '',
166
                                nextText: '',
167
                                pausePlay: false,
168
                                asNavFor: '.flexslider:eq('+index+')',
169
                                minItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
170
                                maxItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
171
                                move: " . $this->owner->obj('CarouselThumbnailCt') . ",
172
                                itemWidth: 100,
173
                                itemMargin: 10
174
                              });
175
                         }
176
 
177
                        if(jQuery('.flexslider').eq(index).length){
178
                            jQuery('.flexslider').eq(index).flexslider({
179
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
180
                                animation: '" . $this->owner->Animation . "',
181
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
182
                                controlNav: " . $this->owner->obj('SliderControlNav')->NiceAsBoolean() . ",
183
                                directionNav: " . $this->owner->obj('SliderDirectionNav')->NiceAsBoolean() . ",
184
                                prevText: '',
185
                                nextText: '',
186
                                pauseOnAction: true,
187
                                pauseOnHover: true,
188
                                " . $sync . "
189
                                start: function(slider){
190
                                  $('body').removeClass('loading');
191
                                },
192
                                before: " . $before . ',
193
                                after: ' . $after . ',
194
                                slideshowSpeed: ' . $speed . ' 
195
                            });
196
                        }
197
                    })
198
                });
199
            }(jQuery));'
200
        );
201
    }
202
203
    /**
204
     *
205
     */
206 3
    public function onBeforeWrite()
207
    {
208 3
        parent::onBeforeWrite();
209
210 3
        if (!$this->owner->CarouselThumbnailCt) {
211
            $this->owner->CarouselThumbnailCt = 6;
212
        }
213
214 3
    }
215
}
216