<?php
\n
\nclass Torrents_model extends CI_Model
\n{
\n
\n    public function __construct()
\n    {
\n        $this->data_listing_context = 'torrents.id , torrents.comments_count , torrents.publish_date , torrents.name ,';
\n        $this->data_listing_context .= 'torrents.size , torrents.seed , torrents.leech , torrents.category ,';
\n        $this->data_listing_context .= 'categoriesp.name AS parent_category_name , categoriesc.name AS sub_category_name';
\n    }
\n
\n    public function get_parent_categories($order = 'order')
\n    {
\n        return $this->db->where('parent', 0)->order_by($order, 'asc')->get('categories')->result();
\n    }
\n
\n    public function get_parent_categories_noadult($order = 'order')
\n    {
\n        return $this->db->where('parent', 0)->order_by($order, 'asc')->where('id !=', 2188)->get('categories')->result();
\n    }
\n    
\n    public function get_categorie_data($id)
\n    {
\n        return $this->db->where('id', $id)->get('categories')->row();
\n    }
\n
\n    public function get_sub_categorie_data($parent_id, $order = 'name')
\n    {
\n        return $this->db->where('parent', $parent_id)->order_by($order, 'asc')->get('categories')->result();
\n    }
\n
\n    public function get_parent_categorie_data($id)
\n    {
\n        return $this->db->select('c2.*')->from('categories as c1')->where('c1.id', $id)->join('categories as c2', 'c1.parent = c2.id')->get()->row();
\n    }
\n
\n    public function get_parent_categoriesid()
\n    {
\n        return $this->db->select('id , name')->from('categories')->where('parent', 0)->order_by('order', 'asc')->get()->result();
\n    }
\n
\n    public function get_parent_sub_categoriesid()
\n    {
\n        return $this->db->select('id , name , parent')->from('categories')->where('parent !=', 0)->order_by('order', 'asc')->get()->result();
\n    }
\n
\n   public function get_exclu_torrents($limit,$offset) {
\n        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, categories.id AS category_id, categories.name AS category')
\n                ->from('torrents')
\n                ->where('is_exclusivity', 1)
\n                ->join('categories','torrents.category = categories.id')
\n                ->order_by('id','desc')
\n                ->limit($limit,$offset)
\n                ->get()
\n                ->result();
\n    }
\n
\n
\n    private function get_prototype_listing($category, $sort, $order, $limit, $offset)
\n    {
\n        $this->db->select($this->data_listing_context)
\n            ->from('torrents')
\n            ->where('torrents.category', (int) $category)
\n            ->where('torrents.state', 0)
\n            ->join('categories AS categoriesp', 'torrents.parent_category = categoriesp.id')
\n            ->join('categories AS categoriesc', 'torrents.category = categoriesc.id')
\n            ->order_by($sort, $order)
\n            ->limit($limit, $offset);
\n
\n        return $this->db;
\n    }
\n
\n    public function get_period_torrents($type, $category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
\n    {
\n        $query = $this->get_prototype_listing($category, $sort, $order, $limit, $offset);
\n
\n        if ($type == 'today_torrents') {
\n            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)';
\n        } elseif ($type == 'week_torrents') {
\n            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)';
\n        } else {
\n            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 30 DAY)';
\n        }
\n
\n        return $query->where($where)->get()->result();
\n    }
\n
\n    public function updateCategoriesStats($parent_cat_id, $sub_cat_id)
\n    {
\n        // mis à jour nombre torrents dans la catégorie parente
\n        $this->db->set('torrents', 'torrents + 1', false);
\n        $this->db->where('id', $parent_cat_id)->update('categories');
\n        // mis à jour nombre torrents dans la catégorie fille
\n        $this->db->set('torrents', 'torrents + 1', false);
\n        $this->db->where('id', $sub_cat_id)->update('categories');
\n        return true;
\n    }
\n
\n    public function generate_category_slug($id)
\n    {
\n        $this->db->select('c1.name AS sub_category_name , c1.id AS sub_category_id , c2.name AS parent_category_name , c2.id AS parent_category_id');
\n        $this->db->from('categories AS c1');
\n        $this->db->where('c1.id', $id);
\n        $this->db->join('categories AS c2', 'c1.parent = c2.id');
\n        $result = $this->db->get()->row();
\n        // mis à jour statististiques
\n        $this->updateCategoriesStats($result->sub_category_id, $result->parent_category_id);
\n        return url_title($result->parent_category_name, '-', true) . '/' . url_title($result->sub_category_name, '-', true);
\n    }
\n
\n    public function torrents_popular($categorie, $limit = 2)
\n    {
\n        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
\n            ->from('track_views as t1')
\n            ->where('t1.date > UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY)')
\n            ->group_by('t1.guid', 'desc')
\n            ->order_by('count(guid)', 'desc')
\n            ->where('torrents.parent_category', $categorie)
\n            ->where('torrents.state', 0)
\n            ->join('torrents', 't1.guid = torrents.id')
\n            ->limit($limit)
\n            ->get()
\n            ->result();
\n
\n    }
\n
\n    public function last_torrents($categorie)
\n    {
\n        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
\n            ->from('torrents')
\n            ->order_by('torrents.id', 'desc')
\n            ->where('torrents.parent_category', $categorie)
\n            ->where('torrents.state', 0)
\n            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
\n            ->limit(30)
\n            ->get()
\n            ->result();
\n    }
\n
\n    public function week_torrents($categorie)
\n    {
\n        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
\n            ->from('torrents')
\n            ->order_by('torrents.id', 'desc')
\n            ->where('torrents.parent_category', $categorie)
\n            ->where('torrents.state', 0)
\n            ->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)')
\n            ->limit(30)
\n            ->get()
\n            ->result();
\n    }
\n
\n    public function getMostSeeded()
\n    {
\n        return $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->join('users', 'torrents.uploader = users.id')->where('torrents.seed >', '0')->order_by('torrents.seed', 'desc')->where('torrents.state', 0)->limit(100)->get()->result_array();
\n    }
\n
\n    public function getMostCompleted()
\n    {
\n        return $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->where('torrents.parent_category !=','2188')->join('users', 'torrents.uploader = users.id')->where('torrents.seed >', '0')->where('torrents.comments_count >', '20')->order_by('torrents.completed', 'desc')->where('torrents.state', 0)->limit(100)->get()->result_array();
\n    }
\n
\n    public function get_torrent_from_period($type, $out = '')
\n    {
\n        if ($type == 'day') {
\n            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)');
\n            //$this->db->where('torrents.seed >', '1');
\n            //$this->db->where('torrents.completed >', '5');
\n        } else if ($type == 'week') {
\n            $this->db->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)');
\n            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 8 DAY)');
\n            $this->db->where('torrents.seed >', 20);
\n            //$this->db->where('torrents.completed >', 30);
\n        } else if ($type == 'month') {
\n            $this->db->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 8 DAY)');
\n            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 38 DAY)');
\n            $this->db->where('torrents.seed >', 25);
\n            //$this->db->where('torrents.completed >', 150);
\n        }
\n        $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->join('users', 'torrents.uploader = users.id')->order_by('torrents.id', 'desc')->where('torrents.state', 0);
\n        return $out == 'object' ? $this->db->get()->result() : $this->db->get()->result_array();
\n    }
\n
\n    public function get_torrent($id)
\n    {
\n        return $this->db->select('torrents.* , users.nickname AS nickname, users.settings AS user_settings')->from('torrents')->join('users', 'torrents.uploader = users.id')->where('torrents.id', $id)->get()->row();
\n    }
\n
\n    public function get_torrent_comments($guid, $limit = 15, $offset = 0)
\n    {
\n        return $this->db->select('comments.* , users.nickname AS publisher , users.last_activity_date AS publisher_activity , users.uploaded AS up , users.downloaded AS down , users.id AS publisher_id  , users.rank AS publisher_rank , users.avatar AS avatar')
\n            ->from('comments')
\n            ->where('comments.guid', $guid)
\n            ->order_by('comments.id', 'desc')
\n            ->join('users', 'comments.publisher = users.id')
\n            ->limit($limit, $offset)
\n            ->get()->result();
\n    }
\n
\n    public function get_focused_comment($id, $torrent)
\n    {
\n        return $this->db->select('comments.* , users.nickname AS publisher , users.last_activity_date AS publisher_activity , users.uploaded AS up , users.downloaded AS down , users.id AS publisher_id  , users.rank AS publisher_rank , users.avatar AS avatar')
\n            ->from('comments')
\n            ->where('comments.id', $id)
\n            ->where('comments.guid', $torrent)
\n            ->order_by('comments.id', 'desc')
\n            ->join('users', 'comments.publisher = users.id')
\n            ->get()->row();
\n    }
\n
\n    public function get_from_ids($torrents_id)
\n    {
\n        $sql = 'SELECT `torrents`.*, `users`.`nickname` AS `nickname`, `categories`.`id` AS `category_id`, `categories`.`name` AS `category`';
\n        $sql .= 'FROM `torrents` JOIN `users` ON `torrents`.`uploader` = `users`.`id` JOIN `categories` ON `torrents`.`category` = `categories`.`id`';
\n        $sql .= "WHERE `torrents`.`id` IN ('$torrents_id') AND `torrents`.`state` = 0 ORDER BY FIELD (torrents.id,'$torrents_id') DESC";
\n        return $this->db->query($sql)->result();
\n    }
\n
\n    public function get_today_torrents($category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
\n    {
\n        return $this->db->select($this->torrent_data)
\n            ->from('torrents')
\n            ->join('users', 'torrents.uploader = users.id')
\n            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
\n            ->where('torrents.parent_category', $category)
\n            ->where('torrents.state', 0)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->order_by($sort, $order)
\n            ->limit($limit, $offset)
\n            ->get()
\n            ->result();
\n    }
\n
\n    public function count_today_torrents($category)
\n    {
\n        return $this->db->select('torrents.id')
\n            ->from('torrents')
\n            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
\n            ->where('torrents.parent_category', $category)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->get()
\n            ->num_rows();
\n    }
\n
\n    public function get_yesterday_torrents($category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
\n    {
\n        return $this->db->select($this->torrent_data)
\n            ->from('torrents')
\n            ->join('users', 'torrents.uploader = users.id')
\n            ->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
\n            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 2 DAY)')
\n            ->where('torrents.state', 0)
\n            ->where('torrents.parent_category', $category)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->order_by($sort, $order)
\n            ->get()
\n            ->result();
\n    }
\n
\n    public function count_yesterday_torrents($category)
\n    {
\n        return $this->db->select('torrents.id')
\n            ->from('torrents')
\n            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)')
\n            ->where('torrents.parent_category', $category)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->get()
\n            ->num_rows();
\n    }
\n
\n    public function get_popular_torrents($category, $sort = 'seed', $order = 'desc', $limit = 12, $offset = 0)
\n    {
\n        return $this->db->select($this->torrent_data)
\n            ->from('torrents')
\n            ->join('users', 'torrents.uploader = users.id')
\n            ->where('torrents.parent_category', (int) $category)
\n            ->where('torrents.state', 0)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->order_by($sort, $order)
\n            ->limit($limit, $offset)
\n            ->get()
\n            ->result();
\n    }
\n
\n    public function count_popular_torrents($category)
\n    {
\n        return $this->db->select('torrents.id')
\n            ->from('torrents')
\n            ->where('torrents.parent_category', (int) $category)
\n            ->join('categories', 'torrents.category = categories.id')
\n            ->get()
\n            ->num_rows();
\n    }
\n
\n    public function get_category_icon($name)
\n    {
\n        return $this->db->where('name', $name)->get('categories')->row()->icon;
\n    }
\n
\n    public function get_torrents_all_categories($sort_type)
\n    {
\n
\n        //$this->output->enable_profiler();
\n        $categories = $this->get_categories();
\n        $torrents   = array();
\n        if ($sort_type == 'popular') {
\n            foreach ($categories as $category) {
\n                $torrents[$category->name]                       = $this->get_popular_torrents($category->id);
\n                $torrents[$category->name]['parent_category_id'] = $category->id;
\n            }
\n        }
\n        if ($sort_type == 'today') {
\n            foreach ($categories as $category) {
\n                $torrents[$category->name]                       = $this->get_today_torrents($category->id);
\n                $torrents[$category->name]['parent_category_id'] = $category->id;
\n            }
\n        }
\n        if ($sort_type == 'yesterday') {
\n            foreach ($categories as $category) {
\n                $torrents[$category->name]                       = $this->get_yesterday_torrents($category->id);
\n                $torrents[$category->name]['parent_category_id'] = $category->id;
\n            }
\n        }
\n        return $torrents;
\n    }
\n
\n    public function get_categories()
\n    {
\n        return $this->db->where('parent', '0')->order_by('order', 'asc')->get('categories')->result();
\n    }
\n}
\n