<?php
/**
 * jeGroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2010 - 2011 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/
// no direct access
defined('_JEXEC') or die('Restricted access');
// Import Joomla! libraries
jimport( 'joomla.application.component.view');

class jeGroupBuyViewproducts extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$db =& JFactory::getDBO();

		if($this->_layout != "form")
		{
			jimport('joomla.html.pagination');
			$context			= 'com_jegroupbuy.s.id.product.';
			$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
			$limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );
			$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order', 		'filter_order', 	'cd.ordering',	'cmd' );
			$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
			$filter_state		= $mainframe->getUserStateFromRequest( $context.'filter_state',		'filter_state',		'',				'word' );
			$search				= $mainframe->getUserStateFromRequest( $context.'search',			'search',			'',			'string' );
	 		JHTML::stylesheet('style.css', JURI::base() . 'components/com_jegroupbuy/assets/css/');
			JToolBarHelper::title(   JText::_( 'CC_JEGROUPBUY' ) . ': <small><small>[ ' .  JText::_( 'JE_GROUPBUY_PRODUCTS' ).' ]</small></small>', 'jegroupbuy.png' );
			JToolBarHelper::addNewX();
			JToolBarHelper::editListX();
			JToolBarHelper::deleteListX();
			JToolBarHelper::publishList();
			JToolBarHelper::unpublishList();
			$where = array();


			if ( $filter_state ) {
				if ( $filter_state == 'P' ) {
					$where[] 	= 's.state = 1';
				} else if ($filter_state == 'U' ) {
					$where[] 	= 's.state = 0';
				}
			}

			if ($search) {
				$where[] = 'LOWER(p.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			}

			// sanitize $filter_order
			if (!in_array($filter_order, array('p.title', 's.shop_name','c.name','p.deal_price', 'p.ordering','p.published', 'p.featured', 'p.id'))) {
				$filter_order = 'a.ordering';
			}
			if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
				$filter_order_Dir = '';
			}

			if ($filter_order == 'a.ordering'){
				$orderby 	= ' ORDER BY  p.ordering'.$filter_order_Dir;
			} else {
				$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' ,  p.ordering ';
			}
			$where		 = count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
			$sql = "SELECT count(*) FROM #__jegroupbuy_product AS p".
					" LEFT JOIN #__jegroupbuy_stores AS s ON s.id = p.store_id ".
					" LEFT JOIN #__jegroupbuy_category AS c ON c.id = p.catid ".
					$where.
					 $orderby;
			$db->setQuery($sql);
			$total = $db->loadResult();
			$pageNav = new JPagination( $total, $limitstart, $limit );
			$sql = "SELECT p.*,s.shop_name,c.name AS category_name FROM #__jegroupbuy_product AS p".
					" LEFT JOIN #__jegroupbuy_stores AS s ON s.id = p.store_id ".
					" LEFT JOIN #__jegroupbuy_category AS c ON c.id = p.catid ".
					$where.
					$orderby;
			$db->setQuery( $sql, $pageNav->limitstart, $pageNav->limit );
			$rows = $db->loadObjectList();
			$state = $this->get('State');

			// table ordering
			$lists['order_Dir']	= $filter_order_Dir;
			$lists['order']		= $filter_order;
			$lists['state']		= $filter_state;
			$lists['search']	= $search;

			$this->assignRef("items",$rows);

			$this->assignRef('pageNav',		$pageNav);
			$this->assignRef('lists',		$lists);

		}else
		{
			$model  			= & $this->getModel();
			$row  = & $this->get('Data');
			$shopname			= $model->getShopName();
			$category			= $model->getCategoryName();
			$context			= 'com_jegroupbuy.s.id.product.';
	 		JHTML::stylesheet('style.css', JURI::base() . 'components/com_jegroupbuy/assets/css/');
	 		$text 		  = $row->id ? JText::_( 'JE_GROUPBUY_PRODUCTS_EDIT' ) : JText::_( 'JE_GROUPBUY_PRODUCTS_NEW' );
			JToolBarHelper::title(   JText::_( 'CC_JEGROUPBUY' ) . ': <small><small>[ ' .  $text.' ]</small></small>', 'jegroupbuy.png' );
			JToolBarHelper::apply();
			JToolBarHelper::save();
			if ($row->id)	{
				JToolBarHelper::cancel( 'cancel', 'Close' );
			} else	{
				JToolBarHelper::cancel();
			}


			
			$currency_list = array(
				JHTML::_('select.option', '1',JText::_('JEGROUPBUY_DOLLAR')),
				JHTML::_('select.option', '2',JText::_('JEGROUPBUY_POUNDS')),
				JHTML::_('select.option', '3',JText::_('JEGROUPBUY_YEN')),
				JHTML::_('select.option', '4',JText::_('JEGROUPBUY_EURO')),
				JHTML::_('select.option', '5',JText::_('JEGROUPBUY_RUPEE'))
				);
			// Imagelist
			$javascript			= 'onchange="changeDisplayImage();"';
			$directory			= '/images/jegroupbuy/category';
			$lists['imageurl']	= JHTML::_('list.images',  'imageurl', $row->imageurl, $javascript, $directory, "bmp|gif|jpg|png|swf"  );

			$this->assignRef('row',$row);
			$this->assignRef('lists',$lists);
			$this->assignRef('shopname',	$shopname);
			$this->assignRef('currencylist', $currency_list);
			$this->assignRef('category',	$category);
		}
		parent::display($tpl);
	}
}
?>