You are here

addtoany.module in AddToAny Share Buttons 5.2

Stand alone module file to handle AddToAny button integration

File

addtoany.module
View source
<?php

/**
 * @file
 * Stand alone module file to handle AddToAny button integration
 */

/**
 * Implementation of hook_perm().
 */
function addtoany_perm() {
  $perms[] = 'administer addtoany';
  return $perms;
}

/**
 * Implementation of hook_link().
 */
function addtoany_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  if ($type === 'node') {
    if ($teaser && variable_get('addtoany_display_in_teasers', '0') || !$teaser && variable_get('addtoany_display_in_links', '0')) {
      $links['addtoany'] = array(
        'title' => _addtoany_create_button($node, $teaser),
        'html' => TRUE,
      );
    }
  }
  return $links;
}

/**
 * Implementation of hook_menu().
 */
function addtoany_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/addtoany',
      'title' => t('AddToAny'),
      'description' => t('Settings for your <a href="http://www.addtoany.com/" target="blank">AddToAny</a> Share/Save buttons.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'addtoany_admin_settings',
      ),
      'access' => user_access('administer addtoany'),
    );
  }
  return $items;
}

/**
 * Administration settings form.
 *
 * @return
 *   The completed form definition.
 */
function addtoany_admin_settings() {
  $form = array();
  global $base_path;
  $button_img = '<img src="' . $base_path . drupal_get_path('module', 'addtoany') . '/images/%s" width="%d" height="%d" />';
  $button_options = array(
    'share_16_16.png|16|16' => sprintf($button_img, 'share_16_16.png', 16, 16),
    'share_save_171_16.png|171|16' => sprintf($button_img, 'share_save_171_16.png', 171, 16),
    'share_save_256_24.png|256|24' => sprintf($button_img, 'share_save_256_24.png', 256, 24),
    'custom' => 'Custom button',
  );
  $form['addtoany_general_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('General'),
  );
  $form['addtoany_general_settings']['addtoany_display_in_links'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display on node pages'),
    '#default_value' => variable_get('addtoany_display_in_links', '0'),
    '#description' => t('Display an addtoany button always on a node page\'s links section.'),
  );
  $form['addtoany_general_settings']['addtoany_display_in_teasers'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display in node teasers'),
    '#default_value' => variable_get('addtoany_display_in_teasers', '0'),
    '#description' => t('Display an addtoany button in the node teasers.'),
  );
  $form['addtoany_button_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Button'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['addtoany_button_settings']['addtoany_image'] = array(
    '#type' => 'radios',
    '#title' => t('Button'),
    '#default_value' => variable_get('addtoany_image', 'share_save_171_16.png|171|16'),
    '#options' => $button_options,
  );
  $form['addtoany_button_settings']['addtoany_custom_image'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom button URL'),
    '#default_value' => variable_get('addtoany_custom_image', ''),
    '#description' => t('URL to the button image of your choosing. Example: http://example.com/share.png'),
  );
  $form['addtoany_button_settings']['addtoany_custom_image_attributes'] = array(
    '#type' => 'textfield',
    '#title' => t('Button image HTML attributes'),
    '#default_value' => variable_get('addtoany_image_attributes', 'alt="Share/Save"'),
    '#description' => t('Extra HTML attributes for img tag. Example: alt=""'),
  );
  $form['addtoany_additional_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['addtoany_additional_settings']['addtoany_additional_js'] = array(
    '#type' => 'textarea',
    '#title' => t('Additional script'),
    '#default_value' => variable_get('addtoany_additional_js', ''),
    '#description' => t('You can set special JavaScript variables for each Share/Save menu. Advanced users might want to check out Add to Any\'s <a href="http://www.addtoany.com/buttons/api/">JavaScript API</a>.'),
  );
  $form['addtoany_widget_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Drop-down menu'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['addtoany_widget_settings']['addtoany_dropdown_disabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable dropdown'),
    '#default_value' => variable_get('addtoany_dropdown_disabled', '0'),
    '#description' => t('You can disable the drop-down for selecting where to share your link and use a pop-up window instead.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_block().
 */
function addtoany_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('AddToAny button');
    return $blocks;
  }
  else {
    if ($op == 'view') {
      $block['subject'] = t('AddToAny');
      $block['content'] = _addtoany_create_button();
      return $block;
    }
  }
}

/**
 * Implementation of hook_footer().  Would use drupal_add_js, but external scripts are not well supported
 *
 * @return
 *   String containing JavaScript code for the drop-down
 */
function addtoany_footer($main = 0) {
  global $_addtoany_script;
  return $_addtoany_script;
}

/**
 * Internal function to generate code for AddToAny button and script
 *
 * @return
 *   String containing html code for the button
 */
function _addtoany_create_button($node = NULL, $teaser = FALSE) {
  global $_addtoany_counter, $base_path;
  $_addtoany_counter++;
  if ($_addtoany_counter == 1) {
    drupal_add_css(drupal_get_path('module', 'addtoany') . '/addtoany.css');
  }
  if ($node) {
    $linkname = $node->title;
    $linkurl = url('node/' . $node->nid, NULL, NULL, TRUE);
  }
  else {
    $linkname = drupal_get_title() ? strip_tags(drupal_get_title()) . " - " . variable_get('site_name', 'Drupal') : variable_get('site_name', 'Drupal');
    $linkurl = url(ltrim(request_uri(), "/"), NULL, NULL, TRUE);
  }

  // Drop-down disabled?
  if (variable_get('addtoany_dropdown_disabled', '0')) {
    return;
  }
  global $_addtoany_script, $_addtoany_script_init;
  if (!$_addtoany_script_init) {
    $_addtoany_script = '<script type="text/javascript">';
  }
  else {
    $_addtoany_script .= '<script type="text/javascript">';
  }
  $_addtoany_script .= 'a2a_linkname="' . addslashes($linkname) . '";a2a_linkurl="' . $linkurl . '";';
  if (!$_addtoany_script_init) {
    $_addtoany_script .= variable_get('addtoany_additional_js', '');
    $_addtoany_script .= '</script><script type="text/javascript" src="http://static.addtoany.com/menu/page.js"></script>';
  }
  else {
    $_addtoany_script .= 'a2a_init("page");</script>';
  }
  $_addtoany_script_init = TRUE;
  $disable_dropdown = variable_get('addtoany_dropdown_disabled', '0');
  $button_setting = variable_get('addtoany_image', 'share_save_171_16.png|171|16');
  if ($button_setting == "custom") {
    $button_image = variable_get('addtoany_custom_image', '');
    $button_width = '';
    $button_height = '';
  }
  else {
    $button = explode('|', $button_setting);
    $button_filename = $button[0];
    $button_width = ' width="' . $button[1] . '"';
    $button_height = ' height="' . $button[2] . '"';
    $button_image = $base_path . drupal_get_path('module', 'addtoany') . '/images/' . $button_filename;
  }
  return sprintf('
      <a%s href="http://www.addtoany.com/share_save?linkurl=%s&amp;linkname=%s"><img src="%s"%s%s %s/></a>
      ', $disable_dropdown ? '' : ' class="a2a_dd"', rawurlencode($linkurl), rawurlencode($linkname), $button_image, $button_width, $button_height, variable_get('addtoany_image_attributes', 'alt="Share/Save"'));
}

Functions

Namesort descending Description
addtoany_admin_settings Administration settings form.
addtoany_block Implementation of hook_block().
addtoany_footer Implementation of hook_footer(). Would use drupal_add_js, but external scripts are not well supported
addtoany_link Implementation of hook_link().
addtoany_menu Implementation of hook_menu().
addtoany_perm Implementation of hook_perm().
_addtoany_create_button Internal function to generate code for AddToAny button and script