Projet

Général

Profil

Paste
Télécharger (16,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flexslider / flexslider.module @ d756b39a

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
    // Flexslider container
162
    'flexslider' => array(
163
      'variables' => array('items' => array(), 'settings' => array()),
164
      'template' => 'theme/flexslider',
165
      'file' => 'theme/flexslider.theme.inc',
166
    ),
167
    
168
    // Slide container
169
    'flexslider_list' => array(
170
      'variables' => array('items' => array(), 'settings' => array()),
171
      'file' => 'theme/flexslider.theme.inc',
172
    ),
173
    
174
    // Slide item
175
    'flexslider_list_item' => array(
176
      'variables' => array('item' => array(), 'settings' => array(), 'caption' => ''),
177
      'file' => 'theme/flexslider.theme.inc',
178
    ),
179
  );
180
}
181

    
182
/**
183
 * Implements hook_image_default_styles().
184
 */
185
function flexslider_image_default_styles() {
186
  $styles = array();
187

    
188
  // Default image preset for FlexSlider
189
  $styles['flexslider_full'] = array(
190
    'effects' => array(
191
      array(
192
        'name' => 'image_scale_and_crop',
193
        'data' => array('width' => 800, 'height' => 500),
194
        'weight' => 0,
195
      ),
196
    ),
197
  );
198

    
199
  // Default image preset for FlexSlider thumbnails
200
  $styles['flexslider_thumbnail'] = array(
201
    'effects' => array(
202
      array(
203
        'name' => 'image_scale_and_crop',
204
        'data' => array('width' => 160, 'height' => 100),
205
        'weight' => 0,
206
      ),
207
    ),
208
  );
209

    
210
  return $styles;
211
}
212

    
213
/**
214
 * Implements hook_ctools_plugin_api().
215
 */
216
function flexslider_ctools_plugin_api($owner, $api) {
217
  if ($owner == 'flexslider' && $api == 'flexslider_default_preset') {
218
    return array('version' => 1);
219
  }
220
}
221

    
222
/**
223
 * Implements hook_ctools_plugin_directory().
224
 */
225
function flexslider_ctools_plugin_directory($module, $type) {
226
  if ($type == 'export_ui') {
227
    return 'plugins/export_ui';
228
  }
229
}
230

    
231
/**
232
 * Create a new optionset object
233
 *
234
 * Note that this function does not save the optionset to the database.
235
 * @see flexslider_optionset_save()
236
 */
237
function flexslider_optionset_create($values = array()) {
238
  ctools_include('export');
239
  $optionset = ctools_export_crud_new('flexslider_optionset');
240

    
241
  // Set the options to an array
242
  $optionset->options = array();
243

    
244
  // Assign specified values
245
  if (isset($values['name'])) {
246
    $optionset->name = $values['name'];
247
  }
248
  if (isset($values['title'])) {
249
    $optionset->title = $values['title'];
250
  }
251
  if (isset($values['options']) and is_array($values['options'])) {
252
    $optionset->options = $values['options'];
253
  }
254

    
255
  // Merge default settings with any given settings
256
  $optionset_defaults = _flexslider_optionset_defaults();
257
  $optionset->options = $optionset_defaults += $optionset->options;
258

    
259
  return $optionset;
260
}
261

    
262
/**
263
 * Fetches all option sets from the database and returns them as an associative array.
264
 */
265
function flexslider_optionset_load_all() {
266
  ctools_include('export');
267
  $optionsets = ctools_export_crud_load_all('flexslider_optionset');
268
  foreach ($optionsets as $optionset) {
269
    // Ensure the optionset is typecast after being loaded from DB
270
    _flexslider_typecast_optionset($optionset->options);
271
  }
272
  return $optionsets;
273
}
274

    
275
/**
276
 * Fetches the given option set and returns it as an object or NULL, if no set could be found.
277
 */
