171 lines
6.1 KiB
PHP
171 lines
6.1 KiB
PHP
<?php
|
|
namespace Admin\Controller;
|
|
use Think\Controller;
|
|
class TemplateController extends AdminController{
|
|
public function __construct(){
|
|
parent::__construct();
|
|
$this->table=D("template");
|
|
}
|
|
public function index(){
|
|
if($this->admin_lang){
|
|
$where["lang"]=$this->admin_lang;
|
|
}
|
|
$where["type"]="index";
|
|
$data=$this->table->where($where)->order('sort asc,template_id desc')->select();
|
|
$this->assign("data",$data);
|
|
$this->display();
|
|
}
|
|
public function listtemp(){
|
|
if($this->admin_lang){
|
|
$where["lang"]=$this->admin_lang;
|
|
}
|
|
$where["type"]="list";
|
|
$data=$this->table->where($where)->order('sort asc,template_id desc')->select();
|
|
$this->assign("data",$data);
|
|
$this->display();
|
|
}
|
|
public function view(){
|
|
if($this->admin_lang){
|
|
$where["lang"]=$this->admin_lang;
|
|
}
|
|
$where["type"]="view";
|
|
$data=$this->table->where($where)->order('sort asc,template_id desc')->select();
|
|
$this->assign("data",$data);
|
|
$this->display();
|
|
}
|
|
public function publictemp(){
|
|
if($this->admin_lang){
|
|
$where["lang"]=$this->admin_lang;
|
|
}
|
|
$where["type"]="public";
|
|
$data=$this->table->where($where)->order('sort asc,template_id desc')->select();
|
|
$this->assign("data",$data);
|
|
$this->display();
|
|
}
|
|
public function add(){
|
|
if(IS_POST){
|
|
$data=$this->table->create();
|
|
if (!$data){//验证
|
|
$this->error($this->table->getError());
|
|
}
|
|
$data[template_id]=$this->table->add();
|
|
D("UserLog")->add("add",$this->table->getTableName(),$data[template_id]);
|
|
$this->table->makeTemplateFile($data[template_id]);
|
|
$this->success($data);
|
|
}else{
|
|
$this->display();
|
|
}
|
|
}
|
|
//设为默认
|
|
public function setDefault(){
|
|
//将之前默认取消掉
|
|
$this->table->where(array("type"=>"index"))->save(array("isdefault"=>0));
|
|
//设新的默认
|
|
$data["template_id"]=I("get.template_id",0,"int");
|
|
D("UserLog")->add("update",$this->table->getTableName(),$data[template_id]);
|
|
$data["isdefault"]=1;
|
|
$this->table->save($data);
|
|
$this->success("操作成功");
|
|
}
|
|
public function editor(){
|
|
if(IS_POST){
|
|
$data=$this->table->create();
|
|
if (!$data){//验证
|
|
$this->error($this->table->getError());
|
|
}
|
|
//记录历吏版本
|
|
$this->setHistory($data[template_id]);
|
|
//
|
|
$this->table->save($data);
|
|
$this->table->makeTemplateFile($data[template_id]);
|
|
D("UserLog")->add("update",$this->table->getTableName(),$data[template_id]);
|
|
$this->success($data);
|
|
|
|
}else{
|
|
$template_id=I("get.template_id",0,"int");
|
|
$r=$this->table->find($template_id);
|
|
$this->assign("r",$r);
|
|
$this->display();
|
|
}
|
|
}
|
|
public function listEditor(){
|
|
$template_id=I("get.template_id",0,"int");
|
|
$r=$this->table->find($template_id);
|
|
$this->assign("r",$r);
|
|
$this->display();
|
|
}
|
|
public function viewEditor(){
|
|
$template_id=I("get.template_id",0,"int");
|
|
$r=$this->table->find($template_id);
|
|
$this->assign("r",$r);
|
|
$this->display();
|
|
}
|
|
public function publicEditor(){
|
|
$template_id=I("get.template_id",0,"int");
|
|
$r=$this->table->find($template_id);
|
|
$this->assign("r",$r);
|
|
$this->display();
|
|
}
|
|
//删除模板
|
|
public function delete(){
|
|
$template_id=I("template_id",0,'intval');
|
|
$template=$this->table->find($template_id);
|
|
if($template["isdefault"]){
|
|
$this->error("默认模板禁止删除!");
|
|
}else{
|
|
$result=$this->table->delete($template_id);
|
|
//删除历吏记录
|
|
M("template_history")->where(array("template_id"=>$template_id))->delete();
|
|
//删除文件
|
|
$this->table->deleteTemplateFile($template_id);
|
|
//
|
|
D("UserLog")->add("delete",$this->table->getTableName(),$template_id);
|
|
$this->success("删除成功");
|
|
}
|
|
}
|
|
//字段排序
|
|
public function sort(){
|
|
$ids=I("template_id");
|
|
$sort=I("sort");
|
|
foreach($ids as $key=>$id){
|
|
$data['template_id']=(int)$id;
|
|
$data['sort']=(int)$sort[$key];
|
|
$this->table->save($data);
|
|
}
|
|
$this->success("排序更改成功");
|
|
}
|
|
//万能标签
|
|
public function cmsinfo(){
|
|
$this->pagetitle="万能标签语法";
|
|
$this->display();
|
|
}
|
|
//记录历吏修改
|
|
private function setHistory($template_id){
|
|
//老的模板内容(未修改前的)
|
|
$old_template=M("template")->where(array("template_id"=>$template_id))->getField("content");
|
|
//获取post过来的新的模板内容
|
|
$new_template=I("post.content");
|
|
//如果没有老的,或者没有新的则不做版本标记
|
|
if(!$old_template||!$new_template){
|
|
return;
|
|
}
|
|
//如果老的模板内容和新的模板内容一样也不做版本标记
|
|
if($old_template==$new_template){
|
|
return;
|
|
}
|
|
//
|
|
$data["template_id"]=$template_id;
|
|
$data["addtime"]=time();
|
|
$data["userid"]=$_SESSION['user_id'];
|
|
$data["username"]=$_SESSION['user_name'];
|
|
$data["content"]=$old_template;
|
|
M("template_history")->add($data);
|
|
//查询备份的模板数据是否大于设置的值,多余的删除
|
|
$maxBake=C("MAX_TEMPLATE_BAKE");
|
|
$count=M("template_history")->where(array("template_id"=>$template_id))->count();
|
|
if($count>$maxBake){
|
|
$min_id=M("template_history")->where(array("template_id"=>$template_id))->min("id");
|
|
M("template_history")->delete($min_id);
|
|
}
|
|
}
|
|
} |