<?php
class News extends Controller
{
	function News()
	{
		parent::Controller();
	}	
	
	function index()
	{
		//redirect('');
		
		hun() ? $data['csik']=array('Főoldal','Hírek') : $data['csik']=array('home','news') ;
		hun() ? $data['title'] = 'ARCHLine.XP Hírek' : $data['title'] = 'ARCHLine.XP News';
		$data['tartalom']='news/index_3';
		/*kezdet*/
		 $this->load->model('mevents');
        $hirek_lista = array();
		$hirek_news = $this->mnews->get_latest($this->muser->user['lang'],true,'fresh');
		foreach($hirek_news as $hir){
			$hir2 = '';
			
			$datum = $hir->news_y . '-' . $hir->news_m . '-' . $hir->news_d;
			$hir2->datum = date('D, d M Y H:i:s +0200', strtotime($datum));
			$hir2->title = $hir->news_title;
			$hir2->short = $hir->news_short;
			if(hun())
			    $hir2->url = "http://www.archline.hu/news/show/" . $hir->news_id;
			elseif(eng())
			    $hir2->url = "http://www.archlinexp.com/news/show/" . $hir->news_id;
			array_push($hirek_lista, $hir2);
			
		}
		$esemenyek = $this->mnews->get_latest($this->muser->user['lang'],true,'fresh');
		foreach($esemenyek as $hir){
			$hir2 = '';
			$hir2->datum = date('D, d M Y H:i:s +0200', $hir->datum);
			$hir2->title = $hir->title;
			$hir2->short = substr($hir->short,0,128) . "...";
			/*$hir2->url = "http://www.archlinexp.com/page/show/" . $hir->page;*/
			if(hun())
			    $hir2->url = "http://www.archline.hu/page/show/" . $hir->news_id;
			elseif(eng())
			    $hir2->url = "http://www.archlinexp.com/page/show/" . $hir->news_id;
			array_push($hirek_lista, $hir2);
		}
		
		
		usort( $hirek_lista, "hasonlito" );
		$data['hirek_lista'] = $hirek_lista;
		/*vége*/
		/*$this->load->view('site',$data);*/
		$this->load->view('site',$data);
	}
	
	function show()
	{
		$news_id = $this->uri->segment(3);
		if(!$news_id || !is_numeric($news_id))
		{
			$news_azon = mysql_real_escape_string($news_id);
			$row = $this->db->where('url',$news_azon)->get('news');
			if( $row->num_rows()==1 ){
				$news = $row->row();
			}else{
				redirect('');
			}
		}
		else{
			$news = $this->mnews->get_row($news_id);
		}
		
		if(!$news)
		{
			redirect('');
		}
		
		if( $news_id == 92 ){
			redirect('workshop/birmingham');
		}
		
		if($news->news_short!="")
			$metadesc = $news->news_short;
		else
			$metadesc = trim(str_replace("\n"," ",substr(($str=wordwrap(strip_tags($news->news_text),140,'$$')),0,strpos($str,'$$'))." ..."));//," \t\n\r\0\x0B\x00..\x1F");
		
		$data['news']=$news;
		$data['mobilink'] = "/mobile/news/".$news->news_id;
		$data['meta']['keyw'] = !hun() ? 'ARCHLine.XP, Interior, Design, CAD, Architect, Architectural' : 'ARCHLine.XP, Interior, Tervezés, CAD, Építészet, Belsőépítész';
		$data['meta']['keyw'] .= ', '.$news->news_title;
		$data['meta']['desc']  = $metadesc;
		$data['javascript']    = hun() ? array("lnkbuilding-hun") : array("lnkbuilding-eng");
		$data['csik']          = array('home','news',$news->news_title);
		$data['title']         = 'ARCHLine.XP - '.$news->news_title;
		$data['tartalom']      = 'news/show';
		
		$this->load->view('site',$data);
	}
	
	function show2(){
		$this->load->model('mclusers');
		
		$hir_id = $this->uri->segment(3);
		
		if(is_numeric($hir_id))
			$level = $this->db->query("SELECT * FROM `cadline2`.`news` WHERE news_id = ".$hir_id)->row();
		else
			$level = $this->db->query("SELECT * FROM `cadline2`.`news` WHERE url = '".$hir_id."'")->row();
		
		// email atalakitasa
		$temptomb = explode("/", $_SERVER[ 'REQUEST_URI'] );
		$email = str_replace("%40","@",end($temptomb));
		
		
		$egyezes = "news/show/".$hir_id;
		if(hun())
			$info = "Hír megtekintve: ".$level->news_title." (http://archline.hu/news/show/".$hir_id.")";
		else
			$info = "Has been seen: ".$level->news_title." (http://archlinexp.com/news/show/".$hir_id.")";
		$hibainfo = $level->url;
		
		if(hun()){
			if(UJUGYFELKEZELO===true)
				$this->mclusers->levelesemeny($email, "news", "show2", 64, $egyezes, $info, "hun", $hibainfo);
			else
				$this->mclusers->levelesemeny($email, "news", "show2", 126, $egyezes, $info, "hun", $hibainfo);
		}
		else{
			if(UJUGYFELKEZELO===true)
				$this->mclusers->levelesemeny($email, "news", "show2", 182, $egyezes, $info, "eng", $hibainfo);
			else
				$this->mclusers->levelesemeny($email, "news", "show2", 178, $egyezes, $info, "eng", $hibainfo);
		}
		
		
		$user = $this->phpsession->get('user');
		$user['voltmobiloldal'] = true;
		$this->phpsession->save('user',$user);
		$mobil = $user['mobil'];
		
		// átírányítás a hírre
		if($mobil===true)
			redirect("mobile/news/".$level->news_id);
		else
			redirect("news/show/".$hir_id);
		return;
	}
	
	function hasonlito( $a, $b ){
	if ( strtotime($a->datum) == strtotime($b->datum) )
		return 0;
	if ( strtotime($a->datum) > strtotime($b->datum) )
		return -1;
	return 1;
    }
}
?>