<?php


class Users_model extends CI_Model {

        public function create_account($data) {
	       $this->db->insert('users',$data);
        }

        public function check_nickname($nickname)  {
        	return $this->db->where('nickname',$nickname)->get('users')->num_rows() == 0;
        }

        public function check_email($email) {
        	return $this->db->where('email',$email)->get('users')->num_rows() == 0;
        }

        public function get_torrents($limit,$offset) {
            return $this->db->select('torrents.id , torrents.state, torrents.age , torrents.comments_count , torrents.publish_date , torrents.name , torrents.category_slug , torrents.size , torrents.seed , torrents.leech , torrents.completed , users.nickname AS nickname , categories.id AS category_id , categories.name AS category')
                    ->from('torrents')
                    ->join('users','torrents.uploader = users.id')
                    ->where('uploader',$this->session->id)
                    ->join('categories','torrents.category = categories.id')
                    ->order_by('id','desc')
                    ->limit($limit,$offset)
                    ->get()
                    ->result();
        }

        public function get_spe_userdata($where_value, $user_data, $where = 'id') 
        {
            return $this->db->select($user_data)->where($where, $where_value)->get('users')->row();
        } 

        public function get_user_comments($userId, $limit = 15, $offset = 0)
	    {
	        return $this->db->select('comments.*, torrents.name AS torrent_name, torrents.id AS torrent_id')
	            ->from('comments')
	            ->where('comments.publisher', $userId)
	            ->order_by('comments.id', 'desc')
                ->join('torrents','torrents.id = comments.guid')
	            ->limit($limit, $offset)
	            ->get()->result();
	    }

        public function get_profile_torrents($user_id,$limit,$offset) {
            return $this->db->select('torrents.id , torrents.state , torrents.age , torrents.comments_count , torrents.publish_date , torrents.name , torrents.category_slug , torrents.size , torrents.completed, torrents.seed ,torrents.leech , users.nickname AS nickname , categories.id AS category_id , categories.name AS category')
                    ->from('torrents')
                    ->join('users','torrents.uploader = users.id')
                    ->where('torrents.state', 0)
                    ->where('uploader',$user_id)
                    ->join('categories','torrents.category = categories.id')
                    ->order_by('id','desc')
                    ->limit($limit,$offset)
                    ->get()
                    ->result();
        }

        public function get_full_profile_torrents($user_id,$limit,$offset) {
            return $this->db->select('torrents.id , torrents.correction_request, torrents.state , torrents.age , torrents.comments_count , torrents.publish_date , torrents.name , torrents.category_slug , torrents.size , torrents.completed, torrents.seed ,torrents.leech , users.nickname AS nickname , categories.id AS category_id , categories.name AS category')
                    ->from('torrents')
                    ->join('users','torrents.uploader = users.id')
                    ->where('uploader',$user_id)
                    ->join('categories','torrents.category = categories.id')
                    ->order_by('id','desc')
                    ->limit($limit,$offset)
                    ->get()
                    ->result();
        }


        public function is_user_exists($id,$pass) {
                return $this->db->select('pass')->where('nickname',$id)->get('users')->row()->pass == md5($pass);
        }

        public function get_userdata($value,$column='id') {
                return $this->db->where($column,$value)->get('users')->row();
        } 

        public function add_comment($guid,$comment,$date) {
                $this->db->insert('comments',array('ip' => $_SERVER['REMOTE_ADDR'], 'guid' => $guid , 'publish_date' => $date , 'publisher' => $this->session->id , 'comment' => $comment));
                return $this->db->insert_id();
        }
        
        public function get_usertracker_data() {
			return $this->db->select('torrent_pass,end_freeleech,download_multiplier,downloaded,uploaded,can_leech,view_disabled_announce')->where('user_id',$this->session->id)->get('xbt_users')->row();
        }

        public function update_favourites($user_id,$favourites) {
            $this->db->where('id',$user_id)->set('favourites',$favourites)->update('users');
        }


}
