colorbox.module 9.09 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
19
20

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


/**
 * Implmentation of hook_theme().
 */
function colorbox_theme() {
  $theme = array(
    'colorbox_imagefield' => array(
21
      'arguments' => array('namespace' => NULL, 'path' => NULL, 'alt' => NULL, 'title' => NULL, 'gid' => NULL, 'field_name' => NULL, 'attributes' => NULL),
22
23
24
25
26
27
      'file' => 'colorbox.theme.inc',
    ),
  );

  if (module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
28
      $theme['colorbox_formatter_'. $preset['presetname'] .'__colorbox'] = array(
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        'function' => 'theme_colorbox_formatter_imagefield',
        'arguments' => array('element' => NULL),
        'file' => 'colorbox.theme.inc',
      );
    }
  }

  return $theme;
}

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

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

52
  $items['admin/settings/colorbox'] = array(
53
    'title' => 'Colorbox',
54
    'description' => 'Adjust Colorbox settings.',
55
    'file' => 'colorbox.admin.inc',
56
57
58
59
    'page callback' => 'drupal_get_form',
    'page arguments' => array('colorbox_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
60
61
62
63
64
  $items['user/login/colorbox'] = array(
    'title' => 'Login',
    'page callback' => 'colorbox_login',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
65
    'file' => 'colorbox.pages.inc',
66
67
68
  );
  $items['colorbox/form'] = array(
    'title' => 'Form',
69
    'page callback' => 'colorbox_form_page',
70
    'page arguments' => array(2),
71
72
    'access callback' => '_colorbox_form_page_access',
    'access arguments' => array(2),
73
    'type' => MENU_CALLBACK,
74
    'file' => 'colorbox.pages.inc',
75
76
  );

77
78
79
  return $items;
}

80
/**
81
 * Colorbox menu access check.
82
 */
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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;
99
  }
100
101

  return $access;
102
103
104
105
106
107
108
109
110
111
112
}

/**
 * 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']);
113
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimg_assist*\nnode/add/*\nnode/*/edit");
114
  // Compare with the internal and path alias (if any).
115
  $page_match = drupal_match_path($path, $colorbox_pages);
116
  if ($path != $_GET['q']) {
117
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  }

  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':
139
      drupal_add_css($path .'/styles/default/colorbox_default_style.css');
140
141
142
143
144
145
146
147
148
149
150
151
152
      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'),
153
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
154
155
156
157
      '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'),
158
159
160
161
      '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'),
162
163
164
      'overlayclose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
      'maxwidth' => variable_get('colorbox_maxwidth', '100%'),
      'maxheight' => variable_get('colorbox_maxheight', '100%'),
165
166
167
168
169
170
    );
  }
  else {
    $js_settings = array(
      'transition' => 'elastic',
      'speed' => 350,
171
      'opacity' => '0.85',
172
      'slideshow' => FALSE,
173
174
175
176
      'slideshowauto' => FALSE,
      'slideshowspeed' => 2500,
      'slideshowstart' => '',
      'slideshowstop' => '',
177
178
179
180
      'current' => t('{current} of {total}'),
      'previous' => t('« Prev'),
      'next' => t('Next »'),
      'close' => t('Close'),
181
182
183
      'overlayclose' => TRUE,
      'maxwidth' => '100%',
      'maxheight' => '100%',
184
    );
185
  }
186
  drupal_add_js(array('colorbox' => $js_settings), 'setting');
187

188
189
190
191
192
193
194
195
196
197
  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)), 'setting');
      drupal_add_js($path .'/js/colorbox_image_module.js');
    }
  }

198
199
  $colorbox_file = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
  drupal_add_js(variable_get('colorbox_path', COLORBOX_PATH) .'/colorbox/'. $colorbox_file[variable_get('colorbox_compression_type', 'min')]);
200
201
  drupal_add_js($path .'/js/colorbox.js');

202
  if ($style == 'default') {
203
    drupal_add_js($path .'/styles/default/colorbox_default_style.js');
204
205
206
207
208
209
  }

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

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

  $already_added = TRUE;
215
216
}

217
218
219
220
221
/**
 * Implementation of hook_form_alter().
 * Reformat the login form.
 */
function colorbox_form_alter(&$form, $form_state, $form_id) {
222
  if ($form_id == variable_get('colorbox_login_form', '') && arg(0) == 'user' && arg(1) == 'login' && arg(2) == 'colorbox') {
223
224
225
226
    $form['#action'] = url('user/login', array('query' => array('destination' => $_GET['destination'])));
    $form['name']['#size'] = 25;
    $form['pass']['#size'] = 25;
  }
227
228
}

229
230
231
/**
 * Implementation of hook_link_alter().
 */
232
function colorbox_link_alter(&$links, $node) {
233
234
235
236
237
238
239
  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']);
    }
240
  }
jdwfly's avatar
jdwfly committed
241
}
242

jdwfly's avatar
jdwfly committed
243
/**
244
 * Implementation of hook_field_formatter_info().
245
 *
246
 * Adds certain colorbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
jdwfly's avatar
jdwfly committed
247
248
 */
function colorbox_field_formatter_info() {
249
250
251
  $formatters = array();
  if (module_exists('imagefield') && module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
252
      $formatters[$preset['presetname'] .'__colorbox'] = array(
253
254
255
256
        'label' => t('Colorbox: @preset image', array('@preset' => $preset['presetname'])),
        'field types' => array('image', 'filefield'),
      );
    }
jdwfly's avatar
jdwfly committed
257
258
  }

259
260
261
  return $formatters;

}
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

/**
 * 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'];
    }
  }

287
288
289
290
291
292
293
294
295
  switch (variable_get('colorbox_insert_gallery', 0)) {
    case 0:
      $gallery_id = 'insert-all';
      break;
    case 3:
      $gallery_id = 'insert-'. $item['fid'];
      break;
  }

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

298
  return theme('colorbox_imagefield', $namespace, $item['filepath'], $item['data']['alt'], $item['data']['title'], $gallery_id, 'insert');
299
}