Projet

Général

Profil

Paste
Télécharger (17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flexslider / flexslider.module @ 651307cd

1
<?php
2
/**
3
 * @file
4
 * A light-weight, customizable image gallery plugin for Drupal based on jQuery
5
 */
6

    
7
define("FLEXSLIDER_COMPATIBLE_MAJOR_VERSION", '2');
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
      'pattern' => '/jQuery FlexSlider v([0-9a-zA-Z\.-]+)/',
21
      'lines' => 2,
22
    ),
23
    'files' => array(
24
      'js' => array(
25
        'jquery.flexslider-min.js',
26
      ),
27
      'css' => array(
28
        'flexslider.css',
29
      ),
30
    ),
31
    'integration files' => array(
32
      'flexslider' => array(
33
        'css' => array('assets/css/flexslider_img.css'),
34
      ),
35
    ),
36
  );
37

    
38
  return $libraries;
39
}
40

    
41
/**
42
 * Implements hook_libraries_info_alter().
43
 */
44
function flexslider_libraries_info_alter(&$libraries) {
45
  $debug = variable_get('flexslider_debug', FALSE);
46
  if ($debug) {
47
    // Switch to the unminified version of the library
48
    if (isset($libraries['flexslider'])) {
49
      $libraries['flexslider']['files']['js'] = array(
50
        'jquery.flexslider.js',
51
      );
52
    }
53
  }
54

    
55
  // Add support for jQuery Easing module
56
  if (module_exists('jqeasing')) {
57
    $libraries['flexslider']['dependencies'][] = 'easing (>=1.3)';
58
  }
59
}
60

    
61
/**
62
 * Implements hook_library().
63
 *
64
 * We also define FlexSlider through the core library callbacks
65
 */
66
function flexslider_library() {
67
  $module_path = drupal_get_path('module', 'flexslider');
68
  $library = libraries_detect('flexslider');
69

    
70
  $libraries['flexslider'] = array(
71
    'title' => $library['name'],
72
    'website' => $library['vendor url'],
73
    'version' => $library['version'],
74
    'js' => array(
75
      $library['library path'] . '/' . key($library['files']['js']) => array(
76
        'scope' => 'footer',
77
      ),
78
    ),
79
    'css' => array(
80
      $library['library path'] . '/' . key($library['files']['css']) => array(
81
        'type' => 'file',
82
        'media' => 'screen',
83
      ),
84
      $module_path . key($library['integration files']['flexslider']['css']) => array(
85
        'type' => 'file',
86
        'media' => 'screen',
87
      ),
88
    ),
89
  );
90
  return $libraries;
91
}
92

    
93
/**
94
 * Implements hook_library_alter().
95
 */
96
function flexslider_library_alter(&$libraries, $module) {
97
  // Enable debug mode
98
  if (FLEXSLIDER_DEBUG) {
99
    if ($module == 'flexslider' and isset($libraries['flexslider'])) {
100
      $libraries['flexslider']['js'] = array(
101
        libraries_get_path() . '/jquery.flexslider.js' => array(
102
          'scope' => 'footer',
103
        ),
104
      );
105
    }
106
  }
107
}
108

    
109
/**
110
 * Implements hook_permission().
111
 */
112
function flexslider_permission() {
113
  return array(
114
    'administer flexslider' =>  array(
115
      'title' => t('Administer the FlexSlider module'),
116
    ),
117
  );
118
}
119

    
120
/**
121
 * Implements hook_menu().
122
 */
123
function flexslider_menu() {
124
  $items = array();
125

    
126
  $items['admin/config/media/flexslider/advanced'] = array(
127
    'title' => 'Advanced settings',
128
    'description' => 'Configure the advanced flexslider module settings.',
129
    'page callback' => 'drupal_get_form',
130
    'page arguments' => array('flexslider_form_settings'),
131
    'access arguments' => array('administer flexslider'),
132
    'type' => MENU_LOCAL_TASK,
133
    'weight' => 2,
134
    'file' => 'flexslider.admin.inc',
135
  );
136

    
137
  return $items;
138
}
139

    
140
/**
141
 * Implements hook_help().
142
 */
143
function flexslider_help($path, $arg) {
144
  switch ($path) {
145
    case 'admin/config/media/flexslider/edit/%':
146
      return
147
          '<p>'
148
        . t('An <em>option set</em> defines exactly how a flexslider image gallery looks like on your site. '
149
          . '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 />'
150
        . t('For a full documentation of all options, refer to the official @docs.', array('@docs' => l(t('FlexSlider documentation'), 'http://www.woothemes.com/flexslider/')))
151
        . '</p>';
152
  }
153
}
154

    
155
/**
156
 * Implements hook_theme().
157
 */
158
function flexslider_theme() {
159
  return array(
160
    // Flexslider container
161
    'flexslider' => array(
162
      'variables' => array('items' => array(), 'settings' => array()),
163
      'template' => 'theme/flexslider',
164
      'file' => 'theme/flexslider.theme.inc',
165
    ),
166

    
167
    // Slide container
168
    'flexslider_list' => array(
169
      'variables' => array('items' => array(), 'settings' => array()),
170
      'file' => 'theme/flexslider.theme.inc',
171
    ),
172

    
173
    // Slide item
174
    'flexslider_list_item' => array(
175
      'variables' => array('item' => array(), 'settings' => array(), 'caption' => ''),
176
      'file' => 'theme/flexslider.theme.inc',
177
    ),
178
  );
179
}
180

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

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

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

    
209
  return $styles;
210
}
211

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

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

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

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

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

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

    
258
  return $optionset;
259
}
260

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

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

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

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

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

    
312

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

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

    
323
  // Prepare the database values.
324
  $db_values = array(
325
    'name' => $optionset->name,
326
    'title' => $optionset->title,
327
    'options' => $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 occurred
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 (!$optionset) {
354
    return;
355
  } elseif (!isset($optionset->name)) {
356
    $optionset = flexslider_optionset_load($optionset);
357
  }
358
  ctools_include('export');
359
  ctools_export_crud_delete('flexslider_optionset', $optionset);
360
  ctools_export_load_object_reset('flexslider_optionset');
361
}
362

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

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

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

    
390
  // If the ID or optionset aren't set, it is assumed the settings will be set
391
  // manually via the calling module/theme
392
  if (!empty($optionset) && !in_array($optionset->name, $cache)) {
393
    $cache[] = $optionset->name;
394
    $js_setting['optionsets'] = array(
395
      $optionset->name => $optionset->options,
396
    );
397
    drupal_add_js(array('flexslider' => $js_setting), 'setting');
398
  }
399

    
400
  if (!empty($id)) {
401
    // JavaScript settings
402
    $js_settings = array(
403
      'instances' => array(
404
        $id => $optionset->name,
405
      ),
406
    );
407
    // @todo add alter hook for optionset
408
    drupal_add_js(array('flexslider' => $js_settings), 'setting');
409
  }
410
  // Loader JavaScript
411
  drupal_add_js(drupal_get_path('module', 'flexslider') . '/assets/js/flexslider.load.js', array('type' => 'file', 'scope' => 'footer'));
412
}
413

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

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

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

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

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

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

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

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

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