Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * A light-weight, customizable lightbox plugin for jQuery 1.3
6
 */
7

    
8
/**
9
 * The default path to the Colorbox directory.
10
 */
11
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.21.1');
12

    
13

    
14
/**
15
 * Implements hook_theme().
16
 */
17
function colorbox_theme() {
18
  return array(
19
    'colorbox_imagefield' => array(
20
      'variables' => array(
21
        'image' => array(),
22
        'path' => NULL,
23
        'title' => NULL,
24
        'gid' => NULL,
25
      ),
26
      'file' => 'colorbox.theme.inc',
27
    ),
28

    
29
    'colorbox_insert_image' => array(
30
      'variables' => array(
31
        'item' => NULL,
32
        'widget' => NULL,
33
      ),
34
      'template' => 'colorbox-insert-image',
35
      'pattern' => 'colorbox_insert_image__[a-z0-9_]+',
36
      'file' => 'colorbox.theme.inc',
37
    ),
38

    
39
    'colorbox_image_formatter' => array(
40
      'variables' => array(
41
        'item' => NULL,
42
        'entity_type' => NULL,
43
        'entity' => NULL,
44
        'node' => NULL,  // Left for legacy support.
45
        'field' => array(),
46
        'display_settings' => array(),
47
        'delta' => null,
48
      ),
49
      'file' => 'colorbox.theme.inc',
50
    ),
51
  );
52
}
53

    
54
/**
55
 * Implements hook_init().
56
 */
57
function colorbox_init() {
58
  // Do not load colorbox during the Drupal installation process, e.g. if part
59
  // of installation profiles.
60
  if (!drupal_installation_attempted()) {
61
    _colorbox_doheader();
62
  }
63
}
64

    
65
/**
66
 * Implements hook_views_api().
67
 */
68
function colorbox_views_api() {
69
  return array(
70
    'api' => 2,
71
    'path' => drupal_get_path('module', 'colorbox') . '/views',
72
  );
73
}
74

    
75
/**
76
 * Implements hook_libraries_info().
77
 */
78
function colorbox_libraries_info() {
79
  $libraries['colorbox'] = array(
80
    'name' => 'Colorbox plugin',
81
    'vendor url' => 'http://www.jacklmoore.com/colorbox',
82
    'download url' => 'https://github.com/jackmoore/colorbox/archive/1.x.zip',
83
    'version arguments' => array(
84
      'file' => 'jquery.colorbox-min.js',
85
      'pattern' => '@(?i:Colorbox)\sv?([0-9\.a-z]+)@',
86
      'lines' => 5,
87
    ),
88
    'files' => array(
89
      'js' => array(
90
        'jquery.colorbox-min.js',
91
      ),
92
    ),
93
    'variants' => array(
94
      'minified' => array(
95
        'files' => array(
96
          'js' => array(
97
            'jquery.colorbox-min.js',
98
          ),
99
        ),
100
      ),
101
      'source' => array(
102
        'files' => array(
103
          'js' => array(
104
            'jquery.colorbox.js',
105
          ),
106
        ),
107
      ),
108
    ),
109
  );
110

    
111
  return $libraries;
112
}
113

    
114
/**
115
 * Implements hook_menu().
116
 */
117
function colorbox_menu() {
118
  $items = array();
119

    
120
  $items['admin/config/media/colorbox'] = array(
121
    'title' => 'Colorbox',
122
    'description' => 'Adjust Colorbox settings.',
123
    'file' => 'colorbox.admin.inc',
124
    'page callback' => 'drupal_get_form',
125
    'page arguments' => array('colorbox_admin_settings'),
126
    'access arguments' => array('administer site configuration'),
127
  );
128

    
129
  return $items;
130
}
131

    
132
/**
133
 * Check if Colorbox should be active for the current URL.
134
 *
135
 * @return
136
 *   TRUE if Colorbox should be active for the current page.
137
 */
138
function _colorbox_active() {
139
  // Make it possible deactivate Colorbox with
140
  // parameter ?colorbox=no in the url.
141
  if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
142
    return FALSE;
143
  }
144

    
145
  // Code from the block_list function in block.module.
146
  $path = drupal_get_path_alias($_GET['q']);
147
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*");
148
  // Compare with the internal and path alias (if any).
149
  $page_match = drupal_match_path($path, $colorbox_pages);
150
  if ($path != $_GET['q']) {
151
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
152
  }
153
  $page_match = variable_get('colorbox_visibility', 0) == 0 ? !$page_match : $page_match;
