You are here

easy_social.module in Easy Social 6

This is the file description for Easy Social module.

File

easy_social.module
View source
<?php

/**
 * @file
 * This is the file description for Easy Social module.
 * 
 */

/**
 * Implements hook_perm().
 */
function easy_social_perm() {
  return array(
    'administer easy social',
  );
}

/**
 * Implements hook_menu().
 */
function easy_social_menu() {
  $items = array();
  $items['admin/settings/easysocial'] = array(
    'title' => 'Easy Social',
    'description' => 'Configure the social buttons and node types',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'admin_settings_easy_social',
    ),
    'access arguments' => array(
      'administer easy social',
    ),
  );
  $items['admin/settings/easysocial/default'] = array(
    'title' => 'Settings',
    'access arguments' => array(
      'administer easy social',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/easysocial/paths-ignore'] = array(
    'title' => 'Ignore Paths',
    'description' => 'Global paths to ignore and do not display Easy Social',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'admin_settings_easy_social_ignore_paths',
    ),
    'access arguments' => array(
      'administer easy social',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Generate the administer settings form
 */
function admin_settings_easy_social() {
  $form = array();
  $node_types = node_get_types();
  $options_types = array();
  foreach ($node_types as $type => $typeobj) {
    $options_types[$type] = $typeobj->name;
  }
  $form['easysocial_global_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enable Easy Social for the Node Types'),
    '#options' => $options_types,
    '#default_value' => variable_get('easysocial_global_node_types', array()),
    '#description' => t('Select the content types which Easy Social will be available'),
  );
  $form['global_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Global Settings'),
    '#description' => t('Settings available for all content types'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['global_settings']['easysocial_global_typebtn'] = array(
    '#type' => 'radios',
    '#title' => t('Type of buttons'),
    '#options' => array(
      t('Horizontal'),
      t('Vertical'),
    ),
    '#default_value' => variable_get('easysocial_global_typebtn', 0),
  );
  $form['global_settings']['easysocial_global_btn_positioning'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Positioning of the buttons in node view'),
    '#description' => t('Available only for full node view'),
    '#options' => array(
      'top' => t('Top'),
      'bottom' => t('Bottom'),
    ),
    '#default_value' => variable_get('easysocial_global_btn_positioning', array(
      'bottom',
    )),
  );
  $form['global_settings']['easysocial_global_node_view'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Manage display in node view'),
    '#options' => array(
      'teaser' => t('Teaser'),
      'fullnode' => t('Full Node'),
    ),
    '#default_value' => variable_get('easysocial_global_node_view', array(
      'teaser',
      'fullnode',
    )),
  );
  $form['global_settings']['easysocial_global_social_buttons'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Social Buttons'),
    '#options' => array(
      'twitter' => 'Twitter',
      'facebook' => 'Facebook',
      'googleplus' => 'Google Plus',
      'linkedin' => 'Linked In',
    ),
    '#default_value' => variable_get('easysocial_global_social_buttons', array(
      'twitter',
      'facebook',
      'googleplus',
      'linkedin',
    )),
  );
  $form['global_settings']['twitter_global_data'] = array(
    '#type' => 'fieldset',
    '#title' => t('Twitter Info'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['global_settings']['twitter_global_data']['easysocial_tt_global_account_via'] = array(
    '#type' => 'textfield',
    '#title' => t('Mention account'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('easysocial_tt_global_account_via', ''),
  );
  $form['global_settings']['twitter_global_data']['easysocial_tt_global_account_related'] = array(
    '#type' => 'textfield',
    '#title' => t('Related account'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('easysocial_tt_global_account_related', ''),
  );
  $form['global_settings']['twitter_global_data']['easysocial_tt_global_account_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Related account description'),
    '#size' => 120,
    '#maxlength' => 120,
    '#default_value' => variable_get('easysocial_tt_global_account_description', 'Check it out!'),
  );

  //Get Types where it is available
  $node_types_variable = variable_get('easysocial_global_node_types', array());

  //Cleanup array
  $node_types_variable = array_filter($node_types_variable);
  if (count($node_types_variable) > 0) {
    $form['override_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Override Settings by Type'),
      '#description' => t('Settings by content type'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    foreach ($node_types_variable as $type => $typev) {
      $form['override_settings']['easysocial_settings_type_' . $type] = array(
        '#type' => 'fieldset',
        '#title' => t('Custom Settings for %type', array(
          '%type' => $options_types[$type],
        )),
        '#collapsible' => FALSE,
        '#collapsed' => TRUE,
      );
      $form['override_settings']['easysocial_settings_type_' . $type]['easysocial_' . $type . '_override'] = array(
        '#type' => 'checkbox',
        '#title' => t('Override'),
        '#description' => t('Check this option to override the global settings for this type'),
        '#default_value' => variable_get('easysocial_' . $type . '_override', ''),
      );
      $form['override_settings']['easysocial_settings_type_' . $type]['easysocial_' . $type . '_typebtn'] = array(
        '#type' => 'radios',
        '#title' => t('Type of buttons'),
        '#options' => array(
          t('Horizontal'),
          t('Vertical'),
        ),
        '#default_value' => variable_get('easysocial_' . $type . '_typebtn', ''),
      );
      $form['override_settings']['easysocial_settings_type_' . $type]['easysocial_' . $type . '_btn_positioning'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Positioning of the buttons in node view'),
        '#description' => t('Available only for full node view'),
        '#options' => array(
          'top' => t('Top'),
          'bottom' => t('Bottom'),
        ),
        '#default_value' => variable_get('easysocial_' . $type . '_btn_positioning', array(
          'bottom',
        )),
      );
      $form['override_settings']['easysocial_settings_type_' . $type]['easysocial_' . $type . '_node_view'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Manage display in node view'),
        '#options' => array(
          'teaser' => t('Teaser'),
          'fullnode' => t('Full Node'),
        ),
        '#default_value' => variable_get('easysocial_' . $type . '_node_view', array(
          'teaser',
          'fullnode',
        )),
      );
      $form['override_settings']['easysocial_settings_type_' . $type]['easysocial_' . $type . '_social_buttons'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Social Buttons'),
        '#options' => array(
          'twitter' => 'Twitter',
          'facebook' => 'Facebook',
          'googleplus' => 'Google Plus',
          'linkedin' => 'Linked In',
        ),
        '#default_value' => variable_get('easysocial_' . $type . '_social_buttons', array()),
      );
    }
  }
  $form = system_settings_form($form);
  return $form;
}

/**
 * Generate the administer ignore paths settings form
 */
function admin_settings_easy_social_ignore_paths() {
  $form = array();
  $form['easysocial_ignore_paths'] = array(
    '#type' => 'textarea',
    '#title' => t('Ignore paths'),
    '#description' => t('Every URL placed here, will not have any Social links.'),
    '#default_value' => variable_get('easysocial_ignore_paths', ''),
  );
  $form = system_settings_form($form);
  return $form;
}

/**
 * Implements hook_theme().
 */
function easy_social_theme() {
  $theme = array(
    'easy_social_links' => array(
      'arguments' => array(
        'social_links' => NULL,
      ),
      'template' => 'easy-social-links',
    ),
  );
  return $theme;
}

/**
 * Prepare node to display easy social
 */
function easy_social_preprocess_node(&$variables) {

  //Prepare a variable to send to prepare links
  $node_info = new stdClass();
  $node_info->nid = $variables['nid'];
  $node_info->type = $variables['type'];
  $node_info->title = $variables['title'];
  $urls_ignored = variable_get('easysocial_ignore_paths', '');
  $page_match = FALSE;
  $path = drupal_get_path_alias($_GET['q']);

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

    //Load the Easy Social stuff
    $prepare_easy_social = _easy_social_prepare_node_links($node_info);

    //Add to node object
    $variables['node']->easy_social = $prepare_easy_social['html'];

    //Teaser Display
    if ($variables['teaser'] && isset($prepare_easy_social['display']['teaser'])) {
      $variables['content'] .= $prepare_easy_social['html'];
    }

    //Full node display
    if ($variables['page'] && isset($prepare_easy_social['display']['fullnode'])) {
      if (isset($prepare_easy_social['position']['top'])) {
        $variables['content'] = $prepare_easy_social['html'] . $variables['content'];
      }
      if (isset($prepare_easy_social['position']['bottom'])) {
        $variables['content'] .= $prepare_easy_social['html'];
      }
    }
  }
}

/**
 * Load the share buttons according the node I am in
 */
function _easy_social_prepare_node_links($node) {

  //Get Types where it is available
  $node_types_variable = variable_get('easysocial_global_node_types', array());

  //Cleanup array
  $node_types_variable = array_filter($node_types_variable);

  //Certify the node can display the easy social
  if ($node_types_variable[$node->type]) {

    //Check if this type has a custom setting
    if (variable_get('easysocial_' . $node->type . '_override', '') == 1) {
      $type = variable_get('easysocial_' . $node->type . '_typebtn', '');
      $buttons = variable_get('easysocial_' . $node->type . '_social_buttons', array());
      $position = variable_get('easysocial_' . $node->type . '_btn_positioning', array(
        'bottom',
      ));
      $display = variable_get('easysocial_' . $node->type . '_node_view', array(
        'teaser',
        'fullnode',
      ));
    }
    else {
      $type = variable_get('easysocial_global_typebtn', '');
      $buttons = variable_get('easysocial_global_social_buttons', array());
      $position = variable_get('easysocial_global_btn_positioning', array(
        'bottom',
      ));
      $display = variable_get('easysocial_global_node_view', array(
        'teaser',
        'fullnode',
      ));
    }
    $buttons = array_filter($buttons);

    //Url to be shared
    $url = url('node/' . $node->nid, array(
      'absolute' => TRUE,
    ));
    $social_links = array();

    //Load Js files and generate respective markups
    foreach ($buttons as $service) {
      if (is_string($service)) {
        $lang = $node->language;
        eval("_easysocial_js_add_{$service}();");
        eval("\$social_links[\$service] = _easysocial_button_{$service}_markup(\$url, \$type, \$lang);");
      }
    }
    $output = "";

    //If at least one button is selected, go on
    if (count($social_links) > 0) {
      $output .= theme('easy_social_links', $social_links);
    }
  }
  $return = array(
    'html' => $output,
  );
  if ($position) {
    $return += array(
      'position' => array_filter($position),
    );
  }
  if ($display) {
    $return += array(
      'display' => array_filter($display),
    );
  }
  return $return;
}

/**
 * Add external facebook js
 */
function _easysocial_js_add_facebook() {
  drupal_add_js(drupal_get_path('module', 'easy_social') . '/js/easy_social.facebook.js');
}

/**
 * Add external twitter js
 */
function _easysocial_js_add_twitter() {
  drupal_add_js(drupal_get_path('module', 'easy_social') . '/js/easy_social.twitter.js');
}

/**
 * Add external googleplus js
 */
function _easysocial_js_add_googleplus() {
  drupal_add_js(drupal_get_path('module', 'easy_social') . '/js/easy_social.googleplus.js');
}

/**
 * Add external linkedin js
 */
function _easysocial_js_add_linkedin() {
  drupal_add_js(drupal_get_path('module', 'easy_social') . '/js/easy_social.linkedin.js');
}

/**
 * Generate the needed markup for the facebook share link
 * @param $url The url to be shared
 * @param $type Generate horizontal or vertical widgets
 * @return the html markup
 */
function _easysocial_button_facebook_markup($url, $type, $lang = 'en_US') {

  // correct for different language identification strings
  if ($lang === 'en') {
    $lang = 'en_US';
  }
  elseif ($lang === 'en-gb') {
    $lang = 'en_GB';
  }
  if ($type == 0) {
    $type_box = 'button_count';
    $markup = <<<FB
      <iframe class="fb-widget"
      src="http://www.facebook.com/plugins/like.php?href={<span class="php-variable">$url</span>}&amp;locale={<span class="php-variable">$lang</span>}&amp;layout={<span class="php-variable">$type_box</span>}&amp;show_faces=false&amp;width=86&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=light&amp;height=21"
      scrolling="no"
      frameborder="0"
      style="border:none; overflow:hidden; width:87px; height:21px;"
      allowTransparency="true"></iframe>
FB;
  }
  else {
    $type_box = 'box_count';
    $markup = <<<FB
      <iframe class="fb-widget"
      src="http://www.facebook.com/plugins/like.php?href={<span class="php-variable">$url</span>}&amp;locale={<span class="php-variable">$lang</span>}&amp;layout={<span class="php-variable">$type_box</span>}&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=light&amp;height=21"
      scrolling="no"
      frameborder="0"
      style="border:none; overflow:hidden; width:48px; height:60px;"
      allowTransparency="true"></iframe>
FB;
  }
  return $markup;
}

/**
 * Generate the needed markup for the twitter share link
 * @param $url The url to be shared
 * @param $type Generate horizontal or vertical widgets
 * @return the html markup
 */
function _easysocial_button_twitter_markup($url, $type, $lang = 'en') {

  // correct for different language identification strings
  if ($lang === 'en-gb') {
    $lang = 'en';
  }
  elseif ($lang === 'pt-br' || $lang === 'pt-pt') {
    $lang = 'pt';
  }
  $type = $type == 0 ? 'horizontal' : 'vertical';
  $account_via = variable_get('easysocial_tt_global_account_via', '');
  $account_related = variable_get('easysocial_tt_global_account_related', '');
  $account_related_description = variable_get('easysocial_tt_global_account_description', '');

  // Twitter uses current page title by default
  $markup = <<<TT
    <a href="http://twitter.com/share"
    class="twitter-share-button"
    data-url="{<span class="php-variable">$url</span>}"
    data-count="{<span class="php-variable">$type</span>}"
    data-lang = "{<span class="php-variable">$lang</span>}"
    data-via="{<span class="php-variable">$account_via</span>}"
    data-related="{<span class="php-variable">$account_related</span>}:{<span class="php-variable">$account_related_description</span>}">Tweet</a>
TT;
  return $markup;
}

/**
 * Generate the needed markeup for the googleplus share link
 * @param $url The url to be shared
 * @param $type Generate horizontal or vertical widgets
 * @return the html markup
 */
function _easysocial_button_googleplus_markup($url, $type, $lang = 'en') {

  // no language options
  $type = $type == 0 ? 'medium' : 'tall';
  $markup = <<<GP
    <g:plusone size="{<span class="php-variable">$type</span>}" href="{<span class="php-variable">$url</span>}"></g:plusone>
GP;
  return $markup;
}

/**
 * Generate the needed markeup for the linkedin share link
 * @param $url The url to be shared
 * @param $type Generate horizontal or vertical widgets
 * @return the html markup
 */
function _easysocial_button_linkedin_markup($url, $type, $lang = 'en') {

  // no language options
  $type = $type == 0 ? 'right' : 'top';
  $markup = <<<LI
    <script type="in/share" data-url="{<span class="php-variable">$url</span>}" data-counter="{<span class="php-variable">$type</span>}"></script>
LI;
  return $markup;
}

Functions

Namesort descending Description
admin_settings_easy_social Generate the administer settings form
admin_settings_easy_social_ignore_paths Generate the administer ignore paths settings form
easy_social_menu Implements hook_menu().
easy_social_perm Implements hook_perm().
easy_social_preprocess_node Prepare node to display easy social
easy_social_theme Implements hook_theme().
_easysocial_button_facebook_markup Generate the needed markup for the facebook share link
_easysocial_button_googleplus_markup Generate the needed markeup for the googleplus share link
_easysocial_button_linkedin_markup Generate the needed markeup for the linkedin share link
_easysocial_button_twitter_markup Generate the needed markup for the twitter share link
_easysocial_js_add_facebook Add external facebook js
_easysocial_js_add_googleplus Add external googleplus js
_easysocial_js_add_linkedin Add external linkedin js
_easysocial_js_add_twitter Add external twitter js
_easy_social_prepare_node_links Load the share buttons according the node I am in