Completed
Push — 1.0 ( 2b76ce...6fb707 )
by Jason
10s
created

FlexSlider::onBeforeWrite()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 6
cts 12
cp 0.5
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 0
crap 6
1
<?php
2
3
class FlexSlider extends DataExtension
4
{
5
    /**
6
     * @var array
7
     */
8
    public static $db = array(
9
        'Animation' => "Enum('slide, fade', 'slide')",
10
        'Loop' => 'Boolean',
11
        'Animate' => 'Boolean',
12
        'ThumbnailNav' => 'Boolean',
13
        'SliderControlNav' => 'Boolean',
14
        'SliderDirectionNav' => 'Boolean',
15
        'CarouselControlNav' => 'Boolean',
16
        'CarouselDirectionNav' => 'Boolean',
17
        'CarouselItemWidth' => 'Int',
18
        'CarouselItemMargin' => 'Int',
19
        'CarouselThumbnailCt' => 'Int',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    public static $has_many = array(
26
        'Slides' => 'SlideImage',
27
    );
28
29
    /**
30
     *
31
     */
32 4
    public function populateDefaults()
33
    {
34 4
        parent::populateDefaults();
35 4
        $this->owner->Loop = 1;
36 4
        $this->owner->Animate = 1;
37 4
        $this->owner->SliderControlNav = 1;
38 4
        $this->owner->SliderDirectionNav = 1;
39 4
        $this->owner->CarouselControlNav = 0;
40 4
        $this->owner->CarouselDirectionNav = 1;
41 4
        $this->owner->CarouselItemWidth = 196;
42 4
        $this->owner->CarouselItemMargin = 10;
43 4
        $this->owner->CarouselThumbnailCt = 4;
44 4
    }
45
46
    /**
47
     * @param FieldList $fields
48
     */
49 2
    public function updateCMSFields(FieldList $fields)
50
    {
51 2
        $fields->removeByName(array(
52 2
            'Animation',
53 2
            'Loop',
54 2
            'Animate',
55 2
            'ThumbnailNav',
56 2
            'SliderControlNav',
57 2
            'SliderDirectionNav',
58 2
            'CarouselControlNav',
59 2
            'CarouselDirectionNav',
60 2
            'CarouselItemWidth',
61 2
            'CarouselItemMargin',
62
            'CarouselThumbnailCt'
63 2
        ));
64
65
        // Slides
66 2
        if ($this->owner->ID) {
67 2
            $config = GridFieldConfig_RecordEditor::create();
68 2
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
69 2
            if (class_exists('GridFieldBulkUpload')) {
70
                $config->addComponent(new GridFieldBulkUpload());
71
                $config->addComponent(new GridFieldBulkManager());
72
            }
73 2
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
74 2
            $config->removeComponentsByType('GridFieldDeleteAction');
75 2
            $config->addComponent(new GridFieldDeleteAction(false));
76 2
            $SlidesField = GridField::create('Slides', 'Slides', $this->owner->Slides()->sort('SortOrder'), $config);
77
78 2
            $slideTitle = $this->owner->stat('slide_tab_title') ? $this->owner->stat('slide_tab_title') : 'Slides';
79
80 2
            $fields->addFieldsToTab("Root.{$slideTitle}", array(
81 2
                $SlidesField,
82 2
                ToggleCompositeField::create('ConfigHD', 'Slider Settings', array(
83 2
                    CheckboxField::create('Animate', 'Animate automatically'),
84 2
                    DropdownField::create('Animation', 'Animation option', $this->owner->dbObject('Animation')->enumValues()),
85 2
                    CheckboxField::create('Loop', 'Loop the carousel'),
86 2
                    CheckboxField::create('SliderControlNav', 'Show ControlNav'),
87 2
                    CheckboxField::create('SliderDirectionNav', 'Show DirectionNav'),
88 2
                    CheckboxField::create('ThumbnailNav', 'Thumbnail Navigation'),
89 2
                    DisplayLogicWrapper::create(
90 2
                        CheckboxField::create('CarouselControlNav', 'Show Carousel ControlNav'),
91 2
                        CheckboxField::create('CarouselDirectionNav', 'Show Carousel DirectionNav'),
92 2
                        TextField::create('CarouselItemWidth', 'Thumbnail width'),
93 2
                        TextField::create('CarouselItemMargin', 'Thumbnail margin'),
94 2
                        NumericField::create('CarouselThumbnailCt', 'Number of thumbnails')
95 2
                    )->displayIf('ThumbnailNav')->isChecked()->end()
96 2
                )),
97 2
            ));
98 2
        }
99 2
    }
100
101
    /**
102
     * @return DataList
103
     */
104 2
    public function SlideShow()
105
    {
106 2
        $owner = $this->owner;
107
108 1
        if (!($owner instanceof SiteTree)) {
109
            $this->getCustomScript();
110
        }
111
112 1
        return $this->owner->Slides()->filter(array('ShowSlide' => 1))->sort('SortOrder');
113
    }
114
115
    /**
116
     * add requirements to Page_Controller init()
117
     */
118
    public function contentcontrollerInit()
119
    {
120
        // only call custom script if page has Slides and DataExtension
121
        if (Object::has_extension($this->owner->ClassName, 'FlexSlider')) {
122
            if ($this->owner->SlideShow()->exists()) {
123
                $this->getCustomScript();
124
            }
125
        }
126
    }
127
128
    /**
129
     *
130
     */
131
    public function getCustomScript()
132
    {
133
        // Flexslider options
134
        $animate = ($this->owner->Animate) ? 'true' : 'false';
0 ignored issues
show
Unused Code introduced by
$animate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
        $loop = ($this->owner->Loop) ? 'true' : 'false';
0 ignored issues
show
Unused Code introduced by
$loop is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
        $sync = ($this->owner->ThumbnailNav == true) ? "sync: '.carousel:eq('+index+')'," : '';
137
        $before = (method_exists($this->owner->ClassName, 'flexSliderBeforeAction'))
138
            ? $this->owner->flexSliderBeforeAction()
139
            : 'function(){}';
140
        $after = (method_exists($this->owner->ClassName, 'flexSliderAfterAction'))
141
            ? $this->owner->flexSliderAfterAction()
142
            : 'function(){}';
143
        $speed = (method_exists($this->owner->ClassName, 'setFlexSliderSpeed'))
144
            ? $this->owner->setFlexSliderSpeed()
145
            : 7000;
146
147
        Requirements::customScript("
148
            (function($) {
149
                $(document).ready(function(){
150
                    jQuery('.flexslider').each(function(index){
151
					 
152
                         if(jQuery('.carousel').eq(index).length) {
153
                             jQuery('.carousel').eq(index).flexslider({
154
                                animation: '" . $this->owner->Animation . "',
155
                                controlNav: " . $this->owner->obj('CarouselDirectionNav')->NiceAsBoolean() . ", 
156
                                directionNav: " . $this->owner->obj('CarouselDirectionNav')->NiceAsBoolean() . ",
157
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
158
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
159
                                itemWidth: " . $this->owner->obj('CarouselItemWidth') . ",
160
                                pausePlay: false,
161
                                asNavFor: '.flexslider:eq('+index+')',
162
                                itemMargin: " . $this->owner->obj('CarouselItemMargin') . ",
163
                                minItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
164
                                maxItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
165
                                move: " . $this->owner->obj('CarouselThumbnailCt') . "
166
                              });
167
                         }
168
 
169
                        if(jQuery('.flexslider').eq(index).length){
170
                            jQuery('.flexslider').eq(index).flexslider({
171
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
172
                                animation: '" . $this->owner->Animation . "',
173
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
174
                                controlNav: " . $this->owner->obj('SliderControlNav')->NiceAsBoolean() . ",
175
                                directionNav: " . $this->owner->obj('SliderDirectionNav')->NiceAsBoolean() . ",
176
                                prevText: '',
177
                                nextText: '',
178
                                pauseOnAction: true,
179
                                pauseOnHover: true,
180
                                " . $sync . "
181
                                start: function(slider){
182
                                  $('body').removeClass('loading');
183
                                },
184
                                before: " . $before . ',
185
                                after: ' . $after . ',
186
                                slideshowSpeed: ' . $speed . ' 
187
                            });
188
                        }
189
                    })
190
                });
191
            }(jQuery));'
192
        );
193
    }
194
195
    /**
196
     *
197
     */
198 3
    public function onBeforeWrite()
199
    {
200 3
        parent::onBeforeWrite();
201
202 3
        if (!$this->owner->CarouselItemWidth) {
203
            $this->owner->CarouselItemWidth = 196;
204
        }
205
206 3
        if (!$this->owner->CarouselItemMargin) {
207
            $this->owner->CarouselItemMargin = 10;
208
        }
209
210 3
        if (!$this->owner->CarouselThumbnailCt) {
211
            $this->owner->CarouselThumbnailCt = 4;
212
        }
213
214 3
    }
215
}
216