You are here

beautytips.module in BeautyTips 6

Same filename and directory in other branches
  1. 8 beautytips.module
  2. 6.2 beautytips.module
  3. 7.2 beautytips.module

Provides API for adding beautytips to pages. Adds Beautytips settings page and provides some built in functionality.

File

beautytips.module
View source
<?php

/**
 * @file
 * Provides API for adding beautytips to pages.
 * Adds Beautytips settings page and provides some built in functionality.
 */

/**
 *  Beautytips administration page
 */
function beautytips_admin() {
  $form['beautytips_text'] = array(
    '#type' => 'fieldset',
    '#title' => 'Text Input Tooltips',
  );
  $form['beautytips_text']['beautytips_text_input'] = array(
    '#type' => 'checkbox',
    '#title' => 'Display Text input popups',
    '#default_value' => variable_get('beautytips_text_input', FALSE),
  );
  $form['beautytips_text']['beautytips_position'] = array(
    '#type' => 'radios',
    '#title' => 'Which side should the text popups appear on?',
    '#options' => array(
      'bottom' => t('bottom'),
      'top' => t('top'),
      'left' => t('left'),
      'right' => t('right'),
    ),
    '#default_value' => variable_get('beautytips_position', 'bottom'),
  );
  $form['beautytips_text']['form_id'] = array(
    '#type' => 'fieldset',
    '#title' => 'Restrict text popups to specific forms. (OPTIONAL)',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['beautytips_text']['form_id']['beautytips_form_id'] = array(
    '#type' => 'textfield',
    '#title' => 'Enter the form id(s) to use for text popup beautytips',
    '#description' => t('You need to use Drupal php syntax like page_node_form'),
    '#default_value' => variable_get('beautytips_form_id', ''),
  );
  $form['beautytips_text']['form_id']['beautytips_show_form'] = array(
    '#type' => 'checkbox',
    '#title' => 'Display form_ids',
    '#description' => t("Turn this on if you want to see the names of the form ids to enter above.  The names will appear on the pages where the forms are displayed.") . '<div>' . t("Make sure that you turn it off when you're done retrieving the form_ids.") . '</div>',
    '#default_value' => variable_get('beautytips_show_form', FALSE),
  );
  $form['beautytips_help'] = array(
    '#type' => 'fieldset',
    '#title' => 'Help Link Tooltips',
  );
  $form['beautytips_help']['beautytips_drupal_help'] = array(
    '#type' => 'checkbox',
    '#title' => 'Display Help link popups',
    '#description' => t('Add popups for Drupal help links as well as advanced help links.'),
    '#default_value' => variable_get('beautytips_drupal_help', FALSE),
  );
  $form['beautytips_styles'] = array(
    '#type' => 'radios',
    '#title' => 'Choose a style',
    '#description' => 'Mouse over the radio buttons to see a preview.',
    '#options' => array(
      'default' => t('default'),
      'netflix' => t('Netflix'),
      'facebook' => t('Facebook'),
      'transparent' => t('Transparent with Big Text'),
      'big_green' => t('Green with no border'),
      'google_maps' => t('Google Maps'),
    ),
    '#default_value' => variable_get('beautytips_styles', 'default'),
  );
  $form['#submit'][] = 'beautytips_admin_submit';
  $path = drupal_get_path('module', 'beautytips') . '/js/bt_admin_page.js';
  drupal_add_js($path);
  beautytips_add_beautytips();
  return system_settings_form($form);
}

/**
 *  Clear menu cache and theme registry in case beautytips help tips turned on or off
 */
function beautytips_admin_submit() {
  module_invoke('menu', 'rebuild');
  cache_clear_all('*', 'cache_menu', TRUE);
  drupal_rebuild_theme_registry();
}

/**
 * Implementation of hook_menu.
 */
function beautytips_menu() {
  $items = array();
  $items['admin/settings/beautytips'] = array(
    'title' => 'BeautyTips',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'beautytips_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  $items['beautytips/settings/form_ids'] = array(
    'page callback' => 'beautytips_form_id_off',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback.  Turns off the display of form-ids.
 */
function beautytips_form_id_off() {
  variable_set('beautytips_show_form', FALSE);
  $destination = drupal_get_destination() ? drupal_get_destination() : 'admin/settings/beautytips';
  drupal_goto($destination);
}

/**
 * This is the API.  Call this function to add beautytips.
 * See README.txt for more info.
 */
function beautytips_add_beautytips($options = NULL) {
  static $added = FALSE;
  $settings = array(
    'beautytips' => array(),
  );
  $js_added = drupal_add_js($data = NULL, 'setting');
  if (is_array($options)) {

    // Make sure the settings are not added more than once
    if (is_array($js_added['setting'])) {
      foreach ($js_added['setting'] as $js_setting) {
        if (isset($js_setting['beautytips'])) {
          foreach ($js_setting['beautytips'] as $key => $value) {
            if (isset($options[$key])) {
              unset($options[$key]);
            }
          }
        }
      }
    }
    $settings['beautytips'] = array();
    foreach ($options as $beautytip => $content) {
      if (is_array($content)) {
        $settings['beautytips'][$beautytip] = $content;
        $get_keys = array_keys($content);
        $count = 0;
        for ($i = 0; $i < count($content); $i++) {
          $key = $get_keys[$i];
          if ($key != 'area' && $key != 'additionalJavascript') {
            if ($key != 'list' && $key != 'text') {
              $settings['beautytips'][$beautytip]['list'][$count] = $key;
              $count++;
            }
          }
        }
      }
    }
    if ($added && !empty($settings['beautytips'])) {
      drupal_add_js($settings, 'setting');
    }
  }

  // Add beautytips jQuery plugin
  if (!$added) {
    $path = drupal_get_path('module', 'beautytips');
    drupal_add_js($path . '/js/jquery.bt.min.js');
    drupal_add_css($path . '/js/jquery.bt.css');
    drupal_add_js($path . '/js/beautytips.js');

    //for IE add this
    $expath = $path . '/other_libs/excanvas_r3';
    if (count(file_scan_directory($expath, 'excanvas.js', array(
      '.',
      '..',
      'CVS',
    ), 0, FALSE))) {
      drupal_add_js($expath . '/excanvas.js');
    }
    $settings['beautytips_defaults'] = theme('beautytips_styles');
    drupal_add_js($settings, 'setting');
    $added = TRUE;
  }
}

/**
 * Implementation of hook_theme().
 */
function beautytips_theme() {
  return array(
    'beautytips_styles' => array(
      'arguments' => array(),
    ),
  );
}

/**
 *  See jQuery.bt.js for descriptions of defaults
 *  Returns an array of default settings to be sent 
 *  to the beautytips plugin
 */
function theme_beautytips_styles() {
  $style = variable_get('beautytips_styles', 'default');
  $bt_defaults = array();

  // The default style isn't necessarily needed here.
  switch ($style) {
    case 'default':
      $bt_defaults = array(
        'cssStyles' => array(),
      );
      break;
    case 'netflix':
      $bt_defaults = array(
        'positions' => array(
          0 => 'right',
          1 => 'left',
        ),
        'fill' => '#FFF',
        'padding' => 5,
        //'shadow' => TRUE,  //Reimplement later

        //'shadowBlur' => 12,
        'strokeStyle' => '#B9090B',
        'spikeLength' => 50,
        'spikeGirth' => 60,
        'cornerRadius' => 10,
        'centerPointY' => 0.1,
        'overlap' => -8,
        'cssStyles' => array(
          'fontSize' => '12px',
          'fontFamily' => 'arial,helvetica,sans-serif',
        ),
      );
      break;
    case 'facebook':
      $bt_defaults = array(
        'fill' => '#F7F7F7',
        'padding' => 8,
        'strokeStyle' => '#B7B7B7',
        'cornerRadius' => 0,
        'cssStyles' => array(
          'fontFamily' => '"lucida grande",tahoma,verdana,arial,sans-serif',
          'fontSize' => '11px',
        ),
      );
      break;
    case 'transparent':
      $bt_defaults = array(
        'fill' => 'rgba(0, 0, 0, .8)',
        'padding' => 20,
        'strokeStyle' => '#CC0',
        'strokeWidth' => 3,
        'spikeLength' => 40,
        'spikeGirth' => 40,
        'cornerRadius' => 40,
        'cssStyles' => array(
          'color' => '#FFF',
          'fontWeight' => 'bold',
        ),
      );
      break;
    case 'big_green':
      $bt_defaults = array(
        'fill' => '#00FF4E',
        'padding' => 20,
        'strokeWidth' => 0,
        'spikeLength' => 40,
        'spikeGirth' => 40,
        'cornerRadius' => 15,
        'cssStyles' => array(
          'fontFamily' => '"lucida grande",tahoma,verdana,arial,sans-serif',
          'fontSize' => '14px',
        ),
      );
      break;
    case 'google_maps':
      $bt_defaults = array(
        'positions' => array(
          0 => 'top',
          1 => 'bottom',
        ),
        'fill' => '#FFF',
        'padding' => 15,
        'strokeStyle' => '#ABABAB',
        'strokeWidth' => 1,
        'spikeLength' => 65,
        'spikeGirth' => 40,
        'cornerRadius' => 25,
        'centerPointX' => 0.9,
        'cssStyles' => array(),
      );
      break;
  }
  $bt_defaults['list'] = array_keys($bt_defaults);
  if (isset($bt_defaults['cssStyles'])) {
    $bt_defaults['css_list'] = array_keys($bt_defaults['cssStyles']);
  }
  return $bt_defaults;
}

/**
 * Implementation of hook_form_alter.
 * Adds beautytips for textareas and textfields
 */
function beautytips_form_alter(&$form, $form_state, $form_id) {

  // Show the form id for every form, if enabled.
  if (variable_get('beautytips_show_form', FALSE)) {
    drupal_set_message(t('The form_id is %form_id.  This message should be ' . l(t('turned off'), 'beautytips/settings/form_ids', array(
      'query' => drupal_get_destination(),
    )) . " when finished you're finished checking form_ids.", array(
      '%form_id' => $form_id,
    )));
  }
  $add_bt = TRUE;

  // Check if beautytips have been enabled for this form.
  if (strlen(variable_get('beautytips_form_id', ''))) {
    if (strpos(variable_get('beautytips_form_id', ''), $form_id) === FALSE) {
      $add_bt = FALSE;
    }
  }
  if (variable_get('beautytips_text_input', FALSE)) {
    if ($add_bt) {
      $path = drupal_get_path('module', 'beautytips') . '/js/bt_text_box.js';
      drupal_add_js($path);
      $options = array();
      $options['bt_text_field'] = array(
        'area' => 'input.form-text',
        'trigger' => array(
          0 => 'focus',
          1 => 'blur',
        ),
        'contentSelector' => "\$(this).next('.description').hide().html()",
        'width' => '275px',
        'positions' => array(
          0 => variable_get('beautytips_position', 'bottom'),
        ),
      );
      $options['bt_text_area'] = array(
        'area' => 'textarea.form-textarea',
        'trigger' => array(
          0 => 'focus',
          1 => 'blur',
        ),
        'contentSelector' => "\$(this).parents('.form-item').find('.description').hide().html()",
        'hideContent' => TRUE,
        'width' => '275px',
        'positions' => array(
          0 => variable_get('beautytips_position', 'bottom'),
        ),
      );
      beautytips_add_beautytips($options);
    }
  }
}

/**
 * Implementation of hook_menu_alter()
 */
function beautytips_menu_alter(&$items) {
  if (variable_get('beautytips_drupal_help', FALSE)) {
    $items['admin/help']['page callback'] = 'beautytips_help_main';
  }
}

/**
 * Menu callback; prints a page listing a glossary of Drupal terminology.
 *  Beautytips added for the help links
 */
function beautytips_help_main() {

  //  Add in beautytips on page - admin/help
  $options['bt_drupal_help_page'] = array(
    'area' => '.help-items li a',
    'ajaxPath' => array(
      0 => "\$(this).attr('href')",
      1 => '.clear-block p',
    ),
    'trigger' => array(
      0 => 'mouseover',
      1 => 'click',
    ),
    'width' => 350,
  );
  beautytips_add_beautytips($options);

  //  Add CSS
  $help_path = drupal_get_path('module', 'help');
  drupal_add_css($help_path . '/help.css', 'module', 'all', FALSE);
  $output = '<h2>' . t('Help topics') . '</h2><p>' . t('Help is available on the following items:') . '</p>' . help_links_as_list();
  return $output;
}

/**
 * Implementation of hook_theme_registry_alter
 */
function beautytips_theme_registry_alter(&$theme_registry) {
  if (variable_get('beautytips_drupal_help', FALSE)) {
    if (module_exists('help')) {

      // Override theme_more_help_link for more-help stuff
      $theme_registry['more_help_link']['function'] = 'theme_beautytips_more_help_link';
    }
    if (module_exists('advanced_help')) {

      // Override theme_advanced_help_topic for advanced help
      $theme_registry['advanced_help_topic']['function'] = 'theme_beautytips_advanced_help_topic';
    }
  }
}

/**
 * Returns code that emits the 'more help'-link.
 * Overrides theme_more_help_link
 */
function theme_beautytips_more_help_link($url) {
  $options = array();
  $options['bt_drupal_help'] = array(
    'area' => '.more-help-link a',
    'ajaxPath' => array(
      0 => "\$(this).attr('href')",
      1 => '.clear-block p',
    ),
    'trigger' => array(
      0 => 'mouseover',
      1 => 'click',
    ),
    'width' => 350,
  );
  beautytips_add_beautytips($options);
  return '<div class="more-help-link">' . t('[<a href="@link">more help...</a>]', array(
    '@link' => check_url($url),
  )) . '</div>';
}

/**
 * Override of theme_advanced_help_topic to add Beautytips
 * Display a help icon with a link to view the topic in a popup.
 *
 * @param $module
 *   The module that owns this help topic.
 * @param $topic
 *   The identifier for the topic
 * @param $type
 *   - 'icon' to display the question mark icon
 *   - 'title' to display the topic's title
 *   - any other text to display the text. Be sure to t() it!
 */
function theme_beautytips_advanced_help_topic($module, $topic, $type = 'icon') {
  $info = advanced_help_get_topic($module, $topic);
  if (!$info) {
    return;
  }

  // Add bt to advanced-help links
  $options['bt_advanced_help'] = array(
    'area' => 'a.advanced-help-link',
    'ajaxPath' => array(
      0 => "\$(this).attr('href')",
      1 => '#content-content:not(#content-content.help-navigation)',
    ),
    'trigger' => array(
      0 => 'mouseover',
      1 => 'click',
    ),
    'width' => 380,
  );
  beautytips_add_beautytips($options);
  switch ($type) {
    case 'icon':
      $text = '<span>' . t('Help') . '</span>';
      $class = 'advanced-help-link';
      break;
    case 'title':
      $text = $info['title'];
      $class = 'advanced-help-title';
      break;
    default:
      $class = 'advanced-help-title';
      $text = $type;
      break;
  }
  if (user_access('view advanced help popup')) {
    drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-icon.css');
    return l($text, "help/{$module}/{$topic}", array(
      'attributes' => array(
        'class' => $class,
        'onclick' => "var w=window.open(this.href, 'advanced_help_window', 'width=" . $info['popup width'] . ",height=" . $info['popup height'] . ",scrollbars,resizable'); w.focus(); return false;",
        'title' => $info['title'],
      ),
      'query' => array(
        'popup' => TRUE,
      ),
      'html' => TRUE,
    ));
  }
  else {
    return l($text, "help/{$module}/{$topic}", array(
      'attributes' => array(
        'class' => $class,
        'title' => $info['title'],
      ),
      'html' => TRUE,
    ));
  }
}

Functions

Namesort descending Description
beautytips_add_beautytips This is the API. Call this function to add beautytips. See README.txt for more info.
beautytips_admin Beautytips administration page
beautytips_admin_submit Clear menu cache and theme registry in case beautytips help tips turned on or off
beautytips_form_alter Implementation of hook_form_alter. Adds beautytips for textareas and textfields
beautytips_form_id_off Menu callback. Turns off the display of form-ids.
beautytips_help_main Menu callback; prints a page listing a glossary of Drupal terminology. Beautytips added for the help links
beautytips_menu Implementation of hook_menu.
beautytips_menu_alter Implementation of hook_menu_alter()
beautytips_theme Implementation of hook_theme().
beautytips_theme_registry_alter Implementation of hook_theme_registry_alter
theme_beautytips_advanced_help_topic Override of theme_advanced_help_topic to add Beautytips Display a help icon with a link to view the topic in a popup.
theme_beautytips_more_help_link Returns code that emits the 'more help'-link. Overrides theme_more_help_link
theme_beautytips_styles See jQuery.bt.js for descriptions of defaults Returns an array of default settings to be sent to the beautytips plugin