<?php
/*  Nyilvános link  */
include('application/controllers/collection2.php');

class Showroom extends Collection2
{
	var $pw = "dfDF34mkZ6";
	var $ervenydesseg = 3600;
	var $kepek_utvonal = "public/img/collection/users/";
	var $nagykepneve = '1__big.jpg';
	var $kiskepneve  = '1__small.jpg';
	var $kozepkepneve= '1__300.jpg';
	var $nagynagykepneve= '1__1024.jpg';

	function Showroom()
	{
		parent::Collection2();
	}
	
	function index(){
		$data['style']    = array('style_new','collection2web');
		$data['tartalom'] = 'site';
		parent::index($data);
	}
	
	function brandgroup(){
		$data['style']    = array('style_new','collection2web');
		$data['tartalom'] = 'site';
		parent::brandgroup($data);
	}
	
	function manufacturer(){
		
		$data['style'] = array('style_new','showroomweb_min');
		$data['tartalom'] = 'site';
		parent::manufacturer($data);
	}
	
	function category(){
		$data['style']    = array('style_new','showroomweb_min');
		$data['tartalom'] = 'site';
		parent::category($data);
	}
	
	function details(){
		$data['style']    = array('style_new','showroomweb_min');
		$data['tartalom'] = 'site';
		parent::details($data);
	}
	
	function search(){
		$data['style']    = array('style_new','showroomweb_min');
		$data['tartalom'] = 'site';
		parent::search($data);
	}
	
	// felhasználó public projektjeinek listája
	function user(){
		$uid = $this->uri->segment(3);
		$kepekutvonal = $this->kepek_utvonal.$uid."/";
		if( !is_numeric($uid) || !is_dir($kepekutvonal) ){
			redirect("showroom");
		}
		
		$project = $this->uri->segment(4);
		if( $project != "" && is_dir($kepekutvonal.$project) ){
			$this->userproject($uid, $project);
			return;
		}
		
		// azok a projektek melyekben van nyilvanos elem
		$projektek = $this->db->query("SELECT cp_id FROM collection_user WHERE userid={$uid} AND deleted=0 and shared=1 GROUP BY cp_id")->result();
		$id_list = "";
		foreach($projektek as $p){
			$id_list .= $p->cp_id.",";
		}
		$id_list = substr($id_list,0,-1);
		
		$kategoriak = $this->db->query("SELECT userid, mappa project, nev FROM collection_projects WHERE userid={$uid} AND deleted=0 and cp_id IN ({$id_list}) ORDER BY nev ASC")->result();
		
		$data['kategoriak'] = $kategoriak;
		
		$data['userproject'] = true;
		$data['style']    = array('style_new','collection2web');
		$data['tartalom'] = 'collection2/category';
		
		$this->load->view("site",$data);
	}
	
	// felhasználó kiválasztott projekt elemeinek megjelenítése
	function userproject($uid,$project){
		$kepekutvonal = $this->kepek_utvonal.$uid."/".$project."/";
		$elemek = $this->db->query("SELECT * FROM `collection_user` WHERE userid={$uid} AND project='".$project."' AND shared=1 AND deleted=0 ")->result();
		
		$kiskepek = array();
		foreach($elemek as $elem){
			// csak azok amikhez van nagy is
			if( file_exists($kepekutvonal.$elem->cu_id."/".$this->kiskepneve) && file_exists($kepekutvonal.$elem->cu_id."/".$this->nagykepneve) ){
				$ujelem       = new stdClass();
				$ujelem->kep  = $kepekutvonal.$elem->cu_id."/".$this->kiskepneve;
				$ujelem->o_id = $elem->cu_id;
				$kiskepek[]   = $ujelem;
			}
		}
		$project2 = $this->db->query("SELECT mappa FROM `collection_projects` WHERE userid={$uid} AND cp_id='".$elemek[0]->cp_id."' AND deleted=0 ")->row();
		
		$mit = array("\r\n", "\n", "\r");
		$adatok = $elemek[0];
		$adatok->title_hun = $adatok->title_eng = $adatok->title;
		$adatok->leiras = str_replace($mit,'<br />', $adatok->desc);
		$adatok->o_id   = $adatok->cu_id;
		$adatok->mappa  = $project2->mappa;
		
		$fajlok = scandir($this->kepek_utvonal.$uid.'/'.$project.'/'.$adatok->cu_id.'/');
		unset($fajlok[array_search($this->nagykepneve, $fajlok)]);
		unset($fajlok[array_search($this->kiskepneve, $fajlok)]);
		unset($fajlok[array_search($this->kozepkepneve, $fajlok)]);
		unset($fajlok[array_search($this->nagynagykepneve, $fajlok)]);
		
		$data['nagykepfix'] = $kepekutvonal.$elemek[0]->cu_id."/".$this->nagykepneve;
		$data['adatok']  = $adatok;
		$data['kepek']   = $kiskepek;
		$data['fajlok']  = $fajlok;
		$data['sorszam'] = 0;
		$data['kovetkezo'] = $kiskepek[1]->o_id;
		$data['elozo']   = "";
		$data['vurl']    = "/showroom/detailssimpleuser/";
		
		$data['login']   = $this->_getlogineduser();
		$data['userid']  = $uid;
		
		$data['javascript'] = array('fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack','fancybox-1.3.4/fancylive');
		$data['style'] = array('style_new','collection2web','../js/Smooth-Div-Scroll/css/smoothDivScroll','../js/fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4v2');
		
		$data['tartalom'] = 'collection2/details';
		$this->load->view('site',$data);
	}
	
