<?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.
**/

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

/**
 * JE GroupBuy Component Controller
 */
class jegroupbuyController extends JController
{
	/**
	 * Method to display a view.
	 */
	public function display($cachable = false, $urlparams = false)
	{

		$cachable		= true;
		$user	=& JFactory::getUser();
		$pid = JRequest::getInt("pid","");
		/*if ( $user->get('guest')) {
			if($pid > 0)
			{
				$url = base64_encode(JRoute::_('index.php?option=com_jegroupbuy&view=productdetails&pid='.$pid));
			}else
			{
				$url = base64_encode(JRoute::_('index.php?option=com_jegroupbuy&view=jegroupbuy'));
			}
			$link = "index.php?option=com_users&view=login&return=".$url;
			$this->setRedirect( $link);
		}
		*/
		// Get the document object.
			$document	= JFactory::getDocument();

		// Set the default view name and format from the Request.
			$vName		= JRequest::getCmd('view', 'faqs');
			JRequest::setVar('view', $vName);

		$user			= JFactory::getUser();

		$safeurlparams = array('catid'=>'INT','id'=>'INT','cid'=>'ARRAY','year'=>'INT','month'=>'INT','limit'=>'INT','limitstart'=>'INT',
			'showall'=>'INT','return'=>'BASE64','filter'=>'STRING','filter_order'=>'CMD','filter_order_Dir'=>'CMD','filter-search'=>'STRING','print'=>'BOOLEAN','lang'=>'CMD');

		parent::display($cachable,$safeurlparams);

		return $this;
	}

	function addComments()
	{
		$db = &JFactory::getDBO();
		$comment = JRequest::getVar("comment", '', '', 'string', JREQUEST_ALLOWRAW);
		$user	=& JFactory::getUser();
		$pid = JRequest::getInt("pid","");
		$cdate = time();
		$sql = "INSERT INTO  #__jegroupbuy_comments (`pid`,`uid`,`comments`,`cdate`) values ('".$pid."','".$user->id."',".$db->Quote($comment).",'".$cdate."');";
		$db->setQuery($sql);
		$db->query();

		$sql = "SELECT c.*,u.name FROM #__jegroupbuy_comments AS c LEFT JOIN #__users AS u ON u.id = c.uid  WHERE c.pid='".$pid."' AND c.uid='".$user->id."' ORDER BY c.cdate DESC";
		$db->setQuery($sql);
		$rows = $db->loadObjectList();
		?>
		<table cellpadding="0" cellspacing="0" width="100%">
			<tr>
		<?php
		$k = 0;
		$bgclr = '';
		foreach($rows AS $row)
		{
		if($k == 0)
		{
			$bgclr = "#F8F8F8";
		}else
		{
			$bgclr = "#FAFDF5";
		}
		?>
		<tr>
			<td style="border: 1px solid rgb(0, 0, 0);background-color:<?php echo $bgclr; ?>;border-color:#F1F1F1;padding:5px 15px 5px 10px;">
				<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td width="50%" style="padding:5px;border-bottom:dotted 1px #999999;">
							<b><?php echo $row->name; ?></b>&nbsp; <i><?php echo JHTML::_('date',  $row->cdate, "d-m-Y"); ?></i>
						</td>
					</tr>
					<tr>
						<td width="100%" style="padding:5px;">
							<?php
								$comments =  chunk_split ($row->comments,80, "<br/>");
								echo $comments;

							?>
						</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<?php
			$k = 1 - $k;
		}
		?>
		</table>
		<?php
		exit;
	}

	function changeToExpired()
	{
		$db = &JFactory::getDBO();
		$pID = JRequest::getInt("pID","");
		$sql = "UPDATE #__jegroupbuy_product SET expired = 1 WHERE id=".$pID;
		$db->setQuery($sql);
		$db->query();
		echo $pID;
		exit;
	}