278
function flexslider_optionset_load($optionset_name) {
279
  ctools_include('export');
280
  $optionset = ctools_export_crud_load('flexslider_optionset', $optionset_name);
281
    // Ensure the optionset is typecast after being loaded from DB
282
  _flexslider_typecast_optionset($optionset->options);
283
  return $optionset;
284
}
285

    
286
/**
287
 * Checks whether an option set with the given name already exists.
288
 */
289
function flexslider_optionset_exists($optionset_name) {
290
  ctools_include('export');
291
  $optionset = ctools_export_crud_load('flexslider_optionset', $optionset_name);
292
  return isset($optionset->name);
293
}
294

    
295
/**
296
 * Saves the given option set to the database.
297
 * Set the $new flag if this set has not been written before.
298
 *
299
 * @return object|boolean
300
 *  Returns the newly saved object, FALSE otherwise.
301
 */
302
function flexslider_optionset_save($optionset, $new = FALSE) {
303
  // If the machine name is missing or already in use, return an error.
304
  if (empty($optionset->name) or (FALSE != flexslider_optionset_exists($optionset->name) and $new)) {
305
    return FALSE;
306
  }
307

    
308
  // Check for an invalid list of options
309
  if (isset($optionset->options) and !is_array($optionset->options)) {
310
    return FALSE;
311
  }
312

    
313

    
314
  // If the title is missing, default to the name
315
  if (empty($optionset->title)) {
316
    $optionset->title = $optionset->name;
317
  }
318

    
319
  // Merge default settings with any given settings
320
  $optionset_defaults = _flexslider_optionset_defaults();
321
  $optionset->options = $optionset_defaults += $optionset->options;
322

    
323
  // Prepare the database values.
324
  $db_values = array(
325
    'name' => $optionset->name,
326
    'title' => $optionset->title,
327
    'options' => _flexslider_typecast_optionset($optionset->options),
328
  );
329

    
330
  if ($new) {
331
    $result = drupal_write_record('flexslider_optionset', $db_values);
332
  }
333
  else {
334
    $result = drupal_write_record('flexslider_optionset', $db_values, 'name');
335
  }
336

    
337
  // Return the object if the values were saved successfully.
338
  if (($new and SAVED_NEW == $result) or (!$new and SAVED_UPDATED == $result)) {
339
    return $optionset;
340
  }
341

    
342
  // Otherwise, an error occured
343
  return FALSE;
344
}
345

    
346
/**
347
 * Deletes the given option set from the database.
348
 *
349
 * @param object|string $optionset
350
 *  Optionset object or machine name
351
 */
352
function flexslider_optionset_delete($optionset) {
353
  if (isset($optionset->name)) {
354
    $name = $optionset->name;
355
  }
356
  else {
357
    $name = $optionset;
358
  }
359
  db_delete('flexslider_optionset')->condition('name', $name)->execute();
360
}
361

    
362
/*
363
 * This function loads the required JavaScripts and settings for a flexslider
364
 * instance.
365
 *
366
 * @param string $id [optional]
367
 *  ID Attribute for FlexSlider container
368
 * @param object|strong $optionset [optional]
369
 *  Option set to load or the machine name of an existing optionset
370
 */
371
function flexslider_add($id = NULL, $optionset = NULL) {
372
  // Check optionset value
373
  if (is_string($optionset)) {
374
    $name = $optionset;
375
    $optionset = flexslider_optionset_load($name);
376
    if (empty($optionset)) {
377
      watchdog('flexslider', 'Invalid optionset name supplied to flexslider_add: @name', array('@name' => $name), WATCHDOG_WARNING);
378
      return;
379
    }
380
  }
381

    
382
  // Static array to remember which scripts are already attached.
383
  // @todo not currently in use
384
  $cache = &drupal_static(__FUNCTION__, array());
385

    
386
  // @todo investigate the best way to cache data loaded from libraries_load()
387
  libraries_load('flexslider');
388

    
389
  // If the ID or optionset aren't set, it is assumed the settings will be set
390
  // manually via the calling module/theme
391
  if (!empty($optionset) && !in_array($optionset->name, $cache)) {
392
    $cache[] = $optionset->name;
393
    $js_setting['optionsets'] = array(
394
      $optionset->name => $optionset->options,
395
    );
396
    drupal_add_js(array('flexslider' => $js_setting), 'setting');
397
  }
398
  
399
  if (!empty($id)) {
400
    // JavaScript settings
401
    $js_settings = array(
402
      'instances' => array(
403
        $id => $optionset->name,
404
      ),
405
    );
406
    // @todo add alter hook for optionset
407
    drupal_add_js(array('flexslider' => $js_settings), 'setting');
408
  }
409
  // Loader JavaScript
410
  drupal_add_js(drupal_get_path('module', 'flexslider') . '/assets/js/flexslider.load.js', array('type' => 'file', 'scope' => 'footer'));
411
}
412

    
413
/**
414
 * Default settings for a newly created option set
415
 *
416
 * @param string $key [optional]
417
 *  Get specific default value
418
 *
419
 * @see https://github.com/woothemes/FlexSlider/blob/master/README.mdown
420
 * @see https://github.com/woothemes/FlexSlider/wiki/FlexSlider-Properties
421
 */
