<?php
class Mnews extends Model
{
	function Mnews()
	{
		parent::Model();
	}
	
	
	function get_row($id)
	{
		$row = $this->db->where('news_id',$id)->get('news');
		if($row->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $row->row();
		}
	}
	
	function get_all($lang = false, $simple = true, $arch = false)
	{
		if($simple)
		{
		    /*array_multisort($news->news_m, SORT_DESC, $news);*/
			/*$query = $this->db->select('news_id, news_title, news_y, news_m, news_d, news_short')->from('news');*/
			$query = $this->db->select('news_id, news_title, news_y, news_m, news_d,  news_short, news_text')->orderby('news_y desc, news_m desc, news_d desc')->from('news');
			
			
		}
		else
		{
			$query = $this->db->select('news_id, news_title, news_y, news_m, news_d, news_short')->orderby('news_y desc, news_m desc, news_d desc')->from('news');
		}
		
		if($lang)
		{
			$query = $query->where('news_lang',$lang);
		}
		
		if($arch=='fresh')
		    $query = $query/*->orderby('news_y desc, news_m desc, news_d desc')*/->where('news_arch',0);
		elseif($arch=='arch')
		    $query = $query/*->orderby('news_y desc, news_m desc, news_d desc')*/->where('news_arch',1);
		
		$query = $query->orderby('news_y desc, news_m desc, news_d desc')->get();
		
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();
		}
	}
	
	function get_latest($count = 10, $lang = false, $simple = true, $arch = false)
	{   
        $hirek=array();
	    //$datum = $news->news_y . '-' . $news->news_m . '-' . $news->news_d;
        //$news->datum = date('D, d M Y H:i:s +0200', strtotime($datum));
		
        //array_push($hirek,$news);	
		if($simple)
		{
		    
		    usort($hirek,"hasonlito");	
			$query = $this->db->select('news_id, news_title, news_y, news_m, news_d, news_short, DATE(CONCAT(news_y,'/',news_m,'/',news_d)) as ujdatum')->from('news');
			/*$query=$hirek->db->select('news_id, news_title, news_short')->from('news');*/
			/*$query=$this->db->select('news_id, news_title, news_short')->orderby('ev desc,honap desc,nap desc')->from('news');*/
		}
		else
		{
			$query = $this->db->from('news');
		}
		
		if($lang=='hun')
		{
			$query = $query->where('news_lang',$lang);
		}
		else
		{
			$query = $query->where('news_lang',$lang);
		}
		
		$query = $query->where('public','1');
		
		if($arch=='fresh')
		    $query = $query->orderby('news_y desc, news_m desc, news_d desc')->where('news_arch',0);
			
		elseif($arch=='arch')
		    $query = $query->orderby('news_y desc, news_m desc, news_d desc')->where('news_arch',1);
		    $query = $query->orderby('news_y desc, news_m desc, news_d desc')->limit($count)->get();
		//    $query = $query->orderby('ujdatum desc')->limit($count)->get();
		
		//$query2 = $this->db->query("SELECT *, DATE(CONCAT(news_y,'/',news_m,'/',news_d)) as ujdatum  FROM `news` WHERE news_lang='".$lang."' order by ujdatum DESC LIMIT 0,".$count);//->get();
			
			
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();
		}
	}
	
	function get_category_dropdown()
	{
		$cat = $this->config->item('news_categories');
		$cat=$cat[$this->muser->user['admin_lang']];

		foreach($cat as $c)
		{
			$tomb[$c]=$c;
		}

		return $tomb;
	}
	
	function hasonlito( $a, $b ){
	if ( strtotime($a->datum) == strtotime($b->datum) )
		return 0;
	if ( strtotime($a->datum) > strtotime($b->datum) )
		return -1;
	return 1;
    }
	
	
}
?>