Completed
Pull Request — 2.0 (#122)
by Matthew
03:42
created

FlexSlider::populateDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
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
        'CarouselThumbnailCt' => 'Int',
18
    );
19
20
    /**
21
     * @var array
22
     */
23
    public static $has_many = array(
24
        'Slides' => 'SlideImage',
25
    );
26
27
    /**
28
     *
29
     */
30 4
    public function populateDefaults()
31
    {
32 4
        parent::populateDefaults();
33 4
        $this->owner->Loop = 1;
34 4
        $this->owner->Animate = 1;
35 4
        $this->owner->SliderControlNav = 1;
36 4
        $this->owner->SliderDirectionNav = 1;
37 4
        $this->owner->CarouselControlNav = 0;
38 4
        $this->owner->CarouselDirectionNav = 1;
39 4
        $this->owner->CarouselThumbnailCt = 6;
40 4
    }
41
42
    /**
43
     * @param FieldList $fields
44
     */
45 2
    public function updateCMSFields(FieldList $fields)
46
    {
47 2
        $fields->removeByName(array(
48 2
            'Animation',
49 2
            'Loop',
50 2
            'Animate',
51 2
            'ThumbnailNav',
52 2
            'SliderControlNav',
53 2
            'SliderDirectionNav',
54 2
            'CarouselControlNav',
55 2
            'CarouselDirectionNav',
56
            'CarouselThumbnailCt'
57 2
        ));
58
59
        // Slides
60 2
        if ($this->owner->ID) {
61 2
            $config = GridFieldConfig_RecordEditor::create();
62 2
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
63 2
            if (class_exists('GridFieldBulkUpload')) {
64
                $config->addComponent(new GridFieldBulkUpload());
65
                $config->addComponent(new GridFieldBulkManager());
66 2
            }
67 2
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
68 2
            $config->removeComponentsByType('GridFieldDeleteAction');
69 2
            $config->addComponent(new GridFieldDeleteAction(false));
70 2
            $SlidesField = GridField::create('Slides', 'Slides', $this->owner->Slides()->sort('SortOrder'), $config);
71
72 2
            $slideTitle = $this->owner->stat('slide_tab_title') ? $this->owner->stat('slide_tab_title') : 'Slides';
73
74 2
            $fields->addFieldsToTab("Root.{$slideTitle}", array(
75 2
                $SlidesField,
76 2
                ToggleCompositeField::create('ConfigHD', 'Slider Settings', array(
77 2
                    DropdownField::create('Animation', 'Animation option', $this->owner->dbObject('Animation')->enumValues()),
78 2
                    CheckboxField::create('Animate', 'Animate automatically'),
79 2
                    CheckboxField::create('Loop', 'Loop the carousel'),
80 2
                    CheckboxField::create('SliderControlNav', 'Show ControlNav'),
81 2
                    CheckboxField::create('SliderDirectionNav', 'Show DirectionNav'),
82 2
                    CheckboxField::create('ThumbnailNav', 'Thumbnail Navigation'),
83 2
                    DisplayLogicWrapper::create(
84 2
                        CheckboxField::create('CarouselControlNav', 'Show Carousel ControlNav'),
85 2
                        CheckboxField::create('CarouselDirectionNav', 'Show Carousel DirectionNav'),
86 2
                        NumericField::create('CarouselThumbnailCt', 'Number of thumbnails')
87 2
                    )->displayIf('ThumbnailNav')->isChecked()->end()
88 2
                )),
89 2
            ));
90 2
        }
91 2
    }
92
93
    /**
94
     * @return DataList
95
     */
96 2
    public function SlideShow()
97 2
    {
98 1
        $owner = $this->owner;
99
100 1
        if (!($owner instanceof SiteTree)) {
101
            $this->getCustomScript();
102
        }
103
104 1
        return $this->owner->Slides()->sort('SortOrder');
105
    }
106
107
    /**
108
     * add requirements to Page_Controller init()
109
     */
110
    public function contentcontrollerInit()
111
    {
112
        // only call custom script if page has Slides and DataExtension
113
        if (Object::has_extension($this->owner->ClassName, 'FlexSlider')) {
114
            if ($this->owner->SlideShow()->exists()) {
115
                $this->getCustomScript();
116
            }
117
        }
118
    }
119
120
    /**
121
     *
122
     */
123
    public function getCustomScript()
124
    {
125
        // Flexslider options
126
        $sync = ($this->owner->ThumbnailNav == true) ? "sync: '.carousel:eq('+index+')'," : '';
127
128
        $before = (method_exists($this->owner->ClassName, 'flexSliderBeforeAction'))
129
            ? $this->owner->flexSliderBeforeAction()
130
            : 'function(){}';
131
132
        $after = (method_exists($this->owner->ClassName, 'flexSliderAfterAction'))
133
            ? $this->owner->flexSliderAfterAction()
134
            : 'function(){}';
135
136
        $speed = (method_exists($this->owner->ClassName, 'setFlexSliderSpeed'))
137
            ? $this->owner->setFlexSliderSpeed()
138
            : 7000;
139
140
        Requirements::customScript("
141
            (function($) {
142
                $(document).ready(function(){
143
                    jQuery('.flexslider').each(function(index){
144
					 
145
                         if(jQuery('.carousel').eq(index).length) {
146
                             jQuery('.carousel').eq(index).flexslider({
147
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
148
                                animation: '" . $this->owner->Animation . "',
149
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
150
                                controlNav: " . $this->owner->obj('CarouselControlNav')->NiceAsBoolean() . ", 
151
                                directionNav: " . $this->owner->obj('CarouselDirectionNav')->NiceAsBoolean() . ",
152
                                prevText: '',
153
                                nextText: '',
154
                                pausePlay: false,
155
                                asNavFor: '.flexslider:eq('+index+')',
156
                                minItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
157
                                maxItems: " . $this->owner->obj('CarouselThumbnailCt') . ",
158
                                move: " . $this->owner->obj('CarouselThumbnailCt') . ",
159
                                itemWidth: 100,
160
                                itemMargin: 10
161
                              });
162
                         }
163
 
164
                        if(jQuery('.flexslider').eq(index).length){
165
                            jQuery('.flexslider').eq(index).flexslider({
166
                                slideshow: " . $this->owner->obj('Animate')->NiceAsBoolean() . ",
167
                                animation: '" . $this->owner->Animation . "',
168
                                animationLoop: " . $this->owner->obj('Loop')->NiceAsBoolean() . ",
169
                                controlNav: " . $this->owner->obj('SliderControlNav')->NiceAsBoolean() . ",
170
                                directionNav: " . $this->owner->obj('SliderDirectionNav')->NiceAsBoolean() . ",
171
                                prevText: '',
172
                                nextText: '',
173
                                pauseOnAction: true,
174
                                pauseOnHover: true,
175
                                " . $sync . "
176
                                start: function(slider){
177
                                  $('body').removeClass('loading');
178
                                },
179
                                before: " . $before . ',
180
                                after: ' . $after . ',
181
                                slideshowSpeed: ' . $speed . ' 
182
                            });
183
                        }
184
                    })
185
                });
186
            }(jQuery));', 'jqueryFlexSlider'
187
        );
188
    }
189
190
    /**
191
     *
192
     */
193 3
    public function onBeforeWrite()
194
    {
195 3
        parent::onBeforeWrite();
196
197 3
        if (!$this->owner->CarouselThumbnailCt) {
198
            $this->owner->CarouselThumbnailCt = 6;
199
        }
200
201 3
    }
202
}
203