<?php

/**

* WDC Like Dislike

* @author WDC <contact@web-designs-company.com>

* @link http://www.web-designs-company.com/

* @copyright (C) 2011 - 2012 JExtension

* @license GNU/GPL, see LICENSE.php for full license.

**/





// Check to ensure this file is included in Joomla!

defined('_JEXEC') or die('Restricted access');



jimport('joomla.application.component.model');





class wdclikedislikeModelwdclikedislike extends JModel

{

	/**

	 * Constructor that retrieves the ID from the request

	 *

	 * @access	public

	 * @return	void

	 */

	function __construct()

	{

		parent::__construct();



		$array = JRequest::getVar('cid',  0, '', 'array');

		$this->setId((int)$array[0]);

	}



	function setId($id)

	{

		// Set id and wipe data

		$this->_id		= $id;

		$this->_data	= null;

	}



	function &getData()

	{

		$row =& $this->getTable();

		$row->load( $this->_id );

		return $row;



	}



	// save

	function store()

	{

		global $mainframe;



		// Check for request forgeries

		JRequest::checkToken() or jexit( 'Invalid Token' );



		$row =& $this->getTable();

		// Bind the form fields

		$post	= JRequest::get( 'post' );



		if (!$row->bind($post)) {

			return 0;

		}

		// Make sure data is valid

		if (!$row->check())

		{

			return 0;

		}

		// Store it

		if (!$row->store())

		{

			return 0;

		}

		return $row->id;

	}

	function publish()

	{

		// Check for request forgeries

		JRequest::checkToken() or jexit( 'Invalid Token' );



		// Initialize variables

		$db			=& JFactory::getDBO();

		$user		=& JFactory::getUser();

		$cid		= JRequest::getVar( 'cid', array(), 'post', 'array' );

		$task		= JRequest::getCmd( 'task' );

		$publish	= ($task == 'publish');

		$result     = ($task == 'publish')?1:2;

		$neg_result     = ($task == 'publish')?-1:-2;

		$n			= count( $cid );



		if (empty( $cid )) {

			return 0;

		}



		JArrayHelper::toInteger( $cid );

		$cids = implode( ',', $cid );



		$query = 'UPDATE #__cvote'

		. ' SET state = ' . (int) $publish

		. ' WHERE id IN ( '. $cids.'  )'

		;

		$db->setQuery( $query );

		if (!$db->query()) {

			return $neg_result;

		}

		else {

			return $result;

		}

	}



	function remove()

	{

		// Check for request forgeries

		JRequest::checkToken() or jexit( 'Invalid Token' );



		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );



		$row =& $this->getTable();



		if (count( $cids ))

		{

			foreach($cids as $cid)

			{

				if (!$row->delete( $cid ))

				{

					return false;

				}

			}

		}

