colorbox.module 16.2 KB
Newer Older
jdwfly's avatar
jdwfly committed
1
<?php
2

jdwfly's avatar
jdwfly committed
3
4
5
6
/**
 * @file
 * A light-weight, customizable lightbox plugin for jQuery 1.3
 */
7
8
9
10
11

/**
 * The default path to the Colorbox directory.
 */
define('COLORBOX_PATH', 'sites/all/libraries/colorbox');
12
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.16');
13
14
15


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

30
31
32
33
34
35
36
37
38
    'colorbox_insert_image' => array(
      'variables' => array(
        'item' => NULL,
        'widget' => NULL,
      ),
      'template' => 'colorbox-insert-image',
      'file' => 'colorbox.theme.inc',
    ),

39
40
41
42
43
    'colorbox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'node' => NULL,
        'field' => array(),
44
45
        'colorbox_node_style' => NULL,
        'colorbox_image_style' => NULL,
46
47
48
49
      ),
      'file' => 'colorbox.theme.inc',
    ),
  );
50
51
52
}

/**
53
 * Implements hook_init().
54
55
56
57
58
 */
function colorbox_init() {
  _colorbox_doheader();
}

59
/**
60
 * Implements hook_views_api().
61
62
63
64
 */
function colorbox_views_api() {
  return array(
    'api' => 2,
65
    'path' => drupal_get_path('module', 'colorbox') . '/views',
66
67
68
  );
}

69
/**
70
 * Implements hook_menu().
71
 */
