You are here

beautytips_manager.module in BeautyTips 6.2

Same filename and directory in other branches
  1. 7.2 beautytips_manager.module

File

beautytips_manager.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function beautytips_manager_menu() {
  $items['admin/settings/beautytips/manager'] = array(
    'title' => 'BeautyTips Manager',
    'page callback' => 'beautytips_manager_tips_manager_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/beautytips/manager/%/edit'] = array(
    'title' => 'BeautyTips Manager Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'beautytips_manager_admin',
      4,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/beautytips/manager/%/delete'] = array(
    'title' => 'BeautyTips Manager Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'beautytips_manager_delete_confirm_form',
      4,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/beautytips/manager/add'] = array(
    'title' => 'BeautyTips Manager Add',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'beautytips_manager_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Listing of all beautytips available.
 */
function beautytips_manager_tips_manager_page() {
  $rows = array();
  $empty = '';
  $header = array(
    t('Element'),
    t('Style'),
    t('Visibility'),
    t('Pages'),
    t('operations'),
  );
  $tips = beautytips_manager_get_custom_tips();
  $link = l(t('Add New Custom Beautytip'), 'admin/settings/beautytips/manager/add');
  if (count($tips)) {
    $visibility = array(
      t('Show on every page except the listed pages.'),
      t('Show on only the listed pages.'),
    );
    foreach ($tips as $tip) {
      $tip->pages = check_plain($tip->pages);
      $pages = $tip->pages != substr($tip->pages, 0, 40) ? substr($tip->pages, 0, 40) . '...' : substr($tip->pages, 0, 40);
      $rows[$tip->bid]['element'] = check_plain($tip->element);
      $rows[$tip->bid]['style'] = $tip->style;
      $rows[$tip->bid]['visibility'] = $visibility[$tip->visibility];
      $rows[$tip->bid]['pages'] = $pages;
      $rows[$tip->bid]['edit'] = l(t('edit'), "admin/settings/beautytips/manager/{$tip->bid}/edit");
      $rows[$tip->bid]['delete'] = l(t('delete'), "admin/settings/beautytips/manager/{$tip->bid}/delete");
    }
  }
  else {
    $empty = t('There are no custom beautytips yet.');
  }
  return $link . theme('table', $header, $rows) . $empty;
}

/**
 *  Implementation of hook_init().
 */
function beautytips_manager_init() {
  $options = array();
  $tips = beautytips_manager_get_custom_tips();
  if (count($tips)) {
    foreach ($tips as $tip) {

      // Match path if necessary
      if ($tip->pages) {
        $path = drupal_get_path_alias($_GET['q']);

        // Compare with the internal and path alias (if any).
        $page_match = drupal_match_path($path, $tip->pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($_GET['q'], $tip->pages);
        }

        // When $tip->visibility has a value of 0, the beautytip is displayed on
        // all pages except those listed in $tip->pages. When set to 1, it
        // is displayed only on those pages listed in $tip->pages.
        $page_match = !($tip->visibility xor $page_match);
      }
      else {
        if (!$tip->visibility) {
          $page_match = TRUE;
        }
        else {
          $page_match = FALSE;
        }
      }
      if ($page_match) {
        $options['beautytips_manager_custom_' . $tip->bid] = beautytips_manager_build_beautytip($tip);
      }
    }
  }
  if (count($options)) {
    beautytips_add_beautytips($options);
  }
}

/**
 * Implementation of hook_perm().
 */
function beautytips_manager_perm() {
  return array(
    'use Javascript for custom beautytip display',
  );
}

/**
 * Form for configuring custom beautytips.
 */
function beautytips_manager_admin(&$form_state, $bid = NULL) {
  drupal_add_css(drupal_get_path('module', 'beautytips') . '/css/beautytips.css');
  $tip = beautytips_manager_get_custom_tip($bid);
  $form = array();
  $form['tip'] = array(
    '#type' => 'markup',
    '#value' => '',
    '#tree' => TRUE,
  );
  $form['tip']['bid'] = array(
    '#type' => 'value',
    '#value' => is_object($tip) ? $tip->bid : 0,
  );
  $form['tip']['element'] = array(
    '#type' => 'textfield',
    '#title' => t('Element'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => is_object($tip) ? $tip->element : '',
  );
  $content_options = array(
    'attribute' => 'attribute',
    'text' => 'text',
    'ajax' => 'ajax',
  );
  $description = t('Content to display in the beautytip.  The data entered here depends on the Content Type.');
  $types = array();
  $types[0] = t('attribute - Enter the attribute of the element that should be displayed. (If empty, the title will be selected.)');
  $types[0] .= '<br />' . t('ex. "alt"');
  $types[1] = t('text - Enter the text that should be displayed with in the beautytip.');
  $types[1] .= '<br />' . t('ex. "This is my beautytip!"');
  $types[2] = t('ajax - This will grab the page from the "href" attribute and display that page.  Enter css selectors to narrow the down the content from that page.');
  $types[2] .= '<br />' . t('ex. "#my-id .my-class"');
  if (user_access('use Javascript for custom beautytip display')) {
    $content_options['js'] = 'js';
    $types[3] = 'js - Directly enter javascript to select the content.';
    $types[3] .= '<br />' . t('ex. "$(this).next(\'.description\').html()"');
  }
  $form['tip']['content_type'] = array(
    '#type' => 'radios',
    '#title' => t('Type of Content'),
    '#description' => t('This helps determine from where to pull the content to be displayed.'),
    '#options' => $content_options,
    '#default_value' => is_object($tip) ? $tip->content_type : 0,
  );
  $form['tip']['content'] = array(
    '#type' => 'textarea',
    '#title' => t('Content to Display'),
    '#description' => theme('item_list', $types),
    '#default_value' => is_object($tip) ? $tip->content : '',
  );
  $triggers = beautytips_manager_get_triggers();
  $form['tip']['trigger_on'] = array(
    '#type' => 'select',
    '#title' => t('Trigger On'),
    '#description' => t('Not all events are available for all elements. See jQuery <a href="@events">events documentation</a> for details.', array(
      '@events' => 'http://docs.jquery.com/Events',
    )),
    '#options' => $triggers,
    '#default_value' => is_object($tip) ? $tip->trigger_on : 0,
    '#prefix' => '<div class="beautytips-triggers">',
  );
  $form['tip']['trigger_off'] = array(
    '#type' => 'select',
    '#title' => t('Trigger Off'),
    '#options' => $triggers,
    '#suffix' => '</div>',
    '#default_value' => is_object($tip) ? $tip->trigger_off : 0,
  );
  $styles = beautytips_get_styles();
  foreach ($styles as $key => $style) {
    $style_options[$key] = $key;
  }
  $form['tip']['style'] = array(
    '#type' => 'select',
    '#title' => t('Style'),
    '#options' => $style_options,
    '#default_value' => is_object($tip) ? $tip->style : 'default',
  );
  $form['tip']['shrink'] = array(
    '#type' => 'checkbox',
    '#title' => t('Shrink to Fit'),
    '#description' => t('Shrink the beautytip to the size of the content. This can sometimes help with sizing problems and is good for tips with just one line.'),
    '#default_value' => is_object($tip) ? $tip->shrink : FALSE,
  );
  $positions = is_object($tip) ? explode(',', $tip->positions) : array();
  $form['tip']['positions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Positions'),
    '#description' => t("Optionally enter the order of positions in which you want the tip to appear.  It will use first in order with available space. The last value will be used if others don't have enough space. If no entries, then the tip will be placed in the area with the most space. Only enter an order for those you wish to use"),
    '#tree' => TRUE,
  );
  $positions_list = array(
    'top',
    'bottom',
    'left',
    'right',
  );
  foreach ($positions_list as $position) {
    $form['tip']['positions'][$position] = array(
      '#type' => 'textfield',
      '#title' => t($position),
      '#default_value' => array_search($position, $positions) !== FALSE ? array_search($position, $positions) : '',
      '#size' => 1,
      '#maxlength' => 1,
      '#prefix' => '<div class="beautytips-positions">',
      '#suffix' => '</div>',
    );
  }
  $form['tip']['animation_on'] = array(
    '#type' => 'select',
    '#title' => t('Animation (On)'),
    '#options' => array(
      '' => '<none>',
      'fadeIn' => 'fadeIn',
    ),
    '#description' => t("These animations will be applied to the tip when it is turn on or off."),
    '#default_value' => is_object($tip) ? $tip->animation_on : '',
    '#prefix' => '<div class="beautytips-animations">',
  );
  $form['tip']['animation_off'] = array(
    '#type' => 'select',
    '#title' => t('Animation (Off)'),
    '#options' => array(
      '' => '<none>',
      'fadeOut' => 'fadeOut',
      'slideOut' => 'slideOut',
    ),
    '#default_value' => is_object($tip) ? $tip->animation_off : '',
    '#suffix' => '</div>',
  );
  $options = array(
    t('Show on every page except the listed pages.'),
    t('Show on only the listed pages.'),
  );
  $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>',
  ));
  $form['tip']['visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Show beautytip on specific pages'),
    '#options' => $options,
    '#default_value' => is_object($tip) ? $tip->visibility : 0,
    '#prefix' => '<div id="edit-tip-visibility-wrapper">',
    '#suffix' => '</div>',
  );
  $form['tip']['pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#default_value' => is_object($tip) ? $tip->pages : '',
    '#description' => $description,
  );
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['#submit'][] = 'beautytips_manager_admin_submit';
  return $form;
}

