<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Track_model extends CI_Model
{
    protected $_table = 'tracks';

    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();
    }

    /**
     * Lay cuoc thi hien hanh
     * @return mixed
     */
    public function getCurrentContest()
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where('is_active', 1);
        $this->db->order_by('created_at', 'desc');
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function cmsCountAll($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('title', $name, 'both');
            $this->db->or_like('description', $name, 'both');
            $this->db->or_like('slug', $name, 'both');
            $this->db->or_like('tag', $name, 'both');
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination($record, $start, $name = null)
    {
        $this->db->select($this->_table . '.*, u1.full_name AS singer_name, u2.full_name AS composer_name, track_user.name AS singer_name2, track_artists.name AS composer_name2');
        $this->db->from($this->_table);
        $this->db->join('track_user', 'track_user.track_id = tracks.id', 'left');
        $this->db->join('track_artists', 'track_artists.track_id = tracks.id', 'left');
        $this->db->join('users u1', 'u1.id = track_user.user_id', 'left');
        $this->db->join('users u2', 'u2.id = track_artists.user_id', 'left');
        if ($name != null) {
            $this->db->like('tracks.title', $name, 'both');
            $this->db->or_like('tracks.description', $name, 'both');
            $this->db->or_like('tracks.slug', $name, 'both');
            $this->db->or_like('tracks.tag', $name, 'both');
        }
        $this->db->order_by($this->_table . '.title', 'asc');
        $this->db->limit($record, $start);
        $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();
    }

}

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