<?php

require __DIR__ . '/Hoosk_page_model.php';

class Cmap_page_model extends Hoosk_page_model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getPageDelivery($pageID)
    {
        $this->db->join('cm_page_attributes', 'cm_page_attributes.pageID=cm_delivery.pageID');
        return $this->db->where('cm_page_attributes.pageID', $pageID)->get('cm_delivery')->result();
    }

    public function getPostByParentCategoryId($parentID, $limit = 10, $offset = 0)
    {
        $rows = $this->db
            ->select('p.*,c.categorySlug')
            ->join('cm_category c', 'p.categoryID=c.categoryID', 'left')
            ->where('c.parentID', $parentID)
            // ->where('p.published', 1)
            ->limit($limit)->offset($offset)
            ->order_by('p.datePosted', 'desc')
            ->get('cm_post p')->result();
        return $rows;
    }

    public function getPagePostByParentCategoryId($parentID, $limit = 10, $offset = 0, $popular = '')
    {
        if (!empty($popular)) {
            $this->db->join('cm_post_ranking pr', "p.postID=pr.postID and pr.title='$popular'", 'left')->order_by('pr.rating');
        } else {
            $this->db->order_by('p.dateUpdated', 'desc');
        }
        return $this->db
            ->select('p.*, pc.*, pa.pageURL, pc.pageTitle, pa.pageIcon')
            ->join('cm_category c', 'c.categoryID=p.categoryID or p.categoryID=0', 'left')
            ->join('cm_page_attributes pa', 'c.categoryID=pa.categoryID and p.pageID=pa.pageID', 'left')
            ->join('cm_page_content pc', 'pa.pageID=pc.pageID', 'left')
            ->where("pa.pagePublished", 1)
            ->where('p.published', 1)
            ->group_by('p.pageID')
            ->limit($limit)->offset($offset)
            ->get('cm_post p')->result();
    }

    public function getReporterByPostId($postId)
    {
        return $this->db->where('postID', $postId)->select('r.*, p.press_name, p.logo_img, p.homepage')
            ->join('cm_press p', 'r.pressID=p.pressID', 'left')
            ->get('cm_post_reporter r')->row();
    }

    public function getRankingByPostId($postId)
    {
        $ranking = ['V' => 0, 'R' => 0, 'L' => 0];
        $rows = $this->db->where('postID', $postId)
            ->get('cm_post_ranking')->result();
        foreach ($rows as $row) {
            foreach ($ranking as $k => $r) {
                if ($row->title == $k) {
                    $ranking[$k] = $row->rating;
                }
            }
        }
        return $ranking;
    }

    public function getPagePostByCategoryId($categoryID)
    {
        $posts = [];
        $recentUpdatedPosts = $this->db->where('categoryID', $categoryID)->get('cm_page_attributes')->result();
        foreach ($recentUpdatedPosts as $p) {
            array_push($posts, $this->db
                ->select('p.*, pc.*, pa.pageURL, pc.pageTitle, pa.pageIcon')
                ->join('cm_page_attributes pa', 'p.pageID=pa.pageID', 'left')
                ->join('cm_page_content pc', 'pa.pageID=pc.pageID', 'left')
                ->where("pa.pagePublished", 1)
                ->where('p.pageID', $p->pageID)->where('p.published', 1)->limit(4)->order_by('p.dateUpdated', 'desc')
                ->get('cm_post p')->result());
        }
        return $posts;
    }

    public function getColumn($limit = 2, $offset = 0)
    {
        return $this->db->limit($limit)->join('cm_user u', 'u.userID = p.userID', 'left')
            ->join('cm_user_group g', 'u.groupID = g.groupID', 'left')->where('g.groupID', 7)
            ->order_by('datePosted', 'desc')->get('cm_post p')->result();
    }

    public function getToonSeries($seriesURL)
    {
        return $this->db->select("cm_post.*, cm_user.name")
            ->join(
                'cm_post_category',
                'cm_post.postID = cm_post_category.postID',
                'inner'
            )
            ->where('cm_post_category.categoryID', $seriesURL)
            ->get('cm_post')->result();
    }

    public function getPostRanking($title = 'V', $categoryID = 0, $exception = [], $limit = 8, $series = 0, $parentID = 0, $isPostCategoryRequired = false)
    {
        $this->db->select("cm_post.*, cm_user.name, cm_page_content.pageTitle, 
        UNIX_TIMESTAMP(datePosted) postedTimestamp, cm_post_ranking.rating, cm_category.categorySlug")
            ->join(
                'cm_post_category',
                'cm_post.postID = cm_post_category.postID',
                'left'
            )
            ->join(
                'cm_post_ranking',
                'cm_post.postID = cm_post_ranking.postID',
                'left'
            )
            ->join(
                'cm_user',
                'cm_post.userID = cm_user.userID',
                'left'
            )
            ->join(
                'cm_page_content',
                'cm_post.pageID = cm_page_content.pageID',
                'left'
            )
            ->group_by('cm_post.postID')
            ->order_by('cm_post.datePosted', 'desc')
            ->order_by('cm_post_ranking.rating', 'desc')
            ->limit($limit);
        if (count($exception)) {
            $this->db->where_not_in('cm_post.postID', $exception);
        }
        if (!is_null($title)) {
            $this->db->where('cm_post_ranking.title', $title);
        }
        if ($categoryID) {
            $this->db->where('cm_category.categoryID', $categoryID);
        }
        if ($parentID) {
            $this->db->where('cm_category.parentID', $parentID);
        }
        if ($isPostCategoryRequired) {
            $this->db->join(
                'cm_category',
                'cm_post_category.categoryID = cm_category.categoryID',
                'inner'
            );
        } else {
            $this->db->join(
                'cm_category',
                'cm_post.categoryID = cm_category.categoryID',
                'left'
            );
        }
        return $this->db->get('cm_post')->result();
    }

    public function getCategoryByParentId($parentID)
    {
        $rows = $this->db->where_in('cm_category.parentID', $parentID)->get('cm_category')->result();
        return array_column($rows, 'categoryID');
    }

    public function getPostRecommend($categoryID = 0, $exception = [], $limit = 8)
    {
        if ($categoryID > 0) {
            $categories = $this->getCategoryByParentId($categoryID);
            $this->db->where_in('cm_category.categoryID', $categories);
        }
        if (count($exception)) {
            $this->db->where_not_in('cm_post.postID', $exception);
        }
        return $this->db->select("cm_post.*, cm_user.name, count(cm_post_recommend.postID) recomCnt, cm_page_content.img")
            ->join(
                'cm_category',
                'cm_post.categoryID = cm_category.categoryID',
                'inner'
            )
            ->join(
                'cm_post_recommend',
                'cm_post.postID = cm_post_recommend.postID',
                'left'
            )
            ->join(
                'cm_user',
                'cm_post.userID = cm_user.userID',
                'left'
            )
            ->join(
                'cm_page_content',
                'cm_post.pageID = cm_page_content.pageID',
                'left'
            )
            ->group_by('cm_post.postID')
            ->order_by('cm_post.datePosted', 'desc')
            ->limit($limit)->get('cm_post')->result();
    }

    public function getPostRecent($categoryID = 0, $exception = [], $limit = 8, $isUniqueAuthor = true)
    {
        if ($categoryID > 0) {
            $categories = $this->getCategoryByParentId($categoryID);
            $this->db->where_in('cm_post_category.categoryID', $categories);
        }
        if (count($exception)) {
            $this->db->where_not_in('cm_post.postID', $exception);
        }
        if ($isUniqueAuthor) {
            $this->db->group_by('cm_user.userID');
        } else {
            $this->db->group_by('cm_post.postID');
        }
        return $this->db->select("cm_post.*, cm_user.name, cm_page_content.pageTitle")
            ->join(
                'cm_post_category',
                'cm_post.postID = cm_post_category.postID',
                'inner'
            )
            ->join(
                'cm_user',
                'cm_post.userID = cm_user.userID',
                'left'
            )
            ->join(
                'cm_page_content',
                'cm_post.pageID = cm_page_content.pageID',
                'left'
            )
            ->order_by('cm_post.datePosted', 'desc')
            ->where('cm_post.published', 1)
            ->limit($limit)->get('cm_post')->result();
    }


    function getArticle($postURL)
    {
        // Get article
        $this->db->select("*");
        $this->db->where("postURL", $postURL);
        $this->db->where("published", 1);

        $query = getPostActiveQuery()->where('p.postURL', $postURL)->get();
        if ($query->num_rows() > 0) {
            $results = $query->result_array();
            foreach ($results as $u) {
                $category = array(
                    'pageID'                => $u['pageID'],
                    'postID'                => $u['postID'],
                    'postTitle'             => $u['postTitle'],
                    'pageKeywords'             => '',
                    'pageDescription'         => $u['postExcerpt'],
                    'postContent'             => empty($u['postContentHTML']) ? $this->getJsonToPostContent($u['postContent'], $u['categoryID']) : $u['postContentHTML'],
                    'datePosted'             => $u['datePosted'],
                    'categoryTitle'         => $u['categoryTitle'],
                    'categorySlug'             => $u['categorySlug'],
                    'categoryID'             => $u['categoryID'],
                    'parentID'                 => $u['parentID'],
                    'postExcerpt'                 => $u['postExcerpt'],
                );
            }
            return $category;
        }
        return array('postID' => "");
    }
}
