<?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 DtregisterControllerValidate extends DtrController {

   var $name = "Validate";

    function __construct($config = array()){

		 parent::__construct($config);

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

	}

   function discountcode(){
	   
	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));
	   $event = & DtrTable::getInstance('Event','Table');
	   $event->load($eventId);
	   $error = true;
	   if(DT_Session::get('Setting.process') == 'register') {
	     $memtot = DT_Session::get('Setting.User.memtot');
		 $reg_type = DT_Session::get('Setting.User.type');
	   } else {
	      $process = DT_Session::get('Setting.process') ;
		  $memtot = DT_Session::get($process.'.User.memtot');
		  $reg_type = DT_Session::get($process.'.User.type');
	   }
	   
	   $default_type = ($reg_type == 'I')?'I':'B';
	   $type = JRequest::getVar('type',$default_type);
	   if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
			
			$discount_code_id = $event->validate_code($_REQUEST['discount_code'],NULL,$type,$memtot,$reg_type); 
			if($discount_code_id !== false){
			   $error = false;	
			  // DT_Session::set('register.User.'.$userIndex.'.discount_code_id', $discount_code_id );
			}else{
			  // DT_Session::set('register.User.'.$userIndex.'.discount_code_id', false );
			    $error = true;
				 if(!isset($event->TableEventdiscountcode->TableDiscountcode->error) || $event->TableEventdiscountcode->TableDiscountcode->error == "")
			  {
			  	$event->TableEventdiscountcode->TableDiscountcode->error = JText::_('DT_CODE_INCORRECT');
			  }	
				
			    $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
				$message = $event->TableEventdiscountcode->TableDiscountcode->error;
//$event->TableEventdiscountcode->TableDiscountcode->error;
			}
			
		}
		//ob_clean();
		echo ($error)?$message:'true';
		die;
	      
   }

	function captcha(){

	    $database = &JFactory::getDBO();

		$code=JRequest::getVar('captcha','');

		$userIp=$_SERVER['REMOTE_ADDR'];

		//Check database

		$sql="Select count(*) From #__dtregister_captcha where user_ip= ".$database->quote($userIp)." and code=".$database->quote($code);

		$database->setQuery($sql);

		$total=$database->loadResult();

		//$_SESSION['register']['secCheck'] = $total;

		if($total){

			echo "true";

		}else{

			echo "false";

		}

die;

	}

	function email(){

	    global $usercreation;

		$database = &JFactory::getDBO();

		ob_clean();
		
		$eventId=intval($_GET['eventId']);
		
		$sql="Select config_value From #__dtregister_config where config_key = 'prevent_duplication'";

		$database->setQuery($sql);

		$prevent_duplication=$database->loadResult();

		$global_prevent_duplication = $prevent_duplication;

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

		$eventTable->load($eventId);
        $prevent_duplication = $eventTable->prevent_duplication;
		$eventTable->overrideGlobal($eventId);
		
		$usercreation = $eventTable->usercreation;
		$my = &JFactory::getUser();
		
		$field_id = key($_REQUEST['Field']);
		$email=array_pop($_REQUEST['Field']);
		
		if(!$my->id && $usercreation > 0 && !isset($_GET['member_check'])){

			$sql="SELECT COUNT(*) FROM #__users WHERE email=".$database->quote($email);

			$database->setQuery($sql);

			$total=$database->loadResult();

			if($total){

				echo "\"".JText::_( 'DT_JOOMLA_EMAIL_EXISTS' )."\"";

			}else{

			    echo "true";	

			}
			
			die;
		}
		
		if($prevent_duplication){

		  $prevent_duplication = $global_prevent_duplication;

		}else{

		  $prevent_duplication = 0;

		}
		
		if(!$prevent_duplication || isset($_REQUEST['dup_check'])){

			echo "true";	

		}else{
			$user_con = "";
            if(isset($_GET['userId'])) {
				$userid = intval($_GET['userId']);
				$user_con = " and u.userId <>  ".$database->quote($userid);
				
			}
			$sql="SELECT COUNT(*) FROM #__dtregister_user u inner join #__dtregister_user_field_values v on u.userId  = v.user_id   WHERE eventId=".$database->quote($eventId)."  AND v.field_id = ".$database->quote($field_id)."  and v.`value` = ".$database->quote($email)." and u.cancel <> 1 and u.type = 'I' and u.status in(0,1) $user_con";

			$database->setQuery($sql);

			$total=$database->loadResult();
			$member_con = "";
			 if(isset($_GET['member_id'])) {
				$memberid = intval($_GET['member_id']);
				$member_con = " and m.groupMemberId <>  ".$database->quote($memberid);
				
			}
		$sql="SELECT COUNT(*) FROM #__dtregister_user u inner join #__dtregister_group_member m on m.groupUserId = u.userId inner join  #__dtregister_member_field_values v on m.groupMemberId   = v.member_id   WHERE eventId= ".$database->quote($eventId)."  AND v.field_id = ".$database->quote($field_id)."  and v.`value` = ".$database->quote($email)." and u.cancel <> 1 and u.type = 'G' and u.status in(0,1) $member_con";

			$database->setQuery($sql);

			$total2=$database->loadResult();
       
			if($total || $total2){

				$clean = ob_get_clean();
				echo "\"".JText::_('DT_ALREADY_REGISTERED')."\"";die;

			}else{

				echo "true";

			}

		}

die;

    }

	function uniqueUser(){

	    $my = &JFactory::getUser();

		$database = &JFactory::getDBO();

		if(isset($_GET['username'])){

		   $username = $_GET['username'];

		   $where = " username=".$database->quote($username)." ";

		}else{
			//".$database->quote($field_id)."  and v.`value` = ".$database->quote($email)."

		   $email=array_pop($_GET['Field']);

		   $where = " email=".$database->quote($email)." ";

		}

		$sql="SELECT COUNT(*) FROM #__users WHERE $where ";

		$database->setQuery($sql);

		$total=$database->loadResult();

		if($total){

			echo JText::_( 'DT_JOOMLA_USERNAME_EXISTS' );

		}else{

		   echo "true";

		}
	
die;

	}	

}

?>