422
function _flexslider_optionset_defaults($key = NULL) {
423

    
424
  // We add typecasts to ensure the variables get json encoded properly
425

    
426
  $defaults = array(
427
    // v2.x options
428
    'namespace' => 'flex-',
429
    'selector' => '.slides > li',
430
    'easing' => 'swing',
431
    'direction' => 'horizontal',
432
    'reverse' => FALSE,  // @todo verify data value
433
    'smoothHeight' => FALSE, // @todo verify data value
434
    'startAt' => 0,
435
    'animationSpeed' => 600,
436
    'initDelay' => 0,
437
    'useCSS' => TRUE,
438
    'touch' => TRUE,
439
    'video' => FALSE,
440
    'keyboard' => TRUE,
441
    'multipleKeyboard' => FALSE,
442
    'mousewheel' => FALSE, // requires https://github.com/brandonaaron/jquery-mousewheel @todo add to make file
443
    'controlsContainer' => '.flex-control-nav-container',
444
    'sync' => '',
445
    'asNavFor' => '',
446
    'itemWidth' => 0,
447
    'itemMargin' => 0,
448
    'minItems' => 0,
449
    'maxItems' => 0,
450
    'move' => 0,
451
    //'start' => '',
452
    //'before' => '',
453
    //'after' => '',
454
    //'end' => '',
455
    //'added' => '',
456
    //'removed' => '',
457

    
458
    // @todo verify the 1.x options are still valid
459
    // v1.x options
460
    'animation' => 'fade',
461
    //'animationDuration' => 6000, -- replaced by 'animationSpeed'
462
    //'slidedirection' => 'horizontal', -- replaced by "direction"
463
    'slideshow' => TRUE,
464
    'slideshowSpeed' => 7000,
465
    'directionNav' => TRUE,
466
    'controlNav' => TRUE,
467
    //'keyboardnav' => TRUE, --  replaced by 'keyboard'
468
    //'mousewheel' => FALSE,
469
    'prevText' => t('Previous'),
470
    'nextText' => t('Next'),
471
    'pausePlay' => FALSE,
472
    'pauseText' => t('Pause'),
473
    'playText' => t('Play'),
474
    'randomize' => FALSE,
475
    'thumbCaptions' => FALSE,
476
    'thumbCaptionsBoth' => FALSE,
477
    //'slidetostart' => 0, // integer value, not boolean --  replace by "startAt"
478
    'animationLoop' => TRUE,
479
    'pauseOnAction' => TRUE,
480
    'pauseOnHover' => FALSE,
481
    //'controlscontainer' => '.flex-nav-container', -- updated in v2
482
    'manualControls' => '',
483
    //'startCallback' => 'function() {}', -- replace by 'start'
484
    //'beforeCallback' => 'function() {}', -- replaced by 'before'
485
    //'afterCallback' => 'function() {}', -- replaced by 'after'
486
    //'endCallback' => 'function() {}', -- replaced by 'end'
487
  );
488

    
489
  // Typecast the values
490
  _flexslider_typecast_optionset($defaults);
491

    
492
  // Return the specific item
493
  if (isset($key) and array_key_exists($key, $defaults)) {
494
    return $defaults[$key];
495
  }
496

    
497
  // Return all items
498
  return $defaults;
499
}
500

    
501
/**
502
 * Adds the typecasting to the values so that the generated
503
 * json array keeps the right values
504
 */
