<?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/users2/';
    $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"); 
		exit;
	}

	$connection = cl_connect();
	cl_select($connection);
	
	// @ Modify?
	if( $in_nUserID != NULL ){

        //$EMail = str_replace(',', ';', $EMail);
        $emails = $_POST['EMail'];
        
        $old_default_email = getDefaultEmail($in_nUserID);
        
        
        if(check_forum_existance($old_default_email) && $emails[0] != $old_default_email){
    //        UpdateForumUser($in_nUserID, $old_default_email, $emails[0]);
        }
        
        $old_login = getForumLogin($in_nUserID);
        
       /* if(check_forum_existance_by_nUserID($in_nUserID) && $emails[0] != $old_login && trim($emails[0]) != ""){
     //       UpdateForumUserByNUserID($in_nUserID, $old_login, $emails[0]);
     		$mailresz = explode('@',$emails[0]);
            $jelszo = $mailresz[0];
            InsertUserToForum2($in_nUserID, md5($jelszo), $emails[0], $Name, $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_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);
        
        if($Esely == ""){
        	$Esely = 'NULL';
        }

		$query =  "UPDATE $g_table_users SET 
				strName='$Name',
				strCompany='$Company',
				strTel='$Tel',
				strMobil='$Mobil',
				strFax='$Fax',
				strEMail='$EMail',
				nCtrID='$Country',
				nRegionID='$Region',
				nOrgID='$Organization',
				strZip='$Zip',
				strCity='$City',
				strStreet='$Street',
				strInfo='$Info',
				nEsely = {$Esely},
				strWeb='$Web',
                strSkype='$Skype',
				nManagerID='$NManagerID',
				nIsCommendatory='$NIsCommendatory',
				nCommendatoryID='$NCommendatoryID',
				strInvoiceAddress='$StrInvoiceAddress',
				strTaxNumber='$StrTaxNumber',
				strCompanyNumber='$StrCompanyNumber',
				strBankNumber='$StrBankNumber',
				nUserProf='$Profession'";
        
        if($Picture_del != 1)
            $Picture == '' ? '' : $query .= ", strPicture='$Picture'";
        else
            $query .= ', strPicture=null';
		
        $query .= " WHERE nUserID=$in_nUserID";
        
        
        $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 üzenet 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_query($query);
		
		cl_redirect("user_detail.php?in_nUserID=$in_nUserID"); 		
	}else{
		$nUserID = cl_get_user_id($Name,$City,$Street);
		
		if( $nUserID>0 ){
			echo $exists_user."<BR>";
			cl_redirect("users_page.php",false); 
		}else{

            $EMail = str_replace(',', ';', $EMail);
            $EMails = $_POST['EMail'];

			$query =  "INSERT INTO $g_table_users 
			(strName, strCompany, strTel, strMobil, strFax, strEMail, nCtrID, nRegionID, nOrgID, strZip, strCity, strStreet, strInfo, strWeb, strSkype, nManagerID,
					nIsCommendatory,nCommendatoryID,strInvoiceAddress,strTaxNumber,strCompanyNumber,strBankNumber,nUserProf";
            $Picture == '' ? '' : $query .= ',strPicture';
            
            $query .= ")";
            
            $query .= " VALUES('$Name','$Company','$Tel','$Mobil','$Fax','$EMail','$Country','$Region','$Organization','$Zip','$City','$Street','$Info','$Web','$Skype','$NManagerID',
					'$NIsCommendatory','$NCommendatoryID','$StrInvoiceAddress','$StrTaxNumber','$StrCompanyNumber','$StrBankNumber', '$Profession'";
            
            $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 változott a 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;
?>
