<?php
/**
 * app/plugins/blog/controllers/blog.php
 *
 * This is the blog controller.
 *
 * @package TP Blog
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers;

use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Classes\Controller;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;
use TheTempusProject\Plugins\Blog as BlogPlugin;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Plugins\Comments;
use TheTempusProject\Models\Comments as CommentsModel;
use TheTempusProject\Models\Posts as PostsModel;

class Blog extends Controller {
    protected static $blog;
    protected static $posts;

    public function __construct() {
        parent::__construct();
        Template::setTemplate( 'blog' );
        self::$posts =  new PostsModel;
    }

    public function index() {
        self::$title = '{SITENAME} Blog';
        self::$pageDescription = 'The {SITENAME} blog is where you can find various posts containing information ranging from current projects and general information to editorial and opinion based content.';
        Views::view( 'blog.list', self::$posts->listPosts() );
    }

    public function rss() {
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );
        self::$title = '{SITENAME} RSS Feed';
        self::$pageDescription = '{SITENAME} blog RSS feed.';
        Template::setTemplate( 'rss' );
        header( 'Content-Type: text/xml' );
        return Views::view( 'blog.rss', self::$posts->listPosts( ['stripHtml' => true] ) );
    }

    public function comments( $sub = null, $data = null ) {
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );

        if ( empty( $sub ) || empty( $data ) ) {
            Issues::add( 'error', 'There was an issue with your request. Please check the url and try again.' );
            return $this->index();
        }

        if ( class_exists( 'TheTempusProject\Plugins\Comments' ) ) {
            $plugin = new Comments;
            if ( ! $plugin->checkEnabled() ) {
                Issues::add( 'error', 'Comments are disabled.' );
                return $this->index();
            }
            $comments = new CommentsModel;
        } else {
            Debug::info( 'error', 'Comments plugin missing.' );
            return $this->index();
        }

        switch ( $sub ) {
            case 'post':
                $content = self::$posts->findById( (int) $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Content.' );
                    return $this->index();
                }
                return $plugin->formPost( self::$posts->tableName, $content, 'blog/post/' );
            case 'edit':
                $content = $comments->findById( $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Comment.' );
                    return $this->index();
                }
                return $plugin->formEdit( self::$posts->tableName, $content, 'blog/post/' );
            case 'delete':
                $content = $comments->findById( $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Comment.' );
                    return $this->index();
                }
                return $plugin->formDelete( self::$posts->tableName, $content, 'blog/post/' );
        }
    }

    public function post( $id = null ) {
        if ( empty( $id ) ) {
            return $this->index();
        }
        $post = self::$posts->findById( $id );
        if ( empty( $post ) ) {
            $post = self::$posts->findBySlug( $id );
            if ( empty( $post ) ) {
                return $this->index();
            }
        }
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );
        self::$title = 'Blog Post';

        if ( Input::exists( 'contentId' ) ) {
            $this->comments( 'post', Input::post( 'contentId' ) );
        }

        Components::set( 'CONTENT_ID', $id );
        Components::set( 'COMMENT_TYPE', self::$posts->tableName );
        Components::set( 'NEWCOMMENT', '' );
        Components::set( 'count', '0' );
        Components::set( 'COMMENTS', '' );

        if ( class_exists( 'TheTempusProject\Plugins\Comments' ) ) {
            $plugin = new Comments;
            if ( $plugin->checkEnabled() ) {
                $comments = new CommentsModel;
                if ( App::$isLoggedIn ) {
                    Components::set( 'NEWCOMMENT', Views::simpleView( 'comments.create' ) );
                } else {
                    Components::set( 'NEWCOMMENT', '' );
                }
                Components::set( 'count', $comments->count( self::$posts->tableName, $post->ID ) );
                Components::set( 'COMMENTS', Views::simpleView( 'comments.list', $comments->display( 10, self::$posts->tableName, $post->ID ) ) );
            }
        }

        self::$title .= ' - ' . $post->title;
        self::$pageDescription = strip_tags( $post->contentSummaryNoLink );
        Views::view( 'blog.post', $post );
    }

    public function author( $data = null ) {
        if ( empty( $data ) ) {
            return $this->index();
        }
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );
        self::$title = 'Posts by author - {SITENAME}';
        self::$pageDescription = '{SITENAME} blog posts easily and conveniently sorted by author.';
        Views::view( 'blog.list', self::$posts->byAuthor( $data ) );
    }

    public function month( $month = null, $year = 0 ) {
        if ( empty( $month ) ) {
            return $this->index();
        }
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );
        self::$title = 'Posts By Month - {SITENAME}';
        self::$pageDescription = '{SITENAME} blog posts easily and conveniently sorted by month.';
        Views::view( 'blog.list', self::$posts->byMonth( $month, $year ) );
    }

    public function year( $year = null ) {
        if ( empty( $year ) ) {
            return $this->index();
        }
        Debug::log( 'Controller initiated: ' . __METHOD__ . '.' );
        self::$title = 'Posts by Year - {SITENAME}';
        self::$pageDescription = '{SITENAME} blog posts easily and conveniently sorted by years.';
        Views::view( 'blog.list', self::$posts->byYear( $year ) );
    }
}
