<?php
@session_start();
include( "f_admin.php" );
include( "f_wfl.php" );
include_once("thumbnail.php");
$html = "";

if ( ( $_SESSION[ "benutzer_anmeldename" ] == "superuser" ) and ( $_SESSION[ "mandant_name" ] == "Standard" ) )
{
	header( "Location: us_uebersicht.php" );
	exit;
}

if ( strlen( $_POST[ "dokarttyp" ] ) == 0 )
{
//	header( "Location: us_uebersicht.php" );
//	exit;
}
else
{
	if ( $_POST[ "dokarttyp" ] == "DOK" )
	{
		//xlog( 1, json_encode( $_FILES ) );
		///////////////////////// sichern der hochgeladenen Datei
		$f = file_get_contents( $_FILES[ "file" ][ "tmp_name" ] );
		
		
		$tmpdir = $_SERVER["DOCUMENT_ROOT"]."/uploads";

		$zaehler = 1;

		while ( file_exists( $tmpdir."/".$_POST[ "praefix" ]."_".$zaehler ) )
		{
			$zaehler = bcadd( $zaehler, 1, 0 );
		}
		
		
		// Nutzdatei
		move_uploaded_file( $_FILES[ "file" ][ "tmp_name" ], $tmpdir."/".$_POST[ "praefix" ]."_".$zaehler );
		
		
		//$fh = fopen( $tmpdir."/".$_POST[ "praefix" ]."_".$zaehler, "w" );
		
		//xlog( 2, "datei ".  $tmpdir."/".$_POST[ "praefix" ]."_".$zaehler );
		
		//xlog( 1, "fh ".json_encode( $fh ) );
		
		//$erg = fwrite( $fh, $f );
		
		//fclose( $fh );
		
		
		// Info-Datei
		$fh = fopen( $tmpdir."/".$_POST[ "praefix" ]."_".$zaehler.".info", "wb" );
		$erg = fwrite( $fh, json_encode( $_FILES ) );
		fclose( $fh );
		
		$praefix = $_POST[ "praefix" ]."_".$zaehler;
		
		$ergebnis = welche_dokumentarten_darf_benutzer_importieren( $_SESSION[ "mandant_name" ], $_SESSION[ "benutzer_anmeldename" ] );
		
	}
	else
	{
		$ergebnis = welche_aktenarten_darf_benutzer_importieren( $_SESSION[ "mandant_name" ], $_SESSION[ "benutzer_anmeldename" ] );
	}
	
	//xlog(5, json_encode( $_FILES ) );
	
	//$html = "<span style='font-size:150%;font-weight:700;'>Dateiname</span>".$_FILES["file"]["name"]."";
	$html = "<hr /><table border=0>";
	$html = $html."<tr><td style='font-weight:700; width:120px;' >Dateiname</td><td>".$_FILES["file"]["name"]."</td></tr>";
	$html = $html."<tr><td style='font-weight:700;' >MIME-Type</td><td>".$_FILES["file"]["type"]."</td></tr>";
	$html = $html."<tr><td style='font-weight:700;' >Dateigröße</td><td>".number_format( $_FILES["file"]["size"], 0, '', '.')." Bytes</td></tr>";
	$html = $html."</table><hr />";
	
	
	if ( $ergebnis[ 0 ] > 0 )
	{
		
		$html = $html.'<select name="alpha_03" size="20" style="height:100%; width:225px;" onchange="$( \'#importmaske\' ).load( \'su_web.php?cmd=liefere_importmaske&mandant_name='.urlencode( $_SESSION[ "mandant_name" ] ).'&alpha_01='.$praefix.'&element_typ=\'+this.value, function() { $( \'.date-pick\' ).datepicker({ showAnim: \'clip\', buttonImageOnly: true, changeMonth: true, changeYear: true, yearRange: \'1990:2020\', showOn: \'both\', buttonImage: \'img/kalender.png\', showWeek: true } ); } ) " >';
		
		//echo '<select name="alpha_03" style="height:100%; width:255px;" onchange="ajaxFunction( \'su_web.php?cmd=liefere_importmaske&mandant_name='.urlencode( $mandant_name ).'&element_typ=\'+this.value+\'alpha_01=, \'importmaske\' );" ><option />';
		
		foreach ( $ergebnis[ 1 ] as $dokart_kuerzel )
		{
			$erg = liefere_dokartinformationen( $_SESSION[ "mandant_name" ], $dokart_kuerzel );
			$html = $html."<option value='".$erg[ 2 ][ "dokart_kuerzel" ]."'>".$erg[ 2 ][ "dokart_name" ]."</option>";
		}
		
		$html = $html."</select><div id='importmaske' style=' vertical-align:top;  position:absolute;  display:inline; margin-top:10px; margin-left:10px;' ></div>";
	}
	else
	{
		header( "Location: us_uebersicht.php" );
		exit;
	}
	
}
/////////////////////////// sichern der Datei fertig



if ( strlen( $_SESSION[ "benutzer_anmeldename" ] ) > 0 )
{
	////////////////////////////////////////
	// Javascript-Dateien
	$_SESSION[ "javascript_dateien" ] = "jquery.fileupload.js|jquery.fileupload-ui.js|jquery.fileupload-jquery-ui.js";
	////////////////////////////////////////
	
	////////////////////////////////////////
	// Javascript-Startup
	$_SESSION[ "javascript_startup" ] = '';
	////////////////////////////////////////
	
	////////////////////////////////////////
	// CSS-Dateien
	$_SESSION[ "css_dateien" ] = "jquery.fileupload.css|jquery.fileupload-ui.css";
	////////////////////////////////////////
	

	$_SESSION[ "title" ] = "Dokumentimport / Aktenanlage";
	include( 'ssi_header.php' );
	$_SESSION[ "title" ] = "";
	
	////////////////////////////////////////
	// Javascript-Dateien
	$_SESSION[ "javascript_dateien" ] = "";
	////////////////////////////////////////
	
	////////////////////////////////////////
	// Javascript-Startup
	$_SESSION[ "javascript_startup" ] = "";
	////////////////////////////////////////
	
	////////////////////////////////////////
	// CSS-Dateien
	$_SESSION[ "css_dateien" ] = "";
	////////////////////////////////////////
	
	//onclick=\"$( '#ergebnis_3' ).load('su_web.php?cmd=benutzer_anlegen', function() { $( '.date-pick' ).datepicker({ showAnim: 'clip', buttonImageOnly: true,  showOn: 'both', buttonImage: 'img/kalender.png', showWeek: true } ); } );
	
	
	$us_rech_imp_button = liefere_benutzerspezifische_einstellung( $_SESSION[ "mandant_name" ], $_SESSION[ "benutzer_anmeldename" ], "us_rech_imp_button" );
	
	if ( $us_rech_imp_button == "ja" )
	{
		echo "<div style='margin-left:200px; padding-bottom:30px;  min-height:550px;'  >";
		include( "us_menu.php" );
	}
	else
	{
		echo "<div style='margin-left:20px; padding-bottom:30px;  min-height:550px;'  >";
	}
	
	echo "<h2>Schritt 2 / 2 - Indexierung angeben</h2>";
	
	echo $html;
	
	
	echo '</div>';
	
	
	echo "<p style='clear:both;' />";
	include( 'ssi_footer.php' );

}
else
{
	header("Location: index.php");
}
?>