<?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 AdodisBlogTableBlog extends JTable
{
	public function __construct(& $db)
	{
		parent::__construct( '#__adodisblog_blogs', 'id', $db );
	}

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

	/**
	 * Stores a Blog
	 */
	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();

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

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

			// Change the order from old to new..
			if ($oldrow->published>=0 && ($this->published < 0 || $oldrow->catid != $this->catid)) {
				$this->ordering				= self::getNextOrder('`catid`=' . $this->_db->Quote($this->catid).' AND published>=0');
			}

			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');
			}

		} else {

			if (!intval($this->created_date)) {
				 $this->created_date		= $date->toMySQL();
			}
			if(!$this->created_by){
				$this->created_by			= $user->get('id');
			}

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

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

			// Set ordering to last if ordering was 0
			$this->ordering					= self::getNextOrder('`catid`=' . $this->_db->Quote($this->catid).' AND published>=0');


			// Mail to Admin when user the post Blogs
			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_b_r', 0)) {
						$this->published	= '1';
						$mg 				= 'r1';
					} else{
						$mg 				= 'r0';
					}
				} else {
					if($config->get('auto_publish_b_a', 0)) {
						$this->published	= '1';
						$mg 				= 'a1';
					} else {
						$mg					= 'a0';
					}
				}
			}

			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('Blog','AdodisBlogModel');
					if($uid) {
						$model->mailtoAdmin( $uemail, $uname, $catid, $this->published );
					} else{
						$model->mailtoAdmin( $this->e_mail, $this->name, $catid, $this->published );
					}
				}
				if($config->get('send_user', 1)) {
					$catid 					= $this->id;
					$uname 					= $user->get('name');
					$uemail 				= $user->get('email');
					$model					= JModel::getInstance('Blog','AdodisBlogModel');
					if($uid) {
						$model->mailtoUser( $uemail, $uname, $catid, $this->published );
					} else{
						$model->mailtoUser( $this->e_mail, $this->name, $catid, $this->published );
					}
				}

			}
			if($uid) {
				$model						= JModel::getInstance('Blog','AdodisBlogModel');
				if($config->get('integrate_joomsocial', 0)){
					if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_community')){
						$model->AddToJoomSocial( $uid, $this->id, $this->title, $this->content, $this->created_date);
					}
				}
				if($config->get('integrate_k2', 0)){
					if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_k2')){
						$model->AddToK2( $uid, $this->id, $this->title, $this->content, $this->created_date);
					}
				}
			}
		}

		$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()
	{
		// Set alias
		$this->alias			= JApplication::stringURLSafe($this->alias);
		if (empty($this->alias)) {
			$this->alias		= JApplication::stringURLSafe($this->title);
		}

		if (trim($this->content) == '') {
			$this->setError(JText::_('COM_ADODISBLOG_CONTENT_MUST_HAVE_TEXT'));
			return false;
		}

		return true;
	}
}
?>
