<?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 jeGroupBuyVieworders extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$db =& JFactory::getDBO();
		if($this->_layout != "form")
		{
			jimport('joomla.html.pagination');
			$context			= 'com_jegroupbuy.s.id.orders.';
			$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_ORDERS' ).' ]</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(o.transaction_id) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			}

			// sanitize $filter_order
			if (!in_array($filter_order, array('p.title', 'o.published', 'o.id', 'o.method', 'o.transaction_id'))) {
				$filter_order = 'id';
			}
			if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
				$filter_order_Dir = '';
			}

			if ($filter_order == 'id'){
				$orderby 	= ' ORDER BY  o.id '.$filter_order_Dir;
			} else {
				$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' ,  o.id ';
			}
			$where		 = count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
			$sql = "SELECT count(o.*) FROM #__jegroupbuy_payment AS o".
					" LEFT JOIN #__jegroupbuy_product AS p ON p.id = o.p_id".
					 $where.
					 $orderby;
			$db->setQuery($sql);
			$total = $db->loadResult();
			$pageNav = new JPagination( $total, $limitstart, $limit );
			$sql = "SELECT s.*, o.*,p.title,p.store_id FROM #__jegroupbuy_payment AS o".
					" LEFT JOIN #__jegroupbuy_product AS p ON p.id = o.p_id".
					" LEFT JOIN #__jegroupbuy_stores AS s ON s.id= p.store_id".
					$where.
					$orderby;
			$db->setQuery( $sql, $pageNav->limitstart, $pageNav->limit );
			$rows = $db->loadObjectList();

			// 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');
			$context			= 'com_jegroupbuy.s.id.stores.';
	 		JHTML::stylesheet('style.css', JURI::base() . 'components/com_jegroupbuy/assets/css/');
	 		$text 		  = $row->id ? JText::_( 'JE_GROUPBUY_ORDER_EDIT' ) : JText::_( 'JE_GROUPBUY_ORDER_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();
			}

			$payment_list = array(
				JHTML::_('select.option', 'Paypal'),
				JHTML::_('select.option', 'Authorise.Net'),
				JHTML::_('select.option', '2CheckOut')
				);


			$sql= "SELECT * FROM #__users WHERE gid != 25";
			$db->setQuery( $sql );
			$rows = $db->loadObjectList();

			if($rows){
				foreach($rows as $listid)
				{
					$user_list[] = JHTML::_('select.option', $listid->id, $listid->username);
				}
			}
			else{
				$user_list[] = JHTML::_('select.option','','');
			}

			foreach ($rows as $listname)
			{
				$user_name[] = JHTML ::_('select.option', $listname->id, $listname->id);
			}


			$sqls= "SELECT * FROM #__jegroupbuy_product  WHERE expired=0";
			$db->setQuery($sqls);
			$listprod= $db->loadObjectList();


			if($listprod)
			{
				foreach ($listprod as $prod)
				{
					$product_list[] = JHTML ::_('select.option', $prod->id, $prod->title);
				}
			}
			else
			{
				 $product_list[] =JHTML::_('select.option','', '');
			}

			if($row->id == "0")
			{
				$row->pdate = time();
			}
			$this->assignRef('row',$row);
			$this->assignRef('userlist',$user_list);
			$this->assignRef('username',$user_name);
			$this->assignRef('productlist',$product_list);
			$this->assignRef('paymentlist', $payment_list);
		}
		parent::display($tpl);
	}
}
?>