You are here

shadowbox.module in Shadowbox 5

Shadowbox, a JavaScript media viewer application for displaying content in a modal dialogue.

File

shadowbox.module
View source
<?php

/**
 * @file
 * Shadowbox, a JavaScript media viewer application for displaying content in a
 * modal dialogue.
 */

/**
 * Implementation of hook_help().
 */
function shadowbox_help($section) {
  switch ($section) {
    case 'admin/help#shadowbox':
      $output = '<p>' . t('Shadowbox is a modal media viewer application akin to !lightbox_project and !thickbox_project. You can use it to display images, movies, and other web content in a window that is overlaid on top of the originating page.', array(
        '!lightbox_project' => l('lightbox2', 'http://drupal.org/project/lightbox2', array(), NULL, NULL, TRUE),
        '!thickbox_project' => l('thickbox', 'http://drupal.org/project/thickbox', array(), NULL, NULL, TRUE),
      )) . '</p>';
      $output .= '<h3>' . t('From the author of Shadowbox:') . '</h3>';
      $output .= '<blockquote>' . t('Shadowbox is a cross-browser, cross-platform, cleanly-coded and fully-documented media viewer application written entirely in JavaScript. Using Shadowbox, website authors can display a wide assortment of media in all major browsers without navigating away from the linking page.') . '</blockquote>';
      $output .= '<p>' . t('This module provides integration with imagefield 2, imagecache 2 and views. It has !global_settings that work on a site-wide basis. Future versions of this module will integrate with image, emfield and other media modules.', array(
        '!global_settings' => l('global settings', 'admin/settings/shadowbox'),
      )) . '</p>';
      $output .= '<h3>' . t('Usage:') . '</h3>';
      $output .= '<p>' . t('Integration with Imagefield, Imagecache and Views is provided so the usual options are available. To manually enable Shadowbox for a link you must add a <strong><code>rel</code></strong> attribute with a value of <strong><code>shadowbox</code></strong> (for compatibility with lightbox you can also use a value of <strong><code>lightbox</code></strong>).') . '</p>';
      $output .= '<strong>' . t('Single image:') . '</strong>';
      $output .= '<p>' . t('The important thing to note here is the <code>rel</code> attribute. This tells Shadowbox you wish to activate it for this link. The <code>title</code> attribute is optional and can be used to give a caption to the instance.') . '</p>';
      $output .= '<p>' . t('<code><pre>&lt;a href=&quot;/files/image1.jpg&quot; rel=&quot;shadowbox&quot; title=&quot;Caption&quot;&gt;Image&lt;/a&gt;</pre></code>') . '</p>';
      $output .= '<strong>' . t('Multiple instances (gallery mode):') . '</strong>';
      $output .= '<p>' . t('Media can be grouped into a gallery using <code>shadowbox[name]</code> where <code>name</code> is any name you wish to apply. The value of <code>[name]</code> is only used internally by Shadowbox and won\'t be shown to the user.') . '</p>';
      $output .= '<p>' . t('<code><pre>&lt;a href=&quot;/files/image1.jpg&quot; rel=&quot;shadowbox[gallery]&quot; title=&quot;Caption 1&quot;&gt;Image&lt;/a&gt;
&lt;a href=&quot;/files/image2.jpg&quot; rel=&quot;shadowbox[gallery]&quot; title=&quot;Caption 2&quot;&gt;Image&lt;/a&gt;
&lt;a href=&quot;/files/image3.jpg&quot; rel=&quot;shadowbox[gallery]&quot; title=&quot;Caption 3&quot;&gt;Image&lt;/a&gt;</pre></code>') . '</p>';
      $output .= '<strong>' . t('External URL:') . '</strong>';
      $output .= '<p>' . t('A powerful feature of Shadowbox is the ability to open web pages directly on top of the current page. Try out the example and see how the site still functions as normal on top of the originating page.') . '</p>';
      $output .= '<p>' . t('<code><pre>&lt;a href=&quot;http://google.com&quot; rel=&quot;shadowbox&quot; title=&quot;Google&quot;&gt;Google&lt;/a&gt;</pre></code>') . '<p>';
      $output .= '<h3>' . t('Advanced usage:') . '</h3>';
      $output .= '<p>' . t('Shadowbox allows you to specify options on a per-instance basis by using a JSON-formatted parameter. More information on this feature can be found on the !shadowbox_usage', array(
        '!shadowbox_usage' => l('Shadowbox usage page', 'http://mjijackson.com/shadowbox/doc/usage.html#markup', array(), NULL, NULL, TRUE),
      )) . '</p>';
      return $output;
    case 'admin/settings/shadowbox':
      return '<p>' . t('This page provides access to the Shadowbox settings. The settings work globally so any changes made here will affect Shadowbox for the entire site.') . '</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function shadowbox_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/shadowbox',
      'title' => 'Shadowbox',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'shadowbox_admin_settings',
      ),
      'access' => user_access('administer shadowbox'),
      'description' => 'Configure the settings for Shadowbox',
      'type' => MENU_NORMAL_ITEM,
    );
  }
  else {
    shadowbox_construct_header();
  }
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function shadowbox_perm() {
  return array(
    'administer shadowbox',
  );
}

