You are here

thickbox.module in Thickbox 5

Same filename and directory in other branches
  1. 6 thickbox.module

Author: Fredrik Jonsson fredrik at combonet dot se The thickbox module is a simple wrapper for the jquery plugin ThickBox http://jquery.com/demo/thickbox/.

File

thickbox.module
View source
<?php

/**
 * @file
 * Author: Fredrik Jonsson fredrik at combonet dot se
 * The thickbox module is a simple wrapper for the jquery plugin
 * ThickBox http://jquery.com/demo/thickbox/.
 */

/**
 * Menu callback for admin_settings.
 */
function thickbox_admin_settings() {
  if (module_exists('image')) {
    $form['thickbox_imagemodule'] = array(
      '#type' => 'fieldset',
      '#title' => t('Image module options'),
    );
    $form['thickbox_imagemodule']['thickbox_auto'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable for image nodes'),
      '#default_value' => variable_get('thickbox_auto', 0),
      '#description' => t('Automatically activate Thickbox for all image nodes (requires the image module).'),
    );
    $options = array();
    $sizes = image_get_sizes();
    foreach ($sizes as $label => $size) {
      $options[$label] = $size['label'];
    }
    $form['thickbox_imagemodule']['thickbox_derivative'] = array(
      '#type' => 'select',
      '#title' => t('Image derivative'),
      '#options' => $options,
      '#default_value' => variable_get('thickbox_derivative', 'preview'),
      '#description' => t('Select which image derivative will be loaded.'),
    );
  }
  if (module_exists('imagefield')) {
    $form['thickbox_imagefield'] = array(
      '#type' => 'fieldset',
      '#title' => t('Image field options (CCK)'),
    );
    $form['thickbox_imagefield']['thickbox_imagefield_gallery'] = array(
      '#type' => 'radios',
      '#title' => t('Image field gallery'),
      '#default_value' => variable_get('thickbox_imagefield_gallery', 1),
      '#options' => array(
        0 => t('Per page gallery'),
        1 => t('Per post gallery'),
        2 => t('Per field gallery'),
        3 => t('No gallery'),
      ),
      '#description' => t('Should the gallery be images within a single field, a single post (default) or all images on the page. The last option disabels gallerys.'),
    );
  }
  $form['thickbox_login_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Login settings'),
  );
  $form['thickbox_login_settings']['thickbox_login'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable for login links'),
    '#default_value' => variable_get('thickbox_login', 0),
    '#description' => t('Automatically activate Thickbox for links to user/login.'),
  );
  $form['thickbox_advanced_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['thickbox_advanced_settings']['thickbox_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Deactivate Thickbox on specific pages'),
    '#default_value' => variable_get('thickbox_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>',
    )),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_menu().
 */
function thickbox_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/thickbox',
      'title' => t('Thickbox'),
      'description' => t('Configure Thickbox behavior.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'thickbox_admin_settings',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'thickbox_login',
      'title' => t('Login'),
      'callback' => 'thickbox_login',
      'access' => !$user->uid,
      'type' => MENU_CALLBACK,
    );
  }
  else {

    // Code from the block_list funtion in block.module.
    // If the path doesn't match any of the exeptions, load header files.
    $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('thickbox_pages', ''), '/')) . ')$/';

    // 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']);
    }
    if (!$page_match) {
      _thickbox_doheader();
    }
  }
  return $items;
}

/**
 * Menu callback for thickbox_login.
 */
function thickbox_login() {
  print drupal_get_form('user_login');
  exit;
}

/**
 * Loads the various js and css files.
 */
