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


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

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

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

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

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

		if(hun())
			$data['title'] = 'ARCHLine.XP Új Csoport';
		else
			$data['title'] = 'ARCHLine.XP New Group';

		if($this->uri->segment(3)!=""/* && $this->uri->segment(4)!=""*/){
			$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"); //or die("$fajlnev nem nyithato meg");
			$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/groups/".$lang."/".$kepnev;
				//$ide= "public/img/groups/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];


			//	$kepresz = substr($data["kepalap"],0,18);
			//	print "rész: ".$kepresz.$lang."/".$data["kepalap"];

		//	}
			fclose($fa);



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

		$data['csik']=array('home','groups');
		$data['javascript'] = array('objfeltoltes');

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

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

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

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

	function kat(){

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

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

		if(!$this->uri->segment(3))
			$kat = 2;
		else
			$kat = $this->uri->segment(3);
		$csoportok = $this->mgroups->get_bykat($kat);
		$data['groups']=$csoportok;

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

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

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

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

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

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

		$data['groups']=$this->mgroups->get_bykategoria($kat);

		$kategoriak = $this->muser->get_kategoriak('group');
		$kategoriak = $this->mgroups->get_darab($kategoriak);

		$data['kategoriak'] = $kategoriak;

		$data['aktiv']=$kat;

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

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

	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/groups/".$lang."/"; //mi itt szeretnénk tárolni
		$error = "";
		$kep = "";
		$link = "public/groups/".$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";
			}

			if (!copy($tempPath,$storePath)){
				$error .= "nem másoltam.<br>mit: ".$tempPath.", hova: ".$storePath;
			}else{
				$link .= $storeFilename;
				$prSize = 6500;
				$prSize = filesize($storePath); //??
				$this->kep($kep);
			}

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

			$storeFolder = "public/groups/".$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 .= "<br />nem másoltam.<br>ezt: ".$_FILES["originalFilenameField"]["tmp_name"].", ide: ".$storeFilename;
			}
			$originalFilename = $_FILES["originalFilenameField"]["name"];
			$storePath = $storeFilename;

			$this->kep($kep);

			$link = $storeFilename;

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



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

			if( $_POST["kategoria"] > 20 )
				$ujgr->kat_id = $_POST["kategoria"];
			else
				$ujgr->kat_id = "";

			$this->mgroups->newgroup($ujgr);

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

		//ha nem volt print:
		redirect('groups/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/groups/".$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);
		//	print " Sikerült a másolás! ";
		}else{
			$prFile = "NULL";
			$error .= "Nem másoltam át a fájlt.<br />Mit: ".$fajlnev.", Hova: ".$fajlnev2;
		}

		$link = "public/groups/".$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('group',$nevhun,$neveng);
		}else{
			$kat_id = $_POST["kategoria"];
		}


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

			$this->mgroups->newgroup($ujgr);

		}

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

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

}

?>
