<?php

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

/**
 * Implements hook_install().
 */
function addtoany_install() {
  drupal_set_message(st("Your AddToAny settings are available under !link",
    array( '!link' => l('Configuration > Web services > AddToAny ',  'admin/config/services/addtoany' ) )
  ));
}

/**
 * Implements hook_permission().
 */
function addtoany_permission() {
  return array(
    'administer addtoany' => array(
      'title' => t('Administer addtoany'),
      'description' => t(''),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_node_view().
 */
function addtoany_node_view($node, $view_mode) {
  $types = variable_get('addtoany_nodetypes', array('page', 'article'));
  $teaser = ($view_mode == 'teaser') ? TRUE : FALSE;
  $rss = ($view_mode == 'rss') ? TRUE : FALSE;

  // Display in link section?
  $show = (!empty($node->type) && in_array($node->type, $types, TRUE) && ($node->status == 1) && (variable_get('addtoany_display_in_nodelink', '1') != 0)
    && (
      (!$teaser) || ($teaser && variable_get('addtoany_display_in_teasers', '1') != 0)
    )
    && (
      (!$rss) || ($rss && variable_get('addtoany_display_in_rss', '1') != 0)
    )
  );

  if ($show) {
    $node->content['links']['#links']['addtoany'] = array(
      'title' => addtoany_create_node_buttons($node),
      'html' => TRUE,
    );
  }

  // Display in content section?
  $show = (!empty($node->type) && in_array($node->type, $types, TRUE) && ($node->status == 1) && (variable_get('addtoany_display_in_nodecont', '0') != 0)
    && (
       (!$teaser) || ($teaser && variable_get('addtoany_display_in_teasers', '1') != 0)
    )
    && (
      (!$rss) || ($rss && variable_get('addtoany_display_in_rss', '1') != 0)
    )
  );
  $weight = variable_get('addtoany_display_weight', 40);

  if ($show) {
    $node->content['addtoany'] = array(
      '#markup' => addtoany_create_node_buttons($node),
      '#weight' => $weight,
    );
  }

}

/**
 * Implements hook_views_api().
 */
function addtoany_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'addtoany') . '/views',
  );
}

/**
 * Implements hook_menu().
 */
