<?php
class Mpages extends Model
{
	function Mpages()
	{
		parent::Model();
	}
	
	function get_row($page_id)
	{
		if($page_id=="")
			return false;
		
		
		if(is_numeric($page_id))
			$query = $this->db->where('page_id',$page_id)->get('pages');
		else
			$query = $this->db->where('page_url',$page_id)->get('pages');
		
		
		if($query->num_rows()==0)
			return false;
		else
			return $query->row();
	}
	
	function get_latest($num=10,$lang,$simple=false)
	{
		if($simple)
		{
			$query = $this->db->select('page_id,page_date,page_title,page_lang,page_locked');
		}
		else
		{
			$query = $this->db->select();
		}
		
		$query = $query->where('page_lang',$lang)->orderby('page_date','desc')->limit($num)->get('pages');
		
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();	
		}
		
	}
	
	function get_pages_dropdown($lang)
	{
		$tomb['none']= $this->lang->line('pages_dropdown_none');
		
		$query = $this->db->select('page_id, page_title')->where('page_lang',$lang)->orderby('page_title','asc')->get('pages');
		
		if($query->num_rows()==0)
		{
			return $tomb;
		}
		else
		{
			foreach($query->result() as $page)
			{
				$tomb['/page/show/'.$page->page_id]=$page->page_title;
			}
			
			return $tomb;
		}
	}
	
	function get_all_by_category($sort = 'page_date',$del="")
	{
		$cats = $this->config->item('page_categories');
		$cats = $cats[$this->muser->user['admin_lang']];
	
		if(!$cats || count($cats)==0)
		{
			
		}
		else
		{
			foreach($cats as $cat)
			{
				if($sort == 'page_date'){
				    $direction = 'desc';
				} else {
				    $direction = 'asc';
				}
				
                $act = $this->db->where('page_lang',$this->muser->user['admin_lang'])->where('page_category',$cat);
				if($del!="")
					$act = $act->where('del',$del);
				$act = $act->orderby($sort,$direction)->get('pages');
				
				if($act->num_rows()!=0)
				{
					$return[$cat]=$act->result();
				}
			}
		}
	
		$return[$this->lang->line('no_category')]=$this->db->query("select page_id, page_title from pages where page_lang='".$this->muser->user['admin_lang']."' and page_category=''")->result();
		
		return $return;
	
	}

    function get_category_pages($category = '', $lang = 'hun', $with_details = false, $order_by = 'page_date', $way = 'desc'){
        $query = $this->db->select('page_id')->where('page_category',$category)->where('page_lang',$lang)->order_by($order_by, $way)->get('pages');
		
        if($query->num_rows()==0){
			return false;
        } else {
            if($with_details){
                foreach($query->result() as $row){
                    $row->details = $this->get_row($row->page_id);
                }
            }
            
			return $query->result();
		}
	}

	function get_category_dropdown()
	{
		$cat = $this->config->item('page_categories');
		$cat=$cat[$this->muser->user['admin_lang']];
		
		foreach($cat as $c)
		{
			$tomb[$c]=$c;
		}
	
		return $tomb;
	}
	
	function bool_pageInterior($page_id, $type = 'pages')
	{
		if($type=='pages')
		{
			$query = $this->db->where('page_id', $page_id)->where('page_category', 'Interior keret')->get('pages');
		}
		elseif($type=='news')
		{
            $query = $this->db->where('news_id', $page_id)->where('news_category', 'Interior keret')->get('news');
		}
		else
		{
			return false;
		}
		
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
?>