72
function colorbox_menu() {
73
74
  $items = array();

75
  $items['admin/config/media/colorbox'] = array(
76
    'title' => 'Colorbox',
77
    'description' => 'Adjust Colorbox settings.',
78
    'file' => 'colorbox.admin.inc',
79
80
81
82
    'page callback' => 'drupal_get_form',
    'page arguments' => array('colorbox_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
83
84
85
86
87
  $items['user/login/colorbox'] = array(
    'title' => 'Login',
    'page callback' => 'colorbox_login',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
88
    'file' => 'colorbox.pages.inc',
89
90
91
  );
  $items['colorbox/form'] = array(
    'title' => 'Form',
92
    'page callback' => 'colorbox_form_page',
93
    'page arguments' => array(2),
94
95
    'access callback' => '_colorbox_form_page_access',
    'access arguments' => array(2),
96
    'type' => MENU_CALLBACK,
97
    'file' => 'colorbox.pages.inc',
98
99
  );

100
101
102
  return $items;
}

103
/**
104
 * Colorbox menu access check.
105
 */
106
function _colorbox_form_page_access($form_id) {
107
108
109
  $access = FALSE;

  // First check if this is one of the forms Colorbox support out of the box.
110
  switch ($form_id) {
111
    case 'contact_site_form':
112
113
      $access = user_access('access site-wide contact form');
      break;
114
    case 'user_register_form':
115
116
117
118
119
120
121
      $access = user_register_access();
      break;
    case 'user_pass':
    case 'user_login':
    case 'user_login_block':
      $access = user_is_anonymous();
      break;
122
123
124
125
126
127
128
129
130
  }

  // Invoke hook_colorbox_form_access for all modules.
  if (!$access) {
    foreach (module_implements('colorbox_form_access') as $module) {
      if ($access = module_invoke($module, 'colorbox_form_access', $form_id)) {
        return $access;
      }
    }
131
  }
132
133

  return $access;
134
135
136
137
138
139
140
141
142
143
144
}

/**
 * Check if Colorbox should be active for the current URL.
 *
 * @return
 *   TRUE if Colorbox should be active for the current page.
 */
function _colorbox_active() {
  // Code from the block_list funtion in block.module.
  $path = drupal_get_path_alias($_GET['q']);
145
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit");
146
  // Compare with the internal and path alias (if any).
147
  $page_match = drupal_match_path($path, $colorbox_pages);
148
  if ($path != $_GET['q']) {
149
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  }

  return !$page_match;
}

/**
 * Loads the various js and css files.
 */
function _colorbox_doheader() {
  static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the JavaScript and CSS multiple times.
  }
  if (!_colorbox_active()) {
    return; // Don't add the JavaScript and CSS on specified paths.
  }

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

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

210
  // Give other modules the possibility to override Colorbox settings and style.
211
212
213
214
  $data = &$js_settings;
  $data['__drupal_alter_by_ref'] = array(&$style);
  drupal_alter('colorbox_settings', $data);

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

217
218
219
220
221
  if (module_exists('image') && variable_get('colorbox_auto_image_nodes', 0)) {
    $image_derivative = variable_get('colorbox_image_derivative', 'preview');
    // If the image derivative is set to IMAGE_ORIGINAL check if the
    // user has access before activating Colorbox.
    if (!($image_derivative == IMAGE_ORIGINAL && !user_access('view original images'))) {
222
223
      drupal_add_js(array('colorbox' => array('image_derivative' => $image_derivative)), array('type' => 'setting', 'scope' => JS_DEFAULT));
      drupal_add_js($path . '/js/colorbox_image_module.js');
224
225
226
    }
  }

227
  // Add and initialise the Colorbox plugin.
228
  drupal_add_js(colorbox_get_js());
229
  drupal_add_js($path . '/js/colorbox.js');
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
  // Add JS and CSS based on selected style.
  switch ($style) {
    case 'none':
      break;
    case 'default':
      drupal_add_css($path . '/styles/default/colorbox_default_style.css');
      drupal_add_js($path . '/styles/default/colorbox_default_style.js');
      break;
    case 'stockholmsyndrome':
      drupal_add_css($path . '/styles/stockholmsyndrome/colorbox_stockholmsyndrome.css');
      drupal_add_js($path . '/styles/stockholmsyndrome/colorbox_stockholmsyndrome.js');
      break;
    default:
      drupal_add_css($style . '/colorbox.css');
245
246
  }

247
  if (variable_get('colorbox_load', 0)) {
248
    drupal_add_js($path . '/js/colorbox_load.js');
249
250
  }

251
252
253
254
  if (variable_get('colorbox_inline', 0)) {
    drupal_add_js($path . '/js/colorbox_inline.js');
  }

255
  if ($GLOBALS['user']->uid == 0 && variable_get('colorbox_login', 0)) {
256
    drupal_add_js($path . '/js/colorbox_login.js');
257
258
259
  }

  $already_added = TRUE;
260
261
}

262
263
264
265
266
267
/**
 * Return the version of Colorbox plugin that is installed.
 *
 * This can be used by other modules' hook_requirements() to ensure that the
 * proper version of Colorbox plugin is installed.
 *
268
 * @see version_compare()
269
 */
270
function colorbox_get_version($colorbox_js = NULL) {
271
272
273
274
  $version = 0;
  $pattern = '#ColorBox v([0-9\.a-z]+)#';

  // No file is passed in so use the default location.
275
276
  if (is_null($colorbox_js)) {
    $colorbox_js = colorbox_get_js();
277
278
  }

279
280
281
282
283
284
  // Return the version of Colorbox plugin, it it exists.
  if (file_exists($colorbox_js)) {
    $colorbox_plugin = file_get_contents($colorbox_js, NULL, NULL, 0, 32);
    if (preg_match($pattern, $colorbox_plugin, $matches)) {
      $version = $matches[1];
    }
285
286
287
288
289
  }

  return $version;
}

290
291
292
293
294
295
296
297
298
/**
 * Return the JS filename for Colorbox plugin.
 *
 * @return
 *   Boolean indicating if the JS is located.
 */
function colorbox_get_js() {
  $library_path = colorbox_get_path();

299
  if (file_exists($library_path . '/colorbox/jquery.colorbox.js') && file_exists($library_path . '/colorbox/jquery.colorbox-min.js')) {
300
301
    $colorbox_js_map = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
    $colorbox_js = $colorbox_js_map[variable_get('colorbox_compression_type', 'min')];
302
    return $library_path . '/colorbox/' . $colorbox_js;
303
304
305
306
307
308
309
  }
  else {
    drupal_set_message(t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)), 'error', FALSE);
    return FALSE;
  }
}

310
311
312
313
/**
 * Return the path to the Colorbox plugin.
 */
function colorbox_get_path() {
314
315
316
317
318
319
  static $library_path = NULL;

  // Try to locate the library path in any possible setup.
  if ($library_path == NULL) {
    // First check the default location.
    $path = variable_get('colorbox_path', COLORBOX_PATH);
320
    if (is_dir($path . '/colorbox')) {
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
      $library_path = $path;
    }
    // Ask the libraries module as a fallback.
    elseif ($library_path == NULL && module_exists('libraries')) {
      if ($path = libraries_get_path('colorbox')) {
        $library_path = $path;
        variable_set('colorbox_path', $library_path);
      }
    }
    // HACK: If libraries api module is not installed but available, load it.
    elseif ($library_path == NULL && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
      require_once(dirname(__FILE__) . '/../libraries/libraries.module');
      if ($path = libraries_get_path('colorbox')) {
        $library_path = $path;
        variable_set('colorbox_path', $library_path);
      }
    }
    // If no path is found suggest the default one.
    elseif ($library_path == NULL) {
      $library_path = COLORBOX_PATH;
    }
  }

  return $library_path;
345
346
}

347
/**
348
 * Implements hook_form_alter().
349
350
 * Reformat the login form.
 */
