View source
<?php
$_LIGHTBOX2_INCLUDE = false;
function lightbox2_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Enables Lightbox V2 for Drupal');
case 'admin/help#lightbox2':
return t('<h3>Overview</h3>
<p align="justify">Lightbox JS V2 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 Scriptaculous/Prototype libraries; it is therefore less likely to conflict with anything else.</p>
<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>
<p align="justify"><h3>Usage</h3></p>
<p>Add rel="lightbox" attribute to any link tag to activate the lightbox. For example: </p>
<p><a href="image-1.jpg" rel="lightbox" title="my caption">image #1</a></p>
<p>Optional: Use the title attribute if you want to show a caption.</p>
<p>If you have a set of related images that you would like to group, follow
step one but additionally include a group name between square brackets in the
rel attribute. For example:</p><p><a href="images/image-1.jpg"
rel="lightbox[roadtrip]">image #1</a><br />
<a href="images/image-2.jpg" rel="lightbox[roadtrip]">image
#2</a><br />
<a href="images/image-3.jpg" rel="lightbox[roadtrip]">image
#3</a><br /></p>
<p>No limits to the number of image sets per page or how many images are allowed
in each set. Go nuts!</p>');
break;
}
}
function lightbox2_perm() {
return array(
'administer lightbox2',
);
}
function lightbox2_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/lightbox2',
'title' => t('Lightbox2'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'lightbox2_settings_form',
),
'access' => user_access('administer lightbox2'),
'description' => t('Allows the user to configure the lightbox2 settings'),
);
}
return $items;
}
function lightbox2_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
global $_LIGHTBOX2_INCLUDE;
if (lightbox2_exclude_these_paths() != 1) {
if ($op == 'view' && !$_LIGHTBOX2_INCLUDE) {
lightbox2_add_files();
$_LIGHTBOX2_INCLUDE = true;
}
elseif ($node->type == 'image' && !$_LIGHTBOX2_INCLUDE) {
lightbox2_add_files();
$_LIGHTBOX2_INCLUDE = true;
}
}
}
function lightbox2_settings_form() {
$form['lightbox2_plus_options'] = array(
'#type' => 'fieldset',
'#title' => t('Lightbox2 Plus'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['lightbox2_plus_options']['lightbox2_plus'] = array(
'#type' => 'checkbox',
'#title' => t('Use Lightbox2 Plus'),
'#description' => t('Un-checking this box will enable Lightbox2 Lite.'),
'#default_value' => variable_get('lightbox2_plus', true),
);
$form['image_node_options'] = array(
'#type' => 'fieldset',
'#title' => t('Lightbox2 Plus Image Node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['image_node_options']['lightbox2_image_node'] = array(
'#type' => 'checkbox',
'#title' => t('Enable for Image Nodes'),
'#description' => t('Checking this box will enable automatic URL formatting for Image Nodes.'),
'#default_value' => variable_get('lightbox2_image_node', true),
);
$form['image_node_options']['lightbox2_image_group'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Grouping'),
'#description' => t('Checking this box will enable automatic grouping of Image Nodes on a page. Useful for image galleries.'),
'#default_value' => variable_get('lightbox2_image_group', true),
);
$form['image_node_options']['lightbox2_disable_nested_galleries'] = array(
'#type' => 'checkbox',
'#title' => t('Disable Lightbox for Gallery Lists'),
'#description' => t('Checking this box will disable the lightbox for images in gallery lists, but will still use the lightbox when viewing gallery images.'),
'#default_value' => variable_get('lightbox2_disable_nested_galleries', true),
);
$form['image_node_options']['lightbox2G2_filter'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Gallery 2 Filter'),
'#description' => t('Checking this box will enable the Gallery 2 filter.'),
'#default_value' => variable_get('lightbox2G2_filter', true),
);
$form['image_node_options']['lightbox2_disable_these_urls'] = array(
'#type' => 'textarea',
'#title' => t('Disable LightBox for these urls'),
'#description' => t('List the urls where the lightbox should be disabled, e.g. "node/add/page". Put each url on a separate line. You can use the % character as a wildcard.'),
'#default_value' => variable_get('lightbox2_disable_these_urls', ''),
);
return system_settings_form($form);
}
function lightbox2_filter_tips($delta, $format, $long = false) {
return t('Image links from G2 are formatted for use with Lightbox.V2');
}
if (variable_get('lightbox2G2_filter', true)) {
function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Lightbox filter'),
);
case 'description':
return t('Turns g2_filter links into Lighbox.V2 appropriate links');
case 'process':
$text = ' ' . $text . ' ';
$text = preg_replace('/ShowItem/', 'DownloadItem', $text);
$text = preg_replace('/target=""/', 'rel="lightbox"', $text);
$text = substr($text, 1, -1);
return $text;
default:
return $text;
}
}
}
function lightbox2_add_files() {
$path = drupal_get_path('module', 'lightbox2');
if (variable_get('lightbox2_plus', true)) {
if (function_exists('drupal_add_css')) {
drupal_add_css($path . '/lightbox.css');
}
else {
theme('add_style', $path . '/lightbox.css');
}
if (file_exists($path . '/js/prototype.js')) {
drupal_add_js($path . '/js/prototype.js');
if (variable_get('lightbox2_image_node', true)) {
if (variable_get('lightbox2_image_group', true)) {
if (variable_get('lightbox2_disable_nested_galleries', true)) {
drupal_add_js($path . '/js/image_nodes_galleries.js');
}
else {
drupal_add_js($path . '/js/image_nodes.js');
}
}
else {
if (variable_get('lightbox2_disable_nested_galleries', true)) {
drupal_add_js($path . '/js/image_nodes_nogroup_galleries.js');
}
else {
drupal_add_js($path . '/js/image_nodes_nogroup.js');
}
}
}
drupal_add_js($path . '/js/scriptaculous.js?load=effects');
drupal_add_js($path . '/js/lightbox.js');
}
else {
drupal_set_message(t('The script.aculo.us library is in not installed correctly. Please download from <a href="http://www.stellapowerdesign.net/scriptaculous.zip">http://www.stellapowerdesign.net/scriptaculous-drupal.zip</a>, follow the instructions in the Lightbox V.2 README.TXT file to copy the files to their correct locations.'), 'error');
}
}
else {
if (function_exists('drupal_add_css')) {
drupal_add_css($path . '/lightbox_lite.css');
}
else {
theme('add_style', $path . '/lightbox_lite.css');
}
drupal_add_js($path . '/js/lightbox_lite.js');
}
drupal_add_js($path . '/js/lightningload.js');
}
function lightbox2_field_formatter_info() {
$formatter = array();
if (module_exists('imagefield') && module_exists('imagecache')) {
$rules = _imagecache_get_presets();
foreach ($rules as $ruleid => $rulename) {
$formatters['lightbox2][' . $rulename] = array(
'label' => 'Lightbox2: ' . $rulename,
'field types' => array(
'image',
),
);
}
}
return $formatters;
}
function lightbox2_field_formatter($field, $item, $formatter) {
if (module_exists('imagefield') && module_exists('imagecache')) {
if (!isset($item['fid'])) {
return '';
}
if (!$_LIGHTBOX2_INCLUDE && lightbox2_exclude_these_paths() != 1) {
lightbox2_add_files();
$_LIGHTBOX2_INCLUDE = true;
}
$file = _imagefield_file_load($item['fid']);
$item = array_merge($item, $file);
if (strpos($formatter, 'lightbox2][') !== false) {
list($null, $namespace) = explode('][', $formatter, 2);
$rules = _imagecache_get_presets();
if (in_array($namespace, (array) $rules)) {
return theme('imagefield_image_imagecache_lightbox2', $namespace, $field, $item);
}
}
}
}
function theme_imagefield_image_imagecache_lightbox2($namespace, $field, $item, $attributes = NULL) {
$imagecache_path = file_create_url(file_directory_path() . '/imagecache/' . $namespace . '/' . $item['filepath']);
$rel = 'lightbox';
if (variable_get('lightbox2_image_group', TRUE)) {
$rel = 'lightbox[cck_image_nodes]';
}
$node_link = '';
if (!empty($item['nid'])) {
$node_link = '<br /><br />' . l(t('View Image Information'), 'node/' . $item['nid']);
}
$link_attributes = array(
'rel' => $rel,
'title' => $item['alt'] . $node_link,
);
$image = theme('imagecache', $namespace, $item['filepath'], $item['alt'], $item['title'], $attributes);
$output = l($image, $item['filepath'], $link_attributes, NULL, NULL, FALSE, TRUE);
return $output;
}
function lightbox2_exclude_these_paths() {
$disabled_urls = variable_get('lightbox2_disable_these_urls', '');
if (!empty($disabled_urls)) {
$urls = explode("\n", $disabled_urls);
foreach ($urls as $url) {
$url = rtrim($url);
$url = preg_replace('/%/', '.*?', preg_quote($url, '/'));
$pattern = '/^' . $url . '$/';
if (preg_match($pattern, $_REQUEST['q'])) {
return 1;
}
}
}
return 0;
}