<?php
class Projects extends Controller
{
	function __construct()
	{
		parent::Controller();

		$this->lang->load('site', $this->muser->user['lang']);

		$this->load->model('mprojects');
	}

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

	function supportupload(){
		include('application/helpers/sajatfeltolto.php');

		$guid = $this->uri->segment(4);
		$sorozatszam = $this->uri->segment(3);

		print_r($this->uri->segments);

		print "guid: ".$guid."<br> sorozatszam: ".$sorozatszam;

		if( !$guid || !$sorozatszam ){
			$record->osztaly = "projects";
			$record->fv = "supportupload";
			$record->hiba = "Hiányzó guid vagy sorozatszám:\n".$guid."\n".$sorozatszam;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);

			return;
		}

		$honnan = '/home/vendeg/support/'.$guid;
		$hova = 'public/ugyfelupload/'.$sorozatszam;

		if( !file_exists($honnan) && file_exists($honnan."=") )
			$honnan .= "=";
		elseif( !file_exists($honnan) && file_exists($honnan."==") )
			$honnan .= "==";


		if(!is_dir($hova)){
			mkdir($hova,0777);
			exec("chmod 777 /var/www/hosting/cadline.hu/www/".$hova);
		}
		$hova .= "/".trim(base64_decode($guid),"=");;

		if( athelyez($honnan,$hova) ){
			exec("chmod 777 /var/www/hosting/cadline.hu/www/".$hova);
		}
		else{
			// sikertelen masolas

			$record->osztaly = "projects";
			$record->fv = "supportupload";
			$record->hiba = "Másolás hiba, honnan:".$honnan."\nhova: ".$hova;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}

	}

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

		if($this->uri->segment(3)=="java"){
			$data['tartalom']='projects/java';
		}
		else if($this->uri->segment(3)=="simple"){
			$data['tartalom']='projects/simple';
		}
		else{
			$data['tartalom']='projects/upload';
		}

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

		$data['csik']=array('home','projects');
		$data['javascript'] = array('onUploadFinished');

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

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

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


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


			$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/projects/".$lang."/".$kepnev;
				//$ide= "public/img/projects/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];

		//	}
			fclose($fa);


			$data['tartalom']='projects/uploadnew';
		}else{
			redirect('projects/upload');
		}

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

		$data['javascript'] = array('objfeltoltes');

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

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

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

	function cadline(){
		$data['title'] = 'ARCHLine.XP Projektek szerkesztése';
		$data['tartalom']='projects/cadline';
		$data['csik']=array('home','projects');

		//$projects = $this->mprojects->get_all();
		//$data['projects'] = $projects;

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

	function kat(){

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

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

		if(!$this->uri->segment(3) || !is_numeric($this->uri->segment(3)))
			$kat = 6;
		else
			$kat = $this->uri->segment(3);

		$projektek = $this->mprojects->get_bykat($kat);
		$data['projects']=$projektek;

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

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

	function kategoria(){
		redirect("/collection/kategoria/1");




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

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

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


		$data['projects']=$this->mprojects->get_bykategoria($kat);

		$kategoriak = $this->muser->get_kategoriak('project');
		$kategoriak = $this->mprojects->get_darab($kategoriak);

		$data['kategoriak'] = $kategoriak;

		$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->mprojects->get_row($download_id);
		if(!$record)
		{
			show_404();
		}

		redirect($record->project);
	}

	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/projects/".$lang."/"; //mi itt szeretnénk tárolni
		$error = "";
		$kep = "";
		$link = "public/projects/".$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 egyszerűsítve:
		else if ($_FILES["originalFilenameField"]["error"]==0){

			$storeFolder = "public/projects/".$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 projekt 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->project = $link;
			$ujpr->szerzo = $_POST["SzerzoField"];
			$ujpr->kat_id = $_POST["kategoria"];

			$this->mprojects->newproject($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('projects/kategoria/'.$_POST["kategoria"]);
	}


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


		$fajlnev = $_POST["originalFilenameField"];  // ez van a vendegnel
		//$fajlnev2 = $_POST["eredetinev"];   // ez legyen a publicban
		//$fajlnev2 = $this->ekezet($_POST["eredetinev"]);   // ez legyen a publicban


		$fajlnev2 = $_POST["originalFilenameField"];	// ez legyen a publicban
		$parameterbe = $_POST["eredetinev"];			// ez a név jelenik meg a letöltéskor az url-ben, paraméterként


		$fajl = $tempFolder.$fajlnev;

		$link = "public/projects/".$lang."/".$fajl;

		$storeFolder = "public/projects/".$lang."/";
		$storeFilename= $storeFolder.time()."_".$fajlnev2;

		if (copy($fajl,$storeFilename)){
			$prFile = $storeFilename;
			$prSize = filesize($storeFilename);
			$kep = $_POST["kepalap"];
			$this->kep($kep);
		}else{
			$prFile = "NULL";
			$error .= "Nem másoltam át a fájlt<br /> vendegben: ".$fajlnev." publicba: ".$fajlnev2;
		}
		$originalFilename = $_FILES["originalFilenameField"]["name"];
		$storePath = $storeFilename;


		$link = $storeFilename;



		// ú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('project',$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 = $kat_id;
			$ujpr->text = $_POST["projectDescriptionField"];
			$ujpr->project = $link;
			$ujpr->eredeti = $parameterbe;
			$ujpr->szerzo = $_POST["SzerzoField"];
			$ujpr->kat_id = $kat_id;

			$this->mprojects->newproject($ujpr);

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

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

	function ekezet($input){
		$mit = array("/á/", "/Á/", "/é/", "/É/", "/í/", "/Í/", "/ö/", "/Ö/", "/ő/", "/Ő/", "/ó/", "/Ó/", "/ú/", "/Ú/", "/ü/", "/Ü/", "/ű/", "/Ű/", "/ /");
		$mire = array("a", "A", "e", "E", "i", "I", "o", "O", "o", "O", "o", "O", "u", "U", "u", "U", "u", "U", "_");
		$output = preg_replace($mit, $mire, $input);
		return $output;
	}

	function kep(&$kep){
		if ($_FILES["kep"]["error"]==0){
			$lang = $this->muser->user['lang'];
			$kepnev = time()."_". $_FILES["kep"]["name"];
			$ide= "public/img/projects/".$lang."/".$kepnev;
			if (copy($_FILES["kep"]["tmp_name"],$ide)){
				$kep = $ide;
			}
		}
	}


}

?>
