summaryrefslogtreecommitdiffstats
path: root/colorbox.module
blob: 8a3cd63a6993e0d7379e99bb9e05b9a77b7d3abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
// $Id$

/**
 * @file
 * A light-weight, customizable lightbox plugin for jQuery 1.3
 */

/**
 * The default path to the Colorbox directory.
 */
define('COLORBOX_PATH', 'sites/all/libraries/colorbox');
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.6');


/**
 * Implmentation of hook_theme().
 */
function colorbox_theme() {
  return array(
    'colorbox_imagefield' => array(
      'variables' => array(
        'style_name' => NULL,
        'path' => NULL,
        'alt' => NULL,
        'title' => NULL,
        'gid' => NULL,
        'attributes' => array(),
        'getsize' => TRUE,
      ),
      'file' => 'colorbox.theme.inc',
    ),

    'colorbox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'node' => NULL,
        'field' => array(),
        'image_style' => NULL,
      ),
      'file' => 'colorbox.theme.inc',
    ),
  );
}

/**
 * Implementation of hook_init().
 */
function colorbox_init() {
  _colorbox_doheader();
}

/**
 * Implementation of hook_views_api().
 */
function colorbox_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'colorbox') . '/views',
  );
}

/**
 * Implementation of hook_menu().
 */
function colorbox_menu() {
  $items = array();

  $items['admin/config/media/colorbox'] = array(
    'title' => 'Colorbox',
    'description' => 'Adjust Colorbox settings.',
    'file' => 'colorbox.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('colorbox_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
  $items['user/login/colorbox'] = array(
    'title' => 'Login',
    'page callback' => 'colorbox_login',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
    'file' => 'colorbox.pages.inc',
  );
  $items['colorbox/form'] = array(
    'title' => 'Form',
    'page callback' => 'colorbox_form_page',
    'page arguments' => array(2),
    'access callback' => '_colorbox_form_page_access',
    'access arguments' => array(2),
    'type' => MENU_CALLBACK,
    'file' => 'colorbox.pages.inc',
  );

  return $items;
}

/**
 * Colorbox menu access check.
 */
function _colorbox_form_page_access($form_id) {
  switch ($form_id) {
    case 'contact_mail_page':
      $access = user_access('access site-wide contact form');
      break;
    case 'user_register':
      $access = user_register_access();
      break;
    case 'user_pass':
    case 'user_login':
    case 'user_login_block':
      $access = user_is_anonymous();
      break;
    default:
      // All other forms get no access.
      $access = FALSE;
  }

  return $access;
}

/**
 * Check Colorbox dependencies, Colorbox plugin.
 *
 * @return
 *   array with TRUE/FALSE for each dependency.
 *
 * @see colorbox_requirements()
 */
function _colorbox_status() {
  $status = array();
  $status['colorbox_plugin'] = FALSE;

  if (version_compare(colorbox_get_version(), COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
    $status['colorbox_plugin'] = TRUE;
  }

  return $status;
}

/**
 * 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']);
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit");
  // Compare with the internal and path alias (if any).
  $page_match = drupal_match_path($path, $colorbox_pages);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
  }

  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.
  }

  $path = drupal_get_path('module', 'colorbox');
  $style = variable_get('colorbox_style', 'default');
  switch ($style) {
    case 'default':
      drupal_add_css($path . '/styles/default/colorbox_default_style.css');
      break;
    case 'none':
      break;
    default:
      drupal_add_css($style . '/colorbox.css');
  }

  // 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'),
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
      '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'),
      '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'),
      'overlayClose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
      'maxWidth' => variable_get('colorbox_maxwidth', '100%'),
      'maxHeight' => variable_get('colorbox_maxheight', '100%'),
    );
  }
  else {
    $js_settings = array(
      'transition' => 'elastic',
      'speed' => 350,
      'opacity' => '0.85',
      'slideshow' => FALSE,
      'slideshowAuto' => FALSE,
      'slideshowSpeed' => 2500,
      'slideshowStart' => '',
      'slideshowStop' => '',
      'current' => t('{current} of {total}'),
      'previous' => t('« Prev'),
      'next' => t('Next »'),
      'close' => t('Close'),
      'overlayClose' => TRUE,
      'maxWidth' => '100%',
      'maxHeight' => '100%',
    );
  }
  drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));

  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'))) {
      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');
    }
  }

  drupal_add_js(colorbox_get_path());
  drupal_add_js($path . '/js/colorbox.js');

  if ($style == 'default') {
    drupal_add_js($path . '/styles/default/colorbox_default_style.js');
  }

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

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

  $already_added = TRUE;
}

/**
 * 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.
 *
 * @see version_compare
 */
function colorbox_get_version($colorbox_path = NULL) {
  $version = 0;
  $pattern = '#ColorBox v([0-9\.a-z]+)#';

  // No file is passed in so use the default location.
  if (is_null($colorbox_path)) {
    $colorbox_path = colorbox_get_path();
  }

  // Return the version of Colorbox plugin.
  $colorbox_plugin = file_get_contents($colorbox_path, NULL, NULL, 0, 32);
  if (preg_match($pattern, $colorbox_plugin, $matches)) {
    $version = $matches[1];
  }

  return $version;
}

/**
 * Return the path to the Colorbox plugin.
 */
function colorbox_get_path() {
  $colorbox_file = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
  return variable_get('colorbox_path', COLORBOX_PATH) .'/colorbox/'. $colorbox_file[variable_get('colorbox_compression_type', 'min')];
}

/**
 * Implementation of hook_form_alter().
 * Reformat the login form.
 */
function colorbox_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == variable_get('colorbox_login_form', '') && arg(0) == 'user' && arg(1) == 'login' && arg(2) == 'colorbox') {
    $form['#action'] = url('user/login', array('query' => array('destination' => $_GET['destination'])));
    $form['name']['#size'] = 25;
    $form['pass']['#size'] = 25;
  }
}

