<?php
class Categories_Model extends CI_Model {
	function __construct(){

	}

	public function getCategories($parent_id=null){
		$this->db->select('*');
		$this->db->from('categories');
		if($parent_id){
			$this->db->where('parent_id',$parent_id);
		}
		$query = $this->db->get();
		return $query->result();
		
	}
	public function getCategory($id){
		$this->db->select('*');
		$this->db->from('categories');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
		
	}
	public function hasSubcategory($id){
		$this->db->select('id');
		$this->db->from('categories');
		$this->db->where('parent_id',$id);
		$query = $this->db->get();
		return count($query->result());
		
	}
	public function getParentCategories(){
		$this->db->select('*');
		$this->db->from('categories');
		$this->db->where('parent_id',0);
		$query = $this->db->get();
		return $query->result();
	}
	public function getSubCategories($parent_id){
		$this->db->select('*');
		$this->db->from('categories');
		$this->db->where('parent_id',$parent_id);
		$query = $this->db->get();
		return $query->result();
		
	}
	public function getPostings($category_id=null,$limit=20, $start=0){
		
		if(!empty($category_id)){
			/*$subcategories=$this->categories_model->getSubCategories($category_id);
			if(count($subcategories) > 0){
				//$subcategories=$this->categories_model->getSubCategories($category_id);
				foreach($subcategories as $subcategory){
					$this->db->or_where('category_id',$subcategory->id);
				}
			}*/
		
			$this->db->or_where('category_id',$category_id);
		}
		$this->db->select('*');
		$this->db->from('postings');
		$this->db->limit($limit,$start);
                $this->db->order_by('id','asc');
		$query = $this->db->get();
		return $query->result();
		
	}
	public function getPostingsCount(){
		return $this->db->count_all("postings");
	}
} 
?>