function _thickbox_doheader() {
  global $user;
  $path = drupal_get_path('module', 'thickbox');
  drupal_add_css($path . '/thickbox.css');

  // Insert translated strings as javascript settings.
  $tb_msg = array(
    'close' => t('Close'),
    'next' => t('Next >'),
    'prev' => t('< Prev'),
    'esc_key' => t('or Esc Key'),
    'next_close' => t('Next / Close on last'),
    'image_count' => t('Image !current of !total'),
  );
  drupal_add_js(array(
    'thickbox' => $tb_msg,
  ), 'setting');
  if ($user->uid == 0 && variable_get('thickbox_login', 0)) {
    drupal_add_js($path . '/thickbox_login.js');
  }
  if (variable_get('thickbox_auto', 0) && module_exists('image')) {
    drupal_add_js(array(
      'thickbox' => array(
        'derivative' => variable_get('thickbox_derivative', 'preview'),
      ),
    ), 'setting');
    drupal_add_js($path . '/thickbox_auto.js');
  }
  drupal_add_js($path . '/thickbox.js');
}

/**
 * Implementation of hook_form_alter().
 * Reformat the login form.
 */
function thickbox_form_alter($form_id, &$form) {
  if ($form_id == 'user_login' && arg(0) == 'thickbox_login') {
    $form['#action'] = url('user/login', 'destination=' . $_GET['destination']);
    $form['name']['#size'] = 25;
    $form['pass']['#size'] = 25;
  }
}

/**
 * Implementation of hook_field_formatter_info().
 * Adds certain thickbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
 */
function thickbox_field_formatter_info() {
  $formatters = array();
  if (module_exists('imagefield') && module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
      $formatters['thickbox][' . $preset['presetname']] = array(
        'label' => 'Thickbox: ' . $preset['presetname'],
        'field types' => array(
          'image',
        ),
      );
    }
  }
  return $formatters;
}

/**
 * Implementation of hook_field_formatter().
 */
function thickbox_field_formatter($field, $item, $formatter, $node = NULL) {
  if (module_exists('imagefield') && module_exists('imagecache') && !empty($item['fid'])) {
    if (!isset($item['filepath'])) {
      $file = _imagefield_file_load($item['fid']);
      $item['filepath'] = $file['filepath'];
    }
    if (strpos($formatter, 'thickbox][') !== FALSE) {
      list($null, $namespace) = explode('][', $formatter, 2);
      $nid = $item['nid'] ? $item['nid'] : ($node->nid ? $node->nid : '');
      switch (variable_get('thickbox_imagefield_gallery', 1)) {
        case 0:
          $gallery_id = 'all';
          break;
        case 1:
          $gallery_id = $nid;
          break;
        case 2:
          $gallery_id = $nid . '-' . $field['field_name'];
          break;
        case 3:
          $gallery_id = $nid . '-' . $item['fid'];
          break;
      }
      if ($preset = imagecache_preset_by_name($namespace)) {
        return theme('imagefield_image_imagecache_thickbox', $namespace, $field, $item['filepath'], $item['alt'], $item['title'], $gallery_id);
      }
    }
  }
}

/**
 * Implementation of theme_imagefield_image_imagecache_thickbox().
 */
function theme_imagefield_image_imagecache_thickbox($namespace, $field, $path, $alt = '', $title = '', $gid = '', $attributes = NULL) {
  if (!empty($path)) {
    $attributes = drupal_attributes($attributes);
    $imagecache_path = imagecache_create_url($namespace, $path);
    $image = '<img src="' . $imagecache_path . '" alt="' . check_plain($alt) . '" ' . $attributes . ' />';
    return l($image, file_create_url($path), array(
      'title' => $title,
      'class' => 'thickbox',
      'rel' => 'gallery-' . $gid,
    ), NULL, NULL, FALSE, TRUE);
  }
}

Functions

Namesort descending Description
theme_imagefield_image_imagecache_thickbox Implementation of theme_imagefield_image_imagecache_thickbox().
thickbox_admin_settings Menu callback for admin_settings.
thickbox_field_formatter Implementation of hook_field_formatter().
thickbox_field_formatter_info Implementation of hook_field_formatter_info(). Adds certain thickbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
thickbox_form_alter Implementation of hook_form_alter(). Reformat the login form.
thickbox_login Menu callback for thickbox_login.
thickbox_menu Implementation of hook_menu().
_thickbox_doheader Loads the various js and css files.