<?php
/*
Site:      shape5.com
Email:     contact@shape5.com
@license:  Copyrighted Commercial Software
@copyright (C) 2016 Shape 5 LLC
*/
// no direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
static $jsloaded;
class plgContentLikeMe extends JPlugin {
  protected $autoloadLanguage = true;
  public function onContentBeforeDisplay($context,&$row,&$params,$page = 0) {
	global $jsloaded;
    $parts = explode('.',$context);
    if($parts[0] != 'com_content') {
      return false;
    }
    $opts = $this->params->toArray();
    $pos = (int)$this->params->get('position', 0);
    $opacity = $this->params->get('opacity', '0.6');
    $opacity_hover = $this->params->get('opacity_hover', '1');
    $fill = $this->params->get('color', '#992929');
    $fill_hover = $this->params->get('color_hover', '#980000');
    $wh = $this->params->get('width_height', '28px');
    $extra_style = $this->params->get('extra_style', '');
    $anitime = $this->params->get('animationtime', '500');
    $num_style = 'float:right;';
    $num_color = $this->params->get('number_color', '#fff');
    $num_color_hover = $this->params->get('number_color_hover', '#fff');
    $num_inside = $this->params->get('number_inside', false);
    if ($pos == 1 || $pos == 3) {
      $num_style = 'float:left;';
    }
    if ($num_inside) {
      $num_style = 'position:absolute;top:0;left:0;width:'.$wh.';';
    }
    $db = JFactory::getDbo();
    if (count($_POST) && isset($_POST['like_me']) && isset($_POST['media'])) {
      ob_clean();
      $query = $db->getQuery(true)->select('*')->from($db->quoteName('#__likeme'))->where($db->quoteName('iid').'='.$db->quote($_POST['media']));
      $db->setQuery($query);
      $db->execute();
      $count = $db->getNumRows();
      $like = null;
      if ($count == 0) {
        $like = new stdClass();
        $like->iid = $_POST['media'];
        $result = $db->insertObject('#__likeme', $like);
        $query = $db->getQuery(true)->select('*')->from($db->quoteName('#__likeme'))->where($db->quoteName('iid').'='.$db->quote($_POST['media']));
        $db->setQuery($query);
        $db->execute();
        $like = $db->loadObject();
      } else {
        $like = $db->loadObject();
      }
      if (!$like) {
        return;
      }
      $like->likes = (int)$like->likes;
      //var_dump($like);
      $ip = '';
      if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        //check for ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // Check for the Proxy User
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
        $ip = $_SERVER['REMOTE_ADDR'];
      }
      $conditions = array($db->quoteName('ip').'='.$db->quote($ip), $db->quoteName('iid').'='.$db->quote($_POST['media']));
      $query = $db->getQuery(true)->select('id')->from($db->quoteName('#__likeme_votes'))->where($conditions);
      $db->setQuery($query);
      $db->execute();
      $vote_count = $db->getNumRows();
      if ($vote_count == 0) {
        $ulike = new stdClass();
        $ulike->ip = $ip;
        $ulike->iid = $_POST['media'];
        $result = $db->insertObject('#__likeme_votes', $ulike);
        //var_dump('insert vote', $result);
        $like->likes++;
        $db->updateObject('#__likeme', $like, 'id');
      } else {
        $query = $db->getQuery(true);
        $query->delete($db->quoteName('#__likeme_votes'));
        $query->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();
        //var_dump('remove vote', $result);
        $like->likes--;
        $db->updateObject('#__likeme', $like, 'id');
      }
      echo json_encode(array('likes' => $like->likes));
      jexit(0);
    }
    if (count($_POST) && isset($_POST['collect'])) {
      if(is_array($_POST['collect'])) {
        $in = array_map(function($a) use ($db) {
          return $db->quote($a);
        }, $_POST['collect']);
        $in = implode(',', $in);
        $query = $db->getQuery(true)->select('*')->from($db->quoteName('#__likeme'))->where($db->quoteName('iid') . " IN ($in)");
        $db->setQuery($query);
        $result = $db->loadObjectList();
        $votes = array();
        foreach($result as $k => $v) {
          $v->iid = str_replace(Juri::base(), '', $v->iid);
          $votes[$v->iid] = (int)$v->likes;
        }
        echo json_encode(array('likes' => $votes));
      }
      jexit(0);
    }
    if (!$jsloaded) {
      $opts = json_encode($opts/* + array('pageUrl' => $page_id, 'votes' => $votes)*/);
      $document = JFactory::getDocument();
      $document->addStyleDeclaration(".like-me-button{opacity:$opacity;-webkit-transition:all {$anitime}ms;-moz-transition:all {$anitime}ms;-o-transition:all {$anitime}ms;transition:all {$anitime}ms}
      .like-me-wrap:hover .like-me-button,.like-me-button:hover{opacity:$opacity_hover}
      .like-me-button a{outline:none}
      .like-me-button a div{font-size:13px;text-align:center;height:$wh;line-height:$wh;margin-top:auto;margin-bottom:auto;top:2px;color:$num_color;$num_style}
      .like-me-wrap:hover .like-me-button div,.like-me-button:hover div{color:$num_color_hover}
      .like-me-button svg{position:relative;fill:$fill;width:$wh;height:$wh;top:2px;$extra_style}
      .like-me-wrap:hover .like-me-button svg,.like-me-button:hover svg{fill:$fill_hover}");
      JHtml::_('jquery.framework');
      $document->addScript(Juri::base() . 'plugins/content/likeme/likeme.js');
      $document->addScriptDeclaration(";(function($){\$(function(){var opts=$opts;if(opts.icon)opts.icon=parseInt(opts.icon);if(opts.position)opts.position=parseInt(opts.position);$(opts.class).likeMe(opts)})})(jQuery);");
      $jsloaded = true;
    }
    return '';
  }
}
