<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Channel extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $totSegments = $this->uri->total_segments();
        if ($totSegments == 2) {
            $pageURL = $this->uri->segment($totSegments);
            $this->load->model('cmap_model');
            $this->data['viewType'] = 'list';
            $this->data['channelurl'] = '/channel/' . $pageURL . '/';
            $this->data['page'] = $this->page_model->getOne($pageURL);
            $this->data['deliveryCount'] = count($this->page_model->getPageDelivery($this->data['page']->pageID));
            $this->data['postsCount'] = $this->post_model->getCountPostsByPageId($this->data['page']->pageID);
            $this->data['posts'] = $this->post_model->getPostsByPageId($this->data['page']->pageID, 15)->get()->result();
            if ($this->data['page']->pageTemplate != "") {
                
                $this->load->view('templates/channel/' . $this->data['page']->pageTemplate, $this->data);
            } else {
                $this->error();
            }
        } elseif (is_numeric($this->uri->segment($totSegments))) {
            $pageURL = $this->uri->segment($totSegments - 1);
            $articleURL = $this->uri->segment($totSegments);
            $this->data['viewType'] = 'article';
            $this->data['page'] = $this->page_model->getOne($pageURL);            
            $this->data['post'] = $this->post_model->getPost($articleURL);
            if ($this->data['post']->postID != "") {
                $this->data['postsCount'] = $this->post_model->getCountPostsByPageId($this->data['page']->pageID);
                $this->data['video'] = $this->post_model->getPostVideo($this->data['post']->postID);
                $this->data['tag'] = $this->post_model->getPostTag($this->data['post']->postID);
                $this->load->view('templates/channel/channel', $this->data);
            } else {
                $this->error();
            }
        }
    }

    public function category()
    {
        if (!$this->maintenanceMode) {
            $catSlug = $this->uri->segment(2);
            $this->data['page'] = $this->cmap_page_model->getCategory($catSlug);
            if ($this->data['page']['categoryID'] != "") {
                $this->data['header'] = $this->load->view('templates/header', $this->data, true);
                $this->data['footer'] = $this->load->view('templates/footer', '', true);
                $this->load->view('templates/category', $this->data);
            } else {
                $this->error();
            }
        } else {
            $this->maintenance();
        }
    }

    public function prayer()
    { }

    public function channel()
    {
        if (!$this->maintenanceMode) {
            $totSegments = $this->uri->total_segments();
            if ($totSegments == 2) {
                $pageURL = $this->uri->segment($totSegments);
                $this->load->model('cmap_model');
                $this->data['viewType'] = 'list';
                $this->data['channelurl'] = '/channel/' . $pageURL . '/';
                $this->data['page'] = $this->cmap_page_model->getPage($pageURL);
                $this->data['deliveryCount'] = count($this->cmap_page_model->getPageDelivery($this->data['page']['pageID']));
                $this->data['posts'] = $this->cmap_model->getPostByPageId($this->data['page']['pageID']);
                if ($this->data['page']['pageTemplate'] != "") {
                    $this->data['header'] = $this->load->view('templates/header', $this->data, true);
                    $this->data['footer'] = $this->load->view('templates/footer', '', true);
                    $this->load->view('templates/channel/' . $this->data['page']['pageTemplate'], $this->data);
                } else {
                    $this->error();
                }
            } elseif (is_numeric($this->uri->segment($totSegments))) {
                $pageURL = $this->uri->segment($totSegments - 1);
                $articleURL = $this->uri->segment($totSegments);
                $this->data['viewType'] = 'article';
                $this->data['page'] = $this->cmap_page_model->getPage($pageURL);
                $this->data['post'] = $this->cmap_page_model->getArticle($articleURL);
                if ($this->data['post']['postID'] != "") {
                    $this->data['header'] = $this->load->view('templates/header', $this->data, true);
                    $this->data['footer'] = $this->load->view('templates/footer', '', true);
                    $this->load->view('templates/channel/' . $this->data['page']['pageTemplate'], $this->data);
                } else {
                    $this->error();
                }
            } else {
                $this->error();
            }
        } else {
            $this->maintenance();
        }
    }

    public function article()
    {
        if (!$this->maintenanceMode) {
            $this->load->model('cmap_model');
            $articleURL = $this->uri->segment(2);
            $this->data['page'] = $this->cmap_page_model->getArticle($articleURL);
            if ($this->data['page']['postID'] != "") {
                $this->data['column'] = $this->cmap_page_model->getColumn();
                $this->data['focus'] = $this->cmap_model->getPosts(30, 0, 0, 10000, [$this->data['page']['postID']]);
                $this->data['page']['parentTitle'] = '';
                if ($this->data['page']['parentID'] != 0 && $this->data['page']['parentID'] % 10000 !== 0) {
                    $parentCategory = $this->cmap_model->getCategory($this->data['page']['parentID']);
                    if (count($parentCategory)) {
                        $this->data['page']['parentTitle'] = $parentCategory[0]['categoryTitle'];
                    }
                }
                $this->data['reporter'] = $this->cmap_page_model->getReporterByPostId($this->data['page']['postID']);
                $this->data['ranking'] = $this->cmap_page_model->getRankingByPostId($this->data['page']['postID']);

                $this->data['header'] = $this->load->view('templates/header', $this->data, true);
                $this->data['footer'] = $this->load->view('templates/footer', '', true);
                $this->load->view('templates/article', $this->data);
            } else {
                $this->error();
            }
        } else {
            $this->maintenance();
        }
    }
}
