<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 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.modelitem');

class jegroupbuyModelCategory extends JModelItem
{
	/**
	 * total faqs
	 */
	var $_total = 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_jegroupbuy.limit', 'limit', $config->get('list_limit'), 'int'));
			$this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
	}

	/**
	 * Method to get FAQ data.
	 */
	public function getItems()
	{
		// Connect db
		$db			= $this->getDbo();
		$query		= $db->getQuery(true);

		$cid 		= JRequest::getInt("catid","");

		$query->select('p.*');
		$query->from('#__jegroupbuy_product AS p');
		if($cid > 0)
		{
			$query->where('p.catid ='.$cid);
		}
		$query->order($db->getEscaped('p.featured DESC'));

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

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

		if( empty($groupbuy) )
		{
			JError::raiseNotice(404, JText::_('COM_JEGROUPBUY_ERROR_PRODUCTS_NOT_FOUND'));
		} else
		{
			//$query->where('p.published = 1');
			$products			= $db->loadObjectList();

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

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

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

			return $this->_data;
		}
	}
	
	/** 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;
	}

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