<?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.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

class AdodisBlogTableComment extends JTable
{
	public function __construct(& $db)
	{
		parent::__construct( '#__adodisblog_comments', 'id', $db );
	}

	/**
	 * Overloaded bind function
	 */
	public function bind($array, $ignore = '')
	{
		return parent::bind($array, $ignore);
	}

	/**
	 * Stores a faq
	 */
	public function store($updateNulls = false)
	{
		// Joomla predefined functions.
		$date									= & JFactory::getDate();
		$user									= & JFactory::getUser();
		$config									= JComponentHelper::getParams('com_adodisblog');

		if ($this->id) {

			// Existing item
			$this->modified_date				= $date->toMySQL();
			$this->modified_by					= $user->get('username');

			// Capture ip Address
			if (empty($this->ip_address)) {
	         	$this->ip_address		 		= $_SERVER['REMOTE_ADDR'];
	        }

			// Get the old row
				$oldrow 						= JTable::getInstance('Comment', 'AdodisBlogTable');
			if (!$oldrow->load($this->id) && $oldrow->getError()) {
				$this->setError($oldrow->getError());
			}

			parent::store($updateNulls);

			// Reorder the oldrow
			if ($oldrow->published>=0 && ($this->published < 0 || $oldrow->catid != $this->catid)) {
				$this->reorder('`catid`=' . $this->_db->Quote($oldrow->catid).' AND published>=0');
			}
			$mg=0;
		} else {

			if (!intval($this->created_date)) {
				 $this->created_date			= $date->toMySQL();
			}

			if(!$this->user_id){
				$this->user_id					= $user->get('id');
			}

			if (empty($this->language)) {
				$this->language					= '*';
	        }

			// Capture ip Address
	         if (empty($this->ip_address)) {
	         	$this->ip_address		 		= $_SERVER['REMOTE_ADDR'];
	         }

			// Mail to Admin when user post the Comments
			require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_adodisblog'.DS.'helpers'.DS.'adodisblog.php');
			$canDo 								= AdodisBlogHelper::getActions();
			if ($canDo->get('core.admin')) {
			} else {
				$uid 							= $user->get('id');
					if($uid)	{
						if($config->get('auto_publish_c_r', 0)) {
							$this->published	= '1';
							$mg 				= 'r1';
						} else{
							$mg 				= 'r0';
						}
					} else {
						if($config->get('auto_publish_c_a', 0)) {
							$this->published	= '1';
							$mg 				= 'a1';
						} else {
							$mg 				= 'a0';
						}
					}
			}

			if($this->blog_id=='') {
				$this->blog_id	 				= $_POST['blogid'];
			}
			if($_POST['frontend']){
					$this->comment	 			= '<p>'.$this->comment.'</p>';
			}
			parent::store($updateNulls);

			if ($canDo->get('core.admin')) {
			}else{
				if($config->get('send_admin', 1)) {
					$catid 						= $this->id;
					$uname 						= $user->get('name');
					$uemail 					= $user->get('email');
					$model						= JModel::getInstance('Comment','AdodisBlogModel');
					if($uid) {
						$model->mailtoAdmin( $uemail, $uname, $catid, $this->published, $this->blog_id );
					} else{
						$model->mailtoAdmin( $this->email, $this->name, $catid, $this->published, $this->blog_id );
					}
				}

				if($config->get('send_user', 1)) {
					$catid 						= $this->id;
					$uname 						= $user->get('name');
					$uemail 					= $user->get('email');
					$model						= JModel::getInstance('Comment','AdodisBlogModel');
					if($uid) {
						$model->mailtoUser( $uemail, $uname, $catid, $this->published, $this->blog_id );
					} else{
						$model->mailtoUser( $this->email, $this->name, $catid, $this->published, $this->blog_id );
					}
				}
			}
		}
		$session 								= JFactory::getSession();
		if($mg=='r1'){
			$session->set('publish_state', 'r1');
		} else if($mg=='r0'){
			$session->set('publish_state', 'r0');
		} else if($mg=='a1'){
			$session->set('publish_state', 'a1');
		} else if($mg=='a0'){
			$session->set('publish_state', 'a0');
		} else{
			$session->set('publish_state', 0);
		}

		// Attempt to store the data.
		return count($this->getErrors())== 0;

	}

	function check()
	{
		if (trim($this->comment) == '') {
			$this->setError(JText::_('COM_ADODISBLOG_COMMENT_MUST_HAVE_TEXT'));
			return false;
		}

		return true;
	}
}
?>
