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

	jimport('joomla.html.pagination');

class jegroupbuyViewcategory  extends JView
{
	function display( $tpl = null )
	{
		global $mainframe;

		$db		 			= & JFactory::getDBO();
		$user				= & JFactory::getUser();
		$params				= & $mainframe->getParams('com_content');
	 	$cid 				= JRequest::getInt("catid","");

	 	$query = " SELECT show_footer FROM #__jegroupbuy_settings WHERE  id = 1";
		$db->setQuery($query);

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$settings			= $db->loadResult();

		$limit =  JRequest::getVar('limit', '', '', 'int');

		$limitstart 		= JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart 		= ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);


        // function for total count
			$total     			= $this->getTotal();
			$pageNav   			= new JPagination( $total, $limitstart, $limit );
			$limit				= $pageNav->limit;
			$limitstart		    = $pageNav->limitstart;

			// function for get testimonials
			$rows     			= $this->getProduct($limitstart,$limit);

		$query = " SELECT layout FROM #__jegroupbuy_settings WHERE  id = 1";
		$db->setQuery($query);

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}

		$layout			= $db->loadResult();

		if($layout == "")
		{
			$layout = "default";
		}
		$this->_layout = $layout;

		$where = array();
		if($cid !="")
		{
			$where[] = " catid = ".$cid;
		}
		$where[] = " published = 1";
		$where		= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
		$sql = "SELECT * FROM #__jegroupbuy_product "
			   . $where
			   . " ORDER BY featured DESC";
		$db->setQuery($sql);

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows = $db->loadObjectList();

		$query 				= " SELECT count(*) FROM #__jegroupbuy_category ";
		$db->setQuery( $query );

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$total				= $db->loadResult();

		$this->assignRef('items',		$rows);
		$this->assignRef('cid',			 $cid);
		$this->assignRef('params',		$params);
		$this->assignRef('settings',	$settings);
		$this->assignRef('pageNav',		$pageNav);

		parent::display($tpl);
	}

	function limit_characters ( $str, $n )
	{
		if ( strlen ( $str ) <= $n )
		{
			return $str;
		}
		else {
			return substr ( $str, 0, $n ) . '...';
		}
	}

	function getProduct($limitstart,$limit)
	{

		$db 				=& JFactory::getDBO();
		$cid 				= JRequest::getInt("cid","");
		$where = array();
		if($cid !="")
		{
			$where[] 		= " catid = ".$cid;
		}
		$where[] 			= " published = 1";
		$where				= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
		$sql 				= "SELECT * FROM #__jegroupbuy_product "
			   					. $where
			   					. " ORDER BY featured DESC";

		$db->setQuery( $sql, $limitstart, $limit );
		$rows = $db->loadObjectList();

		return $rows;
	}

	function getTotal()
	{
		$where = array();
		$db    = & JFactory::getDBO();

		$where[]    = 's.published = 1';   // while published
		$testimonial_id = JRequest::getInt('id');

		if ( $testimonial_id > 0 && is_numeric ( $testimonial_id ) ) {
			$where[]   		= 's.id = '.$db->Quote( $testimonial_id );
		}

		$where		= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';

		// get the total number of records
		$query = 'SELECT count(*) FROM #__jegroupbuy_product AS s '
			. $where
			;
		$db->setQuery( $query );
		if (!$db->query()) {
			echo $db->getErrorMsg();
		}

		$total = $db->loadResult();

		return $total;
	}
}