<?php
// @ Add or modify a record to the user
// @ in: $in_nUserID  - mindex of user if NULL INSERT INTO
?>
<?php include("inc_variables.php");?>
<?php include("inc_functions.php");?>
<?php include("inc_functions_users.php");?>
<?php

    $target_path = 'images/users/';
    $target_path = $target_path . basename( $_FILES['Picture']['name']);

    foreach($_POST as $key => $p)
        $$key = $p;
        
    if($_FILES['Picture']['name']!='' && move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path))
        $Picture = $target_path;
    else
        $Picture = '';

	if( !isset($Save) ){
		if( isset( $in_nUserID )&& $in_nUserID>0)
			cl_redirect("user_detail.php?in_nUserID=$in_nUserID");
		else
			cl_redirect("users_page.php");
		exit;
	}

	$connection = cl_connect();
	cl_select($connection);
	// @ Modify?
	if( $in_nUserID != NULL ){
		if( strcmp($County,$lang_automata)==0)
			$County = cl_get_state($City);
        
        $emails = $_POST['EMail'];
        
        $old_default_email = trim(getDefaultEmail($in_nUserID));
        
        // nem töröljük az eddig használt logint ...
     /*   if(check_forum_existance($old_default_email) && $emails[0] != $old_default_email){
            //UpdateForumUser($in_nUserID, $old_default_email, $emails[0]);
        }
        */
        
        $old_login = trim(getForumLogin($in_nUserID));
        
		
		/*
        if(check_forum_existance_by_nUserID($in_nUserID) && trim($emails[0]) != $old_default_email && trim($emails[0]) != ""){
            //UpdateForumUserByNUserID($in_nUserID, $old_login, $emails[0]);
            $mailresz = explode('@',$emails[0]);
            $jelszo = trim($mailresz[0]);
            InsertUserToForum2($in_nUserID, md5($jelszo), trim($emails[0]), $Name, trim($emails[0]));
        }*/
		
		// minden e-mail címmel be lehessen lépni ..
		if(check_forum_existance_by_nUserID($in_nUserID) && trim($emails[0]) != ""){
			foreach($emails as $email){
				if( !check_forum_login_existance($email) && trim($email) != ""){
					$mailresz = explode('@',$email);
					$jelszo = trim($mailresz[0]);
					InsertUserToForum2($in_nUserID, md5($jelszo), trim($email), $Name, trim($email));
				}
			}
		}
        
        UpdateForumName($in_nUserID, $Name);
        
        DeleteUserEmails($in_nUserID);
        UpdateUserEmails($emails, $in_nUserID);
        
        $Web = webcimme($Web);
        if($Esely == ""){
        	$Esely = 'NULL';
        }
        
        $query =  "UPDATE $g_table_users SET 
				strName = '$Name',
				strCompany = '$Company',
				strTel = '$Tel',
				strMobil = '$Mobil',
				strFax = '$Fax',
				strCounty = '$County',
				nCtrID = {$nCtrID},
				strZip = '$Zip',
				strCity = '$City',
				strStreet = '$Street',
				strInfo = '$Info',
				nEsely = {$Esely},
				strWeb = '$Web',
                strSkype = '$Skype',";
        
        if($Picture_del != 1)
            $Picture == '' ? '' : $query .= "strPicture='$Picture',";
        else
            $query .= 'strPicture = NULL,';
		
        $query .= "nManagerID = {$NManagerID},";
        $query .= !is_numeric($NIsCommendatory) ? "nIsCommendatory = NULL," : "nIsCommendatory = {$NIsCommendatory},";
        $query .= "nCommendatoryID = {$NCommendatoryID},
				strInvoiceAddress = '$StrInvoiceAddress',
				strTaxNumber = '$StrTaxNumber',
				strCompanyNumber = '$StrCompanyNumber',
				strBankNumber = '$StrBankNumber',
				nUserProf = {$Profession}
		WHERE nUserID = $in_nUserID";
        //die($query);
		cl_query($query);
		
		
		$elozoertekesites = cl_query(" SELECT * FROM cl_ertekesites WHERE nUserID=$in_nUserID");
        $elozoertekesitesrow = mysql_fetch_array($elozoertekesites, MYSQL_ASSOC);
        
        if( $elozoertekesitesrow['nUserID'] == "" && ($leiras!="" || $kezzel!="" || $vasarlas!="") ){
        	$query4 = "INSERT INTO `cl_ertekesites` (`nUserID`, `leiras`, `kezzel`, `vasarlas`) VALUES ";
        	$query4 .= "({$in_nUserID}, '{$leiras}', '{$kezzel}', '{$vasarlas}')";
        	
        	cl_query($query4);
        }elseif( $elozoertekesitesrow['nUserID'] != "" ){
        	$query2 = "UPDATE cl_ertekesites SET 
				leiras = '".$leiras."',
				kezzel = '".$kezzel."',
				vasarlas = '".$vasarlas."'";
			$query2 .= " WHERE nUserID=$in_nUserID";
			
			cl_query($query2);
        }
        
        // új esemény ha változott a vásárlási ido
        if( ($elozoertekesitesrow['vasarlas'] == "" || $elozoertekesitesrow['vasarlas'] != $vasarlas ) && ( $vasarlas != 0 && $vasarlas != "") ){
        	$idopont = date("Y-m-d",time()+(60*60*24*30*$vasarlas));
        	$query3 = "INSERT INTO `cl_Messages` (`nUserID`, `nManagerID`, `dateSend`, `strTopic`, `strMsg`, `bFlag`) VALUES ";
        	$query3 .= "({$in_nUserID}, '{$NManagerID}', '{$idopont}', 'Program vasarlasi terv', 'Program vasarlasi terv, az ertekesitesi adatok alapjan', '0')";
        	
        	cl_query($query3);
        }
		
		
		cl_redirect("user_detail.php?in_nUserID=$in_nUserID"); 		
	}else{
	//	$nUserID = cl_get_user_id($Name,$/*$City,$Street*/);
		foreach($_POST['EMail'] as $email){
			$nUserID = cl_get_user_id($Name,$email);
			if( $nUserID>0 && is_numeric($nUserID) )
				break;
		}
		
		if( $nUserID>0 && is_numeric($nUserID) ){
			echo $exists_user."<BR>Felhasználó: <a href='https://clusers.cadline.hu/clusers/user_detail.php?in_nUserID=".$nUserID."'>".$nUserID."</a><BR>";
			cl_redirect("users_page.php",false); 
		}else{
			if( strcmp($County,$lang_automata)==0)
				$County = cl_get_state($City);
				
			$EMails = $_POST['EMail'];
			if($Esely == ""){
				$Esely = 'NULL';
			}

			$query =  "INSERT INTO $g_table_users 
			(strName, strCompany, strTel, strMobil, strFax, strCounty, strZip, strCity, strStreet, strInfo, strWeb, strSkype, nManagerID,
					nIsCommendatory,nCommendatoryID,strInvoiceAddress,strTaxNumber,strCompanyNumber,strBankNumber,nUserProf,nEsely";
            $Picture == '' ? '' : $query .= ',strPicture';
            $query .= ")";
            
            $query .= " VALUES('$Name','$Company','$Tel','$Mobil','$Fax','$County','$Zip','$City','$Street','$Info','$Web','$Skype','$NManagerID','$NIsCommendatory','$NCommendatoryID','$StrInvoiceAddress','$StrTaxNumber','$StrCompanyNumber','$StrBankNumber', '$Profession', $Esely";
            
            $Picture == '' ? '' : $query .= ",'$Picture'";
            
            $query .= ")";
			cl_query($query);
			$UserID=mysql_insert_id();
            
            $query = "INSERT INTO `cl_users_emails` (`nUserID`, `email`, `default`) VALUES ";
            
            $default = 1;
            foreach($EMails as $email){
                $query .= "({$UserID}, '{$email}', {$default}),";
                $default = 0;
            }
            
            $query = rtrim($query, ',');
            
            cl_query($query);
            
            
            $query4 = "INSERT INTO `cl_ertekesites` (`nUserID`, `leiras`, `kezzel`, `vasarlas`) VALUES ";
        	$query4 .= "({$UserID}, '{$leiras}', '{$kezzel}', '{$vasarlas}')";
        	
        	cl_query($query4);
        	
        	// új esemény ha van vásárlási ido
			if( $vasarlas != 0 && $vasarlas != "" ){
				$idopont = date("Y-m-d",time()+(60*60*24*30*$vasarlas));
				$query3 = "INSERT INTO `cl_Messages` (`nUserID`, `nManagerID`, `dateSend`, `strTopic`, `strMsg`, `bFlag`) VALUES ";
				$query3 .= "({$in_nUserID}, '{$NManagerID}', '{$idopont}', 'Program vasarlasi terv', 'Program vasarlasi terv (ertekesitesi adatok alapjan)', '0')";
				
				cl_query($query3);
			}
            
            
            cl_redirect("user_detail.php?in_nUserID=$UserID"); 
		}
	}
	cl_close( $connection );
	exit;
?>
