<?php

if(!defined('BASEPATH')) exit('Hacking Attempt : Get Out of the system ..!');

class Helper_model extends CI_Model
{
	public function __construct(){
		parent::__construct();
	}
	
	public function getSiteSettings(){

 $this->db->select('*');
 $this->db->from('settings');
 $this->db->where('id', 1);
 $query = $this->db->get();
 return $query->result_array();

}


	public function checkunique($field,$value,$table)
	{
		$this->db->select($field);
		$this->db->where($value);
		$query = $this->db->get($table);		
		return $query->num_rows();
	}
	
	
	public function  getdata($select,$table,$where){
		$this->db->select($select);
		if($where){	$this->db->where($where);}
		$query = $this->db->get($table);
		//echo $this->db->last_query();
		return $query->result();
	}
	
	public function getGallery($type)
	{
		$this->db->select('*');
		$this->db->where(array('status'=>1,'type'=>$type));	
		$this->db->order_by('order', 'ASC');
		$query = $this->db->get('gallery_category');
		return $query->result_array();
	}
	
	
	function fetch_country()
	 {
	  $this->db->order_by("name", "ASC");
	  $query = $this->db->get("settings_countries");
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->id.'">'.$row->name.'</option>';
	  }
	  return $output;
	 }

	 function fetch_state($country_id)
	 {
	  $this->db->where('country_id', $country_id);
	  $this->db->order_by('name', 'ASC');
	  $query = $this->db->get('settings_states');
	  $output = '<option value="">Select State</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->id.'">'.$row->name.'</option>';
	  }
	  return $output;
	 }

	 function fetch_city($state_id)
	 {
	  $this->db->where('state_id', $state_id);
	  $this->db->order_by('name', 'ASC');
	  $query = $this->db->get('settings_cities');
	  $output = '<option value="">Select City</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->id.'">'.$row->name.'</option>';
	  }
	  return $output;
	 }
 
 	function fetch_merchant()
	 {
	  $this->db->order_by("storename", "ASC");
	  $query = $this->db->get("merchant");
	  $output = '<option value="">Select Merchants</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->merchantId.'">'.$row->storename.' ('.$row->accountId.')</option>';
	  }
	  return $output;
	 }

	 function fetch_contact_list($merchantId)
	 {
	  $this->db->where('merchantId', $merchantId);
	   $this->db->or_where('merchantId', 0);
	  $this->db->order_by('title', 'ASC');
	  $query = $this->db->get('contact_list');
	  $output = '<option value="">Select List</option>';
	  foreach($query->result() as $row)
	  {
	   $output .= '<option value="'.$row->listId.'">'.$row->title.'</option>';
	  }
	  return $output;
	 }
 
	public function getPress($type)
	{
		$this->db->select('*');
		$this->db->where(array('table'=>$type));	
		$this->db->order_by('order', 'ASC');
		$query = $this->db->get('gallery');
		return $query->result_array();
	}
	
	public function allTestimonials(){
		$this->db->select('*');
		$this->db->where(array('status'=>1));	
		$this->db->order_by('order', 'ASC');
		$query = $this->db->get('testimonials');
		return $query->result_array();
	}
	
	public function addgallery($data){
		$this->db->insert('gallery', $data);			
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	public function removegallery($id){
		$this->db->where('url', $id );
		$this->db->delete('gallery');
	}
	
	public function setgallerydata($data,$id){
		   $this->db->where('id', $id);
			$this->db->update('gallery', $data);
	}
	
	public function getfeature()
	{
		$data['status']=1;
		$this->db->select('property.*,gallery.url as image');
		$this->db->join('gallery',"gallery.itemId=property.propertyId","left");
		$where = array('property.status'=>1,'property.feature'=>1,'gallery.table'=>'property');
		$this->db->where($where);
		$this->db->group_by('gallery.itemId');	
		$this->db->order_by('property.updatedon', 'ASC');
		$query = $this->db->get('property');
		return $query->result();
	}
   
	
	public function getfeaturelocation()
	{
		$this->db->select('*');
		$where = array("status"=>1,"feature"=>1);
		$this->db->where($where);
		$this->db->limit(6);
		$query = $this->db->get('locations');
		return $query->result();
	}
	
	public function getlatestblogs($limit){
		$this->db->select('articles.title,articles.image,articles.slug,articles.updatedon,article_category.title as cat');
		$this->db->join('article_to_category',"article_to_category.articleId=articles.articleId","left");
		$this->db->join('article_category',"article_to_category.articlecatId=article_category.articlecatId","left");
		$this->db->limit($limit);
		$this->db->where(array('articles.status'=>1));
		$this->db->group_by('article_category.articlecatId');	
		 $this->db->order_by('articles.updatedon', 'ASC');
		$query = $this->db->get('articles');		

		return $query->result();
	}
	
	public function siteseo($slug)
	{
		$this->db->select('*');
		$this->db->where(array('slug'=>$slug));
		$query = $this->db->get('site_seo');
//echo $this->db->last_query();		
		return $query->result();
	}
	
	public function delcisession($value){
		$sql="DELETE FROM ci_sessions WHERE `data` LIKE '%".$value."%';";
		$this->db->query($sql);

	}
	
	public function set_seo($data){
		
			$seodata=array();
			$seodata['seotitle']=$data['seotitle'];
			$seodata['seodescription']=$data['seodescription'];
			$seodata['seokeywords']=$data['seokeywords'];
			if(isset($data['seoimage']) && $data['seoimage'] !=null){$seodata['seoimage']=$data['seoimage'];}
			
			$where = array('itemId='=>$data['tableId'],'table='=>$data['table']);
			$this->db->where($where);
			$this->db->update('site_seo', $seodata);

			$this->db->where($data['parentId'],$data['tableId']);
			$this->db->update($data['table'], array('slug'=>$data['slug']));	
	}
		
	public function get_seo($id,$table){
		$this->db->select('*');
		$where = array('itemId='=>$id,'table='=>$table);
		$this->db->where($where);
		$query = $this->db->get('site_seo');		
		return $query->result();
	}
	
	public function updatemessageresponse($contact,$status,$response){
		
		$this->db->where(array('contact='=>$contact,'status<>'=>1));
		$this->db->update('messages', array('response'=>$response,'status'=>$status));	
	}
	
	public function getdailymsgcount($id){
		$sql = "SELECT COUNT(messageId) AS totalmsg FROM messages WHERE `status`=1 AND DATE(sendon)=DATE(CURRENT_DATE()) AND MONTH(sendon)=MONTH(CURRENT_DATE()) AND YEAR(sendon)=YEAR(CURRENT_DATE()) AND merchantId='".$id."'";
		$query = $this->db->query($sql);
    	return $query->result();
	}
		
}

?>