<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use \temmoku\admin;
use \temmoku\db;
use admin\public_class\cache;
use \temmoku\lib\pages;
class hook extends admin
{
	public function index(){
		
		if($_POST['step']=='post'){
			$array=['name'=>$_POST['name'],'content'=>$_POST['content']];
			if($_POST['id']){
				db::update(jab.'hook')->cols($array)->where('id='.$_POST['id'])->query();
			}else{
				$row=db::select('*')->from(jab.'hook')->where("name= '$_POST[name]' ")->row();
				if($row){
					$this->err('已经存在方法名，无需重复添加');
				}
				db::insert(jab.'hook')->cols($array)->query();
			}
			$this->success('创建/修改完成');
		}elseif($_GET['step']=='del'){
			db::delete(jab.'hook')->where('id='.$_GET['id'])->query();
			$this->success('删除完成');
		}
		
		!isset($_GET['page']) && $_GET['page']=1;
		$rows='20';
		$data=db::calcFoundRows()->select('*')->from(jab."hook")->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		$getlistNUM=db::row(' SELECT FOUND_ROWS() AS allNUM');
		foreach($data AS $row){
			$_rows=db::select('count(*) AS num')->from(jab.'hook_list')->where("hook_name like '$row[name]' ")->row();
			$row['num']=$_rows['num'];
			$dataDB[]=$row;
		}
		$Get_Page=(new pages)->Pages('','',"/admin/hook/index/",$rows,$_GET['page'],$getlistNUM['allNUM']);
		$this->assign('Get_Page', $Get_Page);
		$this->assign('data', $dataDB);
		$this->display(tpl('hook/index'));
	}
	
	public function hook_list(){
	
		if($_POST['step']=='one'){
			$_rows=db::select('*')->from(jab.'hook_list')->where("id= '$_POST[id]' ")->row();
			exit(json_encode(['code'=>0,'data'=>$_rows]));
		}elseif($_POST['step']=='post'){
			$array=[
				'reorder'=>intval($_POST['reorder']),
				'content'=>$_POST['content'],
				'class'=>$_POST['class'],
				'method'=>$_POST['method'],
				'hook_name'=>$_POST['hook_name'],
				'state'=>intval($_POST['state']),
			];
			if($_POST['id']){
				db::update(jab.'hook_list')->cols($array)->where('id='.$_POST['id'])->query();
			}else{
				db::insert(jab.'hook_list')->cols($array)->query();
			}
			cache::hook();
			$this->success('创建/修改完成');
		}elseif($_GET['step']=='del'){
			//获取要删除的数据
			$_rows=db::select('*')->from(jab.'hook_list')->where("id= '$_GET[id]' ")->row();
			//搜索是否包含多个。防止有多个方法,把文件删除掉了
			$class=str_replace('\\','\\\\\\\\',$_rows['class']);
			$count=db::select('count(*) AS num')->from(jab.'hook_list')->where("`class` LIKE '$class' ")->row();
			//如果小于2 就把钩子删除掉
			if($count['num'] ==1){
				$obj=$_rows['class'];//兼容传参
				$obj = new $obj;
				//执行一次卸载
				if ((int)method_exists($obj, 'uninit')) {
					$obj->uninit();
				}
				$class=str_replace('\\','/',$_rows['class']);
				if(strpos($class, '::')==true){
					$_file=explode('::',$class,2);
					$_file=trim($_file[0],'/');
				}else{
					$_file=trim($class,'/');
				}
				$flie=APP_PATH.$_file.".php";
				unlink($flie);
			}
			db::delete(jab.'hook_list')->where("id= '$_GET[id]' ")->query();
			cache::hook();
			$this->success('删除完成');
		}
		
		$method= $_GET['method'] ? str_replace("+++","_",$_GET['method']) : '';
		
		if($method){
			$where="hook_name like '$method'";
		}else{
			$where="1";
		}
		
		$rows='20';
		$data=db::calcFoundRows()->select('*')->from(jab."hook_list")->where($where)->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		$getlistNUM=db::row(' SELECT FOUND_ROWS() AS allNUM');
		foreach($data AS $row){
			$_rows=db::select('name,content')->from(jab.'hook')->where("name like '$row[hook_name]' ")->row();
			$row['hook']=$_rows;
			$dataDB[]=$row;
		}
		
		$Get_Page=(new pages)->Pages('','',"/admin/hook/index/",$rows,$_GET['page'],$getlistNUM['allNUM']);
		$this->assign('Get_Page', $Get_Page);
		$this->assign('data', $dataDB);
		
		$hook=db::select('*')->from(jab."hook")->query();
		$this->assign('hook', $hook);
		
		$this->display(tpl('hook/hook_list'));
	}
	
	public function hook_state(){
		$state =intval($_POST[state]);
		db::update(jab."hook_list")->cols(['state'=>$state])->where('id='.$_POST['id'])->query();
		$this->success('状态更改成功');
	}
	
}
?>