		return true;

	}





	function getResponses_like(){



		global $mainframe;

		jimport('joomla.user.helper');

		jimport( 'joomla.database.table.user' );

		JTable::addIncludePath(JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'database'.DS.'table');

		$db 							= & JFactory::getDBO();

		$user 							= & JFactory::getUser();

	 	$remote_ip 						= $_SERVER['REMOTE_ADDR'];



		   $post	= JRequest::get('post');



		   $post['lvote']=JRequest::getVar('lvote');

		   $post['artid']=JRequest::getVar('artid');

		   $post['dvote']=JRequest::getVar('dvote');



		   if(JRequest::getVar('dtext')!=""){

		   		$post['dtext']= JRequest::getVar('dtext');

		   }

		   if(JRequest::getVar('ltext')!=""){

		   		$post['ltext']= JRequest::getVar('ltext');

		   }



			if($post['lvote']!=""){

				$post['lvote']=JRequest::getVar('lvote');

			}else{

				$post['lvote']=0;

			}

			if($post['dvote']!=""){

				$post['dvote']=JRequest::getVar('dvote');

			}else{

				$post['dvote']="0";

			}



	 	$like = @$post['lvote'];

	 	$ltext =@$post['ltext'];

	 	$dlike=@$post['dvote'];

	 	$dtext=@$post['dtext'];

	 	$article_id=@$post['artid'];



		$doc =& JFactory::getDocument();

		$document =& JFactory::getDocument();

		$document->addStyleSheet(JURI::ROOT().'plugins/content/wdc_assets/style.css');



		$select_ip = " SELECT ip FROM #__cvote WHERE ip = '$remote_ip'";

		$db->setQuery($select_ip);$ip_result = $db->loadResult();



		if($ip_result==$remote_ip){

			$like_result	= $this->getResponsecount_like($article_id);

			echo "<b>".$like_result."</b>";



		}else{

			$in="INSERT INTO #__cvote (`id`,`like`,`ltext`,`dlike`,`dtext`,`ip`,`article_id`) VALUES('','$like','$ltext','$dlike','$dtext','$remote_ip','$article_id')";

			$db->setQuery($in);$db->Query();

			$like_result	= $this->getResponsecount_like($article_id);

		 	echo $like_result;

		}



	exit;

	}







		function getResponses_dlike(){



		global $mainframe;

		jimport('joomla.user.helper');

		jimport( 'joomla.database.table.user' );

		JTable::addIncludePath(JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'database'.DS.'table');



		$db 							= & JFactory::getDBO();

		$user 							= & JFactory::getUser();



	 	$remote_ip 						= $_SERVER['REMOTE_ADDR'];



		   $post	= JRequest::get('post');



		   $post['lvote']=JRequest::getVar('lvote');

		   $post['artid']=JRequest::getVar('artid');

		   $post['dvote']=JRequest::getVar('dvote');



		   if(JRequest::getVar('dtext')!=""){

		   		$post['dtext']= JRequest::getVar('dtext');

		   }

		   if(JRequest::getVar('ltext')!=""){

		   		$post['ltext']= JRequest::getVar('ltext');

		   }



			if($post['lvote']!=""){

				$post['lvote']=JRequest::getVar('lvote');

			}else{

				$post['lvote']=0;

			}

			if($post['dvote']!=""){

				$post['dvote']=JRequest::getVar('dvote');

			}else{

				$post['dvote']="0";

			}



	 	$like = @$post['lvote'];

	 	$ltext =@$post['ltext'];

	 	$dlike=@$post['dvote'];

	 	$dtext=@$post['dtext'];

	 	$article_id=@$post['artid'];



		$doc =& JFactory::getDocument();

		$document =& JFactory::getDocument();

		$document->addStyleSheet(JURI::ROOT().'plugins/content/wdc_assets/style.css');



		$select_ip = " SELECT ip FROM #__cvote WHERE ip = '$remote_ip'";

		$db->setQuery($select_ip);$ip_result = $db->loadResult();



		if($ip_result==$remote_ip){



			$dlike_result	= $this->getResponsecount_dlike($article_id);

		 	echo $dlike_result;

		}else{

			$in="INSERT INTO #__cvote (`id`,`like`,`ltext`,`dlike`,`dtext`,`ip`,`article_id`) VALUES('','$like','$ltext','$dlike','$dtext','$remote_ip','$article_id')";

			$db->setQuery($in);$db->Query();

			$dlike_result	= $this->getResponsecount_dlike($article_id);

		 	echo $dlike_result;

		}



		exit;

	}









	function getResponsecount_like($article_id ){



		$db    	= & JFactory::getDBO();



		$query 	= 'SELECT SUM( `like` ) AS `like`

FROM #__cvote WHERE article_id="'.$article_id.'"';



		$db->setQuery( $query );



		if (!$db->query())	{

			echo $db->getErrorMsg();

		}



		$count  	= $db->loadResult();



		return $count;

	}





		function getResponsecount_dlike($article_id ){



		$db    	= & JFactory::getDBO();



		$query 	= 'SELECT  SUM( `dlike` ) AS `dlike`

FROM #__cvote WHERE article_id="'.$article_id.'"';



		$db->setQuery( $query );



		if (!$db->query())	{

			echo $db->getErrorMsg();

		}



		$count  	= $db->loadResult();



		return $count;

	}



}

?>