<?php
namespace app\index\controller;
use think\Loader;
use think\Cache;
use think\Config;
use think\Controller;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://115cms.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
// 插件目录名称
define('ADDON_DIR_NAME', 'app');
// 插件根目录路径
define('ADDON_PATH', ROOT_PATH . DS.ADDON_DIR_NAME . DS);
class Addons extends Controller{
    
	public function muban($controller=null,$method = null,$json=false){
    	$class_path = "\\".ADDON_DIR_NAME."\\template\\".$controller."\\".$controller."";
    	require_once "".ADDON_DIR_NAME."/template/".$controller."/".$controller.".php";
    	$controller = new $class_path();
        if (!file_exists('app/template/'.$controller)) {
            return $this->error('没有此模板','/');
        } 
    	if($json){
        	return json($controller->$method());
        }else{
            $controller->$method();
        }
    }
    //用于显示模板的方法
    protected function guoji_view($templateFile = ''){
        $view_path = ADDON_DIR_NAME.'/template/'.$this->getName().'/';
        $this->assign('static_path', getbaseurl().ADDON_DIR_NAME.'/' . $this->getName().'/static');
        $this->view->engine(['view_path' => $view_path]);
    		$templateFile = $view_path.$templateFile.'.'.Config::get('template')['view_suffix'];
    		if(!is_file($templateFile)){
    			throw new \Exception("模板不存在:$templateFile");
    		}
    	echo $this->fetch($templateFile);
    }
    
    //用于显示插件模板的方法
    protected function view($templateFile = ''){
        $view_path = ADDON_DIR_NAME.'/'.$this->getName().'/view/';
        $this->assign('static_path', getbaseurl().ADDON_DIR_NAME.'/' . $this->getName().'/static');
        $this->view->engine(['view_path' => $view_path]);
    		$templateFile = $view_path.$templateFile.'.'.Config::get('template')['view_suffix'];
    		if(!is_file($templateFile)){
    			throw new \Exception("模板不存在:$templateFile");
    		}
    	echo $this->fetch($templateFile);
    }
    public function getName(){
        $class = get_class($this);
        return substr($class,strrpos($class, '\\')+1);
    }
     /**
     * 获取插件所需的钩子是否存在，没有则新增
     * @param string $str  钩子名称
     * @param string $addons  插件名称
     * @param string $addons  插件简介
     */
    //  $this->Hook('hello_1', $this->info['name']);
    public function Hook($str, $addons){
        $where['name'] = $str;
        $gethook = Db::name('hook')->where($where)->find();
        if(!$gethook || empty($gethook) || !is_array($gethook)){
            //没有新增
            $data['name'] = $str;
            $data['time'] = time();
            $data['addons'] = $addons;
            if( false !== Db::name('hook')->insert($data) ){
            }
        } else {
            // 有更新
            $pieces = explode(',', $gethook['addons']);
            if (in_array($addons,$pieces)) {
                //有数据
                $arr=ltrim($gethook['addons'], ",");
                // $result = array_unique($gethook['addons']);//处理去重
                // $arr=implode(',',$result);//转换字符串
            } else {
                // 没有数据
                $arr=$gethook['addons'].','.$addons;
                $arr=ltrim($arr, ",");
            }
            Db::name('hook')->where($where)->setField('addons', $arr);
        }
    }
}
