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

defined( '_JEXEC' ) or die( 'Restricted access' );

class DtregisterControllerMember extends DtrController { 

	var $name = "member";

	 function __construct($config = array()){

		 parent::__construct($config);

		 $this->view = & $this->getView( 'member', 'html' );  

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

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

		 $this->registerTask( 'new', 'add' );

		 $this->registerTask( 'remove', 'delete' );

		 $this->registerDefaultTask("index");

	}

	function edit(){

		global $mainframe,$Itemid;

		$mMember = $this->getModel('member');

	    $tMember = $mMember->table;

	    $tUser = $this->getModel('user')->table;
        $key = JRequest::getVar('key');
		$process = DT_Session::get('Setting.process');
		$userId =  DT_Session::get($process.'.User.userId');
		
        $tUser->load($userId);
	    $tEvt = $this->getModel('event')->table;
	    $tEvt->load($tUser->eventId);
		
		$eventId = $tUser->eventId;
		if(isset($_POST['formsubmit']) && $_POST['formsubmit']=='edit'){

		    $tUser->load($_POST['Member']['groupUserId']);
            $tUser->members[$_POST['Member']['groupMemberId']]->fields =  JRequest::getVar('Field',array(),null,'array');
            $data = $_POST['Member'];
              if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
				 $event = $tEvt;
				 $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,$type,$memtot,$reg_type); 
				
				if($discount_code_id !== false){
			   		//DT_Session::set('Setting.Member.discount_code_id', $discount_code_id );
				}else{
			   		//DT_Session::set('Setting.Member.discount_code_id', false );
					if(!isset($event->TableEventdiscountcode->TableDiscountcode->error)){
						$event->TableEventdiscountcode->TableDiscountcode->error = 'Invalid Code.';
					}
			 		$this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
			  
				}
				$data['discount_code_id'] = $discount_code_id;
			
		}
			
			$data['fields'] = JRequest::getVar('Field',array(),null,'array');
            DT_Session::set($process.'.User.members.'.$key,$data);
			
	$mainframe->redirect("index.php?option=com_dtregister&controller=member&userId=".$tUser->userId."&Itemid=".$Itemid);

			pr($tUser->TableFee);

			prd($fee);

