| 1 |
<?php
|
| 2 |
// $Id: jlightbox.module,v 1.11 2009/05/06 23:57:17 sun Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* jLightbox is a port of the Lightbox project using jQuery instead of prototype
|
| 7 |
* and script.acolu.ous libraries. It is based on the famous Lightbox v2 script
|
| 8 |
* by Lokesh Dhakar, but will soon be enhanced with Drupal specific
|
| 9 |
* customizations.
|
| 10 |
*
|
| 11 |
* Learn more about Lightbox v2:
|
| 12 |
* <http://www.huddletogether.com/projects/lightbox2>
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_menu()
|
| 17 |
*/
|
| 18 |
function jlightbox_init() {
|
| 19 |
jlightbox_add();
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Add jLightbox stylesheet and javascript to the output.
|
| 24 |
*/
|
| 25 |
function jlightbox_add() {
|
| 26 |
$path = drupal_get_path('module', 'jlightbox');
|
| 27 |
drupal_add_js(array('jlightbox' => array('path' => url($path, array('absolute' => TRUE)))), 'setting');
|
| 28 |
|
| 29 |
// Theme-specific stylesheet override support.
|
| 30 |
// #178364: path_to_theme() invokes init_theme(), thus it must not be invoked
|
| 31 |
// in block settings to allow initializing of the selected theme. Likewise,
|
| 32 |
// the theme system is initialized later when in maintenance mode.
|
| 33 |
if (strpos($_GET['q'], 'admin/build/block') === FALSE && !variable_get('site_offline', 0)) {
|
| 34 |
$css = path_to_theme() .'/jlightbox.css';
|
| 35 |
if (!file_exists($css)) {
|
| 36 |
$css = $path .'/css/jlightbox.css';
|
| 37 |
}
|
| 38 |
drupal_add_css($css, 'module', 'screen');
|
| 39 |
}
|
| 40 |
|
| 41 |
// Image / Image Assist support.
|
| 42 |
if (module_exists('image') && module_exists('img_assist')) {
|
| 43 |
drupal_add_js($path .'/js/jlightbox_image.js');
|
| 44 |
}
|
| 45 |
|
| 46 |
// Inline support.
|
| 47 |
if (module_exists('inline')) {
|
| 48 |
drupal_add_js($path .'/js/jlightbox_inline.js');
|
| 49 |
}
|
| 50 |
|
| 51 |
// jLightbox needs to be loaded last.
|
| 52 |
drupal_add_js($path .'/js/jlightbox.uncompressed.js');
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Implementation of hook_field_formatter_info().
|
| 57 |
*
|
| 58 |
* Adds a field formatter for CCK image field if both imagefield.module and
|
| 59 |
* imagecache.module exist.
|
| 60 |
*/
|
| 61 |
function jlightbox_field_formatter_info() {
|
| 62 |
$formatters = array();
|
| 63 |
if (!module_exists('imagefield') || !module_exists('imagecache')) {
|
| 64 |
return $formatters;
|
| 65 |
}
|
| 66 |
$rules = array();
|
| 67 |
// ImageCache v2 API.
|
| 68 |
if (function_exists('imagecache_presets')) {
|
| 69 |
$presets = imagecache_presets();
|
| 70 |
foreach ($presets as $preset_id => $preset_info) {
|
| 71 |
$rules[$preset_id] = $preset_info['presetname'];
|
| 72 |
}
|
| 73 |
}
|
| 74 |
// ImageCache v1 API (deprecated).
|
| 75 |
else {
|
| 76 |
$rules = _imagecache_get_presets();
|
| 77 |
}
|
| 78 |
foreach ($rules as $ruleid => $rulename) {
|
| 79 |
$formatters['jlightbox]['. $rulename .'][gallery'] = array(
|
| 80 |
'label' => 'jLightbox: '. $rulename .' gallery',
|
| 81 |
'field types' => array('image', 'filefield'),
|
| 82 |
);
|
| 83 |
$formatters['jlightbox]['. $rulename .'][single'] = array(
|
| 84 |
'label' => 'jLightbox: '. $rulename,
|
| 85 |
'field types' => array('image', 'filefield'),
|
| 86 |
);
|
| 87 |
}
|
| 88 |
|
| 89 |
return $formatters;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Implementation of hook_field_formatter().
|
| 94 |
*/
|
| 95 |
function jlightbox_field_formatter($field, $item, $formatter, $node) {
|
| 96 |
if (!module_exists('imagefield') || !module_exists('imagecache')) {
|
| 97 |
return '';
|
| 98 |
}
|
| 99 |
if (!isset($item['fid'])) {
|
| 100 |
return '';
|
| 101 |
}
|
| 102 |
$file = _imagefield_file_load($item['fid']);
|
| 103 |
$item = array_merge($item, $file);
|
| 104 |
if (strpos($formatter, 'jlightbox][') !== FALSE) {
|
| 105 |
list($module, $namespace, $type) = explode('][', $formatter);
|
| 106 |
$rules = array();
|
| 107 |
// ImageCache v2 API.
|
| 108 |
if (function_exists('imagecache_presets')) {
|
| 109 |
$presets = imagecache_presets();
|
| 110 |
foreach ($presets as $preset_id => $preset_info) {
|
| 111 |
$rules[$preset_id] = $preset_info['presetname'];
|
| 112 |
}
|
| 113 |
}
|
| 114 |
// ImageCache v1 API (deprecated).
|
| 115 |
else {
|
| 116 |
$rules = _imagecache_get_presets();
|
| 117 |
}
|
| 118 |
if (in_array($namespace, (array)$rules)) {
|
| 119 |
return theme('imagefield_jlightbox', $namespace, $formatter, $field, $item, $type);
|
| 120 |
}
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Implementation of hook_theme().
|
| 126 |
*/
|
| 127 |
function jlightbox_theme() {
|
| 128 |
return array(
|
| 129 |
'imagefield_jlightbox' => array(
|
| 130 |
'arguments' => array('namespace' => '', 'formatter' => NULL, 'field' => array(), 'item' => array(), 'type' => '', 'attributes' => array()),
|
| 131 |
),
|
| 132 |
);
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Implementation of theme_imagefield_jlightbox().
|
| 137 |
*/
|
| 138 |
function theme_imagefield_jlightbox($namespace, $formatter, $field, $item, $type, $attributes = array()) {
|
| 139 |
$attributes['class'] = 'image '. $namespace;
|
| 140 |
$gallery = '';
|
| 141 |
|
| 142 |
$imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $item['filepath']);
|
| 143 |
$image = theme('imagecache', $namespace, $item['filepath'], $item['alt'], $item['title'], $attributes);
|
| 144 |
|
| 145 |
if ($type == 'gallery') {
|
| 146 |
$gallery = '['. form_clean_id($field['field_name']) .']';
|
| 147 |
}
|
| 148 |
$link_attributes = array(
|
| 149 |
'rel' => 'lightbox'. $gallery,
|
| 150 |
'title' => ($item['alt'] != $item['filename']) ? $item['alt'] : $item['title'],
|
| 151 |
);
|
| 152 |
|
| 153 |
$output = l($image, file_create_url($item['filepath']), array('attributes' => $link_attributes, 'absolute' => FALSE, 'html' => TRUE));
|
| 154 |
|
| 155 |
return $output;
|
| 156 |
}
|
| 157 |
|
| 158 |
|