<?php
/**
* @version		3.0.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die();

class JLTicketCenterControllerCart extends JLTicketCenterController
{
	
	
	function register_save()
	{
		global $mainframe;
		
		// Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		// Get required system objects
		$user 		= clone(JFactory::getUser());
		$pathway 	=& $mainframe->getPathway();
		$config		=& JFactory::getConfig();
		$authorize	=& JFactory::getACL();
		$document   =& JFactory::getDocument();

		// Initialize new usertype setting
		$usersConfig = &JComponentHelper::getParams( 'com_users' );
		$newUsertype = $usersConfig->get('new_usertype');
		if (!$newUsertype) {
			$newUsertype = 'Registered';
		}

		// Bind the post array to the user object
		if (!$user->bind( JRequest::get('post'), 'usertype' )) {
			JError::raiseError( 500, $user->getError());
		}

		// Set some initial user values
		$user->set('id', 0);
		$user->set('usertype', $newUsertype);
		$user->set('gid', $authorize->get_group_id( '',$newUsertype, 'ARO'));

		$date =& JFactory::getDate();
		$user->set('registerDate', $date->toMySQL());

		// Override User Activation 
		// If user activation is turned on, we need to set the activation information
		$useractivation = $usersConfig->get( 'useractivation' );
		jimport('joomla.user.helper');
		
		// If there was an error with registration, set the message and display form
		if ( !$user->save() )
		{
			JError::raiseWarning('', JText::_($user->getError()));
			$this->setRedirect('index.php?option=com_jlticketcenter&view=checkout', $message);
			//$this->register();
			return false;
		}

		// Send registration confirmation mail
		$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
		$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email

		JLoader::import('com_user.controller',JPATH_ROOT.DS.'components');
		UserController::_sendMail($user, $password);
		
		
		$credentials = array();
		$credentials['username'] = $user->name;
		$credentials['password'] = $user->password_clear;

		
		$session = JFactory::getSession();
		$old_seesion_id = $session->getId();
		//preform the login action
		$error = $mainframe->login($credentials, $options);
		
		//update Session Cart

		$db =& JFactory::getDBO();
		$session = JFactory::getSession();
		
		$new_seesion_id = $session->getId();
		
		$query = "UPDATE `#__jltcenter_cart`
				  SET `session_id`='".$new_seesion_id."'
				  WHERE `session_id`='".$old_seesion_id."'"; 
		
		$db->setQuery($query);
		$db->query();
		
		$this->setRedirect('index.php?option=com_jlticketcenter&view=checkout');

	} //EOF register_save
	
	
	
	function checkout_pross(){
		
		// Check Data
		JRequest::checkToken() or jexit('Invalid Token');
		
		$session = JFactory::getSession();
		
		// get cart to check if it's free event registeration
		$err = array();

		$cart = JModelLegacy::getInstance('Cart', 'JLTicketCenterModel');
		$cart_lines =$cart->getCart();

		$free_event = false;
		foreach ($cart_lines as $cart_line){
			if ($cart_line->event_type==1){
				$free_event = true;
			}
			if($free_event && $cart_line->event_type===0){
				$err[] = JText::_('FREE_EVENT_MISMATCH');
			}
		}
                
                
                if(file_exists(JPATH_ADMINISTRATOR.'/components/com_jlticketcenter/helpers/jlticketcenter_pro_helper.php')){
                    $fieldsControl = $cart->getCartFeildsControl();
                    
                    if (array_key_exists('email', $_POST)){
                        if (empty($_POST['email'])){
                            $err[] = JText::_('JLTC_EMAIL_MANDATORY');
                        }
                    }
                    
                    foreach ($fieldsControl as $k=>$v){
                        if ($v==1){
                            $fieldName = str_replace ('cart_','',$k);
                            $checked_var = JRequest::getVar( $fieldName ,null,'post');
                            
                            if (empty($checked_var)){
                                $err[] = JText::_('JLTC_'.strtoupper($fieldName).'_MANDATORY');
                            }

                            unset($checked_var );
                        }
                    }
                }else{
                    // Old static way for fields
                    if (!JRequest::getVar('first_name',null,'post')){
			$err[] = JText::_('JLTC_FIRST_NAME_MANDATORY');
                    }
                    if (!JRequest::getVar('last_name',null,'post')){
                            $err[] = JText::_('JLTC_LAST_NAME_MANDATORY');
                    }

                    if (!JRequest::getVar('street',null,'post')){
                            $err[] = JText::_('JLTC_STREET_MANDATORY');
                    }
                    if (!JRequest::getVar('city',null,'post')){
                            $err[] = JText::_('JLTC_CITY_MANDATORY');
                    }
                }
                
                
		
		if (!$free_event){
			if (!JRequest::getVar('payment_method',null,'post')){
				$err[] = JText::_('JLTC_PAYMENT_METHOD_MANDATORY');
			}	
		}else{
			$session->set('JLTC_Free_event_checkout',true);
		}
				
		if (count($err)>0){
			$error  = implode('<br/>', $err);
			$url = JRoute::_('index.php?option=com_jlticketcenter&view=cart');
			$this->setRedirect($url,$error ,'error');
			$this->redirect();
		}
		
		
		// Write Data
		// 1. complete user detial
		JLoader::import('components.com_jlticketcenter.models.checkout',JPATH_ROOT);
		$model = JModelLegacy::getInstance('Checkout', 'JLTicketCenterModel');
		
		$data  = JRequest::get('POST');
		
		$model->setUserDetials($data);
		$model->setOrder($data);
                
		if(!$session->set('JLTC_Free_event_checkout',false)){
			$model->paymentPross($data);	
		}else{
			$session->clear('JLTC_Free_event_checkout');	
		}
		
		
		$url = JRoute::_('index.php?option=com_jlticketcenter&view=order&layout=complete',false);
		$this->setRedirect($url);
	}// EOF checkout_pross
	
	
	function paypal_notification(){
		JLoader::import('components.com_jlticketcenter.models.payment.paypal',JPATH_ADMINISTRATOR);
		JLTicketCenterPaypal::notify_proccess();
		die;
	}
	
}//EOC