505
function _flexslider_typecast_optionset(&$options) {
506
  if (isset($options) && !empty($options)) {
507
    foreach ($options as $key => $value) {
508
      switch ($key) {
509
        case 'namespace':
510
        case 'selector':
511
        case 'easing':
512
        case 'direction':
513
        case 'controlsContainer':
514
        case 'sync':
515
        case 'asNavFor':
516
        case 'fade':
517
        case 'prevText':
518
        case 'nextText':
519
        case 'pauseText':
520
        case 'playText':
521
        case 'manualControls':
522
          $options[$key] = (string)$value;
523
          break;
524
        case 'startAt':
525
        case 'animationSpeed':
526
        case 'initDelay':
527
        case 'itemWidth':
528
        case 'itemMargin':
529
        case 'minItems':
530
        case 'maxItems':
531
        case 'move':
532
          $options[$key] = (int)$value;
533
          break;
534
        case 'controlNav':
535
          if ($value == 'thumbnails') {
536
            $options[$key] = (string)$value;
537
            break;
538
          }
539
        case 'reverse':
540
        case 'smoothHeight':
541
        case 'useCSS':
542
        case 'touch':
543
        case 'video':
544
        case 'keyboard':
545
        case 'multipleKeyboard':
546
        case 'mouseWheel':
547
        case 'slideshow':
548
        case 'directionNav':
549
        case 'pausePlay':
550
        case 'randomize':
551
        case 'thumbCaptions':
552
        case 'thumbCaptionsBoth':
553
        case 'animationLoop':
554
        case 'pauseOnAction':
555
        case 'pauseOnHover':
556
          $options[$key] = (boolean)$value;
557
          break;
558
      }
559
    }
560
  }
561
}
562

    
563
/**
564
 * List of all easing methods available from jQuery Easing v1.3
565
 */
566
function _flexslider_jqeasing_options() {
567
  return array(
568
    "jswing" => "jswing",
569
    "def" => "def",
570
    "easeInQuad" => "easeInQuad",
571
    "easeOutQuad" => "easeOutQuad",
572
    "easeInOutQuad" => "easeInOutQuad",
573
    "easeInCubic" => "easeInCubic",
574
    "easeOutCubic" => "easeOutCubic",
575
    "easeInOutCubic" => "easeInOutCubic",
576
    "easeInQuart" => "easeInQuart",
577
    "easeOutQuart" => "easeOutQuart",
578
    "easeInOutQuart" => "easeInOutQuart",
579
    "easeInQuint" => "easeInQuint",
580
    "easeOutQuint" => "easeOutQuint",
581
    "easeInOutQuint" => "easeInOutQuint",
582
    "easeInSine" => "easeInSine",
583
    "easeOutSine" => "easeOutSine",
584
    "easeInOutSine" => "easeInOutSine",
585
    "easeInExpo" => "easeInExpo",
586
    "easeOutExpo" => "easeOutExpo",
587
    "easeInOutExpo" => "easeInOutExpo",
588
    "easeInCirc" => "easeInCirc",
589
    "easeOutCirc" => "easeOutCirc",
590
    "easeInOutCirc" => "easeInOutCirc",
591
    "easeInElastic" => "easeInElastic",
592
    "easeOutElastic" => "easeOutElastic",
593
    "easeInOutElastic" => "easeInOutElastic",
594
    "easeInBack" => "easeInBack",
595
    "easeOutBack" => "easeOutBack",
596
    "easeInOutBack" => "easeInOutBack",
597
    "easeInBounce" => "easeInBounce",
598
    "easeOutBounce" => "easeOutBounce",
599
    "easeInOutBounce" => "easeInOutBounce",
600
  );
601
}