<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;
use think\Db;

class ArticleController extends HomeBaseController
{
    /**
     * 文章详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->getTopNav();
        $portalCategoryModel = new PortalCategoryModel();
        $postService         = new PostService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article    = $postService->publishedArticle($articleId, $categoryId);

        if (empty($article)) {
            abort(404, '文章不存在!');
        }

        $tplName = 'article';
        if (empty($categoryId)) {
            $categories = $article['categories'];
            if (count($categories) > 0) {
                $this->assign('category', $categories[0]);
            } else {
                abort(404, '文章未指定分类!');
            }
        } else {
            $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

            if (empty($category)) {
                abort(404, '文章不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }

        Db::name('portal_post')->where('id', $articleId)->setInc('post_hits');
        hook('portal_before_assign_article', $article);
        $this->assign('article', $article);
        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];

        //获取分类
        $id                  = $article['categories'][0]['id'];
        $portalCategoryModel = new PortalCategoryModel();
        $category            = $portalCategoryModel->where(function ($query) use ($id) {
            $query->where('id', $id)->whereOr('parent_id', $id);
        })->where('status', 1)->select();
        $category_self       = [];
        $category_child      = [];
        foreach ($category as $v) {
            if ($v['id'] == $id) {
                $category_self = $v;
            } else {
                $category_child[] = $v;
            }
        }
        $this->assign('category_self', $category_self);
        $this->assign('category_child', $category_child);

        //导航
        $path = explode('-', $category_self['path']);
        array_shift($path);
        $path_list = $portalCategoryModel->where('id', 'in', $path)->select();
        $this->assign('path_list', $path_list);

        return $this->fetch("/" . $tplName);
    }

    // 文章点赞
    public function doLike()
    {
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');


        $canLike = cmf_check_user_action("posts$articleId", 1);

        if ($canLike) {
            Db::name('portal_post')->where('id', $articleId)->setInc('post_like');

            $this->success("赞好啦！");
        } else {
            $this->error("您已赞过啦！");
        }
    }

}
