<h1>Unos dokumenta</h1>
<form action="dokumenta.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<table width="100%">
<tr>
	<td>Izaberite dokument sa Vašeg računara<br/><br/></td></tr>
	<tr><td><input type="file" name="dokument"><br/><br/></td></tr>
</tr>
<tr>
<td>Unesite ključne reči za dokument razdvojene znakom <b>;</b> bez blanko znakova.<br/><br/></td></tr>
<tr><td><input type="text" name="kljucne" size=50></td>
</tr>
<tr>
	<td><br/><br/><input type="submit" name="submit" value="unesi"></td><td></td>
</tr>
</table>						
</form>
<?php

function db_aldina_ngram_connect()
{
$result= mysqli_connect("localhost","tagstudi_aldina","aldina1234567","tagstudi_dokumenta");
if(!$result)
	return false;
return $result;
}

if($_POST['submit']=='unesi')
{
$con=db_aldina_ngram_connect();
		if ( (isset($_FILES['dokument']['name']) &&  //ime datoteke na korisnikovom racunaru
			  is_uploaded_file($_FILES['dokument']['tmp_name']))) //je putanja privremene datoteke na Web serveru
		{
			if (!isset($fileid))
			$fileid = mysqli_insert_id($con);
			$imedokumenta=$_FILES['dokument']['name'];
			$path=$_FILES['dokument']['name'];
			$file = new SplFileInfo($path);
			$ext  = $file->getExtension();
			//echo $ext;
			//$type = basename($_FILES['dokument']['type']);
			$filename = "/home/tagstudio/public_html/shealth/servis1/dokumenta/$imedokumenta";
			$linkname="/shealth/servis1/dokumenta/$imedokumenta";
			switch ($ext) {
			case 'txt':
					
							move_uploaded_file($_FILES['dokument']['tmp_name'], 
							 $filename);
							 $sadrzaj=file_get_contents($filename);// cuva sadrzaj dokumenta u promenljivu
							break;
			case 'pdf':
					//$filename = "euprava/aldina/dokumenta/$imedokumenta";
							move_uploaded_file($_FILES['dokument']['tmp_name'], 
							 $filename);
							 include('pdf2text.php');
							 $sadrzaj= pdf2text($filename);
							// cuva sadrzaj dokumenta u promenljivu
							break;
			case 'docx':
					//$filename = "euprava/aldina/dokumenta/$imedokumenta";
							move_uploaded_file($_FILES['dokument']['tmp_name'], 
							 $filename);
							 include('textkonverzije.php');
							 $content = read_file_docx($filename);
							if($content != false) 
							{
							$sadrzaj=nl2br($content);
							//echo $sadrzaj;
							}
							else 
							{
							echo 'Couldn\'t the file. Please check that file.';
							}
							// cuva sadrzaj dokumenta u promenljivu
							break;
							/*
			case 'doc':
					$filename = "dokumenta/$imedokumenta";
							move_uploaded_file($_FILES['dokument']['tmp_name'], 
							 $filename);
							 include('textkonverzije.php');
							 $sadrzaj = read_doc_file($filename);
							 echo $sadrzaj;
							// cuva sadrzaj dokumenta u promenljivu
							break;*/
			case 'xml':
					//$filename = "euprava/aldina/dokumenta/$imedokumenta";
							move_uploaded_file($_FILES['dokument']['tmp_name'], 
							 $filename);
							 $sadrzaj = file_get_contents($filename);
							 //echo $sadrzaj;
							// cuva sadrzaj dokumenta u promenljivu
							break;
			
			default:        print 'Pogresan format dokumenta, moze biti .txt, .docx, .xml ili .pdf: '.
								  $_FILES['dokument']['type']; //tip datoteke
			}
			
			
			
			
			if($con)
			{
mysqli_query($con,"set names 'utf8'");
	mysqli_query($con, "INSERT INTO dokument(id_dokumenta, izvor, naslov, link) VALUES (null,'$sadrzaj','$imedokumenta','$linkname')");
//	$id=mysqli_insert_id();
	echo "Uspesno unet dokument ".$filename;
	}
	else
	{
	echo "Greska";
	}
	
	include("Normalizacija_edit.php");//ngram analiza 4
      $id_doc=mysqli_query($con,"SELECT MAX( id_dokumenta ) as max FROM  dokument");
$row = mysqli_fetch_array($id_doc, MYSQL_ASSOC);
$id=$row["max"];
 	pripremi($id, $sadrzaj);
	
	if (isset($_POST['kljucne']))
{
$kljucne=explode(';', $_POST['kljucne']);
$result = count($kljucne);
$recenica='';
for($i=0;$i<$result;$i++)
{
$recenica = $recenica.$kljucne[$i].' ';
}
$recenica=$recenica.' |';
$r = normalizacija($recenica);
$rniz=explode(' |', $r);
echo $rniz[0];
$kljuc = explode(' ',$rniz[0]);
$result = count($kljuc)-1;
for($j=0;$j<$result;$j++)
{

$result2 = mysqli_query($con,"SELECT COUNT(*) as broj FROM `kljucne` WHERE `id_dokumenta` ='$id' AND rec='$kljuc[$j]'");
while($row = mysqli_fetch_array($result2)) 
{
	$broj=$row['broj'];
}
if($broj==0)
{
mysqli_query($con,"INSERT INTO kljucne(rec,br_pojava,p,id_dokumenta, hi) VALUES ('$kljuc[$j]','null','null','$id','null')");
}
}


	}


	}
		}
		@chmod($filename,0777);


?>