351
function colorbox_form_alter(&$form, &$form_state, $form_id) {
352
353
354
355
356
357
358
359
360
361
362
  switch ($form_id) {
    case 'user_login':
      if (arg(0) == 'user' && arg(1) == 'login' && arg(2) == 'colorbox') {
        $form['name']['#size'] = 25;
        $form['pass']['#size'] = 25;

        // Add links as needed.
        if (variable_get('colorbox_login_links', 0)) {
          $items = array();
          // Add standard links.
          if (variable_get('colorbox_login_links', 0) == 1) {
363
            if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
364
365
366
367
368
369
              $items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));
            }
            $items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
          }
          // Add links that opens in a Colorbox.
          if (variable_get('colorbox_login_links', 0) == 2) {
370
371
            if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
              $items[] = l(t('Create new account'), 'colorbox/form/user_register_form', array('query' => array('width' => '300', 'height' => 'auto'), 'attributes' => array('title' => t('Create a new user account.'), 'class' => 'colorbox-load')));
372
            }
373
            $items[] = l(t('Request new password'), 'colorbox/form/user_pass', array('query' => array('width' => '300', 'height' => '150'), 'attributes' => array('title' => t('Request new password via e-mail.'), 'class' => 'colorbox-load')));
374
          }
375
          $form['links'] = array('#markup' => theme('item_list', array('items' => $items)));
376
377
378
        }
      }
      break;
379
    case 'user_register_form':
380
      if (arg(0) == 'colorbox' && arg(1) == 'form') {
381
382
        $form['account']['name']['#size'] = 30;
        $form['account']['mail']['#size'] = 30;
383
384
385
386
387
388
389
      }
      break;
    case 'user_pass':
      if (arg(0) == 'colorbox' && arg(1) == 'form') {
        $form['name']['#size'] = 30;
      }
      break;
390
  }
391
392
}

393
/**
394
 * Implements hook_link_alter().
395
 */
396
function colorbox_link_alter(&$links, $node) {
397
  if ($node->type == 'image' && is_array($node->images) && variable_get('colorbox_auto_image_nodes', 0)) {
398
399
    // Add a colorbox class to the image link sizes on the image node.
    foreach ($node->images as $size => $path) {
400
401
402
      $links['image_size_' . $size]['attributes']['class'] = "image image-{$size} colorbox";
      $links['image_size_' . $size]['href'] = $path;
      unset($links['image_size_' . $size]['query']);
403
    }
404
  }
jdwfly's avatar
jdwfly committed
405
}
406

jdwfly's avatar
jdwfly committed
407
/**
408
 * Implements hook_field_formatter_info().
jdwfly's avatar
jdwfly committed
409
410
 */
function colorbox_field_formatter_info() {
411
412
413
414
415
416
417
  return array(
    'colorbox' => array(
      'label' => t('Colorbox'),
      'field types' => array('image'),
      'settings' => array('colorbox_node_style' => '', 'colorbox_image_style' => ''),
    ),
  );
418
}
419

420
/**
421
 * Implements hook_field_formatter_settings_form().
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 */
function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $image_styles = image_style_options(FALSE);
  $element['colorbox_node_style'] = array(
    '#title' => t('Node image style'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_node_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['colorbox_image_style'] = array(
    '#title' => t('Colorbox image style'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );

  return $element;
444
}
445

446
/**
447
 * Implements hook_field_formatter_settings_summary().
448
 */
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $summary = array();

  $image_styles = image_style_options(FALSE);
  // Unset possible 'No defined styles' option.
  unset($image_styles['']);
  // Styles could be lost because of enabled/disabled modules that defines
  // their styles in code.
  if (isset($image_styles[$settings['colorbox_node_style']])) {
    $summary[] = t('Node image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
  }
  else {
    $summary[] = t('Node image style: Original image');
  }
466

467
468
  if (isset($image_styles[$settings['colorbox_image_style']])) {
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
469
  }
470
471
472
473
474
475
476
477
  else {
    $summary[] = t('Colorbox image style: Original image');
  }

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

/**
478
 * Implements hook_field_formatter_view().
479
480
481
 */
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
482
483
484
485
486
487
488

  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'colorbox_image_formatter',
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
489
490
      '#colorbox_node_style' => $display['settings']['colorbox_node_style'],
      '#colorbox_image_style' => $display['settings']['colorbox_image_style'],
491
492
493
494
495
496
    );
  }

  return $element;
}

497
/**
498
 * Implements hook_insert_styles().
499
500
501
 */
function colorbox_insert_styles() {
  $insert_styles = array();
502
503
504
  foreach (image_styles() as $key => $style) {
    $insert_styles['colorbox__' . $key] = array('label' => 'Colorbox ' . $style['name']);
  }
505
506
507
508
509

  return $insert_styles;
}

/**
510
 * Implements hook_insert_content().
511
512
 */
function colorbox_insert_content($item, $style, $widget) {
513
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
514
  return theme('colorbox_insert_image', array('item' => $item, 'widget' => $widget));
515
}