<?php

class MPages extends Model{

	function Pages(){
	    parent::Controller();
		//session_start();

	  }
	function getHotelsByLocation($l_id){
		$data = array();
		$this->db->select('*');
		$this->db->from('company');
		$this->db->join('company_details','company_details.cmp_id = company.cmp_id');
		$this->db->where('company_details.cmp_location =',id_clean($l_id));
		$this->db->where('company_details.lang_id =',id_clean($this->session->userdata('lang_id')));
		$this->db->where('company.published =','Yes');
		$this->db->where('company_details.cat_id =','1');
		//$this->db->where('locations.lang_id=',id_clean($this->session->userdata('lang_id')));
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q-> free_result();
		return $data;
	}
	function getHotelsByLocation2($l_id, $limit, $current, $sort){
		if ($current==''){
		$current=0;
		}
		$data = array();
		$this->db->select('*');
		$this->db->from('company');
		$this->db->join('company_details','company_details.cmp_id = company.cmp_id');
		$this->db->where('company_details.cmp_location =',id_clean($l_id));
		$this->db->where('company_details.lang_id =',id_clean($this->session->userdata('lang_id')));
		$this->db->where('company.published =','Yes');
		$this->db->where('company_details.cat_id =','1');
		if ($sort=='name') {
				$this->db->orderby("company_details.cmp_name","asc");
		}
		if ($sort=='category') {
				$this->db->orderby("company.cmp_stars","desc");
		}
		if ($sort=='rooms') {
				$this->db->orderby("company.cmp_rooms","desc");
		}
		$this->db->limit($limit, $current);
		//$this->db->where('locations.lang_id=',id_clean($this->session->userdata('lang_id')));
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q-> free_result();
		return $data;
	}
	function getLocation($l_id){
		$data = array();
		$this->db->select('location');
		$this->db->from('locations');
		//$this->db->join('locations','project_details.prj_location = locations.location_id');
		$this->db->where('location_id =',id_clean($l_id));
		$this->db->where('lang_id =',id_clean($this->session->userdata('lang_id')));
		//$this->db->where('locations.lang_id=',id_clean($this->session->userdata('lang_id')));
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			$data = $Q->row_array();
		}
		$Q-> free_result();
		return $data;
	}

function getStars($id){
    $data = array();
		$this->db->select('stars');
		$this->db->from('company_facilities');
		$this->db->where('cmp_id',id_clean($id));
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getHotelsData($h_id){
		$this->db->select('*');
 	    $this->db->from('company');
	    $this->db->join('company_details','company.cmp_id=company_details.cmp_id', 'left');
		$this->db->where('company.cmp_id',$h_id);
		$this->db->where('company_details.lang_id =',id_clean($this->session->userdata('lang_id')));
		$this->db->limit(1);
		$Q = $this->db->get();
		//$this->session->set_userdata('lastquery', $this->db->last_query());
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
			//return $login=true;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			//return $login=false;
		}		
	}
	function getHotelPhotos($h_id) {
		$data=array();
		$this->db->select("*");
		$this->db->where('photo_cmpid', $h_id);
		$this->db->where('published','Yes');
		$this->db->orderby("date_added","desc");
		$Q=$this->db->get('photo');
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
	function getHotelFacilities($h_id) {
		$data=array();
		$this->db->select("*");
		$this->db->where('cmp_id', $h_id);
		$Q=$this->db->get('company_facilities');
		if($Q->num_rows() > 0){
     		 $data = $Q->row_array();
		}
		
		$Q->free_result();
		return $data;
	}

	function getPhotosByLocation($l_id){
		$data = array();
		$this->db->select('*');
		$this->db->from('photo_gallery');
		$this->db->where('published =','Yes');
		$this->db->where('photo_locid =',$l_id);
		//$this->db->where('locations.lang_id=',id_clean($this->session->userdata('lang_id')));
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q-> free_result();
		return $data;
	}
	function search($term){
		$data=array();
		$this->db->select("*");
		$this->db->from('company');
	    $this->db->join('company_details','company.cmp_id=company_details.cmp_id', 'left');
		$this->db->where('company_details.lang_id =',id_clean($this->session->userdata('lang_id')));
		$this->db->where('company.published','Yes');
		$this->db->where("(company_details.cmp_name LIKE '%$term%')");
		$this->db->orderby('company_details.cmp_name','asc');	
		$this->db->limit(50);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}








	function getProjectSale($limit) {
		$data=array();
		$temp=array();
		if($limit==0){
			$limit=4;
		}
		$this->db->select("prj_id,prj_thumbnail, prj_category");
		$this->db->where('prj_id !=', '40');
		$this->db->where('published','Yes');
		$this->db->orderby("prj_category","asc");
		$this->db->limit(100);
		$Q=$this->db->get('project');
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$temp[$row['prj_category']] =array(
					"prj_id"=>$row['prj_id'],
					"prj_thumbnail"=>$row['prj_thumbnail']
					);
			}
		}
		
		shuffle($temp);
		if(count($temp)){
			for($i=1;$i<=$limit;$i++){
				$data[]=array_shift($temp);
			}
		}
		
		$Q->free_result();
		return $data;
	}