/**
 * Validation callback for beautytips manager admin form.
 */
function beautytips_manager_admin_validate($form, &$form_state) {

  // TODO: Validate
  $positions = $form_state['values']['tip']['positions'];
  foreach ($positions as $position => $order) {
    if ($order !== '' && !is_numeric($order)) {
      form_set_error("tip][positions][{$position}", t("You must enter a numeric value for position order (Or leave it blank)."));
    }
  }
}

/**
 * Submission callback for beautytips manager admin form.
 */
function beautytips_manager_admin_submit($form, &$form_state) {
  $tip = $form_state['values']['tip'];
  switch ($tip['content_type']) {
    case 'attribute':

      //$tip['content'] = check_plain($tip['content']);
      break;
    case 'text':

      //$tip['content'] = check_markup($tip['content']);
      break;
    case 'ajax':

      //$tip['content'] = check_plain($tip['content']);
      break;
    case 'js':

      // Do nothing
      break;
  }
  $positions = array();
  foreach ($tip['positions'] as $position => $order) {
    if ($order !== '') {
      while (isset($positions[$order])) {
        $order++;
      }
      $positions[$order] = $position;
    }
  }
  ksort($positions);
  $tip['positions'] = count($positions) ? implode(',', $positions) : '';
  beautytips_manager_save_custom_tip($tip);
  cache_clear_all('beautytips:beautytips-ui-custom-tips', 'cache');
  $form_state['redirect'] = 'admin/settings/beautytips/manager';
}