154

    
155
  return $page_match;
156
}
157

    
158
/**
159
 * Loads the various js and css files.
160
 */
161
function _colorbox_doheader() {
162
  static $already_added = FALSE;
163
  if ($already_added) {
164
    return; // Don't add the JavaScript and CSS multiple times.
165
  }
166
  if (!_colorbox_active()) {
167
    return; // Don't add the JavaScript and CSS on specified paths.
168
  }
169

    
170
  // Insert options and translated strings as javascript settings.
171
  if (variable_get('colorbox_custom_settings_activate', 0)) {
172
    $js_settings = array(
173
      'transition' => variable_get('colorbox_transition_type', 'elastic'),
174
      'speed' => variable_get('colorbox_transition_speed', 350),
175
      'opacity' => variable_get('colorbox_opacity', '0.85'),
176
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
177
      'slideshowAuto' => variable_get('colorbox_slideshowauto', 1) ? TRUE : FALSE,
178
      'slideshowSpeed' => variable_get('colorbox_slideshowspeed', 2500),
179
      'slideshowStart' => variable_get('colorbox_text_start', 'start slideshow'),
180
      'slideshowStop' => variable_get('colorbox_text_stop', 'stop slideshow'),
181
      'current' => strip_tags(variable_get('colorbox_text_current', '{current} of {total}')),
182
      'previous' => strip_tags(variable_get('colorbox_text_previous', '« Prev')),
183
      'next' => strip_tags(variable_get('colorbox_text_next', 'Next »')),
184
      'close' => strip_tags(variable_get('colorbox_text_close', 'Close')),
185
      'overlayClose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
186
      'maxWidth' => variable_get('colorbox_maxwidth', '98%'),
187
      'maxHeight' => variable_get('colorbox_maxheight', '98%'),
188
      'initialWidth' => variable_get('colorbox_initialwidth', '300'),
189
      'initialHeight' => variable_get('colorbox_initialheight', '250'),
190
      'fixed' => variable_get('colorbox_fixed', 1) ? TRUE : FALSE,
191
      'scrolling' => variable_get('colorbox_scrolling', 1) ? TRUE : FALSE,
192
      'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
193
      'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
194
    );
195
  }
196
  else {
197
    $js_settings = array(
198
      'opacity' => '0.85',
199
      'current' => t('{current} of {total}'),
200
      'previous' => t('« Prev'),
201
      'next' => t('Next »'),
202
      'close' => t('Close'),
203
      'maxWidth' => '98%',
204
      'maxHeight' => '98%',
205
      'fixed' => TRUE,
206
      'mobiledetect' => variable_get('colorbox_mobile_detect', 1) ? TRUE : FALSE,
207
      'mobiledevicewidth' => variable_get('colorbox_mobile_device_width', '480px'),
208
    );
209
  }
210

    
211
  $path = drupal_get_path('module', 'colorbox');
212
  $style = variable_get('colorbox_style', 'default');
213

    
214
  // Give other modules the possibility to override Colorbox settings and style.
215
  $data = &$js_settings;
216
  drupal_alter('colorbox_settings', $data, $style);
217

    
218
  drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
219

    
220
  // Add and initialise the Colorbox plugin.
221
  $variant = variable_get('colorbox_compression_type', 'minified');
222
  if (module_exists('libraries')) {
223
    libraries_load('colorbox', $variant);
224
  }
225
  drupal_add_js($path . '/js/colorbox.js');
226

    
227
  // Add JS and CSS based on selected style.
228
  switch ($style) {
229
    case 'none':
230
      break;
231
    case 'default':
232
    case 'plain':
233
    case 'stockholmsyndrome':
234
      drupal_add_css($path . '/styles/' . $style . '/colorbox_style.css');
235
      drupal_add_js($path . '/styles/' . $style . '/colorbox_style.js');
236
      break;
237
    default:
238
      drupal_add_css($style . '/colorbox.css');
239
  }
240

    
241
  if (variable_get('colorbox_load', 0)) {
242
    drupal_add_js($path . '/js/colorbox_load.js');
243
  }
244

    
245
  if (variable_get('colorbox_inline', 0)) {
246
    drupal_add_js($path . '/js/colorbox_inline.js');
247
  }
248

    
249
  $already_added = TRUE;
250
}
251

    
252
/**
253
 * Implements hook_field_formatter_info().
254
 */
