Projet

Général

Profil

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

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

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
      ),
48
      'file' => 'colorbox.theme.inc',
49
    ),
50
  );
51
}
52

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

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

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

    
110
  return $libraries;
111
}
112

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

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

    
128
  return $items;
129
}
130

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

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

    
154
  return $page_match;
155
}
156

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

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

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

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

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

    
219
  // Add and initialise the Colorbox plugin.
220
  $variant = variable_get('colorbox_compression_type', 'minified');
221
  libraries_load('colorbox', $variant);
222
  drupal_add_js($path . '/js/colorbox.js');
223

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

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

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

    
246
  $already_added = TRUE;
247
}
248

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

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

    
277
  $image_styles = image_style_options(FALSE);
278
  $image_styles_hide = $image_styles;
279
  $image_styles_hide['hide'] = t('Hide (do not display image)');
280
  $element['colorbox_node_style'] = array(
281
    '#title' => t('Content image style'),
282
    '#type' => 'select',
283
    '#default_value' => $settings['colorbox_node_style'],
284
    '#empty_option' => t('None (original image)'),
285
    '#options' => $image_styles_hide,
286
    '#description' => t('Image style to use in the content.'),
287
  );
288
  $element['colorbox_image_style'] = array(
289
    '#title' => t('Colorbox image style'),
290
    '#type' => 'select',
291
    '#default_value' => $settings['colorbox_image_style'],
292
    '#empty_option' => t('None (original image)'),
293
    '#options' => $image_styles,
294
    '#description' => t('Image style to use in the Colorbox.'),
295
  );
296

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

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

    
382
  return $element;
383
}
384

    
385
/**
386
 * Validate function for colorbox_gallery_custom.
387
 */
388
function colorbox_gallery_custom_validate($element, &$form_state) {
389
  if ($element['#value'] != drupal_html_class($element['#value'])) {
390
    form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
391
  }
392
}
393

    
394
/**
395
 * Implements hook_field_formatter_settings_summary().
396
 */
397
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
398
  $display = $instance['display'][$view_mode];
399
  $settings = $display['settings'];
400

    
401
  $summary = array();
402

    
403
  $image_styles = image_style_options(FALSE);
404
  // Unset possible 'No defined styles' option.
405
  unset($image_styles['']);
406
  // Styles could be lost because of enabled/disabled modules that defines
407
  // their styles in code.
408
  if (isset($image_styles[$settings['colorbox_node_style']])) {
409
    $summary[] = t('Content image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
410
  }
411
  elseif ($settings['colorbox_node_style'] == 'hide') {
412
    $summary[] = t('Content image style: Hide');
413
  }
414
  else {
415
    $summary[] = t('Content image style: Original image');
416
  }
417

    
418
  if (isset($image_styles[$settings['colorbox_image_style']])) {
419
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
420
  }
421
  else {
422
    $summary[] = t('Colorbox image style: Original image');
423
  }
424

    
425
  $gallery = array(
426
    'post' => t('Per post gallery'),
427
    'page' => t('Per page gallery'),
428
    'field_post' => t('Per field in post gallery'),
429
    'field_page' => t('Per field in page gallery'),
430
    'custom' => t('Custom'),
431
    'none' => t('No gallery'),
432
  );
433
  if (isset($settings['colorbox_gallery'])) {
434
    $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
435
  }
436

    
437
  $caption = array(
438
    'auto' =>  t('Automatic'),
439
    'title' => t('Title text'),
440
    'alt' => t('Alt text'),
441
    'node_title' => t('Content title'),
442
    'custom' => t('Custom (with tokens)'),
443
    'none' => t('None'),
444
  );
445
  if (isset($settings['colorbox_caption'])) {
446
    $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
447
  }
448

    
449
  return implode('<br />', $summary);
450
}
451

    
452
/**
453
 * Implements hook_field_formatter_view().
454
 */
455
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
456
  $element = array();
457
  $index = $display['settings']['colorbox_multivalue_index'];
458

    
459
  foreach ($items as $delta => $item) {
460
    if ($index === NULL || $index === $delta) {
461
      $element[$delta] = array(
462
        '#theme' => 'colorbox_image_formatter',
463
        '#item' => $item,
464
        '#entity_type' => $entity_type,
465
        '#entity' => $entity,
466
        '#node' => $entity, // Left for legacy support.
467
        '#field' => $field,
468
        '#display_settings' => $display['settings'],
469
      );
470
    }
471
  }
472

    
473
  return $element;
474
}
475

    
476
/**
477
 * Implements hook_insert_styles().
478
 */
479
function colorbox_insert_styles() {
480
  $insert_styles = array();
481
  foreach (image_styles() as $key => $style) {
482
    $insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $style['name'])));
483
  }
484

    
485
  return $insert_styles;
486
}
487

    
488
/**
489
 * Implements hook_insert_content().
490
 */
491
function colorbox_insert_content($item, $style, $widget) {
492
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
493
  return theme(array('colorbox_insert_image__' . str_replace('-', '_', $item['style_name']), 'colorbox_insert_image'), array('item' => $item, 'widget' => $widget));
494
}
495

    
496
/**
497
 * Machine names normally need to be unique but that does not apply to galleries.
498
 *
499
 * @return
500
 *   Always FALSE
501
 */
502
function colorbox_gallery_exists() {
503
  return FALSE;
504
}