Projet

Général

Profil

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

root / drupal7 / sites / all / modules / colorbox / colorbox.module @ c169e7c4

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) v([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 funtion 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' => variable_get('colorbox_text_current', '{current} of {total}'),
182
      'previous' => variable_get('colorbox_text_previous', '« Prev'),
183
      'next' => variable_get('colorbox_text_next', 'Next »'),
184
      'close' => 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
  libraries_load('colorbox', $variant);
223
  drupal_add_js($path . '/js/colorbox.js');
224

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

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

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

    
247
  $already_added = TRUE;
248
}
249

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

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

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

    
307
  $gallery = array(
308
    'post' => t('Per post gallery'),
309
    'page' => t('Per page gallery'),
310
    'field_post' => t('Per field in post gallery'),
311
    'field_page' => t('Per field in page gallery'),
312
    'custom' => t('Custom'),
313
    'none' => t('No gallery'),
314
  );
315
  $element['colorbox_gallery'] = array(
316
    '#title' => t('Gallery (image grouping)'),
317
    '#type' => 'select',
318
    '#default_value' => $settings['colorbox_gallery'],
319
    '#options' => $gallery,
320
    '#description' => t('How Colorbox should group the image galleries.'),
321
  );
322
  $element['colorbox_gallery_custom'] = array(
323
    '#title' => t('Custom gallery'),
324
    '#type' => 'textfield',
325
    '#maxlength' => 32,
326
    '#default_value' => $settings['colorbox_gallery_custom'],
327
    '#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.'),
328
    '#element_validate' => array('colorbox_gallery_custom_validate'),
329
    '#required' => FALSE,
330
    '#states' => array(
331
      'visible' => array(
332
        ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
333
      ),
334
    ),
335
  );
336

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

    
392
  return $element;
393
}
394

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

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

    
411
  $summary = array();
412

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

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

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

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

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

    
463
  return implode('<br />', $summary);
464
}
465

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

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

    
488
  return $element;
489
}
490

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

    
501
  return $insert_styles;
502
}
503

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

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