/**
 * Implementation of hook_link_alter().
 */
function colorbox_link_alter(&$links, $node) {
  if ($node->type == 'image' && variable_get('colorbox_auto_image_nodes', 0)) {
    // Add a colorbox class to the image link sizes on the image node.
    foreach ($node->images as $size => $path) {
      $links['image_size_' . $size]['attributes']['class'] = "image image-{$size} colorbox";
      $links['image_size_' . $size]['href'] = $path;
      unset($links['image_size_' . $size]['query']);
    }
  }
}

/**
 * Implementation of hook_field_formatter_info().
 *
 * Adds certain colorbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
 */
function colorbox_field_formatter_info() {
  $formatters = array();
  if (module_exists('image')) {
    foreach (image_styles() as $style) {
      $formatters['colorbox__' . $style['name']] = array(
        'label' => t('Colorbox: @preset image', array('@preset' => $style['name'])),
        'field types' => array('image', 'file'),
      );
    }
  }

  return $formatters;

}

/**
 * Implements hook_field_formatter_view().
 */
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Check if the formatter involves a particular image style.
  $matches = array();
  if (preg_match('/__([a-z0-9_]+)/', $display['type'], $matches)) {
    $image_style = $matches[1];
  }

  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'colorbox_image_formatter',
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
      '#image_style' => isset($image_style) ? $image_style : '',
    );
  }

  return $element;
}

/**
 * Implementation of hook_insert_styles().
 */
function colorbox_insert_styles() {
  $insert_styles = array();
  $insert_styles = colorbox_field_formatter_info();

  return $insert_styles;
}

/**
 * Implementation of hook_insert_content().
 */
function colorbox_insert_content($item, $style, $widget) {
  // If the title is empty use description or alt.
  if (empty($item['data']['title'])) {
    if (!empty($item['data']['description'])) {
      $item['data']['title'] = $item['data']['description'];
    }
    elseif (!empty($item['data']['alt'])) {
      $item['data']['title'] = $item['data']['alt'];
    }
  }

  switch (variable_get('colorbox_insert_gallery', 0)) {
    case 0:
      $gallery_id = 'insert-all';
      break;
    case 3:
      $gallery_id = 'insert-' . $item['fid'];
      break;
  }

  list($namespace, $modulename) = explode('__', $style['name'], 2);

  return theme('colorbox_imagefield', array('namespace' => $namespace, 'path' => $item['filepath'], 'alt' => $item['data']['alt'], 'title' => $item['data']['title'], 'gid' => $gallery_id, 'field_name' => 'insert'));
}