/
FlexSlider.php
executable file
·303 lines (257 loc) · 13 KB
/
FlexSlider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* A slider for Silverstripe 3 (uses FlexSlider2: https://github.com/woothemes/FlexSlider)
*
* @package silverstripe-flexslider
*/
class FlexSlider extends DataObject {
public static $singular_name = 'Slider';
public static $plural_name = 'Sliders';
public $imageWidth = 1280; // if no imageWidth is set, images will by default be resized to 1280x800, which should be big enough for all websites
public $imageHeight = 800;
public $FieldMapping = null;
public $cssWidth = false; // when a FlexSlider is added by a Shortcode, we want to add style="width: 1280px"
/**
* @var $extraClasses array Extra CSS-classes for the flexslider
*/
protected $extraClasses;
public static $db = array('Title' => 'Varchar',
'animation' => 'Enum("slide,fade","slide")',
'easing' => 'Enum("linear,swing,easeInQuad,easeOutQuad,easeOutExpo","linear")',
'direction' => 'Enum("horizontal,vertical","horizontal")',
'reverse' => 'Boolean',
'dynamicLoading' => 'Boolean',
'animationLoop' => 'Boolean',
'slideshow' => 'Boolean',
'slideshowSpeed' => 'Int(7000)',
'animationSpeed' => 'Int(600)',
'randomize' => 'Boolean',
'showControlNav' => 'Boolean'
);
public static $has_many = array('FlexSlides' => 'FlexSlide');
// defaults
public function populateDefaults() {
$this->animation = "slide";
$this->easing = "linear";
$this->direction = "horizontal";
$this->reverse = 0;
$this->animationLoop = 0;
$this->slideshow = 0;
$this->slideshowSpeed = 7000;
$this->animationSpeed = 600;
$this->randomize = 0;
$this->showControlNav = 1;
parent::populateDefaults();
}
// column names of grid (translated to the current admin language)
static $field_labels = array('NoSlides' => 'Number of Slides');
// columns in Grid
static $summary_fields = array(
'ID',
'Title',
//'NoSlides'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName("Title");
$fields->removeByName("animation");
$fields->removeByName("easing");
$fields->removeByName("direction");
$fields->removeByName("reverse");
$fields->removeByName("dynamicLoading");
$fields->removeByName("animationLoop");
$fields->removeByName("slideshow");
$fields->removeByName("slideshowSpeed");
$fields->removeByName("animationSpeed");
$fields->removeByName("randomize");
$fields->removeByName("showControlNav");
$animationTypes = $this->dbObject("animation")->enumValues();
$easingTypes = $this->dbObject("easing")->enumValues();
$directions = $this->dbObject("direction")->enumValues();
// Main
$field_Title = new TextField("Title", _t("FlexSlider.Title"));
$field_Title->setRightTitle(_t("FlexSlider.TitleDescription"));
$field_slideshow = new CheckboxField("slideshow", _t("FlexSlider.slideshow"));
$field_animationLoop = new CheckboxField("animationLoop", _t("FlexSlider.animationLoop"));
$field_animation = new Dropdownfield("animation", _t("FlexSlider.animationType"), $animationTypes);
$field_direction = new Dropdownfield("direction", _t("FlexSlider.direction"), $directions);
$field_easing = new Dropdownfield("easing", _t("FlexSlider.easingType"), $easingTypes);
$field_easing->setRightTitle(_t("FlexSlider.easingTypeDescription"));
$field_slideshowSpeed = new NumericField("slideshowSpeed", _t("FlexSlider.slideshowSpeed"));
$field_slideshowSpeed->setRightTitle(_t("FlexSlider.slideshowSpeedDescription"));
$field_animationSpeed = new NumericField("animationSpeed", _t("FlexSlider.animationSpeed"));
$field_animationSpeed->setRightTitle(_t("FlexSlider.animationSpeedDescription"));
$field_randomize = new CheckboxField("randomize", _t("FlexSlider.randomize"));
$field_showControlNav = new CheckboxField("showControlNav", _t("FlexSlider.showControlNav"));
$FieldsArray = array(
$field_Title,
$field_slideshow,
$field_animationLoop,
$field_animation,
$field_direction,
$field_easing,
$field_slideshowSpeed,
$field_animationSpeed,
$field_randomize,
$field_showControlNav
);
$fields->addFieldsToTab('Root.Main', $FieldsArray);
return $fields;
}
public function NoSlides() {
return $this->FlexSlides()->Count();
}
public function setDatalist($datalist) {
if (!$datalist) user_error("you need to pass a datalist to setDatalist()", E_USER_NOTICE);
$this->DataList = $datalist;
}
/**
* Add a CSS-class to the fieldset
*
* @param $class String
*/
public function addExtraClass($class) {
$this->extraClasses[$class] = $class;
return $this;
}
/**
* Compiles all CSS-classes. Optionally includes a "nolabel"-class
* if no title was set on the formfield.
* Uses {@link Message()} and {@link MessageType()} to add validatoin
* error classes which can be used to style the contained tags.
*
* @return string CSS-classnames
*/
public function extraClass() {
$classnames = "";
if ($this->extraClasses) {
foreach ($this->extraClasses AS $classname) {
$classnames.= " flexslider_".$classname;
}
}
return $classnames;
}
/**
* adds style="width: 123" to the FlexSlider
*
* @param mixed $width (either boolean yes/no or int)
*/
public function setCSSWidth($width) {
if (!$width) $this->cssWidth = false;
elseif ($width > 1) $this->cssWidth = $width;
elseif ($width==true) $this->cssWidth = $this->imageWidth;
}
/**
* @param array $fieldmapping (e.g. array("Title" => "Fullname", "Description" => "Text"))
*/
public function setFieldMapping ($fieldmapping) {
if (!isset($fieldmapping["Picture"])) user_error("you need specify at least 'Picture'", E_USER_NOTICE);
$this->FieldMapping = $fieldmapping;
}
/**
* @param array $options
*/
public function setOptions($options) {
foreach ($options AS $option => $value) {
$this->$option = $value;
}
}
public function setImageWidth($width) {
if ((int)$width > 0) $this->imageWidth = $width;
}
public function setImageHeight($height) {
if ((int)$height > 0) $this->imageHeight = $height;
}
public function forTemplate() {
// flex slider configuration
$flexslider_config = array("sliderID" => $this->ID,
"settings_animationType" => $this->animation,
"settings_easing" => $this->easing,
"settings_dynamicLoading" => $this->dynamicLoading,
"settings_direction" => $this->direction,
"settings_reverse" => $this->reverse,
"settings_animationLoop" => $this->animationLoop,
"settings_doSlideshow" => $this->slideshow,
"settings_slideshowSpeed" => $this->slideshowSpeed,
"settings_animationSpeed" => $this->animationSpeed,
"settings_randomize" => $this->randomize,
"settings_showControlNav" => $this->showControlNav
);
// load all needed Javascript
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('flexslider/javascript/jquery.easing.1.3.js');
Requirements::javascript('flexslider/thirdparty/FlexSlider/jquery.flexslider.js');
Requirements::javascriptTemplate('flexslider/javascript/flexslider.template.js', $flexslider_config);
// load css
Requirements::css('flexslider/thirdparty/FlexSlider/flexslider.css'); // original css of flexslider
Requirements::css('flexslider/css/flexslider.css'); // additional css for ss flexslider module
Requirements::css($this->ThemeDir().'/css/flexslider.css'); // look for a custom flexslider.css in the projects themes folder
return $this->renderWith('FlexSlider');
}
public function getSlides() {
// getting the slides of a flexslider that was created in the cms
if ($this->FlexSlides()->exists()) {
$FlexSlides = $this->FlexSlides()->filter("isEnabled",1);
if (!$FlexSlides->exists()) return false;
}
// getting the slides of a flexslider that was instanced with "$Slider = new FlexSlider(); $Slider->setDatalist($datalist);"
elseif ($this->DataList) {
$FlexSlides = $this->createSlidesFromDataList();
}
return $FlexSlides;
}
/**
* If the slided Objects are created with $productSlider->setDatalist(Product::get()),
* wen need to build an ArrayList of them
*/
public function createSlidesFromDataList() {
$slide_title = false;
$slide_description = false;
$slide_internallink = false;
$slide_externallink = false;
$slide_picture = $this->FieldMapping["Picture"];
if (isset($this->FieldMapping["Title"])) $slide_title = $this->FieldMapping["Title"];
if (isset($this->FieldMapping["Description"])) $slide_description = $this->FieldMapping["Description"];
if (isset($this->FieldMapping["InternalLink"])) $slide_internallink = $this->FieldMapping["InternalLink"];
if (isset($this->FieldMapping["ExternalLink"])) $slide_externallink = $this->FieldMapping["ExternalLink"];
$FlexSlides = new ArrayList();
foreach ($this->DataList AS $Obj) {
$FlexSlide = new FlexSlide();
// Picture
if ($Obj->hasMethod($slide_picture)) $FlexSlide->PictureID = $Obj->$slide_picture()->ID;
// SlideTitle
if ($slide_title) {
if (isset($Obj->$slide_title)) $FlexSlide->SlideTitle = $Obj->$slide_title;
elseif ($Obj->hasMethod($slide_title)) $FlexSlide->SlideTitle = $Obj->$slide_title();
}
// SlideDescription
if ($slide_description) {
if (isset($Obj->$slide_description)) $FlexSlide->SlideDescription = $Obj->$slide_description;
elseif ($Obj->hasMethod($slide_description)) $FlexSlide->SlideDescription = $Obj->$slide_description();
}
// InternalLink
if ($slide_internallink) {
if ($Obj->hasMethod($slide_internallink)) $FlexSlide->InternalLinkID = $Obj->$slide_internallink()->ID;
}
// ExternalLink
if ($slide_externallink) {
if (isset($Obj->$slide_externallink)) $FlexSlide->ExternalLink = $Obj->$slide_externallink;
elseif ($Obj->hasMethod($slide_externallink)) $FlexSlide->ExternalLink = $Obj->$slide_externallink();
}
$FlexSlides->push($FlexSlide);
}
return $FlexSlides;
}
/**
* @example [FlexSlider id="2" width="400" height="300"]
*/
public static function FlexSliderShortCodeHandler($arguments) {
if (!isset($arguments['id'])) return;
$imageWidth = (isset($arguments['width'])) && ((int)$arguments['width']>0) ? (int)$arguments['width'] : null;
$imageHeight = (isset($arguments['height'])) && ((int)$arguments['height']>0) ? (int)$arguments['height'] : null;
$newPage = new Page();
$fs = $newPage->FlexSlider($arguments['id'], $arguments['width'], $arguments['height']);
$fs->setCSSWidth(true);
return $fs->forTemplate();
}
}