255
function colorbox_field_formatter_info() {
256
  return array(
257
    'colorbox' => array(
258
      'label' => t('Colorbox'),
259
      'field types' => array('image'),
260
      'settings' => array(
261
        'colorbox_node_style' => '',
262
        'colorbox_node_style_first' => '',
263
        'colorbox_image_style' => '',
264
        'colorbox_gallery' => 'post',
265
        'colorbox_gallery_custom' => '',
266
        'colorbox_caption' => 'auto',
267
        'colorbox_caption_custom' => '',
268
        'colorbox_multivalue_index' => NULL,
269
      ),
270
    ),
271
  );
272
}
273

    
274
/**
275
 * Implements hook_field_formatter_settings_form().
276
 */
277
function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
278
  $display = $instance['display'][$view_mode];
279
  $settings = $display['settings'];
280

    
281
  $image_styles = image_style_options(FALSE);
282
  $image_styles_hide = $image_styles;
283
  $image_styles_hide['hide'] = t('Hide (do not display image)');
284
  $element['colorbox_node_style'] = array(
285
    '#title' => t('Content image style'),
286
    '#type' => 'select',
287
    '#default_value' => $settings['colorbox_node_style'],
288
    '#empty_option' => t('None (original image)'),
289
    '#options' => $image_styles_hide,
290
    '#description' => t('Image style to use in the content.'),
291
  );
292
  $element['colorbox_node_style_first'] = array(
293
    '#title' => t('Content image style for first image'),
294
    '#type' => 'select',
295
    '#default_value' => $settings['colorbox_node_style_first'],
296
    '#empty_option' => t('No special style.'),
297
    '#options' => $image_styles,
298
    '#description' => t('Image style to use in the content for the first image.'),
299
  );
300
  $element['colorbox_image_style'] = array(
301
    '#title' => t('Colorbox image style'),
302
    '#type' => 'select',
303
    '#default_value' => $settings['colorbox_image_style'],
304
    '#empty_option' => t('None (original image)'),
305
    '#options' => $image_styles,
306
    '#description' => t('Image style to use in the Colorbox.'),
307
  );
308

    
309
  $gallery = array(
310
    'post' => t('Per post gallery'),
311
    'page' => t('Per page gallery'),
312
    'field_post' => t('Per field in post gallery'),
313
    'field_page' => t('Per field in page gallery'),
314
    'custom' => t('Custom'),
315
    'none' => t('No gallery'),
316
  );
317
  $element['colorbox_gallery'] = array(
318
    '#title' => t('Gallery (image grouping)'),
319
    '#type' => 'select',
320
    '#default_value' => $settings['colorbox_gallery'],
321
    '#options' => $gallery,
322
    '#description' => t('How Colorbox should group the image galleries.'),
323
  );
324
  $element['colorbox_gallery_custom'] = array(
325
    '#title' => t('Custom gallery'),
326
    '#type' => 'textfield',
327
    '#maxlength' => 32,
328
    '#default_value' => $settings['colorbox_gallery_custom'],
329
    '#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, hyphen and underscores.'),
330
    '#element_validate' => array('colorbox_gallery_custom_validate'),
331
    '#required' => FALSE,
332
    '#states' => array(
333
      'visible' => array(
334
        ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
335
      ),
336
    ),
337
  );
338

    
339
  $caption = array(
340
    'auto' =>  t('Automatic'),
341
    'title' => t('Title text'),
342
    'alt' => t('Alt text'),
343
    'node_title' => t('Content title'),
344
    'custom' => t('Custom (with tokens)'),
345
    'none' => t('None'),
346
  );
347
  $element['colorbox_caption'] = array(
348
    '#title' => t('Caption'),
349
    '#type' => 'select',
350
    '#default_value' => $settings['colorbox_caption'],
351
    '#options' => $caption,
352
    '#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
353
  );
354
  $element['colorbox_caption_custom'] = array(
355
    '#title' => t('Custom caption'),
356
    '#type' => 'textfield',
357
    '#default_value' => $settings['colorbox_caption_custom'],
358
    '#states' => array(
359
      'visible' => array(
360
        ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
361
      ),
362
    ),
363
  );
364
  // Allow users to hide or set a custom recursion limit.
365
  // The module token_tweaks sets a global recursion limit that can not be bypassed.
366
  if (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
367
    $element['colorbox_token'] = array(
368
      '#type' => 'fieldset',
369
      '#title' => t('Replacement patterns'),
370
      '#theme' => 'token_tree',
371
      '#token_types' => array($instance['entity_type'], 'file'),
372
      '#recursion_limit' => $recursion_limit,
373
      '#dialog' => TRUE,
374
      '#states' => array(
375
        'visible' => array(
376
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
377
        ),
378
      ),
379
    );
380
  }
