<?php
class Objects extends Controller
{
	function __construct()
	{
		parent::Controller();
		$this->lang->load('site', $this->muser->user['lang']);
		$this->load->model('mobjects');
	}

	function index()
	{
		$this->kategoria();
	}

	function upload(){
		if(hun())
			$data['title'] = 'ARCHLine.XP Új Objektum';
		else
			$data['title'] = 'ARCHLine.XP New Object';

		if($this->uri->segment(3)=="java"){
			$data['tartalom']='objects/java';
		}
		else if($this->uri->segment(3)=="simple"){
			$data['tartalom']='objects/simple';
		}
		else{
			$data['tartalom']='objects/upload';
		}
		$data['csik']=array('home','objects');
		$data['javascript'] = array('onUploadFinished');
		$data['kategoriak'] = $this->muser->get_kategoriak('object');

		$this->load->view('site',$data);
	}

	function uploadnew(){
		redirect('/collection/upload/'.$this->uri->segment(3).'/object');

		if(hun()){
			$data['title'] = 'ARCHLine.XP Új Objektum';
			$data['feltetel'] = $this->mpages->get_row(572);
		}
		else{
			$data['title'] = 'ARCHLine.XP New Object';
			$data['feltetel'] = $this->mpages->get_row(571);
		}

		if($this->uri->segment(3)!=""/* && $this->uri->segment(4)!=""*/){
			$data["fajl"] = $this->uri->segment(3);
			$data["eredeti"] = $this->uri->segment(4);

			/* szövegfájl átadással */
			if(/*eng()*/true){
				$fajlnev = '/home/vendeg/'.$this->uri->segment(3);
				$fa = fopen( $fajlnev, "r" ) or die("Upload error");
				$ol = fgets( $fa, 1024);
				$adatok = explode("#",$ol);
			//	if( count($adatok) == 5){
					$lang = $this->muser->user['lang'];
					$honnan = "/home/vendeg/".$adatok[2];
					$kepnev = time()."_".$adatok[3];
					$ide= "public/img/objects/".$lang."/".$kepnev;
					//$ide= "public/img/objects/temp/".$kepnev;
					if( copy($honnan,$ide) ){
						$adatok[3] = $ide;
					}else{
						$adatok[3] = "";
					}


					$data["fajl"] = $adatok[0];
					$data["eredeti"] = $adatok[1];
					$data["vendegkep"] = $adatok[2];
					$data["kepalap"] = $adatok[3];
					$data["cim"] = $adatok[4];

				//	echo(unicode_encode($data["fajl"], 'UTF-8'));
					//print_r($data);
			//	}
				fclose($fa);
			}

		}else{
			redirect('objects/upload');
		}

		$data['tartalom']='objects/uploadnew';
		$data['csik']=array('home','objects');
		$data['javascript'] = array('objfeltoltes');
		$data['kategoriak'] = $this->muser->get_kategoriak('object');

		//print_r($data);

		$this->load->view('site_upload',$data);
	}






	function upload2(){
		redirect("collection/upload/".$this->uri->segment(3)."/object");

		/*if(hun()){
			$data['title'] = 'ARCHLine.XP Új Objektum';
			$data['feltetel'] = $this->mpages->get_row(572);
		}
		else{
			$data['title'] = 'ARCHLine.XP New Object';
			$data['feltetel'] = $this->mpages->get_row(571);
		}

		if($this->uri->segment(3)!=""){
			$data["fajl"] = $this->uri->segment(3);
			$data["eredeti"] = $this->uri->segment(4);

			/* szövegfájl átadással */
			/*$fajlnev = '/home/vendeg/'.$this->uri->segment(3);
			$fa = fopen( $fajlnev, "r" ) or die("Upload error");
			$ol = fgets( $fa, 1024);
			$adatok = explode("#",$ol);
			$lang = $this->muser->user['lang'];
			$honnan = "/home/vendeg/".$adatok[2];
			$kepnev = time()."_".$adatok[3];
			$ide= "public/img/objects/".$lang."/".$kepnev;
			if( copy($honnan,$ide) ){
				$adatok[3] = $ide;
			}else{
				$adatok[3] = "";
			}


			$data["fajl"] = $adatok[0];
			$data["eredeti"] = $adatok[1];
			$data["vendegkep"] = $adatok[2];
			$data["kepalap"] = $adatok[3];
			$data["cim"] = $adatok[4];

			fclose($fa);

		}else{
			redirect('objects/upload');
		}

		$data['tartalom']='objects/uploadnew';
		$data['javascript'] = array('objfeltoltes');
		$data['kategoriak'] = $this->muser->get_kategoriak('object');

		$this->load->view('site_upload',$data);*/
	}