function addtoany_menu() {
  $items = array();

  $items['admin/config/services/addtoany'] = array(
    'title'            => t('AddToAny'),
    'description'      => t('Settings for your <a href="https://www.addtoany.com/" target="blank">AddToAny</a> share buttons.'),
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('addtoany_admin_settings'),
    'access arguments' => array('administer addtoany'),
    'file'             => 'addtoany.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_block_info().
 */
function addtoany_block_info() {
  $blocks['addtoany_button'] = array(
    'info' => t('AddToAny buttons'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_list().
 */
function addtoany_block_list() {
  $blocks['addtoany_button'] = array(
    'info'       => t('AddToAny buttons'),
    'status'     => TRUE,
    'weight'     => 0,
    'visibility' => 1,
    'pages'      => '*',
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function addtoany_block_view($delta = 0) {
  $block = array();
  switch ($delta) {
    case 'addtoany_button':
      $block['subject'] = t('AddToAny');
      $block['content'] = addtoany_create_node_buttons(menu_get_object());
    break;
  }
  return $block;
}

/**
 * Implements hook_page_alter().
 *
 * @return
 *   String containing JavaScript code for asynchronous loading of AddToAny's external script.
 */

function addtoany_page_alter(&$page) {
  global $_addtoany_init, $_addtoany_targets;

  // Always add CSS file (necessary for aggregation)
  $page['content']['#attached']['css'][] = drupal_get_path('module', 'addtoany') . '/addtoany.css';

  // Only output script when AddToAny is used
  if ( ! $_addtoany_init)
    return;

  $javascript_header = "window.a2a_config=window.a2a_config||{};"
      . "window.da2a={done:false,"
        . "html_done:false,"
        . "script_ready:false,"
        . "script_load:function(){"
          . "var a=document.createElement('script'),"
            . "s=document.getElementsByTagName('script')[0];"
          . "a.type='text/javascript';a.async=true;"
          . "a.src='https://static.addtoany.com/menu/page.js';"
          . "s.parentNode.insertBefore(a,s);"
          . "da2a.script_load=function(){};"
        . "},"
        . "script_onready:function(){"
          . "da2a.script_ready=true;"
          . "if(da2a.html_done)da2a.init();"
        . "},"
        . "init:function(){"
          . "for(var i=0,el,target,targets=da2a.targets,length=targets.length;i<length;i++){"
            . "el=document.getElementById('da2a_'+(i+1));"
            . "target=targets[i];"
            . "a2a_config.linkname=target.title;"
            . "a2a_config.linkurl=target.url;"
            . "if(el){"
              . "a2a.init('page',{target:el});"
              . "el.id='';" // Remove ID so AJAX can reuse the same ID
            . "}"
            . "da2a.done=true;"
          . "}"
          . "da2a.targets=[];" // Empty targets array so AJAX can reuse from index 0
        . "}"
      . "};"
      // AJAX support
      . "(function ($){"
        . "Drupal.behaviors.addToAny = {"
          . "attach: function (context, settings) {"
            // If not the full document (it's probably AJAX), and window.da2a exists
            . "if (context !== document && window.da2a) {"
              . "if(da2a.script_ready)a2a.init_all();" // Init all uninitiated AddToAny instances
              . "da2a.script_load();" // Load external script if not already called
            . "}"
          . "}"
        . "}"
      . "})(jQuery);"
      . "a2a_config.callbacks=a2a_config.callbacks||[];"
      . "a2a_config.callbacks.push({ready:da2a.script_onready});"
      . "a2a_config.overlays=a2a_config.overlays||[];"
      . "a2a_config.templates=a2a_config.templates||{};"
      . ((variable_get('addtoany_no_3p', '0') != '1') ? "" : "a2a_config.no_3p=1;")
      . variable_get('addtoany_additional_js', '');

  $javascript_footer = "da2a.targets=["
      . implode(",", $_addtoany_targets)
    . "];\n"
    . "da2a.html_done=true;"
    . "if(da2a.script_ready&&!da2a.done)da2a.init();" // External script may load before html_done=true, but will only init if html_done=true.  So call da2a.init() if external script is ready, and if da2a.init() hasn't been called already.  Otherwise, wait for callback to call da2a.init()
    . "da2a.script_load();"; // Load external script if not already called with the first AddToAny instance.  Fixes issues where first instance code is processed internally but without actual code output

  $page['content']['#attached']['js'][] = array(
    'data'  => $javascript_header,
    'scope' => 'header',
    'type'  => 'inline',
  );

  $page['content']['#attached']['js'][] = array(
    'data'  => $javascript_footer,
    'scope' => 'footer',
    'type'  => 'inline',
  );

  /**
   * Custom CSS Setting
   */
  $css = variable_get('addtoany_additional_css', '');
  if(!empty($css)) {
    drupal_add_css($css, 'inline');
  }
}

/**
 * Internal function to call the async script loading function right after the first AddToAny instance, to utilize concurrent downloading and increase overall page-load performance.
 *
 * @param int $id_num
 * @param string $link_name
 * @param string $link_url
 * @return
 *   String containing a JavaScript function call for asynchronous script loading right after the first instance.
 */
function _addtoany_create_script($id_num, $link_name, $link_url) {
	global $_addtoany_targets, $_addtoany_init;

	if ( ! isset($_addtoany_targets)) {
  	$_addtoany_targets = array();
	}

	$button_config = "\n{title:". drupal_json_encode($link_name) . ","
		. "url:" . drupal_json_encode($link_url) . "}";

	$_addtoany_targets[] = $button_config;

	if ( ! $_addtoany_init) {
		$javascript_load_early = "<script type=\"text/javascript\">\n"
			. "<!--//--><![CDATA[//><!--\n"
			. "if(window.da2a)da2a.script_load();\n"
			. "//--><!]]>\n"
			. "</script>";
	}
	else
		$javascript_load_early = "";

	$_addtoany_init = TRUE;

	return $javascript_load_early;
}

/**
 * Generate code for AddToAny buttons for a node.
 *
 * @param object $node
 *   The node object to create the buttons for.
 *
 * @return string
 *   The HTML code for the buttons.
 */
function addtoany_create_node_buttons($node) {
  $url = isset($node) ? 'node/' . $node->nid : NULL;
  $title = isset($node) ? $node->title : NULL;
  return addtoany_create_buttons($url, $title);
}

/**
 * Generate code for AddToAny buttons.
 *
 * @param array $url
 *   If present this will be used as the URL. It can be a relative internal path
 *   or an absolute URL. Use an empty string or <front> for the home page or
 *   NULL to use the current page URL.
 *   The URL will be run through check_url().
 *
 * @param string $title
 *   If present this will be used as the title. Use an empty string for no title
 *   or NULL to use the current page title.
 *   The title will be run through check_plain().
 *
 * @return string
 *   The HTML code for the buttons.
 */
function addtoany_create_buttons($url = NULL, $title = NULL) {
  global $_addtoany_num, $base_path;

  $_addtoany_num++;

  $additional_html = variable_get('addtoany_additional_html', '');
  $additional_html_placement = variable_get('addtoany_additional_html_placement', 'before');

  // Default to <front> or the current path.
  if (!isset($url)) {
    // Use <front> for the front page to avoid '/node' as the final URL,
    // otherwise use current path.
    $url = (drupal_is_front_page()) ? '<front>' : current_path();
  }
  else {
    // Sanitize and encode URL for HTML output.
    $url = check_url($url);
  }

  // Default to the current title, using the site name for the home page.
  if (!isset($title)) {
    if (drupal_is_front_page()) {
      $title  = module_exists('page_title') ? page_title_page_get_title() : check_plain(variable_get('site_name', 'Drupal'));
    }
    else {
      $title = module_exists('page_title') ? page_title_page_get_title() : drupal_get_title();
    }
  }

  // Set the link name.
  $link_name = $title;
  // Make an absolute link URL out of whatever URL/path we have been given.
  $link_url = url($url, array('absolute' => TRUE));

  $buttons_size = variable_get('addtoany_buttons_size', '32');
  // Must be a 3 digit positive integer.
  $buttons_size = (
      $buttons_size !== '32' && strlen($buttons_size) <= 3 && $buttons_size !== ''
      && is_numeric($buttons_size) && intval($buttons_size) == $buttons_size && $buttons_size > 0
    ) ? $buttons_size : '32';

  $button_setting = variable_get('addtoany_image', 'share_save_171_16.png|171|16');

  if ($button_setting == 'a2a_svg_32') {
    $button_setting = 'text';
    $button_text = '';
  }
  elseif ($button_setting == 'custom') {
    $button_image = check_url(variable_get('addtoany_custom_image', ''));
    $button_width = '';
    $button_height = '';
  }
  elseif ($button_setting == 'none') {
    // Universal share button disabled.
  }
  elseif ($button_setting != 'text') {
    $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;
  }

  if ($button_setting != 'none') {
    // Permit blank button_text (so AddToAny Kit can set the icon) if set above,
    // or use addtoany_link_text if set.
    $button_text = (isset($button_text)) ? $button_text : check_plain(variable_get('addtoany_link_text', ''));
    if (strlen($button_text) > 0) {
      $button_text = ' ' . $button_text;
    }

    $button_innerHTML = ($button_setting == "text") ? $button_text : sprintf('<img src="%s"%s%s %s/>%s',
      $button_image,
      $button_width,
      $button_height,
      filter_xss(variable_get('addtoany_image_attributes', 'alt="Share this"'), array()),
      $button_text
    );

    $universal_button = sprintf('<a%s href="https://www.addtoany.com/share#url=%s&amp;title=%s">%s</a>',
      ' class="a2a_dd addtoany_share_save"',
      rawurlencode($link_url),
      rawurlencode($link_name),
      $button_innerHTML
    );
  }
  else {
    $universal_button = '';
  }

  // If doing AJAX
  if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
    $javascript = '';
    $kit_data_attrs = drupal_attributes(array('data-a2a-url' => $link_url, 'data-a2a-title' => $link_name));
    $kit_id_attr = '';
    $kit_target_classname = '';
  } else {
    $javascript = _addtoany_create_script($_addtoany_num, $link_name, $link_url);
    $kit_data_attrs = '';
    $kit_id_attr = ' id="da2a_' . $_addtoany_num . '"'; // The ID is later removed by JS (to support AJAX, previously).
    $kit_target_classname = ' a2a_target';
  }

  return ( sprintf('<span class="a2a_kit%s%s addtoany_list"%s%s>
      %s
      %s
      %s
    </span>
    %s',
    ($buttons_size != '16') ? ' a2a_kit_size_' . $buttons_size : '',
    $kit_target_classname,
    $kit_id_attr,
    $kit_data_attrs,
    ($additional_html_placement == 'before') ? $additional_html : '',
    $universal_button,
    ($additional_html_placement == 'after') ? $additional_html : '',
    $javascript
  ));
}

/**
 * Implements hook_ds_fields_info().
 */
function addtoany_ds_fields_info($entity_type) {
  $fields['node']['addtoany'] = array(
    'title' => t('AddToAny buttons'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function' => 'addtoany_ds_buttons_field',
  );
  return $fields;
}

/**
 * Returns the addtoany buttons field.
 */
function addtoany_ds_buttons_field($field) {
  $entity = $field['entity'];
  return addtoany_create_node_buttons($entity);
}