//	function getRdmPic($cnstr) {
//		$data=array();
//		$temp=array();
//		$this->db->select("photo_thumb,photo_cnstr");
//		$this->db->where('photo_cnstr', $cnstr);
//		$this->db->where('published','Yes');
//		//$this->db->orderby("","asc");
//		$this->db->limit(100);
//		$Q=$this->db->get('photo');
//		if($Q->num_rows() > 0){
//			foreach($Q->result_array() as $row){
//				$temp[$row['photo_cnstr']] =array(
//					"photo_thumb"=>$row['photo_thumb']
//					);
//			}
//		}
//		
//		shuffle($temp);
//		if(count($temp)){
////			for($i=1;$i<=1;$i++){
////				$data[]=array_shift($temp);
////			}
//			$data = $Q->row_array($temp);
//		}
//		
//		$Q->free_result();
//		return $data;
//	}
	function getRdmPic($cnstr){
		$data = array();
		$Q = $this->db->query("SELECT photo_thumb FROM tbl_photo where photo_cnstr =".$cnstr." AND published='Yes' order by RAND() limit 1");
		if ($Q->num_rows() > 0){
			$data = $Q->row_array();
		}
		$Q->free_result();
		return $data;
	}













function getPagePath($path){
    $data = array();
    $this->db->where('path',$path);
    $this->db->where('status', 'active');
    $this->db->limit(1);
    $Q = $this->db->get('pages');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	
 function getAllPages(){
     $data = array();
     $Q = $this->db->get('pages');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }


	
 function addPage(){
	$data = array( 
		'name' => db_clean($_POST['name']),
		'keywords' => db_clean($_POST['keywords']),
		'description' => db_clean($_POST['description']),
		'status' => db_clean($_POST['status'],8),
		'path' => db_clean($_POST['path']),
		'content' => $_POST['content']
	);

	$this->db->insert('pages', $data);	 
 }
 
 function updatePage(){
	$data = array( 
		'name' => db_clean($_POST['name']),
		'keywords' => db_clean($_POST['keywords']),
		'description' => db_clean($_POST['description']),
		'status' => db_clean($_POST['status'],8),
		'path' => db_clean($_POST['path']),
		'content' => $_POST['content']

	);
 	$this->db->where('id', id_clean($_POST['id']));
	$this->db->update('pages', $data);	
 
 }
 
 function deletePage($id){
 	//$data = array('status' => 'inactive');
 	$this->db->where('id', id_clean($id));
	//$this->db->update('pages', $data);
	$this->db->delete('pages');	
 }
 
	
}

?>