Your IP : 216.73.216.91


Current Path : /var/www/html/inatote/application/models/
Upload File :
Current File : /var/www/html/inatote/application/models/Api_model.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');



class Api_model extends CI_Model {



	public function createUser($userInfo)
	{
		if ($this->db->insert('users',$userInfo)) {
			return true;
		}
		return false;
	}



	public function checkUserExist($email, $password)
	{
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('email',$email);
		$this->db->where('password',$password);
		$q = $this->db->get();
		if ($q->num_rows() > 0 ) {
			foreach ($q->result() as $row ) {
				$data[] = $row;
			}
			return $data[0];
		}
		return false;
	}


	function getStores($user_id,$latitude,$longitude,$radius,$search){
		if (isset($search) && !empty($search)){
			$qu = '
			SELECT *,IF(close_time<NOW(),TRUE,FALSE) as store_time,
			( 6371 * 
			    ACOS( 
			        COS( RADIANS( lat ) ) * 
			        COS( RADIANS( '.$latitude.' ) ) * 
			        COS( RADIANS( '.$longitude.') - 
			        RADIANS( lon ) ) + 
			        SIN( RADIANS( lat ) ) * 
			        SIN( RADIANS( '.$latitude.') ) 
			    ) 
			) 
			AS distance FROM store WHERE name like "%'.$search.'%"  HAVING distance <= '.$radius.' ORDER BY distance ASC
			';
		}else{
			$qu = '
			SELECT *,IF(close_time<NOW(),TRUE,FALSE) as store_time,
			( 6371 * 
			    ACOS( 
			        COS( RADIANS( lat ) ) * 
			        COS( RADIANS( '.$latitude.' ) ) * 
			        COS( RADIANS( '.$longitude.') - 
			        RADIANS( lon ) ) + 
			        SIN( RADIANS( lat ) ) * 
			        SIN( RADIANS( '.$latitude.') ) 
			    ) 
			) 
			AS distance FROM store HAVING distance <= '.$radius.' ORDER BY distance ASC
			';
		}
		$q = $this->db->query($qu);
		if ($q->num_rows() > 0 ) {
			$data[] = array(
					'stores' => $q->result(),
					'popular_stores' => ''
					);
			$i = 0;
			foreach ($data[0]['stores'] as $key) {
				$data[0]['stores'][$i]->rating = $this->getRating('store_id',$key->id,'store_rating');
				$data[0]['stores'][$i]->is_favorite = $this->getFavorite($user_id,'store_id',$key->id,'store_favorite');
				$data[0]['stores'][$i]->store_open = $this->getBool($key->store_time);
				$i++;
			}
			return $data[0];
		}
		return false;
	}

	function getBool($bool){
		if($bool == 1){
			return true;
		}
		return false;
	}

	function getRating($col,$val,$table){
		$this->db->select('round(Avg(rating),1) as rating, count(id) as count');
		$this->db->from($table);
		$this->db->where($col,$val);
		$q = $this->db->get();
		if ($q->num_rows() > 0 ) {
			if($q->result()[0]->rating == '' || $q->result()[0]->rating == NULL){
				$ratings = "0";
			}else{
				$ratings = $q->result()[0]->rating;
			}
			$rating = array(
				"rating" => $ratings,
				"count" => $q->result()[0]->count,
			);
			return $rating;
		}
		return 0;
	}

	function getFavorite($user_id,$col,$key,$table){
		$this->db->where('user_id',$user_id);
		$this->db->where($col,$key);
		$q = $this->db->get($table);
		if ($q->num_rows() > 0 ) {
			return true;
		}
		return false;
	}


	function setFavoriteStore($store_id,$user_id){
			if($this->getFavorite($user_id,'store_id',$store_id,'store_favorite')){
				$this->db->where('user_id',$user_id);
				$this->db->where('store_id',$store_id);
				if($this->db->delete('store_favorite')){
					return 'Remove Store From Favorite';
				}
				return false;
			}else{
				$data = array(
					'store_id' => $store_id,
					'user_id' => $user_id,
				);
				if($this->db->insert('store_favorite',$data)){
					return 'Added Store To Favorite';
				}
				return false;
			}
		}

	function setFavoriteProduct($product_id,$user_id){
			if($this->getFavorite($user_id,'product_id',$product_id,'product_favorite')){
				$this->db->where('user_id',$user_id);
				$this->db->where('product_id',$product_id);
				if($this->db->delete('product_favorite')){
					return 'Remove Product From Favorite';
				}
				return false;
			}else{
				$data = array(
					'product_id' => $product_id,
					'user_id' => $user_id,
				);
				if($this->db->insert('product_favorite',$data)){
					return 'Added Product To Favorite';
				}
				return false;
			}
		}

	function setStoreRating($store_id,$user_id,$rating){
			$data = array(
					'store_id' => $store_id,
					'user_id' => $user_id,
					'rating' => $rating,
				);	
				if($this->db->insert('store_rating',$data)){
					return true;
				}
				return false;
		}

	function setProductRating($product_id,$user_id,$rating){
			$data = array(
					'product_id' => $product_id,
					'user_id' => $user_id,
					'rating' => $rating,
				);	
				if($this->db->insert('product_rating',$data)){
					return true;
				}
				return false;
		}

	function getStoresProducts($store_id,$user_id){
		$this->db->select('c.id as catid, c.name');
		$this->db->from('product p');
		$this->db->join('category c','p.category_id = c.id','left');
		$this->db->where('p.store_id',$store_id);
		$this->db->group_by('p.id');
		$q = $this->db->get();
		if ($q->num_rows() > 0 ) {
			$k = 0;
			foreach ($q->result() as $cat_key ) {
				$data[$cat_key->catid]['categoryName'] = $cat_key->name;
				$data[$cat_key->catid]['categoryValues'] = $this->getCatrgoryProduct($cat_key->catid,$store_id,$user_id);
				$k++;
			}
			$l = 0;
			foreach ($data as $key ) {
				$makedata['storeProducts'][$l] = $key;
				$l++;
			}
			return $makedata;

		}
		return false;
	}

	function getCatrgoryProduct($cat_id,$store_id,$user_id){
		$this->db->select('*');
		$this->db->from('product');
		$this->db->where('category_id',$cat_id);
		$this->db->where('store_id',$store_id);
		$q = $this->db->get();
		if ($q->num_rows() > 0 ) {
			$i = 0;
			foreach ($q->result() as $row ) {
				$data[$i] = $row;
				$data[$i]->rating = $this->getRating('product_id',$row->id,'product_rating');
				$data[$i]->is_favorite = $this->getFavorite($user_id,'product_id',$row->id,'product_favorite');
			$i++;
			}
			return $data;
		}
		return false;
	}

}



/* End of file Api_model.php */

/* Location: ./application/models/Api_model.php */