	function detailssimpleuser(){
		if( !$this->uri->segment(3) || !is_numeric($this->uri->segment(3)) ){
			print "Az egyszeru reszletek: hibas parameter";
			return;
		}
		
		$cuid   = $this->uri->segment(3);
		$adatok = $this->db->where('cu_id',$cuid)->where('shared', 1)->where('deleted', 0)->get('collection_user')->row();
		$fajl   = $this->kepek_utvonal.$adatok->userid."/".$adatok->project."/".$adatok->cu_id."/".$adatok->file;
		
		$kovi   = $this->db->where('userid',$adatok->userid)->where('project',$adatok->project)->where('cu_id > '.$cuid)->where('shared', 1)->where('deleted', 0)->orderby('cu_id ASC')->limit('1')->get('collection_user')->row();
		$elozo  = $this->db->where('userid',$adatok->userid)->where('project',$adatok->project)->where('cu_id < '.$cuid)->where('shared', 1)->where('deleted', 0)->orderby('cu_id ASC')->limit('1')->get('collection_user')->row();
		$sorszam = $this->db->where('userid',$adatok->userid)->where('project',$adatok->project)->where('cu_id < '.$cuid)->where('shared', 1)->where('deleted', 0)->orderby('cu_id ASC')->get('collection_user')->num_rows();
		
		$project2 = $this->db->query("SELECT mappa FROM `collection_projects` WHERE userid=".$adatok->userid." AND cp_id='".$adatok->cp_id."' AND deleted=0 ")->row();
		
		$adatok->title_hun = $adatok->title_eng = $adatok->title;
		$mit = array("\r\n", "\n", "\r");
		$adatok->leiras = str_replace($mit,'<br />', $adatok->desc);
		$adatok->mappa  = $project2->mappa;
		
		$fajlok = scandir($this->kepek_utvonal.$adatok->userid.'/'.$adatok->project.'/'.$adatok->cu_id.'/');
		unset($fajlok[array_search($this->nagykepneve, $fajlok)]);
		unset($fajlok[array_search($this->kiskepneve, $fajlok)]);
		unset($fajlok[array_search($this->kozepkepneve, $fajlok)]);
		unset($fajlok[array_search($this->nagynagykepneve, $fajlok)]);
		
		$data['adatok'] = $adatok;
		$data['fajlok'] = $fajlok;
		$data['sorszam']   = $sorszam;
		$data['nagykepfix']= $this->kepek_utvonal.$adatok->userid."/".$adatok->project."/".$adatok->cu_id."/".$this->nagykepneve;
		$data['kovetkezo'] = $kovi->cu_id;
		$data['elozo']  = $elozo->cu_id;
		
		$data['login']  = $this->_getlogineduser();
		$data['userid'] = $adatok->userid;
		
		$this->load->view('collection2/detailssimple',$data);
	}
	
	function edit(){
		$cuid = $this->uri->segment(3);
		if( !$cuid || !is_numeric($cuid) )
			redirect('showroom');
		
		$login = $this->_getlogineduser();
		if( !$login || !is_numeric($login->user_id) )
			redirect('collection/login');
		
		$adatok = $this->db->where('cu_id',$cuid)->where('userid',$login->user_id)->where('deleted', 0)->get('collection_user')->row();
		
		$fajlokhelye = $this->kepek_utvonal.$adatok->userid;
		$data['meret']    = str_replace("\t".$fajlokhelye,"",exec('du -sh '.$fajlokhelye));
		$data['adatok']   = $adatok;
		$data['projektek']= $this->db->where('userid',$login->user_id)->where('deleted', 0)->get('collection_projects')->result();
		$data['tartalom'] = 'collection2/edit';
		
		$this->load->view('collection2/edit',$data);
		//$this->load->view('site',$data);
	}
	
