<?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"); 
		exit;
	}

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

        $EMail = str_replace(',', ';', $EMail);
		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";
        
		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);

			$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();
			cl_redirect("user_detail.php?in_nUserID=$UserID"); 
		}
	}
	cl_close( $connection );
	exit;
?>