381
  else {
382
    $element['colorbox_token'] = array(
383
      '#type' => 'fieldset',
384
      '#title' => t('Replacement patterns'),
385
      '#description' => '<strong class="error">' . t('For token support the <a href="@token_url">token module</a> must be installed.', array('@token_url' => 'http://drupal.org/project/token')) . '</strong>',
386
      '#states' => array(
387
        'visible' => array(
388
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => array('value' => 'custom'),
389
        ),
390
      ),
391
    );
392
  }
393

    
394
  return $element;
395
}
396

    
397
/**
398
 * Validate function for colorbox_gallery_custom.
399
 */
400
function colorbox_gallery_custom_validate($element, &$form_state) {
401
  if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
402
    form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
403
  }
404
}
405

    
406
/**
407
 * Implements hook_field_formatter_settings_summary().
408
 */
409
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
410
  $display = $instance['display'][$view_mode];
411
  $settings = $display['settings'];
412

    
413
  $summary = array();
414

    
415
  $image_styles = image_style_options(FALSE);
416
  // Unset possible 'No defined styles' option.
417
  unset($image_styles['']);
418
  // Styles could be lost because of enabled/disabled modules that defines
419
  // their styles in code.
420
  if (isset($image_styles[$settings['colorbox_node_style']])) {
421
    $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
422
  }
423
  elseif ($settings['colorbox_node_style'] == 'hide') {
424
    $summary[] = t('Content image style: Hide');
425
  }
426
  else {
427
    $summary[] = t('Content image style: Original image');
428
  }
429

    
430
  if (isset($image_styles[$settings['colorbox_node_style_first']])) {
431
    $summary[] = t('Content image style of first image: @style', array('@style' => $image_styles[$settings['colorbox_node_style_first']]));
432
  }
433

    
434
  if (isset($image_styles[$settings['colorbox_image_style']])) {
435
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
436
  }
437
  else {
438
    $summary[] = t('Colorbox image style: Original image');
439
  }
440

    
441
  $gallery = array(
442
    'post' => t('Per post gallery'),
443
    'page' => t('Per page gallery'),
444
    'field_post' => t('Per field in post gallery'),
445
    'field_page' => t('Per field in page gallery'),
446
    'custom' => t('Custom'),
447
    'none' => t('No gallery'),
448
  );
449
  if (isset($settings['colorbox_gallery'])) {
450
    $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
451
  }
452

    
453
  $caption = array(
454
    'auto' =>  t('Automatic'),
455
    'title' => t('Title text'),
456
    'alt' => t('Alt text'),
457
    'node_title' => t('Content title'),
458
    'custom' => t('Custom (with tokens)'),
459
    'none' => t('None'),
460
  );
461
  if (isset($settings['colorbox_caption'])) {
462
    $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
463
  }
464

    
465
  return implode('<br />', $summary);
466
}
467

    
468
/**
469
 * Implements hook_field_formatter_view().
470
 */
471
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
472
  $element = array();
473
  $index = $display['settings']['colorbox_multivalue_index'];
474

    
475
  foreach ($items as $delta => $item) {
476
    if ($index === NULL || $index === $delta) {
477
      $element[$delta] = array(
478
        '#theme' => 'colorbox_image_formatter',
479
        '#item' => $item,
480
        '#entity_type' => $entity_type,
481
        '#entity' => $entity,
482
        '#node' => $entity, // Left for legacy support.
483
        '#field' => $field,
484
        '#display_settings' => $display['settings'],
485
        '#delta' => $delta,
486
      );
487
    }
488
  }
489

    
490
  return $element;
491
}
492

    
493
/**
494
 * Implements hook_insert_styles().
495
 */
496
function colorbox_insert_styles() {
497
  $insert_styles = array();
498
  foreach (image_styles() as $key => $style) {
499
    $label = isset($style['label']) ? $style['label'] : $style['name'];
500
    $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $label)));
501
  }
502

    
503
  return $insert_styles;
504
}
505

    
506
/**
507
 * Implements hook_insert_content().
508
 */
509
function colorbox_insert_content($item, $style, $widget) {
510
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
511
  return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
512
}
513

    
514
/**
515
 * Machine names normally need to be unique but that does not apply to galleries.
516
 *
517
 * @return
518
 *   Always FALSE
519
 */
520
function colorbox_gallery_exists() {
521
  return FALSE;
522
}