User_model.php 8.42 KB
Newer Older
Phạm Văn Đoan committed
1 2
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

3
class User_model extends CI_Model
Phạm Văn Đoan committed
4
{
5
    protected $_table = 'users';
Phạm Văn Đoan committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($dataInsert)
    {
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    public function update($id, $dataUpdate)
    {
        $this->db->where('id', $id);
        $this->db->update($this->_table, $dataUpdate);
    }

    public function getById($id)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

35
    public function getByUsername($username, $account_type, $id = '')
Phạm Văn Đoan committed
36 37 38 39
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.username', $username);
40
        $this->db->where($this->_table . '.user_type', $account_type);
41
        if ($id || $id != '') {
42 43 44 45 46 47 48
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

49 50 51 52 53 54 55 56 57 58 59 60 61 62
    public function getByFullName($full_name, $account_type, $id = '')
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.full_name', $full_name);
        $this->db->where($this->_table . '.user_type', $account_type);
        if ($id || $id != '') {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

63 64 65
    public function getByEmail($email, $account_type, $id = '')
    {  
        // MyHelper::echoPreDie($email);
66 67 68 69
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.email', $email);
        $this->db->where($this->_table . '.user_type', $account_type);
70
        if ($id || $id != '') {
71 72
            $this->db->where_not_in('id', $id);
        }
Phạm Văn Đoan committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Kiểm tra mã dự thưởng xem đã có chưa
     * @param $reward_code
     * @return mixed
     */
    public function getByRewardCode($reward_code)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.reward_code', $reward_code);
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListForSelectBox()
    {
        $this->db->select('id, fullname, username, sid');
        $this->db->from($this->_table);
        $this->db->where('sid !=', '');
        $this->db->order_by($this->_table . '.username', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * @param null $name
     * @return int
     */
    public function cmsCountAll($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
Phạm Văn Đoan committed
113
            $this->db->like('username', $name, 'both');
Phạm Văn Đoan committed
114
            $this->db->or_like('email', $name, 'both');
Phạm Văn Đoan committed
115
            $this->db->or_like('full_name', $name, 'both');
116
            $this->db->or_where('id', intval($name));
Phạm Văn Đoan committed
117 118 119 120 121 122 123 124 125 126 127 128
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination($record, $start, $name = null)
    {
129
        
Phạm Văn Đoan committed
130 131 132
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        if ($name != null) {
Phạm Văn Đoan committed
133
            $this->db->like('username', $name, 'both');
Phạm Văn Đoan committed
134
            $this->db->or_like('email', $name, 'both');
Phạm Văn Đoan committed
135
            $this->db->or_like('full_name', $name, 'both');
136
            $this->db->or_where('id', intval($name));
Phạm Văn Đoan committed
137
        }
Phạm Văn Đoan committed
138
        $this->db->order_by($this->_table . '.username', 'asc');
Phạm Văn Đoan committed
139 140
        $this->db->limit($record, $start);
        $query = $this->db->get();
Phạm Văn Đoan committed
141

Phạm Văn Đoan committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * Thống kê danh sách top: sắp xếp theo thứ tự ưu tiên thời gian hoàn thành, điểm
     * @param null $name
     * @return int
     */
    public function cmsCountAll2($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table . ' u');
        $this->db->join('game g', 'g.user_id = u.id');
        if ($name != null) {
            $this->db->like('u.fullname', $name, 'both');
            $this->db->or_like('u.username', $name, 'both');
            $this->db->or_like('u.address', $name, 'both');
            $this->db->or_like('u.email', $name, 'both');
            $this->db->or_like('u.mobile', $name, 'both');
            $this->db->or_like('u.reward_code', $name, 'both');
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination2($record, $start, $name = null)
    {
        $this->db->select('u.fullname, u.username, u.reward_code, g.total_time, g.total_point, g.total_game');
        $this->db->from($this->_table . ' u');
        $this->db->join('game g', 'g.user_id = u.id');
        if ($name != null) {
            $this->db->like('u.fullname', $name, 'both');
            $this->db->or_like('u.username', $name, 'both');
            $this->db->or_like('u.address', $name, 'both');
            $this->db->or_like('u.email', $name, 'both');
            $this->db->or_like('u.mobile', $name, 'both');
            $this->db->or_like('u.reward_code', $name, 'both');
        }
        $this->db->order_by('g.total_point desc, g.total_time asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * Xuất mã dự thưởng
     * @param null
     * @return int
     */
    public function exportCountAll()
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function exportGetPagination($record, $start)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->order_by($this->_table . '.created_at', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    public function getIdFullname()
    {
        $this->db->select('id, full_name');
        $this->db->from($this->_table);
        $query = $this->db->get();
        $data = $query->result_array();
        $result = [];
        if ($data) {
            foreach ($data as $val) {
                $result[$val['id']] = $val['full_name'];
            }
        }
        return $result;
    }

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    function getUser($id = null, $block = true) {
        /*if ($id) {
            $blockIds = ($block) ? $this->blockIds() : '0';
            $query = $this->db->query("SELECT * FROM users WHERE (id=? OR email=? OR username=?) AND id NOT IN ($blockIds) AND active=?", $id,$id,$id, 1);
            if ($query) $user = $query->result_array();
            return $user;
        } else {
            return  $this->authUser;
        }*/

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

    }

Phạm Văn Đoan committed
254 255 256 257 258

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */