<?php

/**
* @version 2.8.13
* @package Joomla 2.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

class DtregisterControllerUser extends DtrController {

   var $name = "User";

    function __construct($config = array()){
          global $mainframe;
		  parent::__construct($config);
          
		  $user = JFactory::getUser();
		  if(!$user->id){
		  	  
			  JRequest::setVar('task','auth');
			  
		  }
		  $this->view = & $this->getView( 'User', 'html' );

		  $this->view->setModel($this->getModel('event'));

		  $this->view->setModel($this->getModel( 'location' ));

		  $this->view->setModel($this->getModel( 'user' ));

		  $this->view->setModel($this->getModel( 'member' ));

		  $this->view->setModel($this->getModel( 'field' ));
		
		  $this->view->setModel($this->getModel('field'));
		  
		 $this->registerDefaultTask("index");

		 $this->user = &JFactory::getUser();

	}

	function index(){

	   global $mainframe;
       
       DT_Session::clearAll();
		
	   $mUser = $this->getModel( 'user' );
	   
	   $paymentVerified = JRequest::getVar('paymentVerified','');
	   $keyword = JRequest::getVar('keyword','');

	   $order = JRequest::getVar('filter_order','register_date');

	   $dir = JRequest::getVar('filter_order_Dir','desc');

		jimport('joomla.html.pagination');

	    $listLimit = $mainframe->getCfg( 'list_limit', 10 );

        $limit=JRequest::getInt('limit',$listLimit);

	    $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		
		$search = array();
		$search['user_id'] = $this->user->id;
		if (isset($paymentVerified) && $paymentVerified != "") {
			$search['paymentVerified'] = $paymentVerified;
		}
		if (isset($keyword) && $keyword != "") {
			$search['keyword'] = $keyword;
		}

		$users = $mUser->getUsers($search,$order,$dir,$limitstart,$limit);

		$total = $mUser->table->getLastCount();	

		$pageNav = new JPagination( $total, $limitstart, $limit );

		$this->view->assign('users',$users);

		$this->view->assign('pageNav',$pageNav);

	   $this->display();

	}

	function due(){

	    global $mainframe,$Itemid;

	    $mUser = $this->getModel('user');

	    $tUser = $mUser->table;

	    $userId=JRequest::getVar('userId',0);

	    $tUser->load($userId);

	    $userObj = $tUser->getObjData();
        
	    $user = DTrCommon::objectToArray($userObj);
        
		DT_Session::set('Setting.process', 'duepayment');
		DT_Session::set('Setting.eventId',$tUser->eventId);

		DT_Session::set('duepayment.User',$user);

		$mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid."&controller=user&task=confirm");

	}

	function cancel(){

		global $mainframe,$Itemid;

	    $mUser = $this->getModel('user');

	    $tUser = $mUser->table;

	    $userId=JRequest::getVar('userId',0);

	    $tUser->load($userId);

	    $userObj = $tUser->getObjData();

	    $user = DTrCommon::objectToArray($userObj);

        $process = DT_Session::set('Setting.process', 'cancel');
		DT_Session::set('Setting.eventId',$tUser->eventId);
		DT_Session::set('cancel.User',$user);

	$mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid."&controller=user&task=confirm");

	}

	function edit(){

	   global $mainframe,$Itemid,$upanel_edit_show;

	   $mUser = $this->getModel('user');

	   $tUser = $mUser->table;

	   $userId=JRequest::getVar('userId',0);

	   $tUser->load($userId);
	   $tUser->TableEvent->overrideGlobal($tUser->eventId);
	  
	   if($tUser->is_editable()){
	   		
	   } else {
	   		
			$mainframe->redirect(JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&controller=user&task=index"));
	   }
       DT_Session::set('Setting.eventId',$tUser->eventId);
	   $userObj = $tUser->getObjData();
	   $user = DTrCommon::objectToArray($userObj);
	  
	   DT_Session::set('Setting.process', 'change');
		
	   if(isset($_POST['formsubmit']) ){
		   
			$data = $_POST['User'];
            
			 if(DT_Session::get('change.User.members')) {
				$memtot = DTrCommon::cntMemtotInSession( DT_Session::get('change.User.members'));
			 } else {
			  	$memtot = DT_Session::get('change.User.memtot');
			  }

			$user['fields'] = $_POST['Field'];

			$user['memtot'] = $memtot;

			if(DT_Session::get('change.User.members')){

				$temp =  array();

			    foreach(DT_Session::get('change.User.members') as $key => $member){

			     if(isset($member['remove']) && $member['remove']){

				  }else{

					 $temp[$key] = $member;

				  } 

		        }

				$user['members'] = $temp;

		    }
            
			DT_Session::set('change.User',$user);
            
			//unset($_SESSION['DTregister']['register']['members']);
			
	$mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid."&controller=user&task=confirm");

			// pr($_POST);

			// prd($user);

		}else{
			
			if(!DT_Session::get('change.User')){
		   	   DT_Session::set('change.User',$user);
			   DT_Session::set('Setting.eventId',$tUser->eventId);
			}
		}
		
	   $eventId = $tUser->eventId;
       $tUser->get_hidden_fields();
	   $this->view->assign('header_eventId',$eventId);

	   // pr(DTrCommon::objectToArray($userObj));
	   // pr($user);
        
	   $type = ($tUser->type=='I')?'I':'B';
       $tUser->TableEvent->duplicate_check = false;
	   $memtot = DTrCommon::cntMemtotInSession( DT_Session::get('change.User.members'));
	   $this->view->assign( 'form' ,$tUser->TableEvent->form($type,(array)$tUser,false,'frmcart',true,$memtot));
       $tUser->TableEvent->load($eventId);
       $this->view->assign('tEvent',$tUser->TableEvent);
	   $this->view->assign('mUser',$mUser);
	   $this->display();

	}
	
	function edit_group() {

	   global $mainframe,$Itemid;

	   $mUser = $this->getModel('user');

	   $tUser = $mUser->table;

	   $userId=JRequest::getVar('userId',0);
       if(isset($_POST['formsubmit']) ){ 
	       if (isset($_POST['memtot']) && $_POST['memtot'] != "") {
				 
				 DT_Session::set('change.User.memtot',$_POST['memtot']);
			}
			$userId = $_POST['User']['userId'];
	   	 $link =   JRoute::_("index.php?option=com_dtregister&controller=user&task=edit&userId=".$userId."&Itemid=".$Itemid,$xhtml_url);
		 $mainframe->redirect($link);
	   }
	   $tUser->load($userId);
       DT_Session::set('Setting.eventId',$tUser->eventId);
	   $userObj = $tUser->getObjData();
	   $user = DTrCommon::objectToArray($userObj);
	   DT_Session::set('Setting.process', 'change');
	   
	   $eventId = $tUser->eventId;

	   $this->view->assign('header_eventId',$eventId);

	   // pr(DTrCommon::objectToArray($userObj));
	   // pr($user);

	   $type = ($tUser->type=='I')?'I':'B';
       $tUser->TableEvent->duplicate_check = false;
       $tUser->TableEvent->load($eventId);
       $this->view->assign('tEvent',$tUser->TableEvent);
	   $this->view->assign('mUser',$mUser);
	   $this->display();
	
	}
    
	function price_header(){
	   
	   $layout = JRequest::getVar('dttmpl','price_header');
	   $process = DT_Session::get('Setting.process');
	   $this->getModel('field');
	  
	   if(isset($_REQUEST['Field'])){
		   //$userIndex = DT_Session::get('Setting.current.userIndex');
		  
		   if(isset($_REQUEST['key'])){
			   if($_REQUEST['key'] != ""){
				  $key = JRequest::getVar('key',0);
				   
				  if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
				  $eventId = JRequest::getInt('eventId',DT_Session::get('Setting.eventId'));
				  $event = $this->getModel('event')->table;
	              $event->load($eventId);
				  $process = DT_Session::get('Setting.process');
		          $memtot = DT_Session::get($process.'.User.memtot');
		          $reg_type = DT_Session::get($process.'.User.type');
	   			  $type = 'M';
				  $discount_code_id = $event->validate_code($_REQUEST['discount_code'],NULL,'M',$memtot,$reg_type); 
				  				  if($discount_code_id !== false){

					  $memberdata['discount_code_id'] = $discount_code_id;
				  }else{
					  $memberdata['discount_code_id'] = false;
					  $userdata['discount_code_id'] = false;
					  if(!isset($event->TableEventdiscountcode->TableDiscountcode->error)){
					  		$event->TableEventdiscountcode->TableDiscountcode->error = "Inalid code.";
					  }
					  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
					
				  }
				  
			  }else{

				   $memberdata['discount_code_id'] = 0;
				  
			  }
	               DT_Session::set($process.'.User.members.'.$key .'.fields', $_REQUEST['Field']);
				   DT_Session::set($process.'.User.members.'.$key .'.discount_code_id', $memberdata['discount_code_id']);
			 }else { // new member added 
			    
				  $key = JRequest::getVar('key','add');
				  if($key == ""){$key = 'add';}
				  if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
				  $eventId = JRequest::getInt('eventId',DT_Session::get('Setting.eventId'));
				  $event = $this->getModel('event')->table;
	              $event->load($eventId);
				  $process = DT_Session::get('Setting.process') ;
		          $memtot = DT_Session::get($process.'.User.memtot');
		          $reg_type = DT_Session::get($process.'.User.type');
	   			  $type = 'M';
				  $discount_code_id = $event->validate_code($_REQUEST['discount_code'],NULL,'M',$memtot,$reg_type); 
				  
				  if($discount_code_id !== false){
					  //DT_Session::set('Setting.Member.discount_code_id', $discount_code_id );
					  $memberdata['discount_code_id'] = $discount_code_id;
				  }else{
					  // DT_Session::set('Setting.Member.discount_code_id', false );
					   $memberdata['discount_code_id'] = false;
					//  DT_Session::set('Setting.User.discount_code_id', false );
					  $userdata['discount_code_id'] = false;
					  if(!isset($event->TableEventdiscountcode->TableDiscountcode->error)){
					  		$event->TableEventdiscountcode->TableDiscountcode->error = "Inalid code.";
					  }
					  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
					
				  }
				  
			  }else{
				  // DT_Session::set('Setting.Member.discount_code_id', 0 );
				   $memberdata['discount_code_id'] = 0;
				  
			  }
			  $this->view->assign('discount_code_id',$memberdata['discount_code_id']);
			 // $memberdata['fields'] = $_REQUEST['Field'];
	           $members = DT_Session::get($process.'.User.members');

			//$members[] = $memberdata;

			   //DT_Session::set($process.'.User.members',$members);
			 // DT_Session::set($process.'.User.members.'.$key .'.fields', $_REQUEST['Field']);
			  //DT_Session::set($process.'.User.members.'.$key .'.discount_code_id', $memberdata['discount_code_id']);
			   
			 }
			 // echo "<pre>";
			 // print_r($memberdata); die;
		   }else{
			   
			   DT_Session::set($process.'.User.fields', $_REQUEST['Field']);	  
			 
		   }
	   }
	   
	   $this->view->setLayout($layout );
	   $this->view->display();	
	   die;
	   	
	}
	
	function confirm(){

	   global $Itemid,$mainframe,$xhtml,$xhtml_url;
	   
	   $process = DT_Session::get('Setting.process');
	   
	   $this->view->setModel($this->getModel( 'field' ));

	   $this->view->setModel($this->getModel( 'fieldtype' ));
	   
	   $step = JRequest::getVar('step',0);
	   
	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   $this->view->assign('header_eventId',$eventId);

	   $event = & DtrTable::getInstance('Event','Table');

	   $event->load($eventId );
	   if(isset($_REQUEST['back']) && DT_Session::get($process.'.User.0') !== false) {
	   	   $userId = DT_Session::get($process.'.User.0.userId');
		   DT_Session::clear($process.'.User.0');
		    $mUser = $this->getModel('user');

	    	$tUser = $mUser->table;
		    $tUser->load($userId);
			
	    	$userObj = $tUser->getObjData();
        
	    	$user = DTrCommon::objectToArray($userObj);
			DT_Session::set($process.'.User',$user);

	   }
	   if($step ==="confirm"){
           if(isset($_REQUEST['partial_payment'])){
			    if($_REQUEST['partial_payment'] == 'full'){
					$paying_amount = JRequest::getVar('paying_amount',0);
				}else{
					$paying_amount = JRequest::getVar('partial_payment_amount',0);
				}
		   }else{
		   	    $paying_amount = JRequest::getVar('paying_amount',0);
		   }
		   
           if($paying_amount >0 ){
			   $paid_amount = DT_Session::get($process.'.User.fee.paid_amount') + $paying_amount;
		   }else{
			   $paid_amount = DT_Session::get($process.'.User.fee.paid_amount');
		   }

		   if(DT_Session::get($process.'.User.0') === false){
			   DT_Session::set($process.'.User.fee.paying_amount',$paying_amount);

		       DT_Session::set($process.'.User.fee.paid_amount',$paid_amount);
			   $user = DT_Session::get($process.'.User');
		       DT_Session::clear($process.'.User');
		  
		       DT_Session::set($process.'.User.0',$user);
		   
		   }else{
			   
		   }
		   $memtot = DT_Session::get($process.'.User.memtot');
           $event->add_registering($memtot);
		   $paymethod = DT_Session::get($process.'.payment.method');
		   
		   if($paying_amount <= 0){
			   
				$function = DT_Session::get('Setting.process');
				$tableUser = $this->getModel('user')->table;
				$tableUser->{$function}(DT_Session::get($process));
				$messageTask = $function;
				$mainframe->redirect("index.php?option=com_dtregister&controller=message&Itemid=$Itemid&task=".$messageTask);
		    
			} else {
				DT_Session::set($process.'.payment.paying_amount',$paying_amount);
			   $mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=payment&task=methods",$xhtml_url));
			
			}

	   }

	   $fieldtype =$this->getModel( 'fieldtype' );

	   $viewMemFields = "";

	   if(DT_Session::get($process.'.User.type')=='G'){

	       $back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=user&task=edit&back=1&userId=".DT_Session::get($process.'.User.userId'));

		   $type = 'B';

		   $members = DT_Session::get($process.'.User.members');
		    
		   $i = 0;
           if (is_array($members)) 
		   foreach($members as $key => $member){

		      $viewMemFields .= "<dl><dt style='padding:7px 0 3px 0'><u><strong>".JText::_( 'DT_MEMBER' ).($i+1)." </strong></u></dt></dl>";

		      $viewMemFields .= $event->viewFields('M',DT_Session::get($process.'.User.members.'.$key),false,'frmcart',false); 
			  $i++;

		   }

	   }else{
		  $tuser = $this->getModel( 'user' )->table;
		  $tuser->load(DT_Session::get($process.'.User.userId'));
		  $tuser->TableEvent->overrideGlobal($eventId);
		  
		  if($tuser->is_editable()) {
		     $back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&controller=user&task=index");
		  } else {
		  		$back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=user&task=edit&back=1&userId=".DT_Session::get($process.'.User.userId'));
		  }
	      
		  $type = 'I';

	   }
 
	   $TableUser =& DtrTable::getInstance('Duser','Table');

	   $TableUser->create(DT_Session::get($process.'.User'));

	   $feeObj = new DT_Fee($event,$TableUser);

	   $feeObj->setPaidAmount(DT_Session::get($process.'.User.fee.paid_amount'));
       
	  // prd($process.'.User.register_date');
	   $feeObj->getFee(true,DT_Session::get($process.'.User.register_date')); 

	   $feesession = $feeObj;

	   unset($feesession->TableEvent);

	   unset($feesession->TableUser);
	   //echo "<pre>";
	   //print_r(DT_Session::get($process.'.User'));
//print_r($feeObj->getObjectdata());
//die;
	   DT_Session::set($process.'.User.fee',(array)$feeObj->getObjectdata());
       
	   $this->view->assign('feeObj',$feeObj);

	   $this->view->assign('back',$back);

	   $this->view->assign('eventId',$eventId);

	   $event->load($eventId);
	  
	   $this->view->assign( 'viewFields' ,$event->viewFields($type,DT_Session::get($process.'.User'),false,'frmcart',false));

	   $this->view->assign( 'viewMemFields',$viewMemFields);
	   
	   $this->view->assign( 'partial_payment_enable', $event->partial_payment_enable);

	   $this->display();	

	}

}

?>