<?php

class Korea extends MY_Controller
{

    private $parentCategoryName = 'grace-category';

    private $lifeCategories = array('ids' => array());

    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('pagination'));
        $this->load->library('Post', '', 'post');
        $this->load->model('category_model');
        $this->load->model('editor_model');
        $this->load->model('life_model');
        $this->lifeCategories['result'] = $this->category_model->findById()->result();
        foreach ($this->lifeCategories['result'] as $c) {
            array_push($this->lifeCategories['ids'], $c->idx);
        }
    }

    public function index()
    {
        $table = $this->table();
        // print_r($table['rows']);die;
        $page = (((int) $this->input->get('page')) > 0) ? ((int) $this->input->get('page')) : 1;

        $config['base_url'] = '/adm/' . strtolower(__CLASS__);
        $config['total_rows'] = $table['total'];
        $config['per_page'] = $this->param('limit');
        $this->pagination->initialize($config);
        $data = array(
            'lifeCategories' => $this->lifeCategories['result'],
            'graceEditors' => $this->editor_model->findByCategoryId($this->lifeCategories['ids'])->get()->result(),
            'paging' => $this->pagination->create_links(),
            'table' => $table,
            'page' => $page
        );

        $this->load->view('adm/template', $data);
    }

    private function tableClause()
    {

    }

    public function table()
    {
        $sort = $this->param('sort');
        $sortColumns = array('comments' => 'comment_count', 'reg_date' => 'reg_date');

        $this->life_model->findByCategoryId($this->lifeCategories['ids'])
            ->order_by('l.' . $sortColumns['reg_date'], $this->param('order'))
            ->where('l.is_display !=', null)
            ->select('l.display_date, l.is_display')
            ->limit($this->param('limit'))
            ->offset($this->param('limit') * $this->param('page'));

        if ($this->param('term')) {
            $this->db->like('l.title', $this->param('term'));
        }
        if ($this->param('category')) {
            $this->db->like('l.category_idx', $this->param('category'));
        }
        if ($this->param('editor')) {
            $this->db->like('p.idx', $this->param('editor'));
        }

        $graces = $this->db->get()->result();

        foreach ($graces as $k => $g) {
            if (strtotime($g->display_date) > time() && $g->is_display == 'N') {
                $graces[$k]->isSetDptime = 1;
            }
        }

        $graceTotalCount = $this->life_model->findByCategoryId($this->lifeCategories['ids']);

        if ($this->param('term')) {
            $this->db->like('l.title', $this->param('term'));
        }
        if ($this->param('category')) {
            $this->db->like('l.category_idx', $this->param('category'));
        }
        if ($this->param('editor')) {
            $this->db->like('p.idx', $this->param('editor'));
        }

        $graceTotalCount = $this->db->count_all_results();
        $categories = array();
        foreach ($graces as $grace) {
            array_push($categories, $grace->categoryId);
        }
        if (count($categories)) {
            $categories = $this->category_model->findById(implode(',', $categories))->result();
            foreach ($graces as $k => $g) {
                foreach ($categories as $c) {
                    if ($g->categoryId == $c->idx) {
                        $graces[$k]->categoryName = $c->category_name;
                    }
                }
            }
        }

        return array('total' => $graceTotalCount, 'rows' => $graces);
    }

    public function write() {
        $this->load->view('adm/write');

    }

}