/**
 * Confirm form for deleting a custom beautytip.
 */
function beautytips_manager_delete_confirm_form(&$form_state, $bid) {
  $tip = beautytips_manager_get_custom_tip($bid);
  $form['bid'] = array(
    '#type' => 'value',
    '#value' => $bid,
  );
  $question = t('Are you sure you want to the beautytip applied to element %element?', array(
    '%element' => $tip->element,
  ));
  return confirm_form($form, $question, 'admin/settings/beautytips/manager');
}

/**
 * Submit callback for beautytips delete confirm form.
 */
function beautytips_manager_delete_confirm_form_submit($form, &$form_state) {
  beautytips_manager_delete_custom_tip($form_state['values']['bid']);
  cache_clear_all('beautytips:beautytips-ui-custom-tips', 'cache');
  $form_state['redirect'] = 'admin/settings/beautytips/manager';
}

/**
 * Delete a singular custom beautytip.
 */
function beautytips_manager_delete_custom_tip($bid) {
  db_query("DELETE FROM {beautytips_manager} WHERE bid = %d", $bid);
}

/**
 * Save a singular beautytip.
 */
function beautytips_manager_save_custom_tip($tip) {
  $tip = (object) $tip;

  // Mysql doesn't let these fields have default values
  // in strict mode, so ensure they are at least an empty string.
  $text_fields = array(
    'content',
    'pages',
  );
  foreach ($text_fields as $field) {
    if (!isset($tip->{$field})) {
      $tip->{$field} = '';
    }
  }
  if (isset($tip->bid) && $tip->bid) {
    drupal_write_record('beautytips_manager', $tip, 'bid');
  }
  else {
    drupal_write_record('beautytips_manager', $tip);
  }
  return $tip;
}

/**
 * Retrieve a list of all possible triggers.
 * TODO: Don't include all of these
 */
