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

class Post
{
    private $db;
    private $CI;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->library('session');
        $this->db = &$this->CI->db;
    }

    public function getPost($postId)
    {
        $query = $this->db->query("SELECT
            `p`.`idx` `editorId`,
            `p`.`partner_img` `editorImg`,
            `p`.`main_img` `editorMainImg`,
            `p`.`partner_name` `editorName`,
            `p`.`partner_intro` `editorIntro`,
            `l`.`title`,
            `l`.`head_title`,
            `l`.`sub_title`,
            `l`.`content`,
            `l`.`category_idx` `categoryId`,
            `l`.`idx`,
            `l`.`main_img` `mainImg`,
            `l`.`hit_count` `views`,
            `l`.`like_count` `likes`,
            `l`.`comment_count` `comments`,
            `l`.*
        FROM
            `life` `l`
                LEFT JOIN
            `partner` `p` ON `l`.`partner_idx` = `p`.`idx`
        WHERE
            `l`.`idx` IN ($postId)
            AND `l`.`is_display` = 'Y'
            AND `l`.`is_del` = 'N'");

        return $this->urify($query->result())[0];
    }

    public function getPostJoin1($postId)
    {
        $query = $this->db->query("SELECT
            `p`.`idx` `editorId`,
            `p`.`partner_img` `editorImg`,
            `p`.`main_img` `editorMainImg`,
            `p`.`partner_name` `editorName`,
            `p`.`partner_intro` `editorIntro`,
            `l`.`title`,
            `l`.`head_title`,
            `l`.`sub_title`,
            `l`.`content`,
            `l`.`category_idx` `categoryId`,
            `l`.`idx`,
            `l`.`main_img` `mainImg`,
            `l`.`hit_count` `views`,
            `l`.`like_count` `likes`,
            `l`.`comment_count` `comments`,
            `l`.*
        FROM
            `life` `l`
                LEFT JOIN
            `partner` `p` ON `l`.`partner_idx` = `p`.`idx`
            left JOIN
            cm_config cf on l.idx=cf.PostId
        WHERE
            `l`.`idx` IN ($postId)
            AND `l`.`is_display` = 'Y'
            AND `l`.`is_del` = 'N' order by cf.OrderNum ");

        return $this->urify($query->result());
    }

    public function getEditorPosts($editorId, $limit = 16, $orderBy = 'update_date', $sort = 'desc', $ignores = array())
    {
        if (is_array($editorId)) {
            $editorId = implode(",", $editorId);
        }
        $ignoreId = '';
        if (is_array($ignores) && count($ignores)) {
            $ignoreId = " and l.idx not in (" . implode(",", $ignores) . ")";
        }

        $query = $this->db->query("SELECT
            `p`.`idx` `editorId`,
            `p`.`partner_img` `editorImg`,
            `p`.`main_img` `editorMainImg`,
            `p`.`partner_name` `editorName`,
            `p`.`partner_intro` `editorIntro`,
            `l`.`title`,
            `l`.`head_title`,
            `l`.`sub_title`,
            `l`.`content`,
            `l`.`category_idx` `categoryId`,
            `l`.`idx`,
            `l`.`main_img` `mainImg`,
            `l`.`hit_count` `views`,
            `l`.`like_count` `likes`,
            `l`.`comment_count` `comments`,
            `l`.*
        FROM
            `life` `l`
                LEFT JOIN
            `partner` `p` ON `l`.`partner_idx` = `p`.`idx`
        WHERE
            `l`.`partner_idx` IN ($editorId)
                AND `l`.`is_display` = 'Y'
                AND `l`.`is_del` = 'N'
                $ignoreId
        ORDER BY `l`.`$orderBy` $sort
        LIMIT $limit");
        $posts = $query->result();
        return $this->urify($posts);
    }

    public function getPostsByCategoryId($categoryId, $limit = 16, $orderBy = 'reg_date', $sort = 'desc', $ignores = array())
    {
        if (is_array($categoryId)) {
            $categoryId = implode(",", $categoryId);
        }
        $ignoreId = '';
        if (is_array($ignores) && count($ignores)) {
            $ignoreId = " and l.idx not in (" . implode(",", $ignores) . ")";
        }
        $query = $this->db->query("SELECT
            `p`.`idx` `editorId`,
            `p`.`partner_img` `editorImg`,
            `p`.`main_img` `editorMainImg`,
            `p`.`partner_name` `editorName`,
            `p`.`partner_intro` `editorIntro`,
            `l`.`title`,
            `l`.`head_title`,
            `l`.`sub_title`,
            `l`.`content`,
            `l`.`category_idx` `categoryId`,
            `l`.`idx`,
            `l`.`main_img` `mainImg`,
            `l`.`hit_count` `views`,
            `l`.`like_count` `likes`,
            `l`.`comment_count` `comments`,
            `l`.`update_date`,
            `l`.`reg_date`
        FROM
            `life` `l`
                LEFT JOIN
            `partner` `p` ON `l`.`partner_idx` = `p`.`idx`
        WHERE
            `l`.`category_idx` IN ($categoryId)
                AND `l`.`is_display` = 'Y'
                AND `l`.`is_del` = 'N' $ignoreId
        ORDER BY `l`.`$orderBy` DESC
        LIMIT $limit");

        $posts = $query->result();
        return $this->urify($posts);
    }

    public function getPostsEditorRecent($categoryId, $limit = 16, $orderBy = 'reg_date', $sort = 'desc', $ignores = array())
    {
        if (is_array($categoryId)) {
            $categoryId = implode(",", $categoryId);
        }
        $ignoreId = '';
        if (is_array($ignores) && count($ignores)) {
            $ignoreId = " and l.idx not in (" . implode(",", $ignores) . ")";
        }
        $query = $this->db->query("SELECT
            `p`.`idx` `editorId`,
            `p`.`partner_img` `editorImg`,
            `p`.`main_img` `editorMainImg`,
            `p`.`partner_name` `editorName`,
            `p`.`partner_intro` `editorIntro`,
            `l`.`title`,
            `l`.`head_title`,
            `l`.`sub_title`,
            `l`.`content`,
            `l`.`category_idx` `categoryId`,
            `l`.`idx`,
            `l`.`main_img` `mainImg`,
            `l`.`hit_count` `views`,
            `l`.`like_count` `likes`,
            `l`.`comment_count` `comments`,
            `l`.`update_date`,
            `l`.`reg_date`
        FROM
            `life` `l`
                LEFT JOIN
            `partner` `p` ON `l`.`partner_idx` = `p`.`idx`
        WHERE
            `l`.`category_idx` IN ($categoryId)
                AND `l`.`is_display` = 'Y'
                AND `l`.`is_del` = 'N'
                $ignoreId group by `l`.`partner_idx`
        ORDER BY `l`.`$orderBy` DESC
        LIMIT $limit");

        $posts = $query->result();
        return $this->urify($posts);
    }

    public function urify($posts)
    {
        foreach ($posts as $k => $p) {
            $posts[$k]->title = convertEmoji($posts[$k]->title, 'DECODE');
            $posts[$k]->titleUrl = cleanUrl($p->title);
            $posts[$k]->titleUrl = $posts[$k]->titleUrl ? $posts[$k]->titleUrl : cleanUrl($p->editorName);
            $posts[$k]->editorNameUrl = cleanUrl($p->editorName);
        }
        return $posts;
    }

    public function getTop10($categoryId)
    {
        $filePrefix = 'cmtv_top10_';
        $filename = $filePrefix . date('dH-', time() - 3600) . (is_array($categoryId) ? 'all' : $categoryId) . '.json';
        $cacheFilePath = $this->CI->config->item('log_path') . $filename;
        if (file_exists($cacheFilePath)) {
            return json_decode(file_get_contents($cacheFilePath));
        } else {
            $mask = $this->CI->config->item('log_path') . $filePrefix . '*' . (is_array($categoryId) ? 'all' : $categoryId) . '.json';
            array_map('unlink', glob($mask));
            $cmtvTop10 = $this->getPostsByCategoryId($categoryId, 10, 'hit_count', 'desc');
            file_put_contents($cacheFilePath, json_encode($cmtvTop10));
            return $cmtvTop10;
        }
    }

    public function upload($f)
    {
        $config['upload_path'] = FCPATH. 'uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->CI->load->library('upload', $config);
        if (!$this->CI->upload->do_upload($f)) {
            return false;
        } else {
            return $this->CI->upload->data();
        }
    }

}
