<?php
/**
 * @file
 * Implement socialshare support for Drupal
 */
 
/**
 * Implements hook_theme().
 */
function socialloginandsocialshare_share_theme() {
  return array(
   'socialloginandsocialshare_social_share' => array(
      'variables' => array(
        'node' => NULL,
        'position' => NULL,
      ),
    ),
  );
}
/**
 * Implements hook_block_info().
 */
function socialloginandsocialshare_share_block_info() {
  $blocks['sharing'] = array(
    'info' => t('Social Sharing'),
  );
  return $blocks;
}
/**
 * Implements hook_block_view().
 */
function socialloginandsocialshare_share_block_view($delta) {
  $block = array();
  $output='';
 if (variable_get('socialloginandsocialshare_enable_horizontal_share') == 1 && variable_get('socialshare_horizontal_location') == 'block') {
   if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '1' || variable_get('socialloginandsocialshare_share_horizontal_images') == '2' || variable_get('socialloginandsocialshare_share_horizontal_images') == '3') {
     $output .= '<div class="lrsharecontainer"></div>';
   }
   else {
     $output .= '<div class="lrcounter_simplebox"></div>';
   }
 }
 if (variable_get('socialloginandsocialshare_enable_vertical_share') == 1 && variable_get('socialshare_vertical_location') == 'block') {
   if (variable_get('socialloginandsocialshare_share_vertical_images') == '4' || variable_get('socialloginandsocialshare_share_vertical_images') == '5' ) {
     $output .= '<div><div class="lrshareverticalcontainer"></div></div>';
   }
   else {
     $output .= '<div><div class="lrcounter_verticalsimplebox"></div></div>';
   }
 }
  $block['content'] = $output;
  return $block; 
}
/**
 * Theme social share.
 */
function theme_socialloginandsocialshare_social_share($node) {
  $output = "";
  $item = menu_get_object($type = 'node', $position = 1, $path = NULL);
  $item_nid = (!empty($item->nid) ? $item->nid : "");
  $item_title = (!empty($item->title) ? $item->title : "");
  $shareall_withexcept = variable_get('socialshare_show_pages');
  $node_withexcept = variable_get('socialshare_show_exceptpages');
  $node_except = explode(',', $node_withexcept);
  $vertical_shareall_withexcept = variable_get('socialshare_vertical_show_pages');
  $vertical_node_withexcept = variable_get('socialshare_vertical_show_exceptpages');
  $vertical_node_except = explode(',', $vertical_node_withexcept);
  $selected_share_interface=variable_get('socialloginandsocialshare_selected_share_interface');
  $share_title =  variable_get('socialshare_label_string');
   if (variable_get('socialloginandsocialshare_enable_horizontal_share') == 1) {
   if ($node['position'] == 'horizontal') {
     if ($shareall_withexcept == 0 ) {
       if (!in_array($item_nid, $node_except) AND !in_array($item_title, $node_except)) {
         if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '1' || variable_get('socialloginandsocialshare_share_horizontal_images') == '2' || variable_get('socialloginandsocialshare_share_horizontal_images') == '3') {
           $output .= '<div><div>' . $share_title . '</div><div class="lrsharecontainer"></div></div>';
         }
         else {
           $output .= '<div><div>' . $share_title . '</div><div class="lrcounter_simplebox"></div></div>';
         }
       }
     }
     elseif ($shareall_withexcept == 1) {
       if (in_array($item_nid, $node_except) OR in_array($item_title, $node_except)) {
         if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '1' || variable_get('socialloginandsocialshare_share_horizontal_images') == '2' || variable_get('socialloginandsocialshare_share_horizontal_images') == '3') {
           $output .= '<div><div>' . $share_title . '</div><div class="lrsharecontainer"></div></div>';
         }
         else {
           $output .= '<div><div>' . $share_title . '</div><div class="lrcounter_simplebox"></div></div>';
         }
       }
     }
   }
   }
   if (variable_get('socialloginandsocialshare_enable_vertical_share') == 1) {
    if ($node['position'] == 'vertical') {
     if ($vertical_shareall_withexcept == 0) {
       if (!in_array($item_nid, $vertical_node_except) AND !in_array($item_title, $vertical_node_except)) {
         if (variable_get('socialloginandsocialshare_share_vertical_images') == '4' || variable_get('socialloginandsocialshare_share_vertical_images') == '5' ) {
           $output .= '<div><div class="lrshareverticalcontainer"></div></div>';
         }
         else {
           $output .= '<div><div class="lrcounter_verticalsimplebox"></div></div>';
         }
       }
     }
     elseif ($vertical_shareall_withexcept == 1) {
       if (in_array($item_nid, $vertical_node_except) OR in_array($item_title, $vertical_node_except)) {
         if (variable_get('socialloginandsocialshare_share_vertical_images') == '4' || variable_get('socialloginandsocialshare_share_vertical_images') == '5' ) {
           $output .= '<div><div class="lrshareverticalcontainer"></div></div>';
         }
         else {
           $output .= '<div><div class="lrcounter_verticalsimplebox"></div></div>';
         }
       }
     }
    }
  }
  return $output;
}
/**
 * Implements hook_node_view().
 */