	function simple(){
		if(hun())
			$data['title'] = 'ARCHLine.XP Új Objektum';
		else
			$data['title'] = 'ARCHLine.XP New Object';
		$data['tartalom']='objects/simple';
		$data['csik']=array('home','objects');

		$this->load->view('site',$data);
	}

	function kat(){

		if(hun())
			$data['title'] = 'ARCHLine.XP Objektumok';
		else
			$data['title'] = 'ARCHLine.XP Objects';

		$data['tartalom']='objects/kat';
		$data['csik']=array('home','objects');
		$data['javascript']=array('dl_select');

		if(!$this->uri->segment(3))
			$kat = 2;
		else
			$kat = $this->uri->segment(3);

		$data['objects']=$this->mobjects->get_bykat($kat);

		//$nyelv = $this->muser->user['lang'];

		$data['kategoriak'] = $this->muser->get_kategoriak('object');

		$data['aktiv']=$kat;
		$this->load->view('site',$data);

	}

	function kategoria(){
		if(hun())
			$data['title'] = 'ARCHLine.XP Objektumok';
		else
			$data['title'] = 'ARCHLine.XP Objects';

		$data['tartalom']='objects/kat';
		$data['csik']=array('home','objects');
		$data['javascript']=array('dl_select','objfeltoltes');

		if( !$this->uri->segment(3)  ||  !( $this->muser->letezokategoria($this->uri->segment(3),'object') ))
			$kat = 26;
		else
			$kat = $this->uri->segment(3);


		redirect("/collection/kategoria/".$kat);

		$data['objects']=$this->mobjects->get_bykategoria($kat);


		$kategoriak = $this->muser->get_kategoriak('object');
		$kategoriak = $this->mobjects->get_darab($kategoriak);

		$data['kategoriak'] = $kategoriak;
		//$data['kategoriak'] = $this->muser->get_kategoriak('object');

		$data['aktiv']=$kat;
		$data['title'] .= " - " . $this->muser->get_katName($kat);

		$this->load->view('site',$data);
	}

	function get()
	{
		$download_id = $this->uri->segment(3);
		if(!$download_id || !is_numeric($download_id))
		{
			show_404();
		}

		$record = $this->mobjects->get_row($download_id);
		if(!$record)
		{
			show_404();
		}

		redirect($record->object);
	}

	function submit(){
		$lang = $this->muser->user['lang'];
		$tempFolder = "/home/vendeg/"; //ide másolja az FTP applet a fájlt
		$storeFolder = "/home/cadline/public_html/website/public/objects/".$lang."/"; //mi itt szeretnénk tárolni
		$error = "";
		$kep = "";
		$link = "public/objects/".$lang."/";

		//  ha java-val:
		if( isset($_POST["tempFilenameField"]) ){
			$tempFilename = $_POST["tempFilenameField"]; //ezen a néven kerül feltöltésre a fájl (pl. 123456.file)
			$originalFilename = $_POST["originalFilenameField"]; //ez volt az eredeti neve (pl. szoveg.doc)
			$storeFilename = time()."_".$originalFilename;  //ezen a neven taroljuk mi (pl. 2347686323_szoveg.doc)


			$storePath = $storeFolder.$storeFilename;
			$tempPath = $tempFolder.$tempFilename;

			if ((strlen($tempFilename) < 3) || (strlen($originalFilename) < 3)){
				die ("Hibás fáljnevek!");
				$error .= "hiba";
			}

			//chmod($tempPath,0777);
			if (!copy($tempPath,$storePath)){
			//if (!rename($tempPath,$storePath)){
			//	print "Nem sikerül a helyére másolni a feltöltött fájlt!<br />";
				$error .= "hiba";
			}else{
			//	print "<h1>Sikeres feltöltés ($originalFilename)</h1>";
				//$storeFilename = $storePath;
				$link .= $storeFilename;
				$prSize = 6500;
				$prSize = filesize($storePath); //??
				$this->kep($kep);
			}

		}
		//  ha egyszerusitve:
		else if ($_FILES["originalFilenameField"]["error"]==0){

			$storeFolder = "public/objects/".$lang."/";
			$storeFilename= $storeFolder.time()."_". $_FILES["originalFilenameField"]["name"];

			$tempFilename = $_FILES["originalFilenameField"]["tmp_name"];

			if (copy($_FILES["originalFilenameField"]["tmp_name"],$storeFilename)){
				$prFile = $storeFilename;
				$prSize = $_FILES['originalFilenameField']['size'];
			}else{
				$prFile = "NULL";
				$error .= "hiba..";
			}
			$originalFilename = $_FILES["originalFilenameField"]["name"];
			$storePath = $storeFilename;

			$this->kep($kep);

			$link = $storeFilename;

		}else{
			$error = "nincs objektum fajl";
		}


		// Felvétel az adatbázisba
		if(!$error){
			$ujpr->email = $_POST["projectEmailField"];
			$ujpr->title = $_POST["projhectNameField"];
			$ujpr->meret = $prSize;
			$ujpr->datum = date('Y-m-d H:i:s',time());
			$ujpr->nyelv = $lang;
			$ujpr->kep = $kep;
			$ujpr->kategoria = $_POST["kategoria"];
			$ujpr->text = $_POST["projectDescriptionField"];
			$ujpr->object = $link;
			$ujpr->szerzo = $_POST["SzerzoField"];
			$ujpr->kat_id = $_POST["kategoria"];

			$this->mobjects->newobject($ujpr);

			//print "<br /> <b>Új projekt felvéve</b>";
		}else{
			//print "<br /> <b>Nem került adat az adatbázisba</b>";
		}

		//ha nem volt print:
		redirect('objects/kategoria/'.$_POST["kategoria"]);
	}

