Updated the documentation.
[project/lightbox2.git] / lightbox2.module
1 <?php
2 // $Id$
3
4
5 /**
6 * @file
7 * Enables the use of lightbox2 which places images above your current page,
8 * not within. This frees you from the constraints of the layout, particularly
9 * column widths.
10 *
11 * This module is for Drupal 5.x only.
12 *
13 * Module by: Mark Ashmead
14 * Mailto: bugzie@gmail.com
15 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
16 *
17 * Image Node Support: Steve McKenzie
18 *
19 */
20
21 /**
22 * Implementation of hook_help().
23 */
24 function lightbox2_help($section) {
25 switch ($section) {
26 case 'admin/modules#description':
27 return t('Enables Lightbox2 for Drupal');
28
29 case 'admin/help#lightbox2':
30 return t('<h3>Overview</h3>
31 <p align="justify">Lightbox2 JS is a simple, unobtrusive script used to overlay images on the current page. It\'s a snap to setup and works on all modern browsers. The module comes with a Lightbox2 Lite option which does not use the jQuery libraries; it is therefore less likely to conflict with anything else.</p>
32 <p align="justify">Places images above your current page, not within. This frees you from the constraints of the layout, particularly column widths. Keeps users on the same page. Clicking to view an image and then having to click the back button to return to your site is bad for continuity (and no fun!).</p>
33 <h3>Features</h3>
34 The version 2 module has several benefits over the plain Lightbox module. Note, not all of these features are available when the "Lightbox2 Lite" option is enabled.
35 <ul>
36 <li>Image Sets: group related images and navigate through them with ease - ideal for your image galleries.</li>
37 <li>Slideshow Capability: automatically transition between grouped images, includes play/pause and previous and next buttons.</li>
38 <li>HTML Content Support: ability to show websites or other HTML content in a lightbox.</li>
39 <li>Visual Effects: fancy pre-loader and transition when you click on the image.</li>
40 <li>Keyboard Shortcuts: useful <a href="http://drupal.org/node/249827">keyboard shortcuts</a> for switching between images, toggling play / pause, etc.</li>
41 <li>Zoom Capability: larger images are reduced in size so they fit snugly inside the browser window. A zoom button can then be clicked on to see it in its original size.</li>
42 <li>Automatic Image Detection: configurable automatic re-formatting of
43 image thumbnails, so there is no need to add \'rel="lightbox"\' to each
44 image link on your site. <a
45 href="http://drupal.org/project/image">Image</a>, <a
46 href="http://drupal.org/project/inline">Inline</a>, <a
47 href="http://drupal.org/project/flickr">Flickr</a>, <a
48 href="http://drupal.org/project/img_assist">Image Assist</a> and <a
49 href="http://drupal.org/project/imagefield">CCK Imagefield</a> modules are all
50 supported. It\'s also possible to configure a custom list of image classes
51 which should trigger the lightbox functionality.</li>
52 <li><a href="http://drupal.org/project/imagecache">Imagecache</a> Support: adds a Lightbox2 field formatter for <a href="http://drupal.org/project/imagefield">CCK imagefields</a> for your custom <a href="http://drupal.org/project/views">views</a>.</li>
53 <li>Image Page Link: a link to the image node can be provided within the lightbox itself.</li>
54 <li>Page Exclusion Capability: exclude certain pages on your site from having the lightbox2 functionality.</li>
55 <li>Gallery 2 Support: support for Gallery 2 images via the <a href="http://drupal.org/project/gallery">Gallery</a> module (beta).</li>
56 </ul>
57 <p align="justify"><h3>Usage</h3></p>
58 <h5 style="text-decoration: underline;">Adding a Basic Lightbox</h5>
59 <p>Add rel=&quot;lightbox&quot; attribute to any link tag to activate the lightbox. For example: </p>
60 <p>&lt;a href=&quot;image-1.jpg&quot; rel=&quot;lightbox&quot; title=&quot;my caption&quot;&gt;image #1&lt;/a&gt;</p>
61 <p>Optional: Use the title attribute if you want to show a caption.</p>
62 <h5 style="text-decoration: underline;">Grouping Images</h5>
63 <p>If you have a set of related images that you would like to group, follow
64 step one but additionally include a group name between square brackets in the
65 rel attribute. For example:</p><p>&lt;a href="images/image-1.jpg"
66 rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
67 &lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]"&gt;image
68 #2&lt;/a&gt;<br />
69 &lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]"&gt;image
70 #3&lt;/a&gt;<br /></p>
71 <p>No limits to the number of image sets per page or how many images are allowed
72 in each set.</p>
73
74 <p>If you have a set of images that you would like to group together in a lightbox, but only wish for one of these images to be visible on your page, you can assign the "lightbox_hide_image" class to hide the additional images. For example:</p>
75
76 <p>&lt;a href="images/image-1.jpg" rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
77 &lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]" class="lightbox_hide_image"&gt;image #2&lt;/a&gt;<br />
78 &lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]" class="lightbox_hide_image"&gt;image #3&lt;/a&gt;</p>
79
80 <h5 style="text-decoration: underline;">Slideshow</h5>
81 <p>This is very similar to the grouping functionality described above. The only difference is that "rel" attribute should be set to "lightshow" instead of "lightbox". Using the same example as above, we could launch the images in a slideshow by doing:</p>
82
83 <p>&lt;a href="images/image-1.jpg" rel="lightshow[roadtrip]"&gt;image #1&lt;/a&gt;<br />
84 &lt;a href="images/image-2.jpg" rel="lightshow[roadtrip]"&gt;image #2&lt;/a&gt;<br />
85 &lt;a href="images/image-3.jpg" rel="lightshow[roadtrip]"&gt;image #3&lt;/a&gt;</p>
86
87 <h3>Keyboard Shortcuts</h3>
88 <table border=0>
89 <tr><td>Close Lightbox</td><td>x</td></tr>
90 <tr><td></td><td>o</td></tr>
91 <tr><td></td><td>c</td></tr>
92 <tr><td></td><td>ESC</td></tr>
93
94 <tr><td>Previous Image</td><td>p</td></tr>
95 <tr><td></td><td>Left Arrow</td></tr>
96
97 <tr><td>Next Image</td><td>n</td></tr>
98 <tr><td></td><td>Right Arrow</td></tr>
99
100 <tr><td>Toggle Zoom</td><td>z (not available in slideshow)</td></tr>
101
102 <tr><td>Toggle Play / Pause</td><td>Spacebar (slideshow only)</td></tr>
103 </table>
104
105 <p>Not all of the keyboard shortcuts work in the Opera browser, for example "z" for toggling the zoom and "spacebar" for toggling play / pause in slideshows. This can be overcome by updating your shortcut settings in the Opera preferences editor.</p>
106
107 ');
108 break;
109 }
110 }
111
112 /**
113 * Implementation of hook_perm().
114 */
115 function lightbox2_perm() {
116 return array('administer lightbox2');
117 }
118
119 /**
120 * Implementation of hook_menu().
121 */
122 function lightbox2_menu($may_cache) {
123 $items = array();
124
125 if ($may_cache) {
126 $items[] = array('path' => 'admin/settings/lightbox2',
127 'title' => t('Lightbox2'),
128 'callback' => 'lightbox2_settings_page',
129 'access' => user_access('administer lightbox2'),
130 'description' => t('Allows the user to configure the lightbox2 settings'),
131 );
132 $items[] = array('path' => 'admin/settings/lightbox2/general',
133 'title' => t('General'),
134 'callback' => 'drupal_get_form',
135 'callback arguments' => array('lightbox2_general_settings_form'),
136 'access' => user_access('administer lightbox2'),
137 'description' => t('Allows the user to configure the lightbox2 settings'),
138 'type' => MENU_DEFAULT_LOCAL_TASK,
139 'weight' => 0,
140 );
141 $items[] = array('path' => 'admin/settings/lightbox2/slideshow',
142 'title' => t('Slideshow'),
143 'callback' => 'drupal_get_form',
144 'callback arguments' => array('lightbox2_slideshow_settings_form'),
145 'access' => user_access('administer lightbox2'),
146 'description' => t('Allows the user to configure the lightbox2 slideshow functionality.'),
147 'type' => MENU_LOCAL_TASK,
148 'weight' => 1,
149 );
150 $items[] = array('path' => 'admin/settings/lightbox2/automatic',
151 'title' => t('Automatic image handling'),
152 'callback' => 'drupal_get_form',
153 'callback arguments' => array('lightbox2_auto_image_handling_settings_form'),
154 'access' => user_access('administer lightbox2'),
155 'description' => t('Allows the user to configure the lightbox2 automatic image handling settings'),
156 'type' => MENU_LOCAL_TASK,
157 'weight' => 2,
158 );
159 }
160 else {
161 if (lightbox2_exclude_these_paths() != 1) {
162 lightbox2_add_files();
163 }
164 }
165
166 return $items;
167 }
168
169
170 /**
171 * The default lightbox2 settings page.
172 */
173 function lightbox2_settings_page($op = NULL) {
174 $output .= drupal_get_form('lightbox2_general_settings_form');
175 return $output;
176 }
177
178 /**
179 * General configuration form for controlling the lightbox behaviour.
180 */
181 function lightbox2_general_settings_form() {
182
183 // Add the javascript which disables / enables form elements.
184 drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');
185
186 // Enable translation of default strings for potx.
187 $default_strings = array(t('View Image Details'), t('Image !current of !total'));
188
189 // Define Lightbox2 Lite fieldset.
190 /* ------------------------------- */
191 $use_lite = variable_get('lightbox2_lite', FALSE);
192 $form['lightbox2_lite_options'] = array(
193 '#type' => 'fieldset',
194 '#title' => t('Lightbox2 Lite'),
195 '#collapsible' => TRUE,
196 '#collapsed' => !$use_lite,
197 );
198
199 // Add checkbox for Lightbox2 Lite.
200 $form['lightbox2_lite_options']['lightbox2_lite'] = array(
201 '#type' => 'checkbox',
202 '#title' => t('Use Lightbox2 Lite'),
203 '#description' => t('Checking this box will enable Lightbox2 Lite and will disable all of the automatic image URL re-formatting features. It also disables all grouping features.'),
204 '#default_value' => $use_lite,
205 );
206
207
208 // Add checkbox for alternative layout.
209 $form['lightbox2_use_alt_layout'] = array(
210 '#type' => 'checkbox',
211 '#title' => t('Use Alternative Layout'),
212 '#description' => t('Enabling this option alters the layout of the lightbox elements. This doesn\'t apply when using Lightbox Lite.'),
213 '#default_value' => variable_get('lightbox2_use_alt_layout', FALSE),
214 );
215
216 // Add checkbox for force navigation display.
217 $form['lightbox2_force_show_nav'] = array(
218 '#type' => 'checkbox',
219 '#title' => t('Force visibility of navigation links'),
220 '#description' => t('When viewing grouped images using the default layout, the navigational links to the next and previous images are only displayed when you hover over the image. Checking this box forces these links to be displayed all the time.'),
221 '#default_value' => variable_get('lightbox2_force_show_nav', FALSE),
222 );
223
224 // Add text box for image count for grouping.
225 $form['lightbox2_image_count_str'] = array(
226 '#type' => 'textfield',
227 '#title' => t('Image Count Text'),
228 '#description' => t('This text is used to display the image count underneath the image in the lightbox when image grouping is enabled. Use !current as a placeholder for the number of the current image and !total for the total number of images in the group. For example, "Image !current of !total".'),
229 '#default_value' => variable_get('lightbox2_image_count_str', 'Image !current of !total'),
230 );
231
232 // Add checkbox for zoom image.
233 $form['lightbox2_disable_zoom'] = array(
234 '#type' => 'checkbox',
235 '#title' => t('Disable Zoom Feature'),
236 '#description' => t('By default, when the image being displayed in the lightbox is larger than the browser window, it is resized to fit within the window and a zoom button is provided for users who wish to view the image in its original size. Checking this box will disable this feature and all images will be displayed without any resizing.'),
237 '#default_value' => variable_get('lightbox2_disable_zoom', FALSE),
238 );
239
240 // Add textarea for urls without lightbox.
241 $form['lightbox2_disable_these_urls'] = array(
242 '#type' => 'textarea',
243 '#title' => t('Disable LightBox for these urls'),
244 '#description' => t('List the urls where the lightbox should be disabled, e.g. "node/add/page". Use &lt;front&gt; to match the front page. Put each url on a separate line. You can use the % character as a wildcard.'),
245 '#default_value' => variable_get('lightbox2_disable_these_urls', ''),
246 );
247
248
249 // Define imagefield settings fieldset.
250 /* ---------------------------------- */
251 if (module_exists('imagefield') && module_exists('imagecache')) {
252 $form['lightbox2_imagefield_options'] = array(
253 '#type' => 'fieldset',
254 '#title' => t('Imagefield settings'),
255 '#collapsible' => TRUE,
256 '#collapsed' => TRUE,
257 );
258
259 $imagefield_options = array(
260 0 => t('No Grouping'),
261 1 => t('Group by Field Name'),
262 2 => t('Group by Node Id'),
263 3 => t('Group by Field Name and Node Id'),
264 );
265 // Add checkbox for imagefield grouping.
266 $form['lightbox2_imagefield_options']['lightbox2_imagefield_group_node_id'] = array(
267 '#type' => 'select',
268 '#title' => t('Select Imagefield Grouping in Views'),
269 '#description' => t('By default, imagefields in views are grouped by the field name they appear in the view in. You can override that grouping here.'),
270 '#options' => $imagefield_options,
271 '#default_value' => variable_get('lightbox2_imagefield_group_node_id', 1),
272 );
273 }
274
275
276 // Define advanced settings fieldset.
277 /* ---------------------------------- */
278 $form['lightbox2_advanced_options'] = array(
279 '#type' => 'fieldset',
280 '#title' => t('Advanced settings'),
281 '#collapsible' => TRUE,
282 '#collapsed' => TRUE,
283 );
284
285 // Add checkbox for overlay opacity.
286 for ($i = 0; $i < 10; $i++) {
287 $opacity_options["0.$i"] = "0.$i";
288 }
289 $opacity_options["1.0"] = "1.0";
290 $form['lightbox2_advanced_options']['lightbox2_overlay_opacity'] = array(
291 '#type' => 'select',
292 '#title' => t('Overlay Opacity'),
293 '#options' => $opacity_options,
294 '#description' => t('The overlay opacity setting determines how visible or transparent the background page is behind the lightbox. The opacity value can range from 0.0 to 1.0 where 0.0 is 100% transparent and 1.0 is 100% opaque.'),
295 '#default_value' => variable_get('lightbox2_overlay_opacity', '0.6'),
296 );
297
298 // Allow users to decide where javascript should be loaded - header or footer.
299 // Header is recommended so user can click on images before page has finished
300 // loading, but footer is needed for sites in IE which use SWFObject.
301 $form['lightbox2_advanced_options']['lightbox2_js_location'] = array(
302 '#type' => 'select',
303 '#title' => t('Location of Javscript'),
304 '#options' => array('header' => t('Header'), 'footer' => t('Footer')),
305 '#description' => t('By default, the lightbox javascript files are loaded in the HTML header. However, for sites using SWFObject to load their Flash content, the footer setting is recommended to prevent "Operation Aborted" errors in IE. If using the footer setting, please note that not all themes correctly implement the footer region and may require a small change.'),
306 '#default_value' => variable_get('lightbox2_js_location', 'header'),
307 );
308
309
310 return system_settings_form($form);
311 }
312
313 /**
314 * Configuration form for controlling the slideshow behaviour.
315 */
316 function lightbox2_slideshow_settings_form() {
317
318 // Add the javascript which disables / enables form elements.
319 drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');
320
321 // Set up a hidden variable.
322 $form['lightbox2_lite'] = array(
323 '#type' => 'hidden',
324 '#value' => variable_get('lightbox2_lite', FALSE),
325 );
326
327
328 // Add text box for slideshow interval.
329 $form['lightbox2_slideshow_interval'] = array(
330 '#type' => 'textfield',
331 '#title' => t('Interval Seconds'),
332 '#description' => t('The slideshow interval is the length of time in seconds an image is visible before the slideshow shows the next image.'),
333 '#default_value' => variable_get('lightbox2_slideshow_interval', 5),
334 '#size' => 20,
335 );
336
337 // Add checkbox for slideshow automatic exit.
338 $form['lightbox2_slideshow_automatic_exit'] = array(
339 '#type' => 'checkbox',
340 '#title' => t('Automatically Exit Slideshow'),
341 '#description' => t('Enabling this option will cause the lightbox to automatically close after displaying the last image.'),
342 '#default_value' => variable_get('lightbox2_slideshow_automatic_exit', TRUE),
343 );
344
345 // Add checkbox for showing hte play / pause button.
346 $form['lightbox2_slideshow_show_play_pause'] = array(
347 '#type' => 'checkbox',
348 '#title' => t('Show Play / Pause Button'),
349 '#description' => t('If enabled, a play / pause button will be shown in the slideshow allowing the user more control over their viewing experience.'),
350 '#default_value' => variable_get('lightbox2_slideshow_show_play_pause', TRUE),
351 );
352
353 // Add checkbox for "pausing on next click".
354 $form['lightbox2_slideshow_pause_on_next_click'] = array(
355 '#type' => 'checkbox',
356 '#title' => t('Pause Slideshow on "Next Image" Click'),
357 '#description' => t('When enabled the slideshow is automatically paused, and the following image shown, when the "Next" button is clicked.'),
358 '#default_value' => variable_get('lightbox2_slideshow_pause_on_next_click', FALSE),
359 );
360
361 // Add checkbox for "pausing on prev click".
362 $form['lightbox2_slideshow_pause_on_prev_click'] = array(
363 '#type' => 'checkbox',
364 '#title' => t('Pause Slideshow on "Previous Image" Click'),
365 '#description' => t('When enabled the slideshow is automatically paused, and the previous image shown, when the "Previous" button is clicked.'),
366 '#default_value' => variable_get('lightbox2_slideshow_pause_on_previous_click', TRUE),
367 );
368
369 return system_settings_form($form);
370 }
371
372 /**
373 * Validation function for the slideshow configuration form.
374 *
375 * Ensure that the slideshow interval submitted is numeric.
376 */
377 function lightbox2_slideshow_settings_form_validate($form_id, $form_values) {
378 if (!is_numeric($form_values['lightbox2_slideshow_interval'])) {
379 form_set_error('lightbox2_slideshow_interval', t('The "interval seconds" value must be numeric.'));
380 }
381 }
382
383 /**
384 * Configuration form the automatic image handling functionality.
385 */
386 function lightbox2_auto_image_handling_settings_form() {
387
388 // Add the javascript which disables / enables form elements.
389 drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');
390
391 // Set up a hidden variable.
392 $form['lightbox2_lite'] = array(
393 '#type' => 'hidden',
394 '#value' => variable_get('lightbox2_lite', FALSE),
395 );
396
397 $automatic_options = array(
398 0 => t('Disabled'),
399 1 => t('Lightbox'),
400 2 => t('Lightbox grouped'),
401 3 => t('Slideshow'),
402 );
403
404 // Set image node options help text.
405 $form['image_node_options'] = array(
406 '#value' => t('These options allow automatic URL re-formatting of images. This removes the need for you to add \'rel="lightbox"\' to each image link throughout your site. You can select which image sizes will trigger the lightbox and configure a list of image CSS classes which should also have their URLs automatically re-formatted. This feature is not available when using Lightbox2 Lite.'),
407 );
408
409 // Define image nodes settings fieldset.
410 /* ------------------------------------- */
411 $form['lightbox2_image_node_options'] = array(
412 '#type' => 'fieldset',
413 '#title' => t('Image node settings'),
414 '#collapsible' => TRUE,
415 '#collapsed' => FALSE,
416 );
417
418 // Add checkbox for image nodes.
419 $form['lightbox2_image_node_options']['lightbox2_image_node'] = array(
420 '#type' => 'select',
421 '#title' => t('Automatic handler for Image Nodes'),
422 '#options' => $automatic_options,
423 '#description' => t('Choose how URLs for image nodes will be automatically handled.'),
424 '#default_value' => variable_get('lightbox2_image_node', 2),
425 );
426
427 // Add drop-down for list of available image sizes.
428 if (module_exists('image')) {
429 $sizes = _image_get_sizes();
430 foreach ($sizes as $size_key => $size) {
431 if ($size_key == 'original' || $size_key == '_original') {
432 $size_key = '';
433 }
434 $size_options[$size_key] = $size['label'];
435 }
436 $form['lightbox2_image_node_options']['lightbox2_display_image_size'] = array(
437 '#type' => 'select',
438 '#title' => t('Lightbox image display size'),
439 '#options' => $size_options,
440 '#default_value' => variable_get('lightbox2_display_image_size', ''),
441 '#description' => t('Select which image size will be loaded in the lightbox. This only applies to images uploaded with the Image module.'),
442 );
443
444 $form['lightbox2_image_node_options']['lightbox2_trigger_image_size'] = array(
445 '#type' => 'select',
446 '#multiple' => TRUE,
447 '#title' => t('Image trigger size'),
448 '#options' => $size_options,
449 '#default_value' => variable_get('lightbox2_trigger_image_size', array('thumbnail' => t('Thumbnail'))),
450 '#description' => t('Select which image size, when clicked on, will automatically trigger the lightbox. This only applies to images uploaded with the Image module.'),
451 );
452
453 }
454
455 // Add checkbox for disabling lightbox for gallery lists.
456 $form['lightbox2_image_node_options']['lightbox2_disable_nested_galleries'] = array(
457 '#type' => 'checkbox',
458 '#title' => t('Disable Lightbox for Gallery Lists'),
459 '#description' => t('Checking this box will disable the lightbox for images in gallery lists. This means it is possible to open a gallery by clicking on the teaser image, but the lightbox will still appear when viewing images within the gallery. This only applies to image galleries created with the "image gallery" module.'),
460 '#default_value' => variable_get('lightbox2_disable_nested_galleries', TRUE),
461 );
462
463 // Add checkbox for flickr image support.
464 $form['lightbox2_flickr'] = array(
465 '#type' => 'select',
466 '#title' => t('Automatic handler for Flickr images'),
467 '#options' => $automatic_options,
468 '#description' => t('Choose how URLs for Flickr images will be automatically handled.'),
469 '#default_value' => variable_get('lightbox2_flickr', 2),
470 );
471
472 // Add checkbox for gallery2 block image support.
473 $form['lightbox2_gallery2_blocks'] = array(
474 '#type' => 'select',
475 '#title' => t('Automatic handler for Gallery2 block images'),
476 '#options' => $automatic_options,
477 '#description' => t('Choose how URLs for images, contained within Gallery2 image blocks, will be automatically handled.'),
478 '#default_value' => variable_get('lightbox2_gallery2_blocks', 2),
479 );
480
481
482 // Add checkbox for inline image support.
483 $form['lightbox2_inline'] = array(
484 '#type' => 'select',
485 '#title' => t('Automatic handler for Inline images'),
486 '#options' => $automatic_options,
487 '#description' => t('Choose how URLs for Inline images will be automatically handled.'),
488 '#default_value' => variable_get('lightbox2_inline', 2),
489 );
490
491 // Add checkbox for image assist custom size images.
492 $form['lightbox2_image_assist_custom'] = array(
493 '#type' => 'select',
494 '#title' => t('Automatic handler for Image Assist custom size images'),
495 '#options' => $automatic_options,
496 '#description' => t('Choose how URLs for custom size images, displayed by the Image Assist module, will be automatically handled.'),
497 '#default_value' => variable_get('lightbox2_image_assist_custom', 2),
498 );
499
500 // Add text box for custom trigger classes.
501 $form['lightbox2_custom_trigger_classes'] = array(
502 '#type' => 'textarea',
503 '#title' => t('Custom image trigger classes'),
504 '#description' => t('List the image classes which should trigger the lightbox when clicked on. Put each class on a separate line.'),
505 '#default_value' => variable_get('lightbox2_custom_trigger_classes', ''),
506 );
507
508 // Add text box for link text to node.
509 $form['lightbox2_node_link_text'] = array(
510 '#type' => 'textfield',
511 '#title' => t('Text for Image Page Link'),
512 '#description' => t('This is the text that will appear as the link to the image page underneath the image in the lightbox.'),
513 '#default_value' => variable_get('lightbox2_node_link_text', 'View Image Details'),
514 );
515
516 // Add checkbox for node link target.
517 $form['lightbox2_node_link_target'] = array(
518 '#type' => 'checkbox',
519 '#title' => t('Open Image Page in New Window'),
520 '#description' => t('This controls whether the link to the image page underneath the image is opened in a new window or the current window.'),
521 '#default_value' => variable_get('lightbox2_node_link_target', FALSE),
522 '#return_value' => '_blank',
523 );
524
525 // Add checkbox for disabling lightbox for acidfree gallery lists.
526 if (module_exists("acidfree")) {
527 $form['lightbox2_disable_nested_acidfree_galleries'] = array(
528 '#type' => 'checkbox',
529 '#title' => t('Disable Lightbox for Acidfree Gallery Lists'),
530 '#description' => t('Checking this box will disable the lightbox for images in gallery lists. This means it is possible to open a gallery by clicking on the teaser image, but the lightbox will still appear when viewing images within the gallery. This only applies to image galleries created with the "acidfree" module.'),
531 '#default_value' => variable_get('lightbox2_disable_nested_acidfree_galleries', TRUE),
532 );
533 }
534
535 return system_settings_form($form);
536 }
537
538 /**
539 * Implementation of hook_filter_tips().
540 */
541 function lightbox2_filter_tips($delta, $format, $long = FALSE) {
542 if ($delta == 0) {
543 if (!$long) {
544 return t('Image links with \'rel="lightbox"\' in the &lt;a&gt; tag will appear in a Lightbox when clicked on.');
545 }
546 else {
547 $output = '<p>'. t('To add a lightbox to your images, add rel="lightbox" attribute to any link tag to activate the lightbox. For example:') .'</p>';
548 $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox" title="my caption"&gt;image #1&lt;/a&gt;</code>') .'</p>';
549 $output .= '<p>'. t('The title attribute in the link tag is optional. The addition of this attribute enables the display of a caption with the image displayed in the lightbox.') .'</p>';
550 $output .= '<p>'. t('If you have a set of related images that you would like to group, then you will need to include a group name between square brackets in the rel attribute. For example:') .'</p>';
551 $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br /> &lt;a href="image-2.jpg" rel="lightbox[roadtrip]"&gt;image #2&lt;/a&gt;<br /> &lt;a href="image-3.jpg" rel="lightbox[roadtrip]"&gt;image #3&lt;/a&gt;<br /> </code>') .'</p>';
552 $output .= '<p>'. t('There are no limits to the number of image sets per page or how many images are allowed in each set.') .'</p>';
553 $output .= '<p>'. t('If you wish to turn the caption into a link, format your caption in the following way:') .'</p>';
554 $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox" title=\'&lt;a href="http://www.yourlink.com"&gt;View Image Details&lt;/a&gt;\'&gt;image #1&lt;/a&gt;</code>') .'</p>';
555 return $output;
556 }
557 }
558 else if ($delta == 1) {
559 return t('Image links from G2 are formatted for use with Lightbox2');
560 }
561 }
562
563 /**
564 * Implementation of hook_filter().
565 */
566 function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
567 switch ($op) {
568 case 'list':
569 return array(0 => t('Lightbox filter'), 1 => t('Lightbox G2 filter'));
570
571 case 'description':
572 if ($delta == 1) {
573 return t('Turns g2_filter links into Lightbox2 appropriate links');
574 }
575 else {
576 return t('Image links with \'rel="lightbox"\' in the &lt;a&gt; tag will appear in a Lightbox when clicked on.');
577 }
578
579 case 'process':
580 if ($delta == 1) {
581 $text = ' '. $text .' ';
582 $text = preg_replace('/ShowItem/', 'DownloadItem', $text);
583 $text = preg_replace('/target=""/', 'rel="lightbox"', $text);
584 $text = substr($text, 1, -1);
585 }
586 return $text;
587
588 default:
589 return $text;
590 }
591 }
592
593 /**
594 * Provide links to the CSS stylesheet and JS file associated with
595 * this module.
596 */
597 function lightbox2_add_files() {
598 static $already_added = FALSE;
599 if ($already_added) {
600 return; // Don't add the javascript and CSS multiple times.
601 }
602 $already_added = TRUE;
603
604 // Load required js and css files.
605 $path = drupal_get_path('module', 'lightbox2');
606
607 // Initialise some variables.
608 $inline_image_handler = variable_get('lightbox2_inline', 2);
609 $flickr_image_handler = variable_get('lightbox2_flickr', 2);
610 $gallery2_block_handler = variable_get('lightbox2_gallery2_blocks', 2);
611 $image_assist_handler = variable_get('lightbox2_image_assist_custom', 2);
612 $image_node_handler = variable_get('lightbox2_image_node', 2);
613
614 // Set the list of image classes to format urls for.
615 $image_node_sizes = '';
616 $trigger_lightbox_classes = '';
617 $trigger_lightbox_group_classes = '';
618 $trigger_slideshow_classes = '';
619
620 // Inline module images.
621 switch ($inline_image_handler) {
622 case 1:
623 $trigger_lightbox_classes .= "img.inline,";
624 break;
625 case 2:
626 $trigger_lightbox_group_classes .= "img.inline,";
627 break;
628 case 3:
629 $trigger_slideshow_classes .= "img.inline,";
630 break;
631 }
632
633 // Flickr images.
634 switch ($flickr_image_handler) {
635 case 1:
636 $trigger_lightbox_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
637 break;
638 case 2:
639 $trigger_lightbox_group_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
640 break;
641 case 3:
642 $trigger_slideshow_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
643 break;
644 }
645
646 // Gallery2 block images.
647 switch ($gallery2_block_handler) {
648 case 1:
649 $trigger_lightbox_classes .= "img.ImageFrame_image,";
650 break;
651 case 2:
652 $trigger_lightbox_group_classes .= "img.ImageFrame_image,";
653 break;
654 case 3:
655 $trigger_slideshow_classes .= "img.ImageFrame_image,";
656 break;
657 }
658
659 // Image Assist custom size images.
660 switch ($image_assist_handler) {
661 case 1:
662 $trigger_lightbox_classes .= "img.image-img_assist_custom,";
663 break;
664 case 2:
665 $trigger_lightbox_group_classes .= "img.image-img_assist_custom,";
666 break;
667 case 3:
668 $trigger_slideshow_classes .= "img.image-img_assist_custom,";
669 break;
670 }
671
672 // Image nodes.
673 if ($image_node_handler) {
674 $trigger_sizes = variable_get('lightbox2_trigger_image_size', array('thumbnail' => t('Thumbnail')));
675 foreach ($trigger_sizes as $size => $size_name) {
676 $triggers = "img.$size, img.image-$size,";
677 if (empty($size)) {
678 $triggers = "img._original, img.image-_original,";
679 }
680 else {
681 $image_node_sizes .= "\.$size|";
682 }
683 switch ($image_node_handler) {
684 case 1:
685 $trigger_lightbox_classes .= $triggers;
686 break;
687 case 2:
688 $trigger_lightbox_group_classes .= $triggers;
689 break;
690 case 3:
691 $trigger_slideshow_classes .= $triggers;
692 break;
693 }
694 }
695 }
696
697 // Custom images.
698 $custom_triggers = variable_get('lightbox2_custom_trigger_classes', '');
699 if (!empty($custom_triggers)) {
700 $trigger_classes = preg_split("/(\r\n|\n)/", $custom_triggers);
701 foreach ($trigger_classes as $class) {
702 if (!empty($class)) {
703 $trigger_lightbox_classes .= "img.$class,";
704 }
705 }
706 }
707 $trigger_lightbox_classes = rtrim($trigger_lightbox_classes, ",");
708 $trigger_lightbox_group_classes = rtrim($trigger_lightbox_group_classes, ",");
709 $trigger_slideshow_classes = rtrim($trigger_slideshow_classes, ",");
710 $image_node_sizes = '('. rtrim($image_node_sizes, "|") .')';
711
712 // Find out if we're using a RTL language.
713 $rtl = FALSE;
714 if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
715 $rtl = TRUE;
716 }
717
718 // Load the javascript settings.
719 $js_settings = array(
720 'rtl' => $rtl,
721 'file_path' => base_path() .'(\w\w/)'. file_directory_path(),
722 'base_path' => base_path(),
723 'default_image' => base_path() . $path .'/images/brokenimage.jpg',
724 'overlay_opacity' => variable_get('lightbox2_overlay_opacity', '0.6'),
725 'use_alt_layout' => variable_get('lightbox2_use_alt_layout', FALSE),
726 'disable_zoom' => variable_get('lightbox2_disable_zoom', FALSE),
727 'force_show_nav' => variable_get('lightbox2_force_show_nav', FALSE),
728 'node_link_text' => check_plain(t(variable_get('lightbox2_node_link_text', 'View Image Details'))),
729 'node_link_target' => variable_get('lightbox2_node_link_target', FALSE),
730 'image_count' => check_plain(t(variable_get('lightbox2_image_count_str', 'Image !current of !total'))),
731 'lite_press_x_close' => t('press !x to close', array('!x' => '<a href="#" onclick="hideLightbox(); return FALSE;"><kbd>x</kbd></a>')),
732 // Automatic image handling settings.
733 'display_image_size' => variable_get('lightbox2_display_image_size', ''),
734 'image_node_sizes' => $image_node_sizes,
735 'trigger_lightbox_classes' => $trigger_lightbox_classes,
736 'trigger_lightbox_group_classes' => $trigger_lightbox_group_classes,
737 'trigger_slideshow_classes' => $trigger_slideshow_classes,
738 'disable_for_gallery_lists' => variable_get('lightbox2_disable_nested_galleries', TRUE),
739 'disable_for_acidfree_gallery_lists' => variable_get('lightbox2_disable_nested_acidfree_galleries', TRUE),
740 // Slideshow settings.
741 'slideshow_interval' => variable_get('lightbox2_slideshow_interval', 5) * 1000,
742 'slideshow_automatic_exit' => variable_get('lightbox2_slideshow_automatic_exit', TRUE),
743 'show_play_pause' => variable_get('lightbox2_slideshow_show_play_pause', TRUE),
744 'pause_on_next_click' => variable_get('lightbox2_slideshow_pause_on_next_click', FALSE),
745 'pause_on_previous_click' => variable_get('lightbox2_slideshow_pause_on_previous_click', TRUE),
746 );
747 drupal_add_js(array('lightbox2' => $js_settings), 'setting');
748
749 // Check where we should load the javascript files - header or footer.
750 $js_location = variable_get('lightbox2_js_location', 'header');
751
752 // Lightbox2 Plus.
753 if (!variable_get('lightbox2_lite', FALSE)) {
754 $css = $path .'/css/lightbox.css';
755 if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
756 $css = $path .'/css/lightbox-rtl.css';
757 }
758 if (variable_get('lightbox2_use_alt_layout', FALSE)) {
759 // No need for rtl version, everything is centred.
760 $css = $path .'/css/lightbox_alt.css';
761 }
762 drupal_add_css($css);
763
764 // Check to see if any automatic image handling options are enabled.
765 if ($image_node_handler || $flickr_image_handler || $gallery2_block_handler || $inline_image_handler || $image_assist_handler || $custom_triggers != '') {
766 drupal_add_js($path .'/js/auto_image_handling.js', 'module', $js_location);
767 }
768 drupal_add_js($path .'/js/lightbox.js', 'module', $js_location);
769
770 }
771 // Lightbox Lite.
772 else {
773 $css = $path .'/css/lightbox_lite.css';
774 if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
775 $css = $path .'/css/lightbox_lite-rtl.css';
776 }
777 drupal_add_css($css);
778 drupal_add_js($path .'/js/lightbox_lite.js', 'module', $js_location);
779 }
780 }
781
782 /**
783 * Implementation of hook_field_formatter_info().
784 *
785 * Add certain lightbox+imagecache formatters to CCK image fields if the
786 * imagefield.module and the imagecache.module exist.
787 */
788 function lightbox2_field_formatter_info() {
789 $formatter = array();
790 if (module_exists('imagefield') && module_exists('imagecache')) {
791 $rules = array();
792 if (function_exists('imagecache_presets')) {
793 $presets = imagecache_presets();
794 foreach ($presets as $preset_id => $preset_info) {
795 $rules[$preset_id] = $preset_info['presetname'];
796 }
797 }
798 else {
799 $rules = _imagecache_get_presets();
800 }
801 foreach ($rules as $ruleid => $view_rule) {
802 $formatters['lightbox2][original]['. $view_rule] = array(
803 'label' => 'Lightbox2: original->'. $view_rule,
804 'field types' => array('image'),
805 );
806 $formatters['lightbox2]['. $view_rule .'][original'] = array(
807 'label' => 'Lightbox2: '. $view_rule .'->original',
808 'field types' => array('image'),
809 );
810 $formatters['lightshow2][original]['. $view_rule] = array(
811 'label' => 'Lightbox2 slideshow: original->'. $view_rule,
812 'field types' => array('image'),
813 );
814 $formatters['lightshow2]['. $view_rule .'][original'] = array(
815 'label' => 'Lightbox2 slideshow: '. $view_rule .'->original',
816 'field types' => array('image'),
817 );
818 foreach ($rules as $rid => $lightbox_rule) {
819 $formatters['lightbox2]['. $view_rule .']['. $lightbox_rule] = array(
820 'label' => 'Lightbox2: '. $view_rule .'->'. $lightbox_rule,
821 'field types' => array('image'),
822 );
823 $formatters['lightshow2]['. $view_rule .']['. $lightbox_rule] = array(
824 'label' => 'Lightbox2 slideshow: '. $view_rule .'->'. $lightbox_rule,
825 'field types' => array('image'),
826 );
827 }
828 }
829 }
830
831 return $formatters;
832 }
833
834 /**
835 * Implementation of hook_field_formatter().
836 */
837 function lightbox2_field_formatter($field, $item, $formatter) {
838 if (module_exists('imagefield') && module_exists('imagecache')) {
839 if (isset($item['filepath'])) {
840 $filepath = &$item['filepath'];
841 }
842 else if (!empty($item['fid'])) {
843 $file = _imagefield_file_load($item['fid']);
844 $filepath = &$file['filepath'];
845 }
846 else {
847 return '';
848 }
849 if (lightbox2_exclude_these_paths() != 1) {
850 lightbox2_add_files();
851 }
852
853 $item = array_merge($item, (array)$file);
854 $item['filepath'] = $filepath;
855 if (strpos($formatter, 'lightbox2][') !== FALSE || strpos($formatter, 'lightshow2][') !== FALSE) {
856 list($null, $view_preset, $lightbox_preset) = explode('][', $formatter, 3);
857 $item['lightbox_preset'] = $lightbox_preset;
858
859 $rules = array();
860 if (function_exists('imagecache_presets')) {
861 $presets = imagecache_presets();
862 foreach ($presets as $preset_id => $preset_info) {
863 $rules[$preset_id] = $preset_info['presetname'];
864 }
865 }
866 else {
867 $rules = _imagecache_get_presets();
868 }
869 if ($view_preset == 'original' || in_array($view_preset, (array) $rules)) {
870 $slideshow = FALSE;
871 if (strpos($formatter, 'lightshow2][') !== FALSE) {
872 $slideshow = TRUE;
873 }
874 return theme('imagefield_image_imagecache_lightbox2', $view_preset, $field, $item, NULL, $slideshow);
875 }
876 }
877 }
878 }
879
880 /**
881 * Implementation of theme_imagefield_image_imagecache_lightbox2().
882 */
883 function theme_imagefield_image_imagecache_lightbox2($view_preset, $field, $item, $attributes = NULL, $slideshow = FALSE) {
884
885 $rel = 'lightbox';
886 if ($slideshow) {
887 $rel = 'lightshow';
888 }
889 $imagefield_grouping = variable_get('lightbox2_imagefield_group_node_id', 1);
890 if ($imagefield_grouping == 1) {
891 $rel = $rel .'['. $field['field_name'] .']';
892 }
893 else if ($imagefield_grouping == 2 && !empty($item['nid'])) {
894 $rel = $rel .'['. $item['nid'] .']';
895 }
896 else if ($imagefield_grouping == 3 && !empty($item['nid'])) {
897 $rel = $rel .'['. $field['field_name'] . $item['nid'] .']';
898 }
899
900 $node_link = '';
901 if (!empty($item['nid'])) {
902 $attributes = array();
903 $target = variable_get('lightbox2_node_link_target', FALSE);
904 if (!empty($target)) {
905 $attributes = array('target' => $target);
906 }
907 $node_link = '<br /><br />'. l(t(variable_get('lightbox2_node_link_text', 'View Image Details')), 'node/'. $item['nid'], $attributes);
908 }
909
910 $link_attributes = array(
911 'rel' => $rel,
912 'title' => $item['title'] . $node_link);
913
914 if ($view_preset == 'original') {
915 $image = theme('lightbox2_image', $item['filepath'], $item['alt'], $item['title'], $attributes);
916 }
917 else {
918 $image = theme('imagecache', $view_preset, $item['filepath'], $item['alt'], $item['title'], $attributes);
919 }
920 if ($item['lightbox_preset'] == 'original') {
921 $output = l($image, file_create_url($item['filepath']), $link_attributes, NULL, NULL, FALSE, TRUE);
922 }
923 else {
924 $output = l($image, lightbox2_imagecache_create_url($item['lightbox_preset'], $item['filepath']), $link_attributes, NULL, NULL, FALSE, TRUE);
925 }
926
927 return $output;
928 }
929
930 /**
931 * Return TRUE if current path is disabled for lightbox according to
932 * lightbox2_disable_these_urls.
933 */
934 function lightbox2_exclude_these_paths() {
935 // Use lightbox if this url is not excluded.
936 $disabled_urls = variable_get('lightbox2_disable_these_urls', '');
937
938 if (!empty($disabled_urls)) {
939 $urls = explode("\n", $disabled_urls);
940
941 foreach ($urls as $url) {
942 $url = rtrim($url);
943 if ($url == "<front>") {
944 $url = "";
945 }
946 $url = preg_replace('/%/', '.*?', preg_quote($url, '/'));
947 $pattern = '/^'. $url .'$/';
948 if (preg_match($pattern, $_REQUEST['q'])) {
949 return 1;
950 }
951 }
952 }
953
954 return 0;
955 }
956
957 /**
958 * Theme function for displaying the lightbox2 trigger image in an imagefield.
959 */
960 function theme_lightbox2_image($path, $alt = '', $title = '', $attributes = NULL) {
961 $attributes = drupal_attributes($attributes);
962 $image = '<img src="'. file_create_url($path) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
963 return $image;
964 }
965
966 /**
967 * Implementation of the imagecache_create_url() function for integration with
968 * imagecache module versions prior to imagecache 2.
969 */
970 function lightbox2_imagecache_create_url($preset, $filepath) {
971 if (function_exists('imagecache_create_url')) {
972 return imagecache_create_url($preset, $filepath);
973 }
974 else {
975 $path = _imagecache_strip_file_directory($filepath);
976 return file_create_url(file_directory_path() .'/imagecache/'. $preset .'/'. $path);
977 }
978 }