	function processPayment()
	{
		JRequest::setVar( 'view', 'paymentdetails' );
		JRequest::setVar( 'layout', 'process' );
		parent::display();
	}

	function paymentDetails()
	{
		$pid = JRequest::getInt("pid","");
		JRequest::setVar("view","paymentdetails");
		JRequest::setVar("layout","default");
		JRequest::setVar("pid",$pid);
		parent::display();
	}

	function paymentReturnUrl()
	{
		/*JRequest::setVar("view","couponpage");
		JRequest::setVar("layout","default");
		parent::display();
		*/
		$db = &JFactory::getDBO();
		$user	=& JFactory::getUser();
		$id = JRequest::getInt("id","");
		$ppc = JRequest::getInt("ppc","");
		$sql ="SELECT count(*) FROM #__jegroupbuy_payment WHERE p_id=".$id." AND user_id = ".$user->id;
		$db->setQuery($sql);
		$ppcount = $db->loadResult();
		if($ppcount > $ppc)
		{
			//success
			$this->setRedirect(JRoute::_('index.php?option=com_jegroupbuy&view=couponpage&result=succ'));
		}else
		{
			$this->setRedirect(JRoute::_('index.php?option=com_jegroupbuy&task=paymentDetails&pid='.$id.'&result=fail'));
		}

	}

	function getCouponTemplate($pid)
	{
		$db = JFactory::getDBO();
		$user	=& JFactory::getUser();

		$sql = "SELECT pro.*,u.email,s.address,s.contact_number,,py.transaction_id,s.store_logo,s.email,s.shop_url,py.id,py.pdate,py.status FROM #__jegroupbuy_product AS pro"
			   ." LEFT JOIN #__jegroupbuy_stores AS s ON s.id = pro.store_id"
			   ." LEFT JOIN #__jegroupbuy_payment AS py ON py.p_id = pro.id"
			   ." LEFT JOIN #__users AS u ON u.id = py.user_id"
			   ." WHERE py.id=".$pid;
		$db->setQuery( $sql );
		$rows = $db->loadObject();

		$query = 'SELECT template FROM #__jegroupbuy_template where id=1';
		$db->setQuery( $query );
		$template = $db->loadResult();

		//store fields
		$op_val['shop_address_1']=$rows->address;
		$op_val['shop_phone']=$rows->contact_number;
		$op_val['shop_email']=$rows->email;
		$op_val['shop_url']=$rows->shop_url;
		if($rows->store_logo!="")
		{
			$op_val['logo']='<img src="'.JURI::root().$rows->store_logo.'"/>';
		}
		else
		{
			$op_val['logo']="";
		}


		//product fields
		$op_val['coupon_code']=$rows->coupon_code;
		$op_val['coupon_date']=$rows->deal_to_date;

		//payment field
		$op_val['order_number']=$rows->id;
		$op_val['order_date']=JHTML::_('date',$rows->pdate, JText::_('DATE_FORMAT_LC4').' H:i');
		if($rows->status =="1")
		$op_val['order_status']=JText::_('CC_PAYMENT_SUCCESS_MSG_TEMPLATE');
		else
		$op_val['order_status']=JText::_('CC_PAYMENT_UNSUCCESS_MSG_TEMPLATE');
		$op_val['transaction_id']=$rows->transaction_id;

		//product fields
		$price=substr($rows->product_price,1);
		$op_val['product_name']=$rows->title;
		$op_val['product_price']=$rows->product_price;
		$op_val['discount_price']=$rows->product_discount;
		$op_val['you_save']=$rows->you_save;
		$op_val['deal_price']=$rows->deal_price;

		foreach($op_val as $op_vals=>$value)
		{
			if(trim($value)=="")
			{
				$find = "{".$op_vals."}";
				$template=str_replace($find,"",$template);
			}
			else
			{
				$find = "{".$op_vals."}";
				$template=str_replace($find,$value,$template);
			}
		}
		return $template;
	}
}