/**
 * FAPI definition for the Shadowbox admin settings form.
 *
 * @ingroup forms
 * @see shadowbox_admin_settings_validate()
 */
function shadowbox_admin_settings() {

  // Global settings
  $form['shadowbox'] = array(
    '#type' => 'fieldset',
    '#title' => t('Global settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['shadowbox']['shadowbox_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Shadowbox'),
    '#default_value' => variable_get('shadowbox_enabled', TRUE),
    '#description' => t('Check this box to enable Shadowbox for the site.'),
  );
  $form['shadowbox']['shadowbox_css_skin'] = array(
    '#type' => 'select',
    '#title' => t('Skin'),
    '#multiple' => FALSE,
    '#description' => t('Select the skin to use for the Shadowbox overlay.'),
    '#options' => array(
      'original' => 'Original',
      'light' => 'Light',
    ),
    '#default_value' => variable_get('shadowbox_css_skin', 'original'),
  );
  $form['shadowbox']['shadowbox_excluded_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Deactivate Shadowbox on specific pages'),
    '#default_value' => variable_get('shadowbox_excluded_pages', "admin*\nimg_assist*\nnode/add/*\nnode/*/edit"),
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // Supported media
  $form['shadowbox']['shadowbox_supported_media'] = array(
    '#type' => 'fieldset',
    '#title' => t('Supported media'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_supported_media']['shadowbox_supported_images'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Image'),
    '#description' => t('Supported image file extensions'),
    '#options' => drupal_map_assoc(shadowbox_get_types('image')),
    '#default_value' => variable_get('shadowbox_supported_images', shadowbox_get_types('image')),
  );
  $form['shadowbox']['shadowbox_supported_media']['shadowbox_supported_quicktime'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Quicktime'),
    '#description' => t('Movie file extensions supported by QuickTime'),
    '#options' => drupal_map_assoc(shadowbox_get_types('quicktime')),
    '#default_value' => variable_get('shadowbox_supported_quicktime', shadowbox_get_types('quicktime')),
  );
  $form['shadowbox']['shadowbox_supported_media']['shadowbox_supported_wmp'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Windows Media Player'),
    '#description' => t('Movie file extensions supported by Windows Media Player'),
    '#options' => drupal_map_assoc(shadowbox_get_types('wmp')),
    '#default_value' => variable_get('shadowbox_supported_wmp', shadowbox_get_types('wmp')),
  );
  $form['shadowbox']['shadowbox_supported_media']['shadowbox_supported_qtwmp'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Quicktime and Windows Media Player'),
    '#description' => t('Movie file extensions supported by both QuickTime and Windows Media Player'),
    '#options' => drupal_map_assoc(shadowbox_get_types('qtwmp')),
    '#default_value' => variable_get('shadowbox_supported_qtwmp', shadowbox_get_types('qtwmp')),
  );
  $form['shadowbox']['shadowbox_supported_media']['shadowbox_supported_iframe'] = array(
    '#type' => 'checkboxes',
    '#title' => t('IFrame'),
    '#description' => t('File extensions that will be display in an iframe'),
    '#options' => drupal_map_assoc(shadowbox_get_types('iframe')),
    '#default_value' => variable_get('shadowbox_supported_iframe', array(
      'asp',
      'aspx',
      'cgi',
      'cfm',
      'htm',
      'html',
      'pl',
      'php',
      'php3',
      'php4',
      'php5',
      'phtml',
      'rb',
      'rhtml',
      'shtml',
      'vbs',
    )),
  );

  // Options
  $form['shadowbox']['shadowbox_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#description' => t('Shadowbox is highly configurable, but can also be used with little to no configuration at all. The following options may be used to configure Shadowbox on a site-wide basis.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Assets
  $form['shadowbox']['shadowbox_options']['asset_paths'] = array(
    '#type' => 'fieldset',
    '#title' => t('Assets'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['asset_paths']['shadowbox_asset_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Base asset URL'),
    '#description' => t('The path to the assets (loading image, FLV player, and background image). Relative to Drupal root e.g. <code>sites/all/modules/shadowbox/shadowbox</code>.'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_asset_url', drupal_get_path('module', 'shadowbox') . '/shadowbox'),
  );
  $form['shadowbox']['shadowbox_options']['asset_paths']['shadowbox_loading_image'] = array(
    '#type' => 'textfield',
    '#title' => t('Loading image path'),
    '#description' => t('The URL of an image to use as a loading indicator while loading content.'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_loading_image', 'images/loading.gif'),
  );
  $form['shadowbox']['shadowbox_options']['asset_paths']['shadowbox_flv_player'] = array(
    '#type' => 'textfield',
    '#title' => t('FLV player'),
    '#description' => t('The URL of the flash video player.'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_flv_player', 'flvplayer.swf'),
  );

  // Animation
  $form['shadowbox']['shadowbox_options']['animation_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Animation'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_animate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable animation'),
    '#description' => t('Enable all fancy dimension and opacity animations. Disabling this option can improve the overall effect on computers with poor performance.'),
    '#default_value' => variable_get('shadowbox_animate', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_animation_sequence'] = array(
    '#type' => 'select',
    '#title' => t('Animation sequence'),
    '#multiple' => FALSE,
    '#description' => t('The animation sequence to use when resizing Shadowbox.'),
    '#options' => array(
      'wh' => t('Width then height'),
      'hw' => t('Height then width'),
      'sync' => t('Simultaneously'),
    ),
    '#default_value' => variable_get('shadowbox_animation_sequence', 'wh'),
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_resize_duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Resize duration'),
    '#description' => t('The duration (in seconds) of the resizing animations.'),
    '#size' => 5,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_resize_duration', 0.35),
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_fade_duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Fade duration'),
    '#description' => t('The duration (in seconds) of the overlay fade animation.'),
    '#size' => 5,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_fade_duration', 0.35),
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_initial_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Initial height'),
    '#description' => t('The height of Shadowbox (in pixels) when it first appears on the screen.'),
    '#size' => 5,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_initial_height', 160),
  );
  $form['shadowbox']['shadowbox_options']['animation_settings']['shadowbox_initial_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Initial width'),
    '#description' => t('The width of Shadowbox (in pixels) when it first appears on the screen. '),
    '#size' => 5,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_initial_width', 320),
  );

  // Overlay
  $form['shadowbox']['shadowbox_options']['overlay'] = array(
    '#type' => 'fieldset',
    '#title' => t('Overlay'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['overlay']['shadowbox_overlay_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Overlay color'),
    '#description' => t('Select a hexadecimal color value for the overlay (e.g. 000 or 000000 for black).'),
    '#size' => 8,
    '#maxlength' => 7,
    '#default_value' => variable_get('shadowbox_overlay_color', '000'),
    '#field_prefix' => '#',
  );
  $form['shadowbox']['shadowbox_options']['overlay']['shadowbox_overlay_opacity'] = array(
    '#type' => 'textfield',
    '#title' => t('Overlay opacity'),
    '#description' => t('The opacity of the overlay. Accepts values between 0 and 1. 0 is fully transparent, 1 is fully opaque.'),
    '#size' => 5,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_overlay_opacity', 0.85),
  );
  $form['shadowbox']['shadowbox_options']['overlay']['shadowbox_background_image'] = array(
    '#type' => 'textfield',
    '#title' => t('Overlay backround image'),
    '#description' => t('The URL of a pre-made image to use for browsers (such as FF Mac) that don\'t support playing movies over backgrounds that are not 100% opaque.'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_background_image', 'images/overlay-85.png'),
  );
  $form['shadowbox']['shadowbox_options']['overlay']['shadowbox_viewport_padding'] = array(
    '#type' => 'textfield',
    '#title' => t('Viewport padding'),
    '#description' => t('The amount of padding (in pixels) to maintain around the edge of the browser window.'),
    '#size' => 4,
    '#maxlength' => 4,
    '#default_value' => variable_get('shadowbox_viewport_padding', 20),
  );

  // Galleries
  $form['shadowbox']['shadowbox_options']['gallery'] = array(
    '#type' => 'fieldset',
    '#title' => t('Galleries'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['gallery']['shadowbox_display_nav'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display gallery navigation.'),
    '#description' => t('Uncheck this option to hide the gallery navigation controls.'),
    '#default_value' => variable_get('shadowbox_display_nav', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['gallery']['shadowbox_continuous_galleries'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable continuous galleries'),
    '#description' => t('Check this option to enable "continuous" galleries. By default, the galleries will not let a user go before the first image or after the last. Enabling this feature will let the user go directly to the first image in a gallery from the last one by selecting "Next".'),
    '#default_value' => variable_get('shadowbox_continuous_galleries', FALSE),
  );
  $form['shadowbox']['shadowbox_options']['gallery']['shadowbox_display_counter'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable counter'),
    '#description' => t('Uncheck this option to hide the gallery counter. Counters are never displayed on elements that are not part of a gallery.'),
    '#default_value' => variable_get('shadowbox_display_counter', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['gallery']['shadowbox_counter_type'] = array(
    '#type' => 'select',
    '#title' => t('Counter type'),
    '#multiple' => FALSE,
    '#description' => t('The mode to use for the gallery counter. May be either \'default\' or \'skip\'. The default counter is a simple \'1 of 5\' message. The skip counter displays a separate link to each piece in the gallery, enabling quick navigation in large galleries.'),
    '#options' => array(
      'default' => t('Default'),
      'skip' => t('Skip'),
    ),
    '#default_value' => variable_get('shadowbox_counter_type', 'default'),
  );

  // Movies
  $form['shadowbox']['shadowbox_options']['movies'] = array(
    '#type' => 'fieldset',
    '#title' => t('Movies'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['movies']['shadowbox_autoplay_movies'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable auto-play movies'),
    '#description' => t('Set this false to disable automatically playing movies when they are loaded.'),
    '#default_value' => variable_get('shadowbox_autoplay_movies', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['movies']['shadowbox_show_movie_controls'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable movie controls'),
    '#description' => t('Set this false to disable displaying QuickTime and Windows Media player movie control bars.'),
    '#default_value' => variable_get('shadowbox_show_movie_controls', TRUE),
  );

  // Input controls
  $form['shadowbox']['shadowbox_options']['input'] = array(
    '#type' => 'fieldset',
    '#title' => t('Input controls'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['input']['shadowbox_overlay_listen'] = array(
    '#type' => 'checkbox',
    '#title' => t('Mouse click closes overlay.'),
    '#description' => t('Set this false to disable listening for mouse clicks on the overlay that will close Shadowbox.'),
    '#default_value' => variable_get('shadowbox_overlay_listen', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['input']['keyboard'] = array(
    '#type' => 'fieldset',
    '#title' => t('Keyboard controls'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['input']['keyboard']['shadowbox_enable_keys'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable keys'),
    '#description' => t('Set this false to disable keyboard navigation of galleries.'),
    '#default_value' => variable_get('shadowbox_enable_keys', TRUE),
  );
  $form['shadowbox']['shadowbox_options']['input']['keyboard']['shadowbox_keys_close'] = array(
    '#type' => 'textfield',
    '#title' => t('Close keys'),
    '#description' => t('A list of keys (or key codes) that a user may use to close Shadowbox. Values should be separated by a space. Defaults to \'c q 27\' (c, q, or esc).'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_keys_close', 'c q 27'),
  );
  $form['shadowbox']['shadowbox_options']['input']['keyboard']['shadowbox_keys_previous'] = array(
    '#type' => 'textfield',
    '#title' => t('Previous keys'),
    '#description' => t('An list of keys (or key codes) that a user may use to skip to the previous piece in the gallery. Values should be separated by a space. Defaults to \'p, 37\' (p or left arrow).'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_keys_previous', 'p 37'),
  );
  $form['shadowbox']['shadowbox_options']['input']['keyboard']['shadowbox_keys_next'] = array(
    '#type' => 'textfield',
    '#title' => t('Next keys'),
    '#description' => t('A list of keys (or key codes) that a user may use to skip to the next piece in the gallery. Values should be separated by a space. Defaults to \'n, 39\' (n or right arrow).'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('shadowbox_keys_next', 'n 39'),
  );

  // Media handling
  $form['shadowbox']['shadowbox_options']['media_handling'] = array(
    '#type' => 'fieldset',
    '#title' => t('Media handling'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['shadowbox']['shadowbox_options']['media_handling']['shadowbox_handle_large_images'] = array(
    '#type' => 'select',
    '#title' => t('Handle large images'),
    '#multiple' => FALSE,
    '#description' => t('The mode to use for handling images that are too large for the viewport. May be one of "none", "resize", or "drag". The "none" setting will not alter the image dimensions, though clipping will occur. Setting this to "resize" enables on-the-fly resizing of large images. In this mode, the height and width of large images will be adjusted so that they may still be viewed in their entirety while maintaining the original aspect ratio. The "drag" mode will display the image at its original resolution, but will allow the user to drag the image within the view to see portions that may be clipped.'),
    '#options' => array(
      'resize' => t('Resize'),
      'drag' => t('Drag'),
      'none' => t('None'),
    ),
    '#default_value' => variable_get('shadowbox_handle_large_images', 'resize'),
  );
  $form['shadowbox']['shadowbox_options']['media_handling']['shadowbox_handle_unsupported'] = array(
    '#type' => 'select',
    '#title' => t('Handle unsupported'),
    '#multiple' => FALSE,
    '#description' => t('The mode to use for handling unsupported media. May be either <strong>link</strong> or <strong>remove</strong>. Media are unsupported when the browser plugin required to display the media properly is not installed. The link option will display a user-friendly error message with a link to a page where the needed plugin can be downloaded. The remove option will simply remove any unsupported gallery elements from the gallery before displaying it. With this option, if the element is not part of a gallery, the link will simply be followed.'),
    '#options' => array(
      'link' => t('Link'),
      'remove' => t('Remove'),
    ),
    '#default_value' => variable_get('shadowbox_handle_unsupported', 'link'),
  );
  return system_settings_form($form);
}

/**
 * Validate admin settings form inputs.
 */
function shadowbox_admin_settings_validate($form_id, &$form) {
  global $form_values;
  $asset_url = $form_values['shadowbox_asset_url'];
  $loading_image = $form_values['shadowbox_loading_image'];
  $flv_player = $form_values['shadowbox_flv_player'];
  $resize_duration = $form_values['shadowbox_resize_duration'];
  $fade_duration = $form_values['shadowbox_fade_duration'];
  $initial_height = $form_values['shadowbox_initial_height'];
  $initial_width = $form_values['shadowbox_initial_width'];
  $color = $form_values['shadowbox_overlay_color'];
  $opacity = $form_values['shadowbox_overlay_opacity'];
  $background_image = $form_values['shadowbox_background_image'];
  $viewport_padding = $form_values['shadowbox_viewport_padding'];
  $keys_close = $form_values['shadowbox_keys_close'];
  $keys_previous = $form_values['shadowbox_keys_previous'];
  $keys_next = $form_values['shadowbox_keys_next'];
  if (!preg_match('!^([a-z0-9_\\-.]+/)*[a-z0-9_\\-]+$!i', $asset_url)) {
    form_set_error('shadowbox_asset_url', t('You must enter a valid path in the form <code>path/to/asset</code> (no leading or trailing slashes).'));
  }
  if (!preg_match('!^([a-z0-9_\\-]+/)*[a-z0-9_\\-]+\\.gif$!i', $loading_image)) {
    form_set_error('shadowbox_loading_image', t('You must enter a valid path to an animated GIF image in the form <code>images/image.gif</code>.'));
  }
  elseif (!file_exists($asset_url . '/' . $loading_image)) {
    form_set_error('shadowbox_loading_image', t('The specified image does not exist.'));
  }
  if (!preg_match('!^([a-z0-9_\\-]+/)*[a-z0-9_\\-]+\\.swf$!i', $flv_player)) {
    form_set_error('shadowbox_flv_player', t('You must enter a valid path to an SWF Flash Player in the form <code>flvplayer.swf</code>.'));
  }
  elseif (!file_exists($asset_url . '/' . $flv_player)) {
    form_set_error('shadowbox_flv_player', t('The FLV player does not exist.'));
  }
  if (!is_numeric($resize_duration) || $resize_duration < 0 || $resize_duration > 10) {
    form_set_error('shadowbox_resize_duration', t('You must enter a number between 0 and 10.'));
  }
  if (!is_numeric($fade_duration) || $fade_duration < 0 || $fade_duration > 10) {
    form_set_error('shadowbox_fade_duration', t('You must enter a number between 0 and 10.'));
  }
  if (!is_numeric($initial_height)) {
    form_set_error('shadowbox_initial_height', t('You must enter a number.'));
  }
  else {
    $form_values['shadowbox_initial_height'] = floor($initial_height);
  }
  if (!is_numeric($initial_width)) {
    form_set_error('shadowbox_initial_width', t('You must enter a number.'));
  }
  else {
    $form_values['shadowbox_initial_width'] = floor($initial_width);
  }
  if (!_validate_hex_color($color)) {
    form_set_error('shadowbox_overlay_color', t('You must enter a properly formed hex value (e.g. 000 or 000000 for black.)'));
  }
  if ($opacity == '' || floor($opacity) != 0 && $opacity != 1) {
    form_set_error('shadowbox_overlay_opacity', t('You must enter a decimal number between 0 and 1.'));
  }
  if (!preg_match('!^([a-z0-9_\\-]+/)*[a-z0-9_\\-]+\\.png$!i', $background_image)) {
    form_set_error('shadowbox_background_image', t('You must enter a valid path to a PNG image in the form <code>images/overlay-85.png</code>.'));
  }
  elseif (!file_exists($asset_url . '/' . $background_image)) {
    form_set_error('shadowbox_background_image', t('The specified image does not exist.'));
  }
  if (!is_numeric($viewport_padding) || $viewport_padding < 0 || $viewport_padding > 200) {
    form_set_error('shadowbox_viewport_padding', t('You must enter a number between 0 and 200.'));
  }
  else {
    $form_values['shadowbox_viewport_padding'] = (int) $viewport_padding;
  }
  $keys_message = t('You must enter a properly formed string of keys or keycodes seprated by a space.');
  if (_validate_keys_string($keys_close)) {
    form_set_error('shadowbox_keys_close', $keys_message);
  }
  if (_validate_keys_string($keys_previous)) {
    form_set_error('shadowbox_keys_previous', $keys_message);
  }
  if (_validate_keys_string($keys_next)) {
    form_set_error('shadowbox_keys_next', $keys_message);
  }
}

/**
 * Implementation of hook_field_formatter_info().
 *
 * Add certain shadowbox+imagecache formatters to CCK image fields if the
 * imagefield.module and the imagecache.module exist.
 */
function shadowbox_field_formatter_info() {
  $formatter = array();
  if (module_exists('imagefield') && module_exists('imagecache')) {
    $rules = array();
    if (function_exists('imagecache_presets')) {
      $presets = imagecache_presets();
      foreach ($presets as $preset_id => $preset_info) {
        $rules[$preset_id] = $preset_info['presetname'];
      }
    }
    else {
      $rules = _imagecache_get_presets();
    }
    foreach ($rules as $ruleid => $view_rule) {
      $formatters['shadowbox][original][' . $view_rule] = array(
        'label' => 'shadowbox: original to ' . $view_rule,
        'field types' => array(
          'image',
        ),
      );
      $formatters['shadowbox][' . $view_rule . '][original'] = array(
        'label' => 'shadowbox: ' . $view_rule . ' to original',
        'field types' => array(
          'image',
        ),
      );
      $formatters['shadowbox_gallery][original][' . $view_rule] = array(
        'label' => 'shadowbox gallery: original to ' . $view_rule,
        'field types' => array(
          'image',
        ),
      );
      $formatters['shadowbox_gallery][' . $view_rule . '][original'] = array(
        'label' => 'shadowbox gallery: ' . $view_rule . ' to original',
        'field types' => array(
          'image',
        ),
      );
      foreach ($rules as $rid => $shadowbox_rule) {
        $formatters['shadowbox][' . $view_rule . '][' . $shadowbox_rule] = array(
          'label' => 'shadowbox: ' . $view_rule . ' to ' . $shadowbox_rule,
          'field types' => array(
            'image',
          ),
        );
        $formatters['shadowbox_gallery][' . $view_rule . '][' . $shadowbox_rule] = array(
          'label' => 'shadowbox gallery: ' . $view_rule . ' to ' . $shadowbox_rule,
          'field types' => array(
            'image',
          ),
        );
      }
    }
  }
  return $formatters;
}

/**
 * Implementation of hook_field_formatter().
 */
function shadowbox_field_formatter($field, $item, $formatter) {
  if (module_exists('imagefield') && module_exists('imagecache')) {
    if (isset($item['filepath'])) {
      $filepath =& $item['filepath'];
    }
    else {
      if (!empty($item['fid'])) {
        $file = _imagefield_file_load($item['fid']);
        $filepath =& $file['filepath'];
      }
      else {
        return '';
      }
    }
    $item = array_merge($item, (array) $file);
    $item['filepath'] = $filepath;
    if (strpos($formatter, 'shadowbox][') !== FALSE || strpos($formatter, 'shadowbox_gallery][') !== FALSE) {
      list($null, $view_preset, $shadowbox_preset) = explode('][', $formatter, 3);
      $item['shadowbox_preset'] = $shadowbox_preset;
      $rules = array();
      if (function_exists('imagecache_presets')) {
        $presets = imagecache_presets();
        foreach ($presets as $preset_id => $preset_info) {
          $rules[$preset_id] = $preset_info['presetname'];
        }
      }
      else {
        $rules = _imagecache_get_presets();
      }
      if ($view_preset == 'original' || in_array($view_preset, (array) $rules)) {
        $gallery = strpos($formatter, 'shadowbox_gallery][') !== FALSE ? TRUE : FALSE;
        return theme('imagefield_image_imagecache_shadowbox', $view_preset, $field, $item, NULL, $gallery);
      }
    }
  }
}

/**
 * Implementation of theme_imagefield_image_imagecache_shadowbox().
 */
function theme_imagefield_image_imagecache_shadowbox($view_preset, $field, $item, $attributes = NULL, $gallery) {
  $filepath = $item['filepath'];
  $alt = $item['alt'];
  $title = $item['title'];
  $rel = $gallery ? 'shadowbox[' . $view_preset . ']' : 'shadowbox';
  $link_attributes = array(
    'rel' => $rel,
    'title' => $item['title'] . $node_link,
  );
  if ($view_preset == 'original') {
    $image = theme('shadowbox_image', $filepath, $alt, $title, $attributes);
  }
  else {
    $image = theme('imagecache', $view_preset, $filepath, $alt, $item['title'], $attributes);
  }
  if ($item['shadowbox_preset'] == 'original') {
    $output = l($image, file_create_url($filepath), $link_attributes, NULL, NULL, FALSE, TRUE);
  }
  else {
    $output = l($image, shadowbox_imagecache_create_url($item['shadowbox_preset'], $filepath), $link_attributes, NULL, NULL, FALSE, TRUE);
  }
  return $output;
}

/**
 * Theme function for displaying the shadowbox trigger image in an imagefield.
 */
function theme_shadowbox_image($path, $alt = '', $title = '', $attributes = NULL) {
  $attributes = drupal_attributes($attributes);
  $image = '<img src="' . file_create_url($path) . '" alt="' . check_plain($alt) . '" title="' . check_plain($title) . '" ' . $attributes . ' />';
  return $image;
}

/**
 * Implementation of the imagecache_create_url() function for integration with
 * imagecache module versions prior to imagecache 2.
 */
function shadowbox_imagecache_create_url($preset, $filepath) {
  if (function_exists('imagecache_create_url')) {
    return imagecache_create_url($preset, $filepath);
  }
  else {
    $path = _imagecache_strip_file_directory($filepath);
    return file_create_url(file_directory_path() . '/imagecache/' . $preset . '/' . $path);
  }
}

/**
 * Build the Shadowbox header by adding the necessary CSS and JS files.
 */
function shadowbox_construct_header() {
  $path = drupal_get_path('module', 'shadowbox');
  if (shadowbox_active() && variable_get('shadowbox_enabled', TRUE)) {
    variable_get('shadowbox_css_skin', 'original') == 'light' ? drupal_add_css($path . "/shadowbox/build/css/shadowbox-light.css") : drupal_add_css($path . "/shadowbox/build/css/shadowbox.css");
    drupal_add_js($path . '/shadowbox/build/js/adapter/shadowbox-jquery.js');
    drupal_add_js($path . '/shadowbox/build/js/shadowbox.js');
    drupal_add_js($path . '/shadowbox.js');
    drupal_add_js(shadowbox_get_settings(), 'setting');
  }
}

/**
 * Construct the JS settings array.
 *
 * @param $override
 *   An array of settings to override global values.
 *
 * @return
 *   An array containing settings to be used in drupal_add_js.
 */
function shadowbox_get_settings($override = array()) {
  $module_path = drupal_get_path('module', 'shadowbox');
  $images = variable_get('shadowbox_supported_images', shadowbox_get_types('image'));
  $quicktime = variable_get('shadowbox_supported_quicktime', shadowbox_get_types('quicktime'));
  $wmp = variable_get('shadowbox_supported_wmp', shadowbox_get_types('wmp'));
  $qtwmp = variable_get('shadowbox_supported_qtwmp', shadowbox_get_types('qtwmp'));
  $iframe = variable_get('shadowbox_supported_iframe', shadowbox_get_types('iframe'));
  $settings = array(
    'asset_url' => base_path() . variable_get('shadowbox_asset_url', $module_path . '/shadowbox') . '/',
    'loading_image' => variable_get('shadowbox_loading_image', 'images/loading.gif'),
    'animation' => variable_get('shadowbox_animate', TRUE),
    'animation_sequence' => variable_get('shadowbox_animation_sequence', 'wh'),
    'overlay_color' => variable_get('shadowbox_overlay_color', '000'),
    'overlay_opacity' => variable_get('shadowbox_overlay_opacity', 0.85),
    'overlay_listen' => variable_get('shadowbox_overlay_listen', TRUE),
    'resize_duration' => variable_get('shadowbox_resize_duration', 0.35),
    'fade_duration' => variable_get('shadowbox_fade_duration', 0.35),
    'display_nav' => variable_get('shadowbox_display_nav', TRUE),
    'flv_player' => variable_get('shadowbox_flv_player', 'flvplayer.swf'),
    'background_image' => variable_get('shadowbox_background_image', 'images/overlay-85.png'),
    'autoplay_movies' => variable_get('shadowbox_autoplay_movies', TRUE),
    'show_movie_controls' => variable_get('shadowbox_show_movie_controls', TRUE),
    'continuous_galleries' => variable_get('shadowbox_continuous_galleries', FALSE),
    'display_counter' => variable_get('shadowbox_display_counter', TRUE),
    'counter_type' => variable_get('shadowbox_counter_type', 'default'),
    'viewport_padding' => variable_get('shadowbox_viewport_padding', 20),
    'initial_height' => variable_get('shadowbox_initial_height', 160),
    'initial_width' => variable_get('shadowbox_initial_width', 320),
    'enable_keys' => variable_get('shadowbox_enable_keys', TRUE),
    'keys_close' => _explode_to_int(variable_get('shadowbox_keys_close', 'c q 27')),
    'keys_previous' => _explode_to_int(variable_get('shadowbox_keys_previous', 'p 37')),
    'keys_next' => _explode_to_int(variable_get('shadowbox_keys_next', 'n 39')),
    'handle_large_images' => variable_get('shadowbox_handle_large_images', 'resize'),
    'handle_unsupported' => variable_get('shadowbox_handle_unsupported', 'link'),
    'extensions_img' => _array_to_string_values($images),
    'extensions_qt' => _array_to_string_values($quicktime),
    'extensions_wmp' => _array_to_string_values($wmp),
    'extensions_qtwmp' => _array_to_string_values($qtwmp),
    'extensions_iframe' => _array_to_string_values($iframe),
    'text_cancel' => t('Cancel'),
    'text_loading' => t('Loading'),
    'text_close' => t('Close'),
    'text_next' => t('Next'),
    'text_previous' => t('Previous'),
    'errors_single' => t('You must install the <a href="{0}">{1}</a> browser plugin to view this content.'),
    'errors_shared' => t('You must install both the <a href="{0}">{1}</a> and <a href="{2}">{3}</a> browser plugins to view this content.'),
    'errors_either' => t('You must install either the <a href="{0}">{1}</a> or the <a href="{2}">{3}</a> browser plugin to view this content.'),
  );
  $settings = array_merge($settings, $override);
  $settings = array(
    'shadowbox' => $settings,
  );
  return $settings;
}

/**
 * Verify that Shadowbox should be active for the current URL.
 *
 * @return
 *   TRUE if Shadowbox should be active for the current page.
 */
function shadowbox_active() {
  $path = drupal_get_path_alias($_GET['q']);
  $regexp = '/^(' . preg_replace(array(
    '/(\\r\\n?|\\n)/',
    '/\\\\\\*/',
    '/(^|\\|)\\\\<front\\\\>($|\\|)/',
  ), array(
    '|',
    '.*',
    '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
  ), preg_quote(variable_get('shadowbox_excluded_pages', "admin*\nimg_assist*\nnode/add/*\nnode/*/edit"), '/')) . ')$/';

  // Compare with the internal and path alias (if any).
  $page_match = preg_match($regexp, $path);
  if ($path != $_GET['q']) {
    $page_match = $page_match || preg_match($regexp, $_GET['q']);
  }
  return !$page_match;
}

/**
 * Helper function to get default values for supported media types.
 *
 * @param $input
 *   The type of supported media options to return.
 *
 * @return
 *   An array with default values for each type.
 */
function shadowbox_get_types($type) {
  switch ($type) {
    case 'image':
      return array(
        'png',
        'jpg',
        'jpeg',
        'gif',
        'bmp',
      );
      break;
    case 'quicktime':
      return array(
        'dv',
        'mov',
        'moov',
        'movie',
        'mp4',
      );
      break;
    case 'wmp':
      return array(
        'asf',
        'wm',
        'wmv',
      );
      break;
    case 'qtwmp':
      return array(
        'avi',
        'mpg',
        'mpeg',
      );
      break;
    case 'iframe':
      return array(
        'asp',
        'aspx',
        'cgi',
        'cfm',
        'htm',
        'html',
        'pdf',
        'pl',
        'php',
        'php3',
        'php4',
        'php5',
        'phtml',
        'rb',
        'rhtml',
        'shtml',
        'txt',
        'vbs',
      );
      break;
  }
}

/**
 * Convert numerical value(s) within a delimited string to integer(s).
 *
 * Explode a space delimited string e.g. 'a b 2' and cast numeric string values
 * to integers.
 *
 * @param $string
 *   A space delimited string.
 *
 * @return
 *   An array containing strings and integers.
 */
function _explode_to_int($string) {
  foreach (explode(' ', $string) as $value) {
    $output[] = is_numeric($value) ? (int) $value : $value;
  }
  return $output;
}

/**
 * Validate a delimited string of keys and/or keycodes.
 *
 * @param $input
 *   A delimited string of keys and/or keycodes to validate.
 *
 * @return
 *   TRUE if $input is valid otherwise FALSE.
 */
function _validate_keys_string($input) {
  foreach (explode(' ', $input) as $value) {
    $items[] = preg_match('!^(?:[a-z]|\\d{1,3})$!i', $value);
  }
  return in_array(FALSE, $items);
}

/**
 * Validate a hex color value.
 *
 * @param $input
 *   The string to validate.
 *
 * @return
 *   TRUE if $input is a valid hex color value (e.g. 000 or 000000)
 */
function _validate_hex_color($input) {
  return preg_match('!^[a-f0-9]{3}([a-f0-9]{3})?$!i', $input);
}

/**
 * Filter values that are not strings from an array.
 *
 * @param $array
 *   An array to filter.
 *
 * @return $output
 *   The filtered array.
 */
function _array_to_string_values($array) {
  foreach ($array as $value) {
    if (is_string($value)) {
      $output[] = $value;
    }
  }
  return $output;
}

Functions

Namesort descending Description
shadowbox_active Verify that Shadowbox should be active for the current URL.
shadowbox_admin_settings FAPI definition for the Shadowbox admin settings form.
shadowbox_admin_settings_validate Validate admin settings form inputs.
shadowbox_construct_header Build the Shadowbox header by adding the necessary CSS and JS files.
shadowbox_field_formatter Implementation of hook_field_formatter().
shadowbox_field_formatter_info Implementation of hook_field_formatter_info().
shadowbox_get_settings Construct the JS settings array.
shadowbox_get_types Helper function to get default values for supported media types.
shadowbox_help Implementation of hook_help().
shadowbox_imagecache_create_url Implementation of the imagecache_create_url() function for integration with imagecache module versions prior to imagecache 2.
shadowbox_menu Implementation of hook_menu().
shadowbox_perm Implementation of hook_perm().
theme_imagefield_image_imagecache_shadowbox Implementation of theme_imagefield_image_imagecache_shadowbox().
theme_shadowbox_image Theme function for displaying the shadowbox trigger image in an imagefield.
_array_to_string_values Filter values that are not strings from an array.
_explode_to_int Convert numerical value(s) within a delimited string to integer(s).
_validate_hex_color Validate a hex color value.
_validate_keys_string Validate a delimited string of keys and/or keycodes.