Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flexslider / flexslider.module @ 87dbc3bf

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($id) && !empty($optionset)) {
392
    // JavaScript settings
393
    $js_settings = array(
394
      'optionsets' => array(
395
        $optionset->name => $optionset->options,
396
      ),
397
      'instances' => array(
398
        $id => $optionset->name,
399
      ),
400
    );
401
    // @todo add alter hook for optionset
402
    drupal_add_js(array('flexslider' => $js_settings), 'setting');
403
  }
404
  // Loader JavaScript
405
  drupal_add_js(drupal_get_path('module', 'flexslider') . '/assets/js/flexslider.load.js', array('type' => 'file', 'scope' => 'footer'));
406
}
407

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

    
419
  // We add typecasts to ensure the variables get json encoded properly
420

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

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

    
484
  // Typecast the values
485
  _flexslider_typecast_optionset($defaults);
486

    
487
  // Return the specific item
488
  if (isset($key) and array_key_exists($key, $defaults)) {
489
    return $defaults[$key];
490
  }
491

    
492
  // Return all items
493
  return $defaults;
494
}
495

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

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