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

class CommentService
{
    private $db;
    private $CI;
    private $commentModel;
    private $likeLogModel;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->library('session');
        $this->CI->load->model('comment_model');
        $this->CI->load->model('category_model');
        $this->CI->load->model('editor_model');
        $this->lifeModel = &$this->CI->life_model;
        $this->categoryModel = &$this->CI->category_model;
        $this->editorModel = &$this->CI->editor_model;
        $this->db = &$this->CI->db;
    }

    public function getPostById($postId)
    {
        return $this->lifeModel->findById($postId)
        // ->where('l.is_display', 1)
        ->where('l.is_del', 'N')
            ->get()->row();
    }

    public function getPostsByEditorId($editorId, $limit = 16, $orderBy = 'update_date', $sort = 'desc', $ignores = array())
    {
        $this->lifeModel->findByEditorId($editorId)->order_by('l.' . $orderBy, $sort)
        // ->where('l.is_display', 1)
        ->where('l.is_del', 'N')
            ->limit($limit);
        if (is_array($ignores) && count($ignores)) {
            $this->db->where_not_in('l.idx', $ignores);
        }
        $posts = $this->db->get()->result();
        return $this->urify($posts);
    }

    public function getPostsByCategoryId($categoryId, $limit = 16, $orderBy = 'update_date', $sort = 'desc', $ignores = array())
    {
        $this->lifeModel->findByCategoryId($categoryId)
        // ->where('l.is_display', 1)
        ->where('l.is_del', 'N')
            ->order_by('l.' . $orderBy, $sort)->limit($limit);
        if (is_array($ignores) && count($ignores)) {
            $this->db->where_not_in('l.idx', $ignores);
        }
        $posts = $this->db->get()->result();
        return $this->urify($posts);
    }

    private function urify(&$posts)
    {
        foreach ($posts as $k => $p) {
            $posts[$k]->titleUrl = cleanUrl($p->title);
            $posts[$k]->editorNameUrl = cleanUrl($p->editorName);
        }
        return $posts;
    }
}
