295 lines
11 KiB
PHP
295 lines
11 KiB
PHP
<?php
|
||
namespace Admin\Controller;
|
||
use Think\Controller;
|
||
class CatController extends AdminController{
|
||
public function __construct(){
|
||
parent::__construct();
|
||
$this->table=D("Cat");
|
||
$this->modelid=I("modelid",0,"int");
|
||
$this->model=M("model")->find($this->modelid);
|
||
$this->modelList=M("model")->where(array("status"=>1))->order("sort asc,modelid desc")->select();
|
||
$this->catModelform=APP_PATH.'Data/modelTemp/'.$this->modelid."_cat.php";
|
||
//列表模板
|
||
$where[type]="list";
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$this->listtemp=M("template")->where($where)->order("sort asc,template_id desc")->select();
|
||
//内容模板
|
||
$where[type]="view";
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$this->viewtemp=M("template")->where($where)->order("sort asc,template_id desc")->select();
|
||
//排序选项
|
||
$this->orderInc=array(
|
||
"sort asc,newstime desc"=>"默认排序",
|
||
"newstime desc"=>"按发布时间降序排序",
|
||
"id desc"=>"按ID降序排序",
|
||
"views desc"=>"按点击率降序排序",
|
||
);
|
||
//角色列表
|
||
$this->roleInc=M("role")->order('id desc')->select();
|
||
//会员组列表
|
||
$this->memberGroupInc=M("member_group")->order("group_id asc")->select();
|
||
|
||
}
|
||
//栏目分类
|
||
public function getCat($pid){
|
||
$catstr=$this->forCat($pid);
|
||
return $catstr;
|
||
}
|
||
protected function forCat($pid=0,$str=""){
|
||
$where["pid"]=$pid?$pid:0;
|
||
$where["status"]=1;
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$cat=M("cat")->where($where)->order("sort asc")->select();
|
||
if(count($cat)>0){
|
||
foreach($cat as $v){
|
||
if(!check_user_cat("info_view",$v[catid])){
|
||
continue;
|
||
}
|
||
$nbsp_str="";
|
||
for($i=1;$i<$v[level];$i++){
|
||
$nbsp_str.=" ";
|
||
}
|
||
if($this->catid&&$this->catid==$v[catid]){
|
||
$this_selected="selected";
|
||
}else{
|
||
$this_selected="";
|
||
}
|
||
$str.="<option value='{$v[catid]}' ".$this_selected.">{$nbsp_str}|-{$v[name]}</option>";
|
||
$str=$this->forCat($v[catid],$str);
|
||
}
|
||
return $str;
|
||
}else{
|
||
return $str;
|
||
}
|
||
}
|
||
//返回的是一个数组
|
||
public function forCat2($pid=0,$str=array()){
|
||
$where["pid"]=$pid;
|
||
if($this->modelid){
|
||
$where["modelid"]=$this->modelid;
|
||
}
|
||
$where["status"]=1;
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$cat=M("cat")->field("catid,level,name,modelid,status,sort,pid")->where($where)->order("sort asc,catid desc")->select();
|
||
if(count($cat)>0){
|
||
foreach($cat as $v){
|
||
if(!check_user_cat("info_view",$v[catid])){
|
||
continue;
|
||
}
|
||
$nbsp_str="";
|
||
for($i=1;$i<$v[level];$i++){
|
||
$nbsp_str.=" ";
|
||
}
|
||
if($this->catid&&$this->catid==$v[catid]){
|
||
$this_selected="selected";
|
||
}else{
|
||
$this_selected="";
|
||
}
|
||
$str1=array();
|
||
$str1=$v;
|
||
$str1["str"]=$nbsp_str."|-";
|
||
$str[]=$str1;
|
||
$str=$this->forCat2($v[catid],$str);
|
||
}
|
||
return $str;
|
||
}else{
|
||
return $str;
|
||
}
|
||
}
|
||
//返回的是一个数组,显示所有栏目
|
||
public function forCat3($pid=0,$str=array()){
|
||
$where["pid"]=$pid;
|
||
if($this->modelid){
|
||
$where["modelid"]=$this->modelid;
|
||
}
|
||
$where["status"]=1;
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$cat=M("cat")->field("catid,level,name,modelid,status,sort,pid")->where($where)->order("sort asc,catid desc")->select();
|
||
if(count($cat)>0){
|
||
foreach($cat as $v){
|
||
if(!check_user_cat("info_view",$v[catid])){
|
||
continue;
|
||
}
|
||
$nbsp_str="";
|
||
for($i=1;$i<$v[level];$i++){
|
||
$nbsp_str.=" ";
|
||
}
|
||
if($this->catid&&$this->catid==$v[catid]){
|
||
$this_selected="selected";
|
||
}else{
|
||
$this_selected="";
|
||
}
|
||
$str1=array();
|
||
$str1=$v;
|
||
$str1["str"]=$nbsp_str."|-";
|
||
$str[]=$str1;
|
||
$str=$this->forCat3($v[catid],$str);
|
||
}
|
||
return $str;
|
||
}else{
|
||
return $str;
|
||
}
|
||
}
|
||
public function index(){
|
||
$pid=I("get.pid");
|
||
$p=$this->table->find($pid);
|
||
$ptitle=$p[name]?$p[name]:$this->model[name];
|
||
$where["pid"]=$pid;
|
||
$where["modelid"]=$this->modelid;
|
||
if($this->admin_lang){
|
||
$where["lang"]=$this->admin_lang;
|
||
}
|
||
$data=$this->table->where($where)->order('sort asc')->select();
|
||
$this->assign("pid",$pid);
|
||
$this->assign("ppid",$p[pid]);
|
||
$this->assign("ptitle",$ptitle);
|
||
$this->assign("data",$data);
|
||
$this->display();
|
||
}
|
||
public function validate_model($modelid){
|
||
$modelinfo=M("model")->find($modelid);
|
||
if(!$modelinfo){
|
||
return false;
|
||
}else{
|
||
return true;
|
||
}
|
||
}
|
||
//添加
|
||
public function add(){
|
||
if(IS_POST){
|
||
$data=$this->table->relation(true)->create();
|
||
if(!$this->validate_model($data[modelid])){
|
||
$this->error("请选择栏目模型!");
|
||
}
|
||
if (!$data){//验证
|
||
$this->error($this->table->relation(true)->getError());
|
||
}
|
||
//数据添加
|
||
$result=$this->table->data_add();
|
||
if(!$result){
|
||
$this->error($this->table->getError());
|
||
}
|
||
D("UserLog")->add("add",$this->table->getTableName(),$result[catid]);
|
||
//更新缓存
|
||
$this->table->updateCache();
|
||
$this->success($data);
|
||
}else{
|
||
$pid=I("get.pid");
|
||
$this->parentinfo=$this->table->find($pid);
|
||
//获取最大的排序
|
||
$sort=$this->table->where(array("pid"=>$pid))->max('sort');
|
||
$sort=$sort+10;
|
||
$this->pagetitle="添加栏目";
|
||
$this->catid=$pid;
|
||
$this->cat=$this->getCat();
|
||
//默认路径
|
||
if($this->parentinfo["classpath"]){
|
||
$this->defaultPath=$this->parentinfo["classpath"]."path_".rand(1000,9999)."/";
|
||
}else{
|
||
$this->defaultPath="/path_".rand(1000,9999)."/";
|
||
}
|
||
|
||
//
|
||
$this->pubid=make_pubid();
|
||
$this->assign("sort",$sort);
|
||
$this->assign("pid",$pid);
|
||
$this->display();
|
||
}
|
||
}
|
||
public function editor(){
|
||
if(IS_POST){
|
||
$data=$this->table->create();
|
||
if(!$this->validate_model($data[modelid])){
|
||
$this->error("请选择栏目模型!");
|
||
}
|
||
if (!$data){//验证
|
||
$this->error($this->table->getError());
|
||
}
|
||
$result=$this->table->data_editor();
|
||
if(!$result){
|
||
$this->error($this->table->getError());
|
||
}
|
||
D("UserLog")->add("update",$this->table->getTableName(),$result[catid]);
|
||
//更新缓存
|
||
$this->table->updateCache();
|
||
$this->success($data);
|
||
}else{
|
||
//获取最大的排序
|
||
$catid=I("get.catid");
|
||
$r=$this->table->relation(true)->find($catid);
|
||
//副表数据合并
|
||
if($r["cat_data"]){
|
||
$newdata=array_merge($r,$r["cat_data"]);
|
||
unset($newdata["cat_data"]);
|
||
$r=$newdata;
|
||
}
|
||
$this->catid=$r[pid];
|
||
$this->cat=$this->getCat();
|
||
$this->pagetitle="修改栏目";
|
||
$this->assign("r",$r);
|
||
$this->display();
|
||
}
|
||
}
|
||
//删除
|
||
public function delete(){
|
||
$catid=I("catid",0,'intval');
|
||
$count=$this->table->where(array("pid"=>$catid))->count();
|
||
if($count>0){
|
||
$this->error("该节点下有子节点,如要删除请先删除子节点");
|
||
}else{
|
||
//找出父栏目catid
|
||
$r=$this->table->find($catid);
|
||
$result=$this->table->relation(true)->delete($catid);
|
||
D("UserLog")->add("delete",$this->table->getTableName(),$catid);
|
||
//更新所有父级的son_catids字段
|
||
$arrid_str_arr=explode("|",$r[parent_catids]);
|
||
foreach($arrid_str_arr as $catid_val){
|
||
if($catid_val){
|
||
$this->table->updateSon_catids($catid_val);
|
||
}
|
||
}
|
||
//如果存在父栏目id
|
||
//并且在删除当前子栏目的同时未发现其它子栏目,那么把父栏目的is_last变成1,因为父栏目已经绝后了
|
||
if($result&&$r[pid]){
|
||
$this->table->updateCatIslast($r[pid]);
|
||
}
|
||
//更新缓存
|
||
$this->table->updateCache();
|
||
if($result){
|
||
$this->success("删除成功");
|
||
}else{
|
||
$this->error("删除失败");
|
||
}
|
||
}
|
||
}
|
||
//字段排序
|
||
public function sort(){
|
||
$catids=I("catids");
|
||
$sort=I("sort");
|
||
foreach($catids as $key=>$catid){
|
||
$data['catid']=(int)$catid;
|
||
$data['sort']=(int)$sort[$key];
|
||
$this->table->save($data);
|
||
}
|
||
$this->success("排序更改成功");
|
||
}
|
||
//字段排序
|
||
public function showCat(){
|
||
$pid=I("get.pid");
|
||
$this->data=$this->forCat3($pid);
|
||
$this->assign("pagetitle","栏目管理");
|
||
$this->assign("ptitle","栏目管理");
|
||
$this->assign("pid",$pid);
|
||
$this->display();
|
||
}
|
||
} |