Mercurial > hg > rr-repo
comparison sites/all/modules/flexslider/flexslider.module @ 2:b74b41bb73f0
-- Google analytics module
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 22 Aug 2013 17:22:54 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:67ce89da90df | 2:b74b41bb73f0 |
---|---|
1 <?php | |
2 /** | |
3 * @file | |
4 * A light-weight, customizable image gallery plugin for Drupal based on jQuery | |
5 */ | |
6 | |
7 define("FLEXSLIDER_VERSION", variable_get('flexslider_version', '2.0')); | |
8 define("FLEXSLIDER_DEBUG", variable_get('flexslider_debug', FALSE)); | |
9 | |
10 /** | |
11 * Implements hook_libraries_info(). | |
12 */ | |
13 function flexslider_libraries_info() { | |
14 $libraries['flexslider'] = array( | |
15 'name' => 'FlexSlider', | |
16 'vendor url' => 'http://www.woothemes.com/flexslider/', | |
17 'download url' => 'https://github.com/woothemes/FlexSlider', | |
18 'version arguments' => array( | |
19 'file' => 'jquery.flexslider-min.js', | |
20 // jQuery FlexSlider v2.1 | |
21 'pattern' => '/jQuery FlexSlider v(\d+\.+\d+)/', | |
22 'lines' => 2, | |
23 ), | |
24 'files' => array( | |
25 'js' => array( | |
26 'jquery.flexslider-min.js', | |
27 ), | |
28 'css' => array( | |
29 'flexslider.css', | |
30 ), | |
31 ), | |
32 'integration files' => array( | |
33 'flexslider' => array( | |
34 'css' => array('assets/css/flexslider_img.css'), | |
35 ), | |
36 ), | |
37 ); | |
38 | |
39 return $libraries; | |
40 } | |
41 | |
42 /** | |
43 * Implements hook_libraries_info_alter(). | |
44 */ | |
45 function flexslider_libraries_info_alter(&$libraries) { | |
46 $debug = variable_get('flexslider_debug', FALSE); | |
47 if ($debug) { | |
48 // Switch to the unminified version of the library | |
49 if (isset($libraries['flexslider'])) { | |
50 $libraries['flexslider']['files']['js'] = array( | |
51 'jquery.flexslider.js', | |
52 ); | |
53 } | |
54 } | |
55 | |
56 // Add support for jQuery Easing module | |
57 if (module_exists('jqeasing')) { | |
58 $libraries['flexslider']['dependencies'][] = 'easing (>=1.3)'; | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * Implements hook_library(). | |
64 * | |
65 * We also define FlexSlider through the core library callbacks | |
66 */ | |
67 function flexslider_library() { | |
68 $module_path = drupal_get_path('module', 'flexslider'); | |
69 $library_path = libraries_get_path('flexslider'); | |
70 | |
71 $libraries['flexslider'] = array( | |
72 'title' => 'FlexSlider', | |
73 'website' => 'http://flexslider.woothemes.com', | |
74 'version' => FLEXSLIDER_VERSION, | |
75 'js' => array( | |
76 $library_path . '/jquery.flexslider-min.js' => array( | |
77 'scope' => 'footer', | |
78 ), | |
79 ), | |
80 'css' => array( | |
81 $library_path . '/flexslider.css' => array( | |
82 'type' => 'file', | |
83 'media' => 'screen', | |
84 ), | |
85 $module_path . '/assets/css/flexslider_img.css' => array( | |
86 'type' => 'file', | |
87 'media' => 'screen', | |
88 ), | |
89 ), | |
90 ); | |
91 return $libraries; | |
92 } | |
93 | |
94 /** | |
95 * Implements hook_library_alter(). | |
96 */ | |
97 function flexslider_library_alter(&$libraries, $module) { | |
98 // Enable debug mode | |
99 if (FLEXSLIDER_DEBUG) { | |
100 if ($module == 'flexslider' and isset($libraries['flexslider'])) { | |
101 $libraries['flexslider']['js'] = array( | |
102 libraries_get_path() . '/jquery.flexslider.js' => array( | |
103 'scope' => 'footer', | |
104 ), | |
105 ); | |
106 } | |
107 } | |
108 } | |
109 | |
110 /** | |
111 * Implements hook_permission(). | |
112 */ | |
113 function flexslider_permission() { | |
114 return array( | |
115 'administer flexslider' => array( | |
116 'title' => t('Administer the FlexSlider module'), | |
117 ), | |
118 ); | |
119 } | |
120 | |
121 /** | |
122 * Implements hook_menu(). | |
123 */ | |
124 function flexslider_menu() { | |
125 $items = array(); | |
126 | |
127 $items['admin/config/media/flexslider/advanced'] = array( | |
128 'title' => 'Advanced settings', | |
129 'description' => 'Configure the advanced flexslider module settings.', | |
130 'page callback' => 'drupal_get_form', | |
131 'page arguments' => array('flexslider_form_settings'), | |
132 'access arguments' => array('administer flexslider'), | |
133 'type' => MENU_LOCAL_TASK, | |
134 'weight' => 2, | |
135 'file' => 'flexslider.admin.inc', | |
136 ); | |
137 | |
138 return $items; | |
139 } | |
140 | |
141 /** | |
142 * Implements hook_help(). | |
143 */ | |
144 function flexslider_help($path, $arg) { | |
145 switch ($path) { | |
146 case 'admin/config/media/flexslider/edit/%': | |
147 return | |
148 '<p>' | |
149 . t('An <em>option set</em> defines exactly how a flexslider image gallery looks like on your site. ' | |
150 . 'It is s a combination of <a href="@styles">image styles</a> for the various image sizes and FlexSlider library options.', array('@styles' => url('admin/config/media/image-styles'))) . '<br />' | |
151 . t('For a full documentation of all options, refer to the official @docs.', array('@docs' => l(t('FlexSlider documentation'), 'http://www.woothemes.com/flexslider/'))) | |
152 . '</p>'; | |
153 } | |
154 } | |
155 | |
156 /** | |
157 * Implements hook_theme(). | |
158 */ | |
159 function flexslider_theme() { | |
160 return array( | |
161 // Container for nav elements (arrows) | |
162 'flexslider' => array( | |
163 'variables' => array('items' => array(), 'settings' => array()), | |
164 'template' => 'theme/flexslider', | |
165 'file' => 'theme/flexslider.theme.inc', | |
166 ), | |
167 'flexslider_list' => array( | |
168 'variables' => array('items' => array(), 'settings' => array()), | |
169 'file' => 'theme/flexslider.theme.inc', | |
170 ), | |
171 'flexslider_list_item' => array( | |
172 'variables' => array('item' => array(), 'settings' => array()), | |
173 'file' => 'theme/flexslider.theme.inc', | |
174 ), | |
175 ); | |
176 } | |
177 | |
178 /** | |
179 * Implements hook_image_default_styles(). | |
180 */ | |
181 function flexslider_image_default_styles() { | |
182 $styles = array(); | |
183 | |
184 // Default image preset for FlexSlider | |
185 $styles['flexslider_full'] = array( | |
186 'effects' => array( | |
187 array( | |
188 'name' => 'image_scale_and_crop', | |
189 'data' => array('width' => 800, 'height' => 500), | |
190 'weight' => 0, | |
191 ), | |
192 ), | |
193 ); | |
194 | |
195 // Default image preset for FlexSlider thumbnails | |
196 $styles['flexslider_thumbnail'] = array( | |
197 'effects' => array( | |
198 array( | |
199 'name' => 'image_scale_and_crop', | |
200 'data' => array('width' => 160, 'height' => 100), | |
201 'weight' => 0, | |
202 ), | |
203 ), | |
204 ); | |
205 | |
206 return $styles; | |
207 } | |
208 | |
209 /** | |
210 * Implements hook_ctools_plugin_api(). | |
211 */ | |
212 function flexslider_ctools_plugin_api($owner, $api) { | |
213 if ($owner == 'flexslider' && $api == 'flexslider_default_preset') { | |
214 return array('version' => 1); | |
215 } | |
216 } | |
217 | |
218 /** | |
219 * Implements hook_ctools_plugin_directory(). | |
220 */ | |
221 function flexslider_ctools_plugin_directory($module, $type) { | |
222 if ($type == 'export_ui') { | |
223 return 'plugins/export_ui'; | |
224 } | |
225 } | |
226 | |
227 /** | |
228 * Create a new optionset object | |
229 * | |
230 * Note that this function does not save the optionset to the database. | |
231 * @see flexslider_optionset_save() | |
232 */ | |
233 function flexslider_optionset_create($values = array()) { | |
234 ctools_include('export'); | |
235 $optionset = ctools_export_crud_new('flexslider_optionset'); | |
236 | |
237 // Set the options to an array | |
238 $optionset->options = array(); | |
239 | |
240 // Assign specified values | |
241 if (isset($values['name'])) { | |
242 $optionset->name = $values['name']; | |
243 } | |
244 if (isset($values['title'])) { | |
245 $optionset->title = $values['title']; | |
246 } | |
247 if (isset($values['options']) and is_array($values['options'])) { | |
248 $optionset->options = $values['options']; | |
249 } | |
250 | |
251 // Merge default settings with any given settings | |
252 $optionset_defaults = _flexslider_optionset_defaults(); | |
253 $optionset->options = $optionset_defaults += $optionset->options; | |
254 | |
255 return $optionset; | |
256 } | |
257 | |
258 /** | |
259 * Fetches all option sets from the database and returns them as an associative array. | |
260 */ | |
261 function flexslider_optionset_load_all() { | |
262 ctools_include('export'); | |
263 $optionsets = ctools_export_crud_load_all('flexslider_optionset'); | |
264 foreach ($optionsets as $optionset) { | |
265 // Ensure the optionset is typecast after being loaded from DB | |
266 _flexslider_typecast_optionset($optionset->options); | |
267 } | |
268 return $optionsets; | |
269 } | |
270 | |
271 /** | |
272 * Fetches the given option set and returns it as an object or NULL, if no set could be found. | |
273 */ | |
274 function flexslider_optionset_load($optionset_name) { | |
275 ctools_include('export'); | |
276 $optionset = ctools_export_crud_load('flexslider_optionset', $optionset_name); | |
277 // Ensure the optionset is typecast after being loaded from DB | |
278 _flexslider_typecast_optionset($optionset->options); | |
279 return $optionset; | |
280 } | |
281 | |
282 /** | |
283 * Checks whether an option set with the given name already exists. | |
284 */ | |
285 function flexslider_optionset_exists($optionset_name) { | |
286 ctools_include('export'); | |
287 $optionset = ctools_export_crud_load('flexslider_optionset', $optionset_name); | |
288 return isset($optionset->name); | |
289 } | |
290 | |
291 /** | |
292 * Saves the given option set to the database. | |
293 * Set the $new flag if this set has not been written before. | |
294 * | |
295 * @return object|boolean | |
296 * Returns the newly saved object, FALSE otherwise. | |
297 */ | |
298 function flexslider_optionset_save($optionset, $new = FALSE) { | |
299 // If the machine name is missing or already in use, return an error. | |
300 if (empty($optionset->name) or (FALSE != flexslider_optionset_exists($optionset->name) and $new)) { | |
301 return FALSE; | |
302 } | |
303 | |
304 // Check for an invalid list of options | |
305 if (isset($optionset->options) and !is_array($optionset->options)) { | |
306 return FALSE; | |
307 } | |
308 | |
309 | |
310 // If the title is missing, default to the name | |
311 if (empty($optionset->title)) { | |
312 $optionset->title = $optionset->name; | |
313 } | |
314 | |
315 // Merge default settings with any given settings | |
316 $optionset_defaults = _flexslider_optionset_defaults(); | |
317 $optionset->options = $optionset_defaults += $optionset->options; | |
318 | |
319 // Prepare the database values. | |
320 $db_values = array( | |
321 'name' => $optionset->name, | |
322 'title' => $optionset->title, | |
323 'options' => _flexslider_typecast_optionset($optionset->options), | |
324 ); | |
325 | |
326 if ($new) { | |
327 $result = drupal_write_record('flexslider_optionset', $db_values); | |
328 } | |
329 else { | |
330 $result = drupal_write_record('flexslider_optionset', $db_values, 'name'); | |
331 } | |
332 | |
333 // Return the object if the values were saved successfully. | |
334 if (($new and SAVED_NEW == $result) or (!$new and SAVED_UPDATED == $result)) { | |
335 return $optionset; | |
336 } | |
337 | |
338 // Otherwise, an error occured | |
339 return FALSE; | |
340 } | |
341 | |
342 /** | |
343 * Deletes the given option set from the database. | |
344 * | |
345 * @param object|string $optionset | |
346 * Optionset object or machine name | |
347 */ | |
348 function flexslider_optionset_delete($optionset) { | |
349 if (isset($optionset->name)) { | |
350 $name = $optionset->name; | |
351 } | |
352 else { | |
353 $name = $optionset; | |
354 } | |
355 db_delete('flexslider_optionset')->condition('name', $name)->execute(); | |
356 } | |
357 | |
358 /* | |
359 * This function loads the required JavaScripts and settings for a flexslider | |
360 * instance. | |
361 * | |
362 * @param string $id [optional] | |
363 * ID Attribute for FlexSlider container | |
364 * @param object|strong $optionset [optional] | |
365 * Option set to load or the machine name of an existing optionset | |
366 */ | |
367 function flexslider_add($id = NULL, $optionset = NULL) { | |
368 // Check optionset value | |
369 if (is_string($optionset)) { | |
370 $name = $optionset; | |
371 $optionset = flexslider_optionset_load($name); | |
372 if (empty($optionset)) { | |
373 watchdog('flexslider', 'Invalid optionset name supplied to flexslider_add: @name', array('@name' => $name), WATCHDOG_WARNING); | |
374 return; | |
375 } | |
376 } | |
377 | |
378 // Static array to remember which scripts are already attached. | |
379 // @todo not currently in use | |
380 $cache = &drupal_static(__FUNCTION__, array()); | |
381 | |
382 // @todo investigate the best way to cache data loaded from libraries_load() | |
383 libraries_load('flexslider'); | |
384 | |
385 // If the ID or optionset aren't set, it is assumed the settings will be set | |
386 // manually via the calling module/theme | |
387 if (!empty($id) && !empty($optionset)) { | |
388 // JavaScript settings | |
389 $js_settings = array( | |
390 'optionsets' => array( | |
391 $optionset->name => $optionset->options, | |
392 ), | |
393 'instances' => array( | |
394 $id => $optionset->name, | |
395 ), | |
396 ); | |
397 // @todo add alter hook for optionset | |
398 drupal_add_js(array('flexslider' => $js_settings), 'setting'); | |
399 } | |
400 // Loader JavaScript | |
401 drupal_add_js(drupal_get_path('module', 'flexslider') . '/assets/js/flexslider.load.js', array('type' => 'file', 'scope' => 'footer')); | |
402 } | |
403 | |
404 /** | |
405 * Default settings for a newly created option set | |
406 * | |
407 * @param string $key [optional] | |
408 * Get specific default value | |
409 * | |
410 * @see https://github.com/woothemes/FlexSlider/blob/master/README.mdown | |
411 * @see https://github.com/woothemes/FlexSlider/wiki/FlexSlider-Properties | |
412 */ | |
413 function _flexslider_optionset_defaults($key = NULL) { | |
414 | |
415 // We add typecasts to ensure the variables get json encoded properly | |
416 | |
417 $defaults = array( | |
418 // v2.x options | |
419 'namespace' => 'flex-', | |
420 'selector' => '.slides > li', | |
421 'easing' => 'swing', | |
422 'direction' => 'horizontal', | |
423 'reverse' => FALSE, // @todo verify data value | |
424 'smoothHeight' => FALSE, // @todo verify data value | |
425 'startAt' => 0, | |
426 'animationSpeed' => 600, | |
427 'initDelay' => 0, | |
428 'useCSS' => TRUE, | |
429 'touch' => TRUE, | |
430 'video' => FALSE, | |
431 'keyboard' => TRUE, | |
432 'multipleKeyboard' => FALSE, | |
433 'mousewheel' => FALSE, // requires https://github.com/brandonaaron/jquery-mousewheel @todo add to make file | |
434 'controlsContainer' => '.flex-control-nav-container', | |
435 'sync' => '', | |
436 'asNavFor' => '', | |
437 'itemWidth' => 0, | |
438 'itemMargin' => 0, | |
439 'minItems' => 0, | |
440 'maxItems' => 0, | |
441 'move' => 0, | |
442 //'start' => '', | |
443 //'before' => '', | |
444 //'after' => '', | |
445 //'end' => '', | |
446 //'added' => '', | |
447 //'removed' => '', | |
448 | |
449 // @todo verify the 1.x options are still valid | |
450 // v1.x options | |
451 'animation' => 'fade', | |
452 //'animationDuration' => 6000, -- replaced by 'animationSpeed' | |
453 //'slidedirection' => 'horizontal', -- replaced by "direction" | |
454 'slideshow' => TRUE, | |
455 'slideshowSpeed' => 7000, | |
456 'directionNav' => TRUE, | |
457 'controlNav' => TRUE, | |
458 //'keyboardnav' => TRUE, -- replaced by 'keyboard' | |
459 //'mousewheel' => FALSE, | |
460 'prevText' => t('Previous'), | |
461 'nextText' => t('Next'), | |
462 'pausePlay' => FALSE, | |
463 'pauseText' => t('Pause'), | |
464 'playText' => t('Play'), | |
465 'randomize' => FALSE, | |
466 //'slidetostart' => 0, // integer value, not boolean -- replace by "startAt" | |
467 'animationLoop' => TRUE, | |
468 'pauseOnAction' => TRUE, | |
469 'pauseOnHover' => FALSE, | |
470 //'controlscontainer' => '.flex-nav-container', -- updated in v2 | |
471 'manualControls' => '', | |
472 //'startCallback' => 'function() {}', -- replace by 'start' | |
473 //'beforeCallback' => 'function() {}', -- replaced by 'before' | |
474 //'afterCallback' => 'function() {}', -- replaced by 'after' | |
475 //'endCallback' => 'function() {}', -- replaced by 'end' | |
476 ); | |
477 | |
478 // Typecast the values | |
479 _flexslider_typecast_optionset($defaults); | |
480 | |
481 // Return the specific item | |
482 if (isset($key) and array_key_exists($key, $defaults)) { | |
483 return $defaults[$key]; | |
484 } | |
485 | |
486 // Return all items | |
487 return $defaults; | |
488 } | |
489 | |
490 /** | |
491 * Adds the typecasting to the values so that the generated | |
492 * json array keeps the right values | |
493 */ | |
494 function _flexslider_typecast_optionset(&$options) { | |
495 if (isset($options) && !empty($options)) { | |
496 foreach ($options as $key => $value) { | |
497 switch ($key) { | |
498 case 'namespace': | |
499 case 'selector': | |
500 case 'easing': | |
501 case 'direction': | |
502 case 'controlsContainer': | |
503 case 'sync': | |
504 case 'asNavFor': | |
505 case 'fade': | |
506 case 'prevText': | |
507 case 'nextText': | |
508 case 'pauseText': | |
509 case 'playText': | |
510 case 'manualControls': | |
511 $options[$key] = (string)$value; | |
512 break; | |
513 case 'startAt': | |
514 case 'animationSpeed': | |
515 case 'initDelay': | |
516 case 'itemWidth': | |
517 case 'itemMargin': | |
518 case 'minItems': | |
519 case 'maxItems': | |
520 case 'move': | |
521 $options[$key] = (int)$value; | |
522 break; | |
523 case 'controlNav': | |
524 if ($value == 'thumbnails') { | |
525 $options[$key] = (string)$value; | |
526 break; | |
527 } | |
528 case 'reverse': | |
529 case 'smoothHeight': | |
530 case 'useCSS': | |
531 case 'touch': | |
532 case 'video': | |
533 case 'keyboard': | |
534 case 'multipleKeyboard': | |
535 case 'mouseWheel': | |
536 case 'slideshow': | |
537 case 'directionNav': | |
538 case 'pausePlay': | |
539 case 'randomize': | |
540 case 'animationLoop': | |
541 case 'pauseOnAction': | |
542 case 'pauseOnHover': | |
543 $options[$key] = (boolean)$value; | |
544 break; | |
545 } | |
546 } | |
547 } | |
548 } | |
549 | |
550 /** | |
551 * List of all easing methods available from jQuery Easing v1.3 | |
552 */ | |
553 function _flexslider_jqeasing_options() { | |
554 return array( | |
555 "jswing" => "jswing", | |
556 "def" => "def", | |
557 "easeInQuad" => "easeInQuad", | |
558 "easeOutQuad" => "easeOutQuad", | |
559 "easeInOutQuad" => "easeInOutQuad", | |
560 "easeInCubic" => "easeInCubic", | |
561 "easeOutCubic" => "easeOutCubic", | |
562 "easeInOutCubic" => "easeInOutCubic", | |
563 "easeInQuart" => "easeInQuart", | |
564 "easeOutQuart" => "easeOutQuart", | |
565 "easeInOutQuart" => "easeInOutQuart", | |
566 "easeInQuint" => "easeInQuint", | |
567 "easeOutQuint" => "easeOutQuint", | |
568 "easeInOutQuint" => "easeInOutQuint", | |
569 "easeInSine" => "easeInSine", | |
570 "easeOutSine" => "easeOutSine", | |
571 "easeInOutSine" => "easeInOutSine", | |
572 "easeInExpo" => "easeInExpo", | |
573 "easeOutExpo" => "easeOutExpo", | |
574 "easeInOutExpo" => "easeInOutExpo", | |
575 "easeInCirc" => "easeInCirc", | |
576 "easeOutCirc" => "easeOutCirc", | |
577 "easeInOutCirc" => "easeInOutCirc", | |
578 "easeInElastic" => "easeInElastic", | |
579 "easeOutElastic" => "easeOutElastic", | |
580 "easeInOutElastic" => "easeInOutElastic", | |
581 "easeInBack" => "easeInBack", | |
582 "easeOutBack" => "easeOutBack", | |
583 "easeInOutBack" => "easeInOutBack", | |
584 "easeInBounce" => "easeInBounce", | |
585 "easeOutBounce" => "easeOutBounce", | |
586 "easeInOutBounce" => "easeInOutBounce", | |
587 ); | |
588 } |