function socialloginandsocialshare_share_node_view($node, $view_mode, $langcode) {
  if (variable_get('socialloginandsocialshare_enable_share') == 1) {
    if (variable_get('socialloginandsocialshare_enable_horizontal_share') == 1 && variable_get('socialshare_horizontal_location') == 'content') {
      if (variable_get('socialshare_position_top', 1)) {
        $node->content['socialshare_top'] = array(
         '#markup' => theme('socialloginandsocialshare_social_share', array('node' => $node, 'position' => 'horizontal')), 
         '#weight' => intval(variable_get('socialshare_top_weight', -10)),
        );
      }
      if (variable_get('socialshare_position_bottom', 1)) {
        $node->content['socialshare_bottom'] = array(
          '#markup' => theme('socialloginandsocialshare_social_share', array('node' => $node, 'position' => 'horizontal')), 
          '#weight' => intval(variable_get('socialshare_bottom_weight', 10)), 
        );
      }
    }
    if (variable_get('socialloginandsocialshare_enable_vertical_share') == 1 && variable_get('socialshare_vertical_location') == 'content') {
      $node->content['socialshare'] = array(
         '#markup' => theme('socialloginandsocialshare_social_share', array('node' => $node, 'position' => 'vertical')), 
         '#weight' => 10,
      );
    }
  }
}
/**
*  Horizontal Social Sharing code.
*/
function socialloginandsocialshare_horizontal_script_code() {
//$selected_share_interface=variable_get('socialloginandsocialshare_selected_share_interface');
$share_providers =variable_get("socialloginandsocialshare_rearrange_providers_list");
$share_script='';
  if (empty($share_providers)) {
    variable_set('socialloginandsocialshare_rearrange_providers_list', array( "0" => "facebook", "1" => "googleplus", "2" => "google", "3" => "pinterest", "4" => "twitter", ));
  }
  if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '1' || variable_get('socialloginandsocialshare_share_horizontal_images') == '2' || variable_get('socialloginandsocialshare_share_horizontal_images') == '3') {
    $providers = implode('","', variable_get('socialloginandsocialshare_rearrange_providers_list'));
    if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '1') {
      $interface='horizontal';
    }
    else {
      $interface='simpleimage';
    }
    if (variable_get('socialloginandsocialshare_share_horizontal_images') == '0' || variable_get('socialloginandsocialshare_share_horizontal_images') == '2') {
      $size='32';
    }
    else {
      $size='16';
    } 
    $api_key =trim(diy_code_get_loginradius_apikey());
    $sharecounttype = (!empty($api_key) ? ('$u.apikey="' . $api_key . '";$u.sharecounttype=' . "'url'" . ';') : '$u.sharecounttype=' . "'url'" . ';'); 
    $share_script .= '<script type="text/javascript">LoginRadius.util.ready(function () { $i = $SS.Interface.' . $interface . '; $SS.Providers.Top = ["' . $providers . '"]; $u = LoginRadius.user_settings; ' . $sharecounttype . ' $i.size = ' . $size . ';$i.show("lrsharecontainer"); });'; 
  }
  else {
    $counter_providers =variable_get('socialcounter_rearrange_providers_list');
    if (empty($counter_providers)) {
      variable_set('socialcounter_rearrange_providers_list', array( "0" => "Facebook Like", "1" => "Google+ +1", "2" => "Pinterest Pin it" , "3" => "LinkedIn Share", "4" => "Facebook Send", ));
    }
    $show_provider_list=array();
    $provider_list= variable_get('socialcounter_rearrange_providers_list');
    $providers = implode('","', $provider_list);
    if (variable_get('socialloginandsocialshare_share_horizontal_images') == '8' || variable_get('socialloginandsocialshare_share_horizontal_images') == '9') {
      $interface='simple';
    }
    if (variable_get('socialloginandsocialshare_share_horizontal_images') == '8') {
     $type ='vertical';
    }
    else {
      $type ='horizontal';
    }
    $share_script .= '<script type="text/javascript">LoginRadius.util.ready(function () { $SC.Providers.Selected = ["' . $providers . '"]; $S = $SC.Interface.' . $interface . '; $S.isHorizontal = true; $S.countertype = \'' . $type . '\'; $S.show("lrcounter_simplebox"); });';
  }
  $share_script = str_replace('<script type="text/javascript">', '', $share_script);
  $share_script = str_replace('}); </script>', '});', $share_script);
  return $share_script;
}
/**
*  Vertical Social Sharing code.
*/
function socialloginandsocialshare_vertical_script_code() {
 $share_script='';
 if (variable_get('socialloginandsocialshare_share_vertical_images') == '4' || variable_get('socialloginandsocialshare_share_vertical_images') == '5') {
   $share_providers =variable_get("socialshare_vertical_rearrange_providers_list");
   if (empty($share_providers)) {
     variable_set('socialshare_vertical_rearrange_providers_list', array( "0" => "facebook", "1" => "googleplus", "2" => "google", "3" => "pinterest", "4" => "twitter", ));
   }
   $providers = implode('","', variable_get('socialshare_vertical_rearrange_providers_list'));
   $interface='Simplefloat';
   if (variable_get('socialloginandsocialshare_share_vertical_images') == '4') {
     $size='32';
   }
   else {
     $size='16';
   }
   $api_key = trim(diy_code_get_loginradius_apikey());
   $sharecounttype = (!empty($api_key) ? ('$u.apikey="' . $api_key . '";$u.sharecounttype=' . "'url'" . ';') : '$u.sharecounttype=' . "'url'" . ';');
   $share_script .= '<script type="text/javascript">LoginRadius.util.ready(function () { $i = $SS.Interface.' . $interface . '; $SS.Providers.Top = ["' . $providers . '"]; $u = LoginRadius.user_settings; ' . $sharecounttype . ' $i.size = ' . $size . ';';
   if (variable_get('socialshare_vertical_position') == '0') {
     $position1 = 'top';
     $position2 = 'left';
   }
   elseif (variable_get('socialshare_vertical_position') == '1') {
     $position1 = 'top';
     $position2 = 'right';
   }
   elseif (variable_get('socialshare_vertical_position')=='2') {
     $position1 = 'bottom';
     $position2 = 'left';
   }
   else {
     $position1 = 'bottom';
     $position2 = 'right';
   }
   $sharing_offset = variable_get('socialshare_vertical_topweight');
   if (isset($sharing_offset) && trim($sharing_offset) != "" && is_numeric($sharing_offset)) {
     $share_script .= '$i.top = \'' . trim($sharing_offset) . 'px\'; $i.' . $position2 . ' = \'0px\';$i.show("lrshareverticalcontainer"); });';
   }
   else {
     $share_script .= '$i.' . $position1 . ' = \'0px\'; $i.' . $position2 . ' = \'0px\';$i.show("lrshareverticalcontainer"); });';
   }
 }
 else {
   $counter_providers =variable_get('socialcounter_rearrange_providers_list');
   if (empty($counter_providers)) {
     variable_set('socialcounter_rearrange_providers_list', array( "0" => "Facebook Like", "1" => "Google+ +1", "2" => "Pinterest Pin it" , "3" => "LinkedIn Share", "4" => "Facebook Send", ));
   }
   $provider_list= variable_get('socialcounter_vertical_rearrange_providers_list');
   $providers = implode('","', $provider_list);
   if (variable_get('socialloginandsocialshare_share_vertical_images') == '6') {
     $type = 'vertical';
   }
   elseif (variable_get('socialloginandsocialshare_share_vertical_images') == '7') {
      $type = 'horizontal';
   }
   $share_script .= '<script type="text/javascript">LoginRadius.util.ready(function () { $SC.Providers.Selected = ["' . $providers . '"]; $S = $SC.Interface.simple; $S.isHorizontal = false; $S.countertype = \'' . $type . '\';';
   if (variable_get('socialshare_vertical_position') == '0') {
     $position1 = 'top';
     $position2 = 'left';
   }
   elseif (variable_get('socialshare_vertical_position') == '1') {
     $position1 = 'top';
     $position2 = 'right';
   }
   elseif (variable_get('socialshare_vertical_position')=='2') {
     $position1 = 'bottom';
     $position2 = 'left';
   }
   else {
     $position1 = 'bottom';
     $position2 = 'right';
   }
   $counter_offset=variable_get('socialshare_vertical_topweight');
   if (isset($counter_offset) && trim($counter_offset) != "" && is_numeric($counter_offset)) {
     $share_script .= '$S.top = \'' . trim($counter_offset) . 'px\'; $S.' . $position2 . ' = \'0px\';$S.show("lrcounter_verticalsimplebox"); });';
   }
   else {
     $share_script .='$S.' . $position1 . ' = \'0px\'; $S.' . $position2 . ' = \'0px\';$S.show("lrcounter_verticalsimplebox"); });';
   }
 }
 $share_script = str_replace('<script type="text/javascript">', '', $share_script);
 $share_script = str_replace('}); </script>', '});', $share_script);
 return $share_script;  
}