function beautytips_manager_get_triggers() {
  return array(
    'hover' => 'hover',
    'hoverIntent' => 'hoverIntent',
    'click' => 'click',
    'dblclick' => 'dblclick',
    'blur' => 'blur',
    'focus' => 'focus',
    'mouseover' => 'mouseover',
    'mouseout' => 'mouseout',
    'mousedown' => 'mousedown',
    'mousemove' => 'mousemove',
    'mouseenter' => 'mouseenter',
    'mouseleave' => 'mouseleave',
    'change' => 'change',
    'select' => 'select',
    'submit' => 'submit',
    'keydown' => 'keydown',
    'keypress' => 'keypress',
    'keyup' => 'keyup',
    'error' => 'error',
    'load' => 'load',
    'unload' => 'unload',
    'resize' => 'resize',
    'scroll' => 'scroll',
  );
}

/**
 * Retrieve all custom beautytips.
 */
function beautytips_manager_get_custom_tips() {
  $cache = cache_get('beautytips:beautytips-ui-custom-tips');
  if (!$cache) {
    $tips = array();
    $results = db_query("SELECT * FROM {beautytips_manager}");
    while ($tip = db_fetch_object($results)) {
      $tips[] = $tip;
    }
    cache_set('beautytips:beautytips-ui-custom-tips', $tips);
  }
  else {
    $tips = $cache->data;
  }
  return $tips;
}

/**
 * Retrieves a single custom beautytip.
 */
function beautytips_manager_get_custom_tip($bid) {
  $sql = "SELECT *\n          FROM {beautytips_manager}\n          WHERE bid = %d";
  return db_fetch_object(db_query($sql, $bid));
}

/**
 * Given a custom tip, build an array of options
 *  that can be passed to beautytips_add_beautytips().
 */
function beautytips_manager_build_beautytip($tip) {
  $single_triggers = array(
    'hover',
    'hoverIntent',
  );
  $trigger = in_array($tip->trigger_on, $single_triggers) ? $tip->trigger_on : array(
    $tip->trigger_on,
    $tip->trigger_off,
  );
  $options = array(
    'cssSelect' => check_plain($tip->element),
    'style' => $tip->style,
    'trigger' => $trigger,
    'shrinkToFit' => (bool) $tip->shrink,
  );
  if ($tip->animation_on) {
    $options['animate']['on'] = $tip->animation_on;
  }
  if ($tip->animation_off) {
    $options['animate']['off'] = $tip->animation_off;
  }
  if ($tip->positions) {
    $options['positions'] = explode(',', $tip->positions);
  }
  switch ($tip->content_type) {
    case 'attribute':
      if ($tip->content) {
        $options['contentSelector'] = "\$(this).attr('" . check_plain($tip->content) . "')";
      }
      break;
    case 'text':
      $options['text'] = check_markup($tip->content);
      break;
    case 'ajax':
      $options['ajaxPath'] = !$tip->content ? "\$(this).attr('href')" : array(
        "\$(this).attr('href')",
        check_plain($tip->content),
      );
      break;
    case 'js':
      $options['contentSelector'] = check_plain($tip->content);
      break;
  }
  return $options;
}

Functions

Namesort descending Description
beautytips_manager_admin Form for configuring custom beautytips.
beautytips_manager_admin_submit Submission callback for beautytips manager admin form.
beautytips_manager_admin_validate Validation callback for beautytips manager admin form.
beautytips_manager_build_beautytip Given a custom tip, build an array of options that can be passed to beautytips_add_beautytips().
beautytips_manager_delete_confirm_form Confirm form for deleting a custom beautytip.
beautytips_manager_delete_confirm_form_submit Submit callback for beautytips delete confirm form.
beautytips_manager_delete_custom_tip Delete a singular custom beautytip.
beautytips_manager_get_custom_tip Retrieves a single custom beautytip.
beautytips_manager_get_custom_tips Retrieve all custom beautytips.
beautytips_manager_get_triggers Retrieve a list of all possible triggers. TODO: Don't include all of these
beautytips_manager_init Implementation of hook_init().
beautytips_manager_menu Implementation of hook_menu().
beautytips_manager_perm Implementation of hook_perm().
beautytips_manager_save_custom_tip Save a singular beautytip.
beautytips_manager_tips_manager_page Listing of all beautytips available.