<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Rss extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $action = $this->input->get('action');

        if ($action == 'generate') {
            $this->build();
        } else {

            if (empty($this->session->logged)) {
                $this->session->set_flashdata('show_login_form', true);
                redirect('engine/error?http_code=403');
            } else {

                $parent_category = intval($this->input->get('parent_category'));

                if ($parent_category != '') {

                    $parent_category_data         = $this->db->select('id, name')->where('id', $parent_category)->get('categories')->row();
                    $data['parent_category_name'] = $parent_category_data->name;
                    $data['parent_category_id']   = $parent_category_data->id;

                    $query = ' SELECT categories.id, categories.name AS cat , (SELECT torrents.publish_date FROM torrents WHERE category = categories.id AND torrents.state = 0 ORDER BY publish_date DESC limit 1) AS publish_date FROM categories WHERE categories.parent = ' . $parent_category . ';';

                    $data['categories'] = $this->db->query($query)->result();

                    $this->load->view('rss/home_sub_category', $data);

                } else {
                    if ($this->session->settings['enable_at_content'] == false) {
                        $query = ' SELECT categories.id, categories.name AS cat , (SELECT torrents.publish_date FROM torrents WHERE parent_category = categories.id AND torrents.state = 0 ORDER BY publish_date DESC limit 1) AS publish_date FROM categories WHERE categories.parent = 0 AND categories.id != 2188 ORDER BY categories.name ASC;';
                    }  else {
                        $query = ' SELECT categories.id, categories.name AS cat , (SELECT torrents.publish_date FROM torrents WHERE parent_category = categories.id AND torrents.state = 0 ORDER BY publish_date DESC limit 1) AS publish_date FROM categories WHERE categories.parent = 0 ORDER BY categories.name ASC;';
                    }
                    $data['parent_categories'] = $this->db->query($query)->result();

                    $this->load->view('rss/home_rss', $data);

                }
            }
        }
    }

    public function build()
    {
        $type    = $this->input->get('type');
        $id      = intval($this->input->get('id'));
        $passkey = $this->input->get('passkey');

        $this->load->model(array('feed_model' => 'feed'));

        $data['passkey'] = '';

        if (strlen($passkey) == 32) {
            $data['passkey'] = $passkey;
        }

        if ($type == 'cat') {
            $search_url       = site_url('engine/search?name=&description=&file=&uploader=&category={target}&sub_category=all&do=search');
            $data['torrents'] = $this->feed->get_from_cat('parent_category', $id);
        } elseif ($type == 'subcat') {
            $search_url       = site_url('engine/search?name=&description=&file=&uploader=&category=&sub_category={target}&do=search');
            $data['torrents'] = $this->feed->get_from_cat('category', $id);
        }

        if (count($data['torrents']) > 0) {
            $this->load->helper('xml');
        }

        header('Content-Type: application/xml');

        $data['search_url'] = $search_url;
        $this->load->view('rss/template', $data);
    }

    /*public function download()
    {
        $torrent_id = intval($this->input->get('id'));
        $passkey    = $this->input->get('passkey');

        if (isset($torrent_id) && strlen($passkey) == 32) {
            $torrent             = $this->db->select('publish_date, file_key')->where('id', $torrent_id)->get('torrents')->row();
            $announce_url        = 'http://'.$this->config->item('first_tracker_host').':8080/' . $passkey . '/announce';
            $date_folder_torrent = date('d-m-y', $torrent->publish_date);
            $this->load->library('TorrentParser', array($date_folder_torrent . '/' . $torrent->file_key));
            $builder = $this->torrentparser->parser;
            $builder->announce(false);
            $builder->announce($announce_url);
            $builder->is_private(true);
            $builder->send();
        }

    }*/

}
