<?php
/**
 * Adodis Blog package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// No direct access
defined('_JEXEC') or die;

// Base this model on the backend version.
require_once JPATH_ADMINISTRATOR.'/components/com_adodisblog/models/comment.php';

/**
 * Adodis Blog Component Form Model
 */
class AdodisBlogModelComment1 extends AdodisBlogModelComment
{
	/**
	 * Method to auto-populate the model state.
	 */
	protected function populateState()
	{
		$app					= JFactory::getApplication();

		// Load state from the request.
		$pk						= JRequest::getInt('id');
		$this->setState('comment.id', $pk);

		$this->setState('comment.catid', JRequest::getInt('catid'));

		$return					= JRequest::getVar('return', null, 'default', 'base64');
		$this->setState('return_page', base64_decode($return));

		// Load the parameters.
		$params					= $app->getParams();
		$this->setState('params', $params);

		$this->setState('layout', JRequest::getCmd('layout'));
	}

	/**
	 * Method to get Blog data.
	 */
	public function getItem($itemId = null)
	{
		// Initialise variables.
		$itemId				= (int) (!empty($itemId)) ? $itemId : $this->getState('comment.id');

		// Get a row instance.
		$table				= $this->getTable();

		// Attempt to load the row.
		$return				= $table->load($itemId);

		// Check for a table object error.
		if ($return === false && $table->getError()) {
			$this->setError($table->getError());
			return false;
		}

		$properties			= $table->getProperties(1);
		$value				= JArrayHelper::toObject($properties, 'JObject');

		// Convert attrib field to Registry.
		$value->params		= new JRegistry;

		// Compute selected asset permissions.
		$user				= JFactory::getUser();
		$userId				= $user->get('id');
		$asset				= 'com_adodisblog.comment.'.$value->id;

		// Check general edit permission first.
		if ($user->authorise('core.edit', $asset)) {
			$value->params->set('access-edit', true);
		}


		// Now check if edit.own is available.
		else if (!empty($userId) && $user->authorise('core.edit.own', $asset)) {
			// Check for a valid user and that they are the owner.
			if ($userId == $value->created_by) {
				$value->params->set('access-edit', true);
			}
		}

		// Check edit state permission.
		if ($itemId) {
			// Existing item
			$value->params->set('access-change', $user->authorise('core.edit.state', $asset));
		} else {
			// New item.
			$catId		= (int) $this->getState('comments.catid');
			if ($catId) {
				$value->params->set('access-change', $user->authorise('core.edit.state', 'com_adodisblog.category.'.$catId));
			} else {
				$value->params->set('access-change', $user->authorise('core.edit.state', 'com_adodisblog'));
			}
		}

		return $value;
	}

	/**
	 * Get the return URL.
	 */
	public function getReturnPage()
	{
		return base64_encode($this->getState('return_page'));
	}

	public function updatelikes($id,$likes,$ival){
		$db							= $this->getDbo();
		$query						= $db->getQuery(true);
		$user						= & JFactory::getUser();

		// to find the rated user
		$query->select('c.*');
		$query->from('#__adodisblog_comments as c');
		$query->where('c.id='.$id);
		$db->setQuery($query);
		$result 					= $db->loadObjectList();
		$isgood 					= $result[0]->isgood;
		$isbad						= $result[0]->isbad;

		$userid_check 				= $result[0]->rated_ids;
		$userid_check 				= explode(",",$userid_check);
		//condition to check the user existance
		if(!$user->id){
			echo JText::_('COM_ADODISBLOG_COMMENTS_LOGIN_ACCESS_LIKES_DISLIKES').',none';
		}
		else if(in_array($user->id,$userid_check)){
			echo JText::_('COM_ADODISBLOG_COMMENTS_ALREADY_VOTED').',none';
		}else{
			$query					= $db->getQuery(true);
			$query->update('`#__adodisblog_comments`');
			if($likes == '1' ){
				$addisgood 			= $isgood+1;
				$query->set("isgood = ".$addisgood);
			}else{
				$addisbad 			= $isbad+1;
				$query->set("isbad = ".$addisbad);
			}
			$query->where('id ='.$id);
			$db->setQuery($query);
			if($db->query()){

				//to display the number of likes

				$query->select('c.*');
				$query->from('#__adodisblog_comments as c');
				$query->where('c.id='.$id);
				$db->setQuery($query);
				$new_result 		= $db->loadObjectList();

				// update user id in comments

				$rated_ids 			= $new_result[0]->rated_ids;
				if($rated_ids){
					$updated_ids 	= $rated_ids.",".$user->id;
				}else{
					$updated_ids 	= $user->id;
				}
				$query				= $db->getQuery(true);
				$query->update('`#__adodisblog_comments`');
				$query->set("rated_ids = '$updated_ids'");
				$query->where('id ='.$id);
				$db->setQuery($query);
				$db->query();

				// display likes and dislikes
				$output 			= $new_result[0]->isgood - $new_result[0]->isbad;

				echo JText::_('COM_ADODISBLOG_COMMENTS_LIKES').$new_result[0]->isgood." ".JText::_('COM_ADODISBLOG_COMMENTS_DISLIKES').$new_result[0]->isbad.','.$output;
			}
			return $new_result;
		}
	}

}