<?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 news\admin\model;
use \temmoku\db;
class module{
	
	public function add($data){
		$id = db::insert(M_jab."module")->cols(['name'=>$data['name']])->query();
		db::query("CREATE TABLE  `".M_jab."content_{$id}` (
					  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
					  `aid` mediumint(9) NULL DEFAULT 0,
					  PRIMARY KEY (`id`),
					  INDEX `aid` (`aid`)
					) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");
	}
	public function alllist(){
		$data=db::select('*')->from(M_jab."module")->orderByDESC(['reorder'])->query();
		return $data;
	}
	public function del($id){
		//把本模型下的字段全部删除
		db::delete(M_jab.'field')->where('mid='.$id)->query();
		//把本模型下的字段数据全部删除
		db::delete(M_jab.'attribute_list')->where('mid='.$id)->query();
		//查询是否存在表
		$result = db::query("SHOW TABLES LIKE '".M_jab."content_$id'");
		if($result){//存在在删除
			db::query("DROP TABLE ".M_jab."content_$id");
		}
		//删除模型分类
		db::delete(M_jab."module")->where("id=$id")->query();
		//改变一下信息模型值,要不然无法修改信息数据,会爆数据库错误
		db::update(M_jab.'article')->cols(['mid'=>0])->where('mid='.$id)->query();
		//改变一下分类绑定的模型
		db::update(M_jab.'category')->cols(['cmid'=>0])->where('cmid='.$id)->query();
		$this->del_html($id);//删除发布模板
	}
	
	public function field_test($mid){
		$field="field_".rand('1000','9999');
		if(db::query("Describe ".M_jab."content_$mid"." $field")){
			$this->field_test($mid);
		}
		return $field;
	}
	
	public function make_html($mid){
		$dir=APP_PATH.M_MODULE."/home/view/template/default/";
		$_device=['pc','wap','wechat'];
		$_file=['articles.tpl','category.tpl'];
		$data=db::select('*')->from(M_jab."field")->where("mid = '$mid'")->orderByDESC(['reorder'])->query();
		foreach($_device AS $device){
			foreach($_file AS $file){
				$_dir=$dir.$device."/".$file;
				if(is_file($_dir)){
					if($file=='articles.tpl'){
						$text=fils_content($_dir);
						$html='	
								<div class="module_more">
									<table class="layui-table my-tabel">
										<tbody>';
						foreach($data AS $key=>$row){
							$html.='
											<tr>
												<td width="100">'.$row['name'].'</td>
												<td>{{$data[\''.$key.'\']}}</td>
											</tr>
							';
						}
						$html.='
										</tbody>
									</table>
								</div>';
						
						$text=str_ireplace('{{$module_more}}',$html,$text);
						$olddir=$dir.$device."/articles_$mid.tpl";
						write_file($olddir,$text);
					}else{
						$olddir=$dir.$device."/category_$mid.tpl";
						copy  ( $_dir  , $olddir );
					}
				}
				
			}
		}
	}
	private function del_html($mid){
		$dir=APP_PATH.M_MODULE."/home/view/template/default/";
		$_device=['pc','wap','wechat'];
		$_file=['articles_'.$mid.'.tpl','category_'.$mid.'.tpl'];
		foreach($_device AS $device){
			foreach($_file AS $file){
				$_dir=$dir.$device."/".$file;
				if(is_file($_dir)){
					unlink ($_dir);
				}
			}
		}
	}
}
?>