<?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');
jimport( 'joomla.application.component.controller' );
class jeGroupBuyControllerorders extends JController
{
    function __construct()
    {
		//Get View
        if(JRequest::getCmd('view') == '')
        {
            JRequest::setVar('view', 'orders');
		}
        parent::__construct();
        $this->registerTask( 'add',	        'edit' );
		$this->registerTask( 'unpublish',	'publish' );
		$this->registerTask( 'apply',       'save' );
    }

    function edit()
	{
		JRequest::setVar( 'view', 'orders');
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}

	function save()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$model = $this->getModel('orders');
        $post	= JRequest::get( 'post',JREQUEST_ALLOWRAW );

        if ($id=$model->store($post))
        {
        	$msg = JText::_( 'JEGROUPBUY_ORDERS_SAVED_SUCC' );
        }
        else
        {
        	$msg = JText::_( 'JEGROUPBUY_ORDERS_SAVED_UNSUCC' );
        }
        if($this->_task == 'apply')
        {
        	$link 	= 'index.php?option=com_jegroupbuy&controller=orders&task=edit&cid[]='. $id;
        }
        else
        {
        	$link = 'index.php?option=com_jegroupbuy&controller=orders';
        }
        $this->setRedirect( $link ,$msg);
	}

	function remove()
	{
		$model = $this->getModel('orders');

		if($model->remove()) {
			$msg = JText::_( 'JEGROUPBUY_ORDERS_DELETE_SUCCESS' );
		} else {
			return JError::raiseWarning( 500, JText::_( 'JEGROUPBUY_ORDERS_DELETE_FAILURE' ));
		}

		$this->setRedirect( 'index.php?option=com_jegroupbuy&controller=orders', $msg );
	}

	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db			=& JFactory::getDBO();
		$cid		= JRequest::getVar( 'cid', '', 'post', 'array' );
		$task		= JRequest::getCmd( 'task' );
		$publish = '';
		if($task == 'unpublish')
		{
			$value = "0";
			$msg = JText::_( 'JEGROUPBUY_ORDER_UNPUBLISH_SUCC' );
		}else if($task == 'publish')
		{
			$value = "1";
			$msg = JText::_( 'JEGROUPBUY_ORDER_PUBLISH_SUCC' );
		}

		if (empty( $cid )) {
			return 0;
		}

		JArrayHelper::toInteger( $cid );
		$cids = implode( ',', $cid );

		$query = 'UPDATE #__jegroupbuy_payment'
				. ' SET  status = "' . $value. '"'
				. ' WHERE id IN ( '. $cids.'  )'
				;
		$db->setQuery( $query );
		$db->query();

		$link = 'index.php?option=com_jegroupbuy&controller=orders';
		$this->setRedirect( $link ,$msg);

	}
	public function resendmail()
	{
		$app		= JFactory::getApplication();
		$config		= JComponentHelper::getParams('com_jegroupbuy');

 		$id = JRequest::getVar('id');

		$db = JFactory::getDBO();
		$user	=& JFactory::getUser();

		$sql = "SELECT pro.*,s.address,s.contact_number,py.transaction_id,py.method,s.store_logo,s.email,s.shop_url,s.owner,py.id,py.pdate,py.status,u.name,u.email as user_email 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=".$id;
		$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;
		$op_val['owner']= $rows->owner;
		if($rows->store_logo!="")
		{
			$op_val['logo']='<img width="25%" src="'.JURI::root().'images/jegroupbuy/storelogo/'.$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);
		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;

		$op_val['user_name']		= $rows->name;
		$op_val['user_mailid']		= $rows->user_email;
		$op_val['user_payment']		= $rows->method;

		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);
			}
		}

		$from		= $app->getCfg('mailfrom');
		$name		= $app->getCfg('fromname');
		$site		= $app->getCfg('sitename');
		$emailSubject=  JText::_("PRODUCT_COUPON_FROM").$site;


		$query = "SELECT email FROM #__users As u LEFT JOIN #__jegroupbuy_payment AS p ON p.user_id = u.id WHERE p.id=".$id;
		$db->setQuery( $query );
		$emailid = $db->loadResult();
		$to = $emailid;

		JUtility::sendMail($from, $name,$to, $emailSubject, $template,1);
		$app->redirect(JRoute::_('index.php?option=com_jegroupbuy&view=orders'),JText::_("MAIL_SUCCESSFULLY_SEND"));
	}
}

class JHTMLInput
{
	function checkbox($name,$value,$checked='checked')
	{
		$html = "<input type=\"checkbox\" name=\"" . $name . "\" value=\"" . $value . "\"  id=\"" . $name . "\"  onClick=\"DoTheCheck('$name')\" $checked	/>";
		return $html;
	}
}

?>