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

// Import Joomla predefined functions
jimport('joomla.application.component.modelform');



class AdodisBlogModelBlog extends JModelForm
{
	/**
	 * total Blogs
	 */
	var $_total								= null;

	/**
	 * Pagination object
	 */
	var $_pagination						= null;

	/**
	 * Constructor
	 */
	function __construct()
	{
		parent::__construct();

		//Get configuration
		$app								= JFactory::getApplication();
		$config								= JFactory::getConfig();

		// Get the pagination request variables
		$this->setState('limit', $app->getUserStateFromRequest('com_adodisblog.limit', 'limit', $config->get('list_limit'), 'int'));
		$this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
	}

	protected function populateState($ordering = null, $direction = null)
	{
		$settings							= $this->getSettings();
		$order_by							= $settings->orderby;
		$sort_by							= $settings->sortby;

		$order								= 'ct.'.'id';

		$this->setState('list.ordering', $order);

		$this->setState('list.direction', $sort_by);
	}

	/**
	 * Method to get Blogs data.
	 */
	public function getItems()
	{
		$blogId = JRequest::getVar('id');

		if($blogId)
		{
			// Connect db
			$db								= $this->getDbo();
			$query							= $db->getQuery(true);

			$query->select('bg.id AS blogid, bg.name AS post_by_name, bg.isgood AS bg_isgood, bg.isbad AS bg_isbad, bg.*');
			$query->from('#__adodisblog_blogs AS bg');
			$query->where('bg.id = '.$blogId);

			$query->select( 'u.*' );
			$query->join( 'LEFT', '#__adodisblog_users AS u ON u.user_id = bg.created_by ' );

			$query->select( 'cat.title AS category_title' );
			$query->join( 'LEFT', '#__categories AS cat ON cat.id = bg.catid' );

			$query->select( 'ct.id AS ct_id, ct.comment AS ct_comment, ct.created_date AS ct_created_date, ct.name AS ct_comment_by_name, ct.website AS ct_website, ct.isgood - ct.isbad AS total_likes' );
			$query->join( 'LEFT', '#__adodisblog_comments AS ct ON ct.blog_id = bg.id AND ct.published = 1' );

			$query->select( 'un.name AS ct_comment_by' );
			$query->join( 'LEFT', '#__adodisblog_users AS un ON un.user_id = ct.user_id ' );

			$query->select( 'cmt.count_comment' );
			$query->join( 'LEFT', '(SELECT cmt.blog_id , count(cmt.blog_id) AS count_comment FROM #__adodisblog_comments AS cmt WHERE cmt.published=1 GROUP By cmt.blog_id) as cmt ON cmt.blog_id = bg.id' );

			$db->setQuery($query);
			$blog							= $db->loadObjectList();

			if ($error = $db->getErrorMsg()) {
				throw new Exception($error);
			}

			if( empty($blog) ) {
				JError::raiseNotice(404, JText::_('COM_ADODISBLOG_ERROR_BLOGS_NOT_FOUND'));
			} else {
				$query->where('bg.published = 1');
				$query->order($db->getEscaped($this->getState('list.ordering', 'bg.ordering')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));

				$blogs						= $db->loadObjectList();

				if( empty( $blogs )) {
					JError::raiseNotice(404, JText::_('COM_ADODISBLOG_ERROR_BLOGS_NOT_PUBLISHED'));
				}

				$this->_total				= count($blogs);

				if ($this->getState('limit') > 0) {
					$this->_data			= array_splice($blogs, $this->getState('limitstart'), $this->getState('limit'));
				} else {
					$this->_data			= $blogs;
				}

				return $this->_data;

			}
		} else {
			return 0;
		}
	}

	/**
	 * Method to get the total number of weblink items for the category
	 */
	function getTotal()
	{
		return $this->_total;
	}

	/**
	 * Method to get a pagination object of the weblink items for the category
	 */
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination		= new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_pagination;
	}

	public function getSettings()
	{
		$id					= 1;
		$settings  			= JTable::getInstance('Settings', 'AdodisBlogTable');
		$settings->load($id);

		return $settings;
	}

	public function activateBlog(){

		$id					= JRequest::getInt('tid');
		$table  			= JTable::getInstance('Blog', 'AdodisBlogTable');

		$table->load($id);
		$table->published 	= '1';

		if (!$table->store()) {
			return false;
		} else {
			return true;
		}
	}

	//load Form
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_adodisblog.comment', 'comment', array('control' => 'jform', 'load_data' => $loadData));

		if (empty($form)) {
			return false;
		}
		return $form;
	}
	protected function loadFormData()
	{

		return $this->getData();
	}

	public function getData()
	{
		$this->data = '';

		return $this->data;
	}

	public function checkUserBlock()
	{
		$user			= & JFactory::getUser();
		$uid			= $user->get('id');

		if($uid){
			$db			= $this->getDbo();
			$query		= $db->getQuery(true);

			$query->select('ur.block');
			$query->from('#__adodisblog_users AS ur');
			$query->where('ur.user_id ='.$uid);
			$db->setQuery($query);
			$block		= $db->loadResult();
			return $block;
		}
	}
}
?>