			die;

	   }
	   $member = DT_Session::get($process.'.User.members.'.$key );
	    $i = 0;
	   foreach(array_keys(DT_Session::get($process.'.User.members')) as $vkey) {
	   	  
		  if($vkey == $key) {
		  	 break;
		  }
		  $i++;
		  
	   }
	   DT_Session::set('Setting.current.memberIndex',$i);
	   //DT_Session::set('register.Setting.current.memberIndex',$i);
	   $member = DT_Session::get($process.'.User.members.'.$key );
	   if(DT_Session::get($process.'.User.members.'.$key.'.groupMemberId')){
	   		$groupMemberId=DT_Session::get($process.'.User.members.'.$key.'.groupMemberId');
	   
            $tMember->load($groupMemberId);
            
	   }
	 
	   $tUser->load(DT_Session::get($process.'.User.members.'.$key.'.groupUserId'));
	   $type = 'M';

	   $eventId = $tUser->eventId;
       
       if(isset($member['discount_code_id'])) {
	       $tUser->TableEvent->TableEventdiscountcode->TableDiscountcode->load($member['discount_code_id']);
		   $this->view->assign('discount_code',$tUser->TableEvent->TableEventdiscountcode->TableDiscountcode->name);
	   }
       $this->view->assign('key',$key);
	   $this->view->assign('tEvent',$tEvt);
	   $this->view->assign('header_eventId',$eventId);
       $memtot = DT_Session::get($process.'.User.memtot');
	   $this->view->assign( 'form' ,$tUser->TableEvent->form($type,$member ,false,'frmcart',true,$memtot));

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

	   $this->display();	

	}

	function index(){

	  global $mainframe;

	  $mMember = $this->getModel('member');

	  $tMember = $mMember->table;

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

	  $members = $tMember->find(' groupUserId = '.$userId);

	  $temp = array();
       $process = DT_Session::get('Setting.process');
	   if(DT_Session::get($process.'.User.members')=== false || count(DT_Session::get($process.'.User.members')) < 1){

		 if (is_array($members)) 
		 foreach($members as $key => &$member){

		   $tMember->load($member->groupMemberId);

		   $temp[$member->groupMemberId] = $tMember->getObjData();

	     }

		 DT_Session::set($process.'.User.members',DTrCommon::objectToArray($temp));

	   }else{

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

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

				  }else{

					 $temp[$key] = $member;

				  } 

		   }

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

	    $tUser = $mUser->table;

	    $tUser->load($userId);
	    
      $eventId = $tUser->eventId;

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

	  $this->view->assign('members',$temp);

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

	  $this->view->setLayout('list');

	  $this->view->display();	

	}

	function delete(){

	  global $mainframe,$Itemid;
	  $process = DT_Session::get('Setting.process');
	  $key = JRequest::getVar('key');

	  $userId = DT_Session::get($process.'.User.members.'.$key.'.groupUserId');

	  $memtot = DTrCommon::cntMemtotInSession( DT_Session::get($process.'.User.members'));
       
	   $tUser = $this->getModel('user')->table;
	   $tUser->load($userId);
	   $tEvt = $this->getModel('event')->table;
	   $tEvt->load($tUser->eventId);
	   $min = ($tEvt->min_group_size=="")?1:$tEvt->min_group_size;
	   
	  if( $memtot <= $min){
 $mainframe->redirect("index.php?option=com_dtregister&controller=member&userId=".$userId."&Itemid=".$Itemid,JText::sprintf( 'DT_MEMBER_REMOVE_ERROR' , $min));

      }

      $memtot = DT_Session::get($process.'.User.memtot');
	  $memtot--;
	  DT_Session::set($process.'.User.memtot',$memtot);
	  DT_Session::set($process.'.User.members.'.$key.'.remove',true);

 $mainframe->redirect("index.php?option=com_dtregister&controller=member&userId=".$userId."&Itemid=".$Itemid);

    }

    function add(){

		global $mainframe,$Itemid,$now;
        $process = DT_Session::get('Setting.process');
		

		$mMember = $this->getModel('member');	

	    $tMember = $mMember->table;

	    $tUser = $this->getModel('user')->table;
		
		$userId =  DT_Session::get($process.'.User.userId');
		
        $tUser->load($userId);
	    $tEvt = $this->getModel('event')->table;
	    $tEvt->load($tUser->eventId);
		$eventId = $tUser->eventId;
        $memtot = DT_Session::get($process.'.User.memtot');
		if($memtot >= $tEvt->max_group_size && $tEvt->max_group_size > 0){
			$mainframe->redirect("index.php?option=com_dtregister&controller=member&userId=".$userId."&Itemid=".$Itemid,JText::_('DT_MAX_GROUP_SIZE_REACHED'));
	    }
		if(isset($_POST['formsubmit']) ){
	       
            $min = ($tEvt->min_group_size=="")?2:$tEvt->min_group_size;

			$data = $_POST['Member'];

			$data['fields'] = JRequest::getVar('Field',array(),null,'array');

			$data['created'] = $now->toMySQL();
            $discount_code_id = 0 ;
			$event = $tEvt ;
			 if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
				 $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,$type,$memtot,$reg_type); 
				
				if($discount_code_id !== false){
			   		//DT_Session::set('Setting.Member.discount_code_id', $discount_code_id );
				}else{
			   		//DT_Session::set('Setting.Member.discount_code_id', false );
					if(!isset($event->TableEventdiscountcode->TableDiscountcode->error)){
						$event->TableEventdiscountcode->TableDiscountcode->error = 'Invalid Code.';
					}
			 		$this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
			  
				}
			
		}
			$data['discount_code_id'] = $discount_code_id ;
			$members = DT_Session::get($process.'.User.members');

			$members[] = $data;

			DT_Session::set($process.'.User.members',$members);
            $memtot = DT_Session::get($process.'.User.memtot');
	        $memtot++;
	        DT_Session::set($process.'.User.memtot',$memtot);
			$mainframe->redirect("index.php?option=com_dtregister&controller=member&userId=".$userId."&Itemid=".$Itemid);

			pr($tUser->TableFee);

			prd($fee);

			die;

		}

	    $type = 'M';
	
	   $this->view->assign('header_eventId',$eventId);
       $memtot = DT_Session::get($process.'.User.memtot');
	   $this->view->assign( 'form' ,$tUser->TableEvent->form($type,array(),false,'frmcart',false,$memtot));

	   $this->view->assign('userId',$userId);
	    $this->view->assign('tEvent',$tEvt);

	    $this->display();

    }

}

?>