<?php
// +----------------------------------------------------------------------
// | HkCms 网站前台
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\admin\model\cms\Model;
use app\index\model\cms\Category;
use think\facade\Db;
use think\facade\Validate;

class Index extends BaseController
{
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 内容列表
     * @return string|void
     */
    public function lists()
    {
        // 栏目英文目录
        $catname = $this->request->param('catname','');
        // 栏目ID
        $catId = $this->request->param('catid','');

        $cateInfo = (new Category)->getCateInfo(empty($catId)?$catname:$catId);
        if (empty($cateInfo)) {
            $this->error(lang('Column doesn\'t exist.'));
        }
        $model = Model::where(['id'=>$cateInfo['model_id']])->cache()->find();
        if (empty($model)) {
            $this->error(lang('Model doesn\'t exist.'));
        }

        if ('category'==$cateInfo['type']) {
            // 栏目首页模板
            $template = 'category/'.(!empty($cateInfo['category_tpl']) ? $cateInfo['category_tpl']:'category');
        } else if ('list'==$cateInfo['type']) {
            $template = 'list/'.(!empty($cateInfo['list_tpl']) ? $cateInfo['list_tpl']:'list');
        } else if ('link'==$cateInfo['type'] && $cateInfo['model_id']) {
            $c = '\app\admin\model\cms\\'.$model->controller;
            $info = (new $c)->setTable($model->tablename)->with(['category','model'])->where(['category_id'=>$cateInfo['id']])->find();
            $info = empty($info) ? [] : $info->toArray();
            $this->view->assign('Info', $info);
            if (!empty($info['show_tpl'])) {
                $cateInfo['show_tpl'] = $info['show_tpl'];
            }
            $template = 'page/'.(!empty($cateInfo['show_tpl']) ? $cateInfo['show_tpl']:'page');
        } else {
            $this->error(lang('The page doesn\'t exist.'));
        }

        // seo 标题
        $seo_title = empty($cateInfo['seo_title'])?$cateInfo['title']:$cateInfo['seo_title'];
        $page = $this->request->param("page","","intval");
        $page = $page==0?1:$page;
        $seo_title = str_replace(['$name','$page','$site'], [$seo_title,$page,site("title")], site('category_format'));

        $template = explode(".", $template, 2);
        $this->view->assign('Cate', $cateInfo);
        $this->view->assign('seo_title', $seo_title);
        $this->view->assign('seo_keywords', $cateInfo['seo_keywords']);
        $this->view->assign('seo_desc', $cateInfo['seo_desc']);
        return $this->view->fetch($template[0]);
    }

    /**
     * 详情页
     * @return string|void
     */
    public function show()
    {
        // 栏目ID
        $catId = $this->request->param('catid','', 'intval');
        // 栏目英文
        $list = $this->request->param('catname','');
        // 父级栏目
        $catdir = $this->request->param('catdir','');
        // 文章ID
        $id = $this->request->param('id', 0, 'intval');
        // 模型
        $model = $this->request->param('model', 0);

        $key = $this->request->param('key','');

        if (!is_numeric($catId) && empty($list) && !empty($catdir)) {
            $catId = $catdir;
        } else if (!is_numeric($catId) && !empty($list) && empty($catdir)) {
            $catId = $list;
        } else if (!is_numeric($catId) && !empty($list) && !empty($catdir)) {
            $catId = $list;
        }

        if (empty($model) && !empty($catId)) {
            $cateInfo = (new Category)->getCateInfo($catId);
            if (empty($cateInfo)) {
                $this->error(lang('The page doesn\'t exist.'));
            }
            $model = Model::where(['id'=>$cateInfo['model_id'],'status'=>'normal'])->cache()->find();
            if (empty($model)) {
                $this->error(lang('Model doesn\'t exist.'));
            }
        } else {
            $model = Model::where(['tablename'=>$model,'status'=>'normal'])->cache()->find();
            if (empty($model)) {
                $this->error(lang('Model doesn\'t exist.'));
            }
        }

        $type = Db::name('model_controller')->where(['status'=>'normal','name'=>$model->controller])->cache()->value('type');
        $c = '\app\admin\model\cms\\'.$model->controller;

        if (!empty($key) && md5(app('session')->getId())==$key) { // 授权临时访问禁用的文章
            $info = (new $c)->setTable($model->tablename)->with(['category','model'])->where(['id'=>$id])->append(['publish_time_text','fullurl'])->find();
        } else {
            $info = (new $c)->setTable($model->tablename)->with(['category','model'])->where(['id'=>$id,'status'=>'normal'])->append(['publish_time_text','fullurl'])->find();
        }

        if (empty($info)) {
            $this->error(lang('The document doesn\'t exist.'));
        }
        $info = $info->toArray();

        // 获取扩展字段
        $fields = Db::name('model_field')->where(['status'=>'normal','model_id'=>$info['model_id']])->cache()->select()->toArray();
        // 格式化
        foreach ($fields as $k=>$v) {
            field_format($v, $info);
        }

        if ($type=='more' && $model->allow_single!=1) {
            $obj = Db::name($model->tablename.'_data')->where(['id'=>$info['id']])->find();
            if (isset($obj['content'])) {
                $content = htmlspecialchars_decode($obj['content']);

                // 内容分页
                $page = $this->request->param('page');
                $page = max(1, $page);
                $arr = explode('#page#', $content);
                $obj['content'] = $arr[$page-1] ?? $arr[0];

                $__page__ = \app\common\library\Bootstrap::make([],1,$page,count($arr),false,['path'=>'/index/show','query'=>['page'=>'','id'=>$info['id']]]);
                $this->view->assign('__page__', $__page__);
            }

            if (!empty($obj)) {
                // 格式化
                foreach ($fields as $k=>$v) {
                    field_format($v, $obj);
                }
                $info = array_merge($info, $obj);
                // 兼容旧版
                $info['more'] = $obj;
            } else {
                // 兼容旧版
                $info['more'] = [];
            }
        }

        // 父级栏目矫正
        if (!isset($cateInfo) || $cateInfo['id']!=$info['category_id']) {
            $cateInfo = (new Category)->getCateInfo($info['category_id']);
        }

        Db::name($model->tablename)->where(['id'=>$id])->inc('views')->update();
        $this->view->assign('Cate', $cateInfo);
        $this->view->assign('Info', $info);

        // seo 模型固定的默认字段 keywords description
        $seo_title = empty($info['seotitle'])?$info['title']:$info['seotitle'];
        $seo_title = str_replace(['$title','$name','$site'], [$seo_title,$cateInfo['title'],site("title")], site('content_format'));

        $this->view->assign('seo_title', $seo_title);
        $this->view->assign('seo_keywords', isset($info['keywords'])?$info['keywords']:$cateInfo['seo_keywords']);
        $this->view->assign('seo_desc', isset($info['description'])?$info['description']:$cateInfo['seo_desc']);
        $template = explode(".", $info['show_tpl'], 2);
        return $this->view->fetch('show/'.$template[0]);
    }

    /**
     * 文件下载
     * @return \think\response\File
     */
    public function download()
    {
        // 资源ID
        $id = $this->request->param('id','', 'intval');
        // 文档ID
        $aid = $this->request->param('aid','','intval');
        // 模型ID
        $model = $this->request->param('m', '', 'intval');
        // 字段名称
        $field = $this->request->param('fd', '');

        if (empty($id)) {
            $this->error(lang('Illegal access'));
        }

        // 获取资源ID
        $info = Db::name('attachment')->find($id);
        if (empty($info)) {
            $this->error(lang('Resource does not exist'));
        }

        if (is_numeric($aid) && is_numeric($model) && !empty($field) && Validate::is($field, 'alphaDash')) { // 统计下载
            $key = md5(app('session')->getId()).$field.$aid.$model;
            if (!cache($key)) {
                $this->error(lang('Illegal access'));
            }

            $model_field = Db::name('model_field')->where(['field_name'=>$field,'model_id'=>$model])->find();
            if (empty($model_field)) {
                $this->error(lang('Field does not exist'));
            }

            if ($model_field['iscore']==1) {
                // 主表
                controller($model,function ($obj, $model, $category) use($field,$aid) {
                    $obj->where(['id'=>$aid])->inc($field)->update();
                });
            } else {
                $tablename = Db::name('model')->where(['id'=>$model])->value('tablename');
                Db::name($tablename.'_data')->where(['id'=>$aid])->inc($field)->update();
            }
        }

        $base = public_path();
        $path = str_replace('\\','/', $base.$info['path']);
        $path = str_replace('//','/', $path);
        $path = realpath($path);
        if (!file_exists($path)) {
            $this->error(lang('File does not exist'));
        }

        // 禁止下载脚本文件
        $sExt = explode(',',config('cms.script_ext'));
        $info = pathinfo($path);
        if (strpos($path, $base) !== 0 || in_array($info['extension'], $sExt)) {
            $this->error(lang('File does not exist'));
        } else {
            return download($path,md5(time()));
        }
    }
}
