<?php

/**
* @version 2.8.17
* @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 DtregisterControllerMessage extends DtrController {
   
   var $name ='message';
   function __construct($config = array()){
	   
	   parent::__construct($config);
	   $this->view = & $this->getView( 'message', 'html' );  
	   $this->registerDefaultTask("index");	
	
   }
    function checkpermission() {
      
	  return true;
   }
   
   function prequisite() {
	    global $prerequisite_event_msg;
	    $eventTable = $this->getModel('event')->table;
		$eventTable->load(JRequest::getVar('eventId'));
		$selection = array();
	    foreach($eventTable->prerequisite as $prerequisite){
			     
			$data = $eventTable->find('slabId = '.$prerequisite);
			if($data){
				$data = $data[0];
				$selection[] = $data->title;
			}
			    
		}
	  if(count($selection)){
		  $prerequisite = "<ul><li>".implode("</li><li>",$selection)."</li></ul>";	
	  }else{
	      $prerequisite = "";
	  }
	  
	  $prerequisite_event_msg = str_replace("[PREREQ_CATEGORY]",'',$prerequisite_event_msg); 
	  echo stripslashes(str_replace("[PREREQ_EVENTS]",$prerequisite,$prerequisite_event_msg));   
   }
   
   function prequisitecat(){
	    global $prerequisite_event_msg;
	    $eventTable = $this->getModel('event')->table;
		$cat = $this->getModel('category')->table;
		$eventTable->load(JRequest::getVar('eventId'));
		$selection = array();
		$events = array();
	    foreach($eventTable->prerequisitecategory as $prerequisite){
			     
			$data = $cat->find('categoryId = '.$prerequisite);
			$evts = array();
			if($data){
				$data = $data[0];
				$selection[] = $data->categoryName;
				$evts = $eventTable->TableEventcategory->find('category_id ='.$prerequisite);
				
				foreach($evts as $event){
					
					$aevent = $eventTable->find('slabId = '.$event->event_id);
					
					if(isset($aevent[0])) {
					   $events[] = $aevent[0]->title;
					   
					}
					
				}
			}
	    
		}
	
	   if(count($events)){
		   
		  $prerequisiteEvt = "<ul><li>".implode("</li><li>",$events)."</li></ul>";	
	  }else{
	      $prerequisiteEvt = "";
	  }
	  
	  if(count($selection)){
		  $prerequisite = "<ul><li>".implode("</li><li>",$selection)."</li></ul>";	
	  }else{
	      $prerequisite = "";
	  }
	  $prerequisite_event_msg = str_replace("[PREREQ_EVENTS]",$prerequisiteEvt,$prerequisite_event_msg);
	  echo stripslashes(str_replace("[PREREQ_CATEGORY]",$prerequisite,$prerequisite_event_msg));   
   }
   
   function privatevent(){
	   global $private_event_msg;

		echo stripslashes($private_event_msg);
   }
   
   function change(){
	  global $mainframe,$Itemid;
	  JText::_('DT_REGISTRATION_MODIFIED');
	  
	  DT_Session::clearAll();
	  $mainframe->redirect('index.php?option=com_dtregister&controller=user&task=index&Itemid='.$Itemid ,JText::_( 'DT_RECORD_CHANGE_SUCCESSFUL'));
   }
   
    function cancel(){
	   global $mainframe,$Itemid;
	   JText::_('DT_CANCEL_SUCCESSFUL');
	   DT_Session::clearAll();
	   
	   $mainframe->redirect('index.php?option=com_dtregister&controller=user&task=index&Itemid='.$Itemid ,JText::_( 'DT_CANCEL_SUCCESSFUL') );
	  
   }
   
   function duepayment(){
		global $mainframe,$Itemid;
		DT_Session::clearAll();
		$mainframe->redirect('index.php?option=com_dtregister&controller=user&task=index&Itemid='.$Itemid ,JText::_( 'DT_PAYMENT_SUCCESSFUL' ) );
   }
   
   function index(){
	   
	   // payment thanks 
	   // prd("index");
	   DT_Session::clearAll();
	   global $mainframe;
	   
   }
   function waiting(){
	   
	   global $full_message;
	   
	   echo stripslashes($full_message);
   }
   
   function waiting_msg(){
	     global $waiting_msg;
	   
	   echo $waiting_msg;
	    DT_Session::clearAll();
   }
   
   function paylater(){
	    global $mainframe,$pay_later_thk_msg,$pay_later_redirection,$pay_later_redirect_url;
	  
	   $userTable = $this->getModel('user')->table;
	   $TableEvent = $this->getMOdel('event')->table;
	   $messages = array();
	  
	   $Tagparser = new DT_Tagparser();
	   // prd(DT_Session::get('register.User'));
	   
	   if(DT_Session::get('register.User') && is_array(DT_Session::get('register.User'))) {
	
	   foreach(DT_Session::get('register.User') as $key => $user){
		    
		    if (isset($user)) {
				$msg = "";
				$userTable->load($user['userId']);
				$TableEvent->load($user['eventId']);
				if(!isset($first_event)) {
					   $first_event = $TableEvent;
				}
				$tokenmessage = ($TableEvent->pay_later_thk_msg_set)?$TableEvent->pay_later_thk_msg:$pay_later_thk_msg;
				$tokenmessage = $Tagparser->parse_paylater_tag($tokenmessage,$userTable);
				$grpmessages = array();
				$grpmsg = "";
				 if($userTable->type == "G") {
					 $groupmsg = $Tagparser->getTagcontent('GROUP_MEMBER',$tokenmessage);
					 foreach($userTable->members as $member ) {
				     	$grpmessages[] = stripslashes($Tagparser->parsetags($groupmsg,$member));
					 }
					
					 $grpmsg = implode(" ",$grpmessages);
				}
				 $tokenmessage = $Tagparser->replaceTagContent('GROUP_MEMBER',$tokenmessage,$grpmsg);
				 $msg .= stripslashes($Tagparser->parsetags($tokenmessage,$userTable));
				
				 $messages[] = $msg;
			
			}
			 
	   }
	    if($first_event->pay_later_thk_msg_set) {
	   		$pay_later_redirection = $first_event->pay_later_redirection;
			$pay_later_redirect_url = $first_event->pay_later_redirect_url;
			
	   }
	   
	   if ( $first_event->pay_later_thk_msg_set && isset($pay_later_redirection) && $pay_later_redirection == 0 && isset($pay_later_redirect_url)) {
			DT_Session::clearAll();
			$mainframe->redirect($pay_later_redirect_url);
			exit;
		}else if(isset($pay_later_redirection) && $pay_later_redirection == 0 && isset($pay_later_redirect_url)) {
			DT_Session::clearAll();
			$mainframe->redirect($pay_later_redirect_url);
			exit;
		}
	   }
	   $this->view->assign('messages',$messages);
	   $this->view->display();
	   
	  DT_Session::clearAll();
   }
   function thanks(){
	   global $mainframe,$thanksmsg,$thanks_redirection,$thanks_redirect_url;
	   //thanksmsg_set
	   
	   $userTable = $this->getModel('user')->table;
	   $TableEvent = $this->getMOdel('event')->table;
	   $TableMember = $this->getMOdel('member')->table;
	   $Tagparser = new DT_Tagparser();
	   $messages = array();
	   $process = DT_Session::get('Setting.process');
	    if(DT_Session::get($process.'.User') && is_array(DT_Session::get($process.'.User'))) {
		   foreach(DT_Session::get($process.'.User') as $key => $user){
				$msg = "";
				$userTable->load($user['userId']);
				$TableEvent->load($user['eventId']);
				if(!isset($first_event)) {
					   $first_event = $TableEvent;
					   $first_user = $userTable;
					}
				$tokenmessage = ($TableEvent->thanksmsg_set)?$TableEvent->thanksmsg:$thanksmsg;
				$tokenmessage = $Tagparser->parse_paylater_tag($tokenmessage,$userTable);
				$grpmessages = array();
				$grpmsg = "";
				 if($userTable->type == "G") {
					 $groupmsg = $Tagparser->getTagcontent('GROUP_MEMBER',$tokenmessage);
					 foreach($userTable->members as $member ) {
				     	$grpmessages[] = stripslashes($Tagparser->parsetags($groupmsg,$member));
					 }
					
					 $grpmsg = implode(" ",$grpmessages);
				}
				 $tokenmessage = $Tagparser->replaceTagContent('GROUP_MEMBER',$tokenmessage,$grpmsg);
				 $msg .= stripslashes($Tagparser->parsetags($tokenmessage,$userTable));
				
				 $messages[] = $msg;
				
		   }
		    if($first_user->status == -2) {
			$this->waiting_msg();
			
			return;
		}
		    if($first_event->thanksmsg_set) {
				$thanks_redirection = $first_event->thanks_redirection;
				$thanks_redirect_url = $first_event->thanks_redirect_url;
				
			}
			if ($first_event->thanksmsg_set && isset($thanks_redirection) && $thanks_redirection == 0 && isset($thanks_redirect_url)) {
				DT_Session::clearAll();
				$mainframe->redirect($thanks_redirect_url);
				exit;
			} else if(isset($thanks_redirection) && $thanks_redirection == 0 && isset($thanks_redirect_url)) {
				DT_Session::clearAll();
				$mainframe->redirect($thanks_redirect_url);
				exit;
			}
		}
	  
	   $this->view->assign('messages',$messages);
	   $this->view->display();
	   DT_Session::clearAll();
	  
   }
   
   function freethanks(){
	   global $mainframe,$thanksmsg,$thanks_redirection,$thanks_redirect_url;
	   $process = DT_Session::get('Setting.process');
	   $userIndex = DT_Session::get($process.'.Setting.current.userIndex');
	   $userTable = $this->getModel('user')->table;
	   $TableEvent = $this->getModel('event')->table;
	   $Tagparser = new DT_Tagparser();
	   $messages = array();
	   $process = DT_Session::get('Setting.process') ;
	  
	   if(DT_Session::get($process.'.User') && is_array(DT_Session::get($process.'.User'))) {
		   /// print_r(DT_Session::get($process.'.User'));
			foreach(DT_Session::get($process.'.User') as $key => $user){
				$msg = "";
				$userTable->load($user['userId']);
				$TableEvent->load($user['eventId']);
				if(!isset($first_event)) {
					   $first_event = $TableEvent;
					   $first_user = $userTable;
					}
				 $tokenmessage = ($TableEvent->thanksmsg_set)?$TableEvent->thanksmsg:$thanksmsg;
				$grpmessages = array();
				$grpmsg = "";
				 if($userTable->type == "G") {
					 $groupmsg = $Tagparser->getTagcontent('GROUP_MEMBER',$tokenmessage);
					 foreach($userTable->members as $member ) {
				     	$grpmessages[] = stripslashes($Tagparser->parsetags($groupmsg,$member));
					 }
					
					 $grpmsg = implode(" ",$grpmessages);
				}
				 $tokenmessage = $Tagparser->replaceTagContent('GROUP_MEMBER',$tokenmessage,$grpmsg);
				  $msg .= stripslashes($Tagparser->parsetags($tokenmessage,$userTable));
				
				 $messages[] = $msg;
				 
		    }
	   }
	   
		 if($first_user->status == -2) {
			$this->waiting_msg();
			
			return;
		}
		 if($first_event->thanksmsg_set) {
			$thanks_redirection = $first_event->thanks_redirection;
	   		$thanks_redirect_url = $first_event->thanks_redirect_url;
			
	   }
	    if ($first_event->thanksmsg_set && isset($thanks_redirection) && $thanks_redirection == 0 && isset($thanks_redirect_url)) {
			DT_Session::clearAll();
			$mainframe->redirect($thanks_redirect_url);
			exit;
		} else if(isset($thanks_redirection) && $thanks_redirection == 0 && isset($thanks_redirect_url)) {
			DT_Session::clearAll();
			$mainframe->redirect($thanks_redirect_url);
			exit;
		}
		
	    $this->view->setLayout('freethanks');
	    $this->view->assign('messages',$messages);
		$this->view->display();
		//die;
	    DT_Session::clearAll();
   }
   
    function overlap(){
   		global $overlap_event_msg;
		echo $overlap_event_msg;
    }
}
?>