<?php

class Setgrace extends MY_Controller
{

    private $parentCategoryName = 'grace-category';

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

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

    public function index()
    {
    }

    public function hometop()
    {
        $configType = 'grace';
        $configPage = 'gracehometop';
        $this->load->model('config_model');
        if ($this->input->post('title')) {
            $this->db->where('Page', $configPage)->delete('cm_config');
            foreach ($this->input->post('title') as $k => $title) {
                $this->db->insert($this->config_model->table, array(
                    'PostId' => $this->input->post('postid')[$k],
                    'Type' => $configType,
                    'Page' => $configPage,
                    'OrderNum' => $this->input->post('display_sort')[$k]
                ));
            }
            go($this->input->post('redirect'), 'Success');
        } else {

            $config = $this->config_model->findByTypeAndPage($configType, $configPage)->order_by('OrderNum')->get()->result();
            $data = array($configPage => null, 'posts' => array(null));
            if ($config) {
                $postIds = array();
                foreach($config as $c) {
                    $postIds[] = $c->PostId;
                }
                $postIds = implode(',', $postIds);
                $query = $this->db->query("SELECT
                    `l`.*
                FROM
                    `life` `l`
                WHERE
                    `l`.`idx` IN ($postIds)");

                $post = $query->result();
                foreach($post as $k => $p) {
                    foreach($config as $c) {
                        if($p->idx == $c->PostId) {
                            $post[$k]->display_sort = $c->OrderNum;
                            if(!$c->OrderNum) $post[$k]->display_sort = $k+1;
                        }
                    }
                }
                $data = array($configPage => $config, 'posts' => $post);
            }

            $data['lifeCategories'] = $this->lifeCategories['result'];
            $data['graceEditors'] = $this->editor_model->findByCategoryId($this->lifeCategories['ids'])->get()->result();

            $this->load->view('adm/pageset/grace/hometop', $data);
        }       
    }

    public function homekeyword()
    {
        $configType = 'dailyqt';
        $configPage = 'home-dailyqt';
        $this->load->model('config_model');
        $config = $this->config_model->findByTypeAndPage($configType, $configPage)->order_by('OrderNum')->get()->row();
        $data = array('dailyqt' => null, 'post' => null);
        if($config) {
            $query = $this->db->query("SELECT
                `l`.*
            FROM
                `life` `l`
            WHERE
                `l`.`idx` IN ($config->PostId)");

            $post = $query->row();
            $data = array('dailyqt' => $config, 'post' => $post);
        }

        $data['lifeCategories'] = $this->lifeCategories['result'];
        $data['graceEditors'] = $this->editor_model->findByCategoryId($this->lifeCategories['ids'])->get()->result();
        
        $this->load->view('adm/pageset/grace/homekeyword', $data);
    }
}