	function submitnew(){
		$lang = $this->muser->user['lang'];
		$tempFolder = "/home/vendeg/"; //ide másolja az FTP applet a fájlt
		$storeFolder = "/home/cadline/public_html/website/public/objects/".$lang."/"; //mi itt szeretnénk tárolni
		$error = "";
		$kep = "";

		$fajlnev = $_POST["originalFilenameField"];  // ez van a vendegnel
		$fajlnev2 = $_POST["eredetinev"];   // ez legyen a publicban
		$fajl = $tempFolder.$fajlnev;

		$ido = time();

		$storeFilename= $storeFolder.$ido."_".$fajlnev2;


		if (copy($fajl,$storeFilename)){
			$prFile = $storeFilename;
			$prSize = filesize($storeFilename);
		}else{
			$prFile = "NULL";
			$error .= "Nem másoltam át a fájlt<br /> vendegben: ".$fajlnev." publicba: ".$fajlnev2;
		}

		$link = "public/objects/".$lang."/".$ido."_".$fajlnev2;

		$kep = $_POST["kepalap"];
		$this->kep($kep);


		// új kategória felvétele, ha megadták
		if( $_POST["ujkat"] != "" ){
			if(hun()){
				$nevhun = $_POST["ujkat"];
				$neveng = "";
			}else{
				$nevhun = "";
				$neveng = $_POST["ujkat"];
			}

			$kat_id = $this->muser->new_kategoria('object',$nevhun,$neveng);
		}else{
			$kat_id = $_POST["kategoria"];
		}


		// Felvétel az adatbázisba
		if(!$error){
			$ujpr->email = $_POST["projectEmailField"];
			$ujpr->title = $_POST["projhectNameField"];
			$ujpr->meret = $prSize;
			$ujpr->datum = date('Y-m-d H:i:s',time());
			$ujpr->nyelv = $lang;
			$ujpr->kep = $kep;
			$ujpr->kategoria = $_POST["kategoria"];
			$ujpr->text = $_POST["projectDescriptionField"];
			$ujpr->object = $link;
			$ujpr->szerzo = $_POST["SzerzoField"];
			$ujpr->kat_id = $kat_id;

			$this->mobjects->newobject($ujpr);

			//print "<br /> <b>Új projekt felvéve</b>";
		}else{
			//print "<br /> <b>Nem került adat az adatbázisba</b>";
			//print $error;
		}

		//ha nem volt print:
		redirect('objects/kategoria/'.$kat_id);
	}

	function kep(&$kep){
		if ($_FILES["kep"]["error"]==0){
			$lang = $this->muser->user['lang'];
			$kepnev = time()."_". $_FILES["kep"]["name"];
			$ide= "public/img/objects/".$lang."/".$kepnev;
			if (copy($_FILES["kep"]["tmp_name"],$ide)){
				$kep = $ide;
				$prSize = $_FILES['kep']['size'];
				//print " Sikerült a képmásolás! ";
			}else{
				$prFile = "NULL";
				//print " Nem sikerült a képet felmásolni! ";
			}
		}
	}

}

?>
