<?php
/*------------------------------------------------------------------------
# plg_jo_socialshare - JO Social Share for Joomla 1.6, 1.7, 2.5 Plugin
# -----------------------------------------------------------------------
# author: http://www.joomcore.com
# copyright Copyright (C) 2011 Joomcore.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.joomcore.com
# Technical Support:  Forum - http://www.joomcore.com/Support
-------------------------------------------------------------------------*/

// no direct access
defined('_JEXEC') or die('Restricted access');

class plgContentJo_socialshare extends JPlugin 
{
    public function onContentBeforeDisplay($context, &$row, &$params, $page = 0)
    {    
    	$document = JFactory::getDocument();
		$view   = JRequest::getVar('view');
		$layout = JRequest::getVar('layout');
		$option = JRequest::getVar('option');
		
        if($option == 'com_content'){
        	
        	$frontPage = $this->params->get('fb_frontpage', 0);
			if (!$frontPage && $view == "featured"){
				return ;
			}	
			//show or hide plugin on categorys
			$category = $this->params->get('fb_category', 0);
			if(!$category && (($view == "categories")||($view == "category"))){
				return ;
			}
            //hide plugin on category
            $exclusion_categories = $this->params->get('fb_categories', 0);
            if(!empty($exclusion_categories)){
                if(strlen(array_search($row->catid, $exclusion_categories))){
                    return false;
                }
            }
            
            //hide plugin on article 
            $exclusion_articles = $this->params->get('fb_articles', '');
            $articlesArray = explode(",",$exclusion_articles);
            if(!empty($exclusion_articles)){
                if(strlen(array_search($row->id, $articlesArray))){
                    return false;
                }
            }
            //plugin
            require_once(JPATH_BASE.'/components/com_content/helpers/route.php');    
            $Itemid = JRequest::getVar("Itemid","1");
            if($row->id){
                $link = JRoute::_(ContentHelperRoute::getArticleRoute($row->id, $row->catid));
                $jURI = JURI::getInstance();
                $link = $jURI->getScheme()."://".$jURI->getHost().$link;
            }else{
                $jURI = JURI::getInstance();
                $link = $jURI->toString();
            }
            
            //setting css 
            $fblike_type = $this->params->get('fblike_type');
            if($fblike_type == 'button_count'){
                $widthtype= 90;
                $heighttype = 28;
            }else{
                $widthtype= 60;
                $heighttype = 70;
            }
            
            $twitter_type = $this->params->get('twitter_type');
            if($twitter_type == 'Horizontal Count'){
                $widthweet= 96;
            }else{
                $widthweet= 60;
            }
            
            $linkedin_type = $this->params->get('linkedin_type');
            if($linkedin_type == 'top'){
                $margin_top = 3;
            }else{
                $margin_top='';
            }
            
            
            
            //code plugin
            $html  = '
            	<style type="text/css" media="screen">
					.jo-social-share iframe{
						max-width: none;
					}
				</style>				
            ';
            
            if($this->params->get('facebooksend') ==1 || $this->params->get('fblike')){
				$html .='
					<div id="fb-root"></div>
					<script>(function(d, s, id) {
							var js, fjs = d.getElementsByTagName(s)[0];
							if (d.getElementById(id)) return;
							js = d.createElement(s); js.id = id;
							js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=282604855153215";
							fjs.parentNode.insertBefore(js, fjs);
						}(document, "script", "facebook-jssdk"));
					</script>
				';
			}
            
            $html .= '<div style="clear:both;"></div>';
            $html .= '<div class="jo-social-share" style="padding-top: 10px; float: '.$this->params->get('fb_align', 'left').';">';
            
         
            
            if($this->params->get('fblike')==1){
                $html .= '<div style="margin-left: 10px; width: '.$widthtype.'px; height :'.$heighttype.'px; float: left;">';
                $html .= '<div class="fb-like" data-href="'.$link.'" data-send="false" data-layout="'.$fblike_type.'" data-width="75" data-show-faces="false"></div>';
                $html .= '</div>';
            }else{
                $html .='';
            }
            
               if($this->params->get('facebooksend')==1){
            $html .= '
            
            <div class="jo_send" style="float: left;"><div class="fb-send" data-href="'.$link.'"></div></div>';
            }else{
                $html .='';
            }
            
            if($this->params->get('twitter')==1){
                $html .='<div style="width: '.$widthweet.'px !important; float: left; margin-left: 10px;">';
                $html .= '<a href="http://twitter.com/share" class="twitter-share-button" style="width: '.$widthweet.'px;" data-url="'.$link.'" data-count="'.$twitter_type.'" >Twitter</a><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>'; 
                $html .= '</div>';
            }else{
                $html .='';
            }
            
            if($this->params->get('linkedin')==1){
                $html .='<div style="float: left; margin-left: 10px; border: none;">';
                $html .= '<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="IN/share" data-url="' . $link . '" data-counter="'.$this->params->get('linkedin_type').'"></script>'; 
                $html .= '</div>';
            }else{
                $html .='';
            }
            if($this->params->get('pin')==1){
                $html .='<div style="float: left; margin-left: 10px; border: none;">';
                $html .= '
                <a href="http://pinterest.com/pin/create/button/?url='.urlencode($link).'" class="pin-it-button" count-layout="'.$this->params->get("pin_type").'"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>
                <script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>'; 
                $html .= '</div>';
            }else{
                $html .='';
            }
            
            
            $html .= '</div>';
            $html .= '<div style="clear:both;"></div>';
            //end plugin
            
            //show plugin
            $position = $this->params->get('fb_position', 'above');
            
            if($position == 'above'){
                if($row->fulltext == ''){
                    $row->text = $html . $row->text;
                    $row->introtext = $html . $row->introtext;
                }else{
                    $row->text = $html . $row->text;
                }
            }else{
                $row->text .= $html;
                $row->introtext .= $html;
            }
        }
        return; 
    }
}
?>