colorbox.module 10.1 KB
Newer Older
jdwfly's avatar
jdwfly committed
1
2
<?php
// $Id$
3

jdwfly's avatar
jdwfly committed
4
5
6
7
/**
 * @file
 * A light-weight, customizable lightbox plugin for jQuery 1.3
 */
8
9
10
11
12
13
14
15
16
17
18

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


/**
 * Implmentation of hook_theme().
 */
function colorbox_theme() {
19
  return array(
20
    'colorbox_imagefield' => array(
21
22
23
24
25
26
27
28
29
      'variables' => array(
        'style_name' => NULL,
        'path' => NULL,
        'alt' => NULL,
        'title' => NULL,
        'gid' => NULL,
        'attributes' => array(),
        'getsize' => TRUE,
      ),
30
31
32
      'file' => 'colorbox.theme.inc',
    ),

33
34
35
36
37
38
39
40
41
42
    'colorbox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'node' => NULL,
        'field' => array(),
        'image_style' => NULL,
      ),
      'file' => 'colorbox.theme.inc',
    ),
  );
43
44
45
46
47
48
49
50
51
}

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

52
53
54
55
56
57
/**
 * Implementation of hook_views_api().
 */
function colorbox_views_api() {
  return array(
    'api' => 2,
58
    'path' => drupal_get_path('module', 'colorbox') . '/views',
59
60
61
  );
}

62
63
64
/**
 * Implementation of hook_menu().
 */
65
function colorbox_menu() {
66
67
  $items = array();

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

93
94
95
  return $items;
}

96
/**
97
 * Colorbox menu access check.
98
 */
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
115
  }
116
117

  return $access;
118
119
120
121
122
123
124
125
126
127
128
}

/**
 * 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']);
129
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit");
130
  // Compare with the internal and path alias (if any).
131
  $page_match = drupal_match_path($path, $colorbox_pages);
132
  if ($path != $_GET['q']) {
133
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  }

  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':
155
      drupal_add_css($path . '/styles/default/colorbox_default_style.css');
156
157
158
159
      break;
    case 'none':
      break;
    default:
160
      drupal_add_css($style . '/colorbox.css');
161
162
163
164
165
166
167
168
  }

  // 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'),
169
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
170
171
172
173
      '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'),
174
175
176
177
      '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'),
178
179
180
      'overlayclose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
      'maxwidth' => variable_get('colorbox_maxwidth', '100%'),
      'maxheight' => variable_get('colorbox_maxheight', '100%'),
181
182
183
184
185
186
    );
  }
  else {
    $js_settings = array(
      'transition' => 'elastic',
      'speed' => 350,
187
      'opacity' => '0.85',
188
      'slideshow' => FALSE,
189
190
191
192
      'slideshowauto' => FALSE,
      'slideshowspeed' => 2500,
      'slideshowstart' => '',
      'slideshowstop' => '',
193
194
195
196
      'current' => t('{current} of {total}'),
      'previous' => t('« Prev'),
      'next' => t('Next »'),
      'close' => t('Close'),
197
198
199
      'overlayclose' => TRUE,
      'maxwidth' => '100%',
      'maxheight' => '100%',
200
    );
201
  }
202
  drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
203

204
205
206
207
208
  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'))) {
209
210
      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');
211
212
213
    }
  }

214
  $colorbox_file = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
215
216
  drupal_add_js(variable_get('colorbox_path', COLORBOX_PATH) . '/colorbox/' . $colorbox_file[variable_get('colorbox_compression_type', 'min')]);
  drupal_add_js($path . '/js/colorbox.js');
217

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

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

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

  $already_added = TRUE;
231
232
}

233
234
235
236
237
/**
 * Implementation of hook_form_alter().
 * Reformat the login form.
 */
function colorbox_form_alter(&$form, $form_state, $form_id) {
238
  if ($form_id == variable_get('colorbox_login_form', '') && arg(0) == 'user' && arg(1) == 'login' && arg(2) == 'colorbox') {
239
240
241
242
    $form['#action'] = url('user/login', array('query' => array('destination' => $_GET['destination'])));
    $form['name']['#size'] = 25;
    $form['pass']['#size'] = 25;
  }
243
244
}

245
246
247
/**
 * Implementation of hook_link_alter().
 */
248
function colorbox_link_alter(&$links, $node) {
249
250
251
  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) {
252
253
254
      $links['image_size_' . $size]['attributes']['class'] = "image image-{$size} colorbox";
      $links['image_size_' . $size]['href'] = $path;
      unset($links['image_size_' . $size]['query']);
255
    }
256
  }
jdwfly's avatar
jdwfly committed
257
}
258

jdwfly's avatar
jdwfly committed
259
/**
260
 * Implementation of hook_field_formatter_info().
261
 *
262
 * Adds certain colorbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
jdwfly's avatar
jdwfly committed
263
264
 */
function colorbox_field_formatter_info() {
265
  $formatters = array();
266
267
268
269
270
  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'),
271
272
      );
    }
jdwfly's avatar
jdwfly committed
273
274
  }

275
276
277
  return $formatters;

}
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
/**
 * Implements hook_field_formatter_view().
 */
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
// dpm($entity_type);
// dpm($entity);
// dpm($field);
// dpm($instance);
// dpm($langcode);
// dpm($items);
// dpm($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;
}

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
 * 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'];
    }
  }

335
336
337
338
339
  switch (variable_get('colorbox_insert_gallery', 0)) {
    case 0:
      $gallery_id = 'insert-all';
      break;
    case 3:
340
      $gallery_id = 'insert-' . $item['fid'];
341
342
343
      break;
  }

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

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