<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user_modal
 *
 * @author user
 */
class User_modal extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_all_user() {
        $query = $this->db->get('user');
        return $query->result();
    }

    public function get_user($data) {
        $this->db->select('username,type');
        $this->db->from('z_mobile_user');
        $this->db->where($data);
        $query = $this->db->get();
        $user = $query->first_row('array');
        return $user;
    }

    public function insert_user_token($username, $token) {
        $data = array(
            'username' => $username,
            'token' => $token
        );

        $this->db->select();
        $this->db->from('user_token');
        $this->db->where('username', $username);
        $query = $this->db->get();
        $user = $query->first_row('array');
        if ($user != null) {
            $this->db->where('username', $username);
            $this->db->update('user_token', $data);
        } else {
            $this->db->insert('user_token', $data);
        }
    }
    
    public function get_username($token) {
        $this->db->select('username');
        $this->db->from('user_token');
        $this->db->where('token',$token);
        $query = $this->db->get();
        $user = $query->first_row('array');
        return $user['username'];
    }

}