	function editdo(){
		$login = $this->_getlogineduser();
		$cuid = $this->input->post("cu_id");
		$uj['title']   = $this->input->post("title");
		$uj['desc']    = $this->input->post("desc");
		$uj['cp_id']   = $this->input->post("cp_id");
		$uj['shared']  = ($this->input->post("shared")=="on") ? 1 : 0;
		
		// saját elem-e
		$query = $this->db->where('cu_id',$cuid)->where('userid',$login->user_id)->where('deleted', 0)->get('collection_user');
		if($query->num_rows() == 0){
			redirect('showroom');
		}
		include('application/helpers/sajatfeltolto.php');
		include('application/helpers/myimg_helper.php');
		
		$adatok = $query->row();
		$projectdir = $this->db->query("SELECT mappa FROM collection_projects WHERE cp_id=".$uj['cp_id']." AND userid=".$adatok->userid." AND deleted=0")->row();
		$projectdir = $projectdir->mappa;
		
		// projekt változtatás esetén áthelyezni a szerveren az új helyére
		if( $adatok->cp_id != $uj['cp_id'] ){
			$honnan = $this->kepek_utvonal.$adatok->userid."/".$projectdir."/".$cuid;
			$hova   = $this->kepek_utvonal.$adatok->userid."/".$projectdir."/".$cuid."/";
			exec("mv {$honnan} {$hova}");
		}
		
		// új nagy kép
		if( isset($_FILES['nagykep']) && $_FILES['nagykep']['error'] == 0 ){
			$helye =  $this->kepek_utvonal.$adatok->userid."/".$projectdir."/".$cuid."/";
			$ujnagykep = $helye.$this->nagykepneve;
			exec("rm {$ujnagykep}");
			feltolt("nagykep",$helye,$this->nagykepneve,true);
			
				// nagy kep atmeretezese, 0,3-as arány eltérésig kivágja a közepét, egyébként kiegészíti fehérrel
			$nagykeparany = (488/396);
			list($ow,$oh) = getimagesize($ujnagykep);
			$orignagykeparany = ($ow/$oh);
			$diff = abs($nagykeparany-$orignagykeparany);
			if( $diff < 0.3 ){
				kemeretezesvagassh($ujnagykep,$ujnagykep,488,396);
			}else{
				kemeretezesvagasshbg($ujnagykep,$ujnagykep,488,396);
			}
			
			chmod($ujnagykep,0777);
		}
		
		// új kis kép
		if( isset($_FILES['kiskep']) && $_FILES['kiskep']['error'] == 0 ){
			$helye = $this->kepek_utvonal.$adatok->userid."/".$projectdir."/".$cuid."/";
			$ujkiskep = $helye.$this->kiskepneve;
			exec("rm {$ujkiskep}");
			feltolt("kiskep",$helye,$this->kiskepneve,true);
			
				// kis kep atmeretezese
			kemeretezesvagassh($ujkiskep,$ujkiskep,68,68);
			chmod($ujkiskep,0777);
		}
		
		// mentés
		$this->db->update('collection_user',$uj,"cu_id={$cuid}");
		
		redirect("showroom/user/".$login->user_id."/".$projectdir);
	}
	
	private function _getlogineduser(){
		if( !isset($_COOKIE['azonosito'],$_COOKIE['sessid']) )
			return false;
		
		$ell = md5($_COOKIE['azonosito'].$this->pw.$_COOKIE['sessid']);
		
		if ( !empty($_COOKIE['hash']) && $_COOKIE['hash']== $ell ){
			setcookie("sessid",$_COOKIE['sessid'],time()+$this->ervenydesseg,"/");
			setcookie("azonosito", $_COOKIE['azonosito'] , time()+$this->ervenydesseg,"/");
			setcookie("hash",$_COOKIE['hash'] ,time()+$this->ervenydesseg,"/");
			
			$lang  = $this->muser->user['lang'];
			$query = "SELECT * FROM forum_user WHERE login = '".$_COOKIE['azonosito']."' AND nyelv='".$lang."' AND active=1 AND deleted=0";
			
			$query = $this->db->query($query);
			
			if($query->num_rows() == 0){
				return false;
			} else {
				return $query->row();
			}
			
		}else{
			return false;
		}
	}
	
}

?>