// +---------------------------------------------------------------------- namespace Admin\Controller; /** * 模型数据管理控制器 * @author 麦当苗儿 */ class ThinkController extends AdminController { /** * 显示指定模型列表数据 * @param String $model 模型标识 * @author 麦当苗儿 */ public function lists($model = null, $p = 0,$extend_map = array()){ $model || $this->error('模型名标识必须!'); $page = intval($p); $page = $page ? $page : 1; //默认显示第一页数据 //获取模型信息 $model = M('Model')->getByName($model); $model || $this->error('模型不存在!'); //解析列表规则 $fields = array(); $grids = preg_split('/[;\r\n]+/s', trim($model['list_grid'])); foreach ($grids as &$value) { if(trim($value) === ''){ continue; } // 字段:标题:链接 $val = explode(':', $value); // 支持多个字段显示 $field = explode(',', $val[0]); $value = array('field' => $field, 'title' => $val[1]); if(isset($val[2])){ // 链接信息 $value['href'] = $val[2]; // 搜索链接信息中的字段信息 preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']); } if(strpos($val[1],'|')){ // 显示格式定义 list($value['title'],$value['format']) = explode('|',$val[1]); } foreach($field as $val){ $array = explode('|',$val); $fields[] = $array[0]; } } // 过滤重复字段信息 $fields = array_unique($fields); // 关键字搜索 $map = $extend_map; $key = $model['search_key']?$model['search_key']:'title'; if(isset($_REQUEST[$key])){ $map[$key] = array('like','%'.$_GET[$key].'%'); unset($_REQUEST[$key]); } // 条件搜索 foreach($_REQUEST as $name=>$val){ if(in_array($name,$fields)){ $map[$name] = $val; } } if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;} //读取模型数据列表 if($model['extend']){ $name = get_table_name($model['id']); $parent = get_table_name($model['extend']); $fix = C("DB_PREFIX"); $key = array_search('id', $fields); if(false === $key){ array_push($fields, "{$fix}{$parent}.id as id"); } else { $fields[$key] = "{$fix}{$parent}.id as id"; } /* 查询记录数 */ $count = D($parent)->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id")->where($map)->count(); // 查询数据 $data = D($parent) ->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id") /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order("{$fix}{$parent}.id DESC") /* 数据分页 */ ->page($page, $row) /* 执行查询 */ ->select(); } else { if($model['need_pk']){ in_array('id', $fields) || array_push($fields, 'id'); } $name = parse_name(get_table_name($model['id']), true); $data = D($name) /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order(empty($map['order'])?"id desc":$map['order']) /* 数据分页 */ ->page($page, $row) /* 执行查询 */ ->select(); /* 查询记录总数 */ $count = D($name)->where($map)->count(); } //分页 $page = set_pagination($count,$row); if($page) {$this->assign('_page', $page);} $data = $this->parseDocumentList($data,$model['id']); if(isset($extend_map['for_show_pic_list'])){ if($extend_map['for_show_pic_list']=='icon'){ foreach ($data as $key => $value) { $data[$key]['pic_path']=get_cover($value['icon'],'path'); } } } $this->assign('model', $model); $this->assign('list_grids', $grids); $this->assign('list_data', $data); $this->meta_title = $model['title'].'列表'; $this->display($model['template_list']); } /** * [数组分页,二维数组字段排序] * @param [type] $model [description] * @param integer $p [description] * @param array $extend_map [description] * @return [type] [description] * @author [yyh] <[email address]> */ public function order_lists($model = null, $p = 0,$extend_map = array()){ $model || $this->error('模型名标识必须!'); $page = intval($p); $page = $page ? $page : 1; //默认显示第一页数据 $arraypage = $page; //默认显示第一页数据 //获取模型信息 $model = M('Model')->getByName($model); $model || $this->error('模型不存在!'); //解析列表规则 $fields = array(); $grids = preg_split('/[;\r\n]+/s', trim($model['list_grid'])); foreach ($grids as &$value) { if(trim($value) === ''){ continue; } // 字段:标题:链接 $val = explode(':', $value); // 支持多个字段显示 $field = explode(',', $val[0]); $value = array('field' => $field, 'title' => $val[1]); if(isset($val[2])){ // 链接信息 $value['href'] = $val[2]; // 搜索链接信息中的字段信息 preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']); } if(strpos($val[1],'|')){ // 显示格式定义 list($value['title'],$value['format']) = explode('|',$val[1]); } foreach($field as $val){ $array = explode('|',$val); $fields[] = $array[0]; } } // 过滤重复字段信息 $fields = array_unique($fields); // 关键字搜索 $map = $extend_map; $key = $model['search_key']?$model['search_key']:'title'; if(isset($_REQUEST[$key])){ $map[$key] = array('like','%'.$_GET[$key].'%'); unset($_REQUEST[$key]); } // 条件搜索 foreach($_REQUEST as $name=>$val){ if(in_array($name,$fields)){ $map[$name] = $val; } } if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;} //读取模型数据列表 if($model['extend']){ $name = get_table_name($model['id']); $parent = get_table_name($model['extend']); $fix = C("DB_PREFIX"); $key = array_search('id', $fields); if(false === $key){ array_push($fields, "{$fix}{$parent}.id as id"); } else { $fields[$key] = "{$fix}{$parent}.id as id"; } /* 查询记录数 */ $count = D($parent)->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id")->where($map)->count(); // 查询数据 $data = D($parent) ->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id") /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order("{$fix}{$parent}.id DESC") /* 数据分页 */ // ->page($page, $row) 需要查询所有数据 后面用数组分页,原因: 页面要排序 /* 执行查询 */ ->select(); } else { if($model['need_pk']){ in_array('id', $fields) || array_push($fields, 'id'); } $name = parse_name(get_table_name($model['id']), true); $data = D($name) /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order(empty($map['order'])?"id desc":$map['order']) /* 数据分页 */ // ->page($page, $row)//需要查询所有数据 后面用数组分页,原因: 页面要排序 /* 执行查询 */ ->select(); /* 查询记录总数 */ $count = count($data); } //分页 $page = set_pagination($count,$row); if($page) {$this->assign('_page', $page);} $data = $this->parseDocumentList($data,$model['id']); if(isset($extend_map['for_show_pic_list'])){ if($extend_map['for_show_pic_list']=='icon'){ foreach ($data as $key => $value) { $data[$key]['pic_path']=get_cover($value['icon'],'path'); } } if($extend_map['for_show_pic_list']=='novice'){ foreach ($data as $key => $value) { $data[$key]['novice']=arr_count($value['novice']); } } } if($_REQUEST['data_order']!=''){ $data_order=reset(explode(',',$_REQUEST['data_order'])); $data_order_type=end(explode(',',$_REQUEST['data_order'])); $this->assign('userarpu_order',$data_order); $this->assign('userarpu_order_type',$data_order_type); } $data=my_sort($data,$data_order_type,(int)$data_order); $size=$row;//每页显示的记录数 $pnum = ceil(count($data) / $size); //总页数,ceil()函数用于求大于数字的最小整数 //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度) $data = array_slice($data, ($arraypage-1)*$size, $size); $this->assign('model', $model); $this->assign('list_grids', $grids); $this->assign('list_data', $data); $this->meta_title = $model['title'].'列表'; $this->display($model['template_list']); } /** * [数组分页,二维数组字段排序] * @param [type] $model [description] * @param integer $p [description] * @param array $extend_map [description] * @return [type] [description] * @author [yyh] <[email address]> */ public function order_lists2($model = null, $p = 0,$extend_map = array()){ $model || $this->error('模型名标识必须!'); $page = intval($p); $page = $page ? $page : 1; //默认显示第一页数据 $arraypage = $page; //默认显示第一页数据 //获取模型信息 $model = M('Model')->getByName($model); $model || $this->error('模型不存在!'); //解析列表规则 $fields = array(); $grids = preg_split('/[;\r\n]+/s', trim($model['list_grid'])); foreach ($grids as &$value) { if(trim($value) === ''){ continue; } // 字段:标题:链接 $val = explode(':', $value); // 支持多个字段显示 $field = explode(',', $val[0]); $value = array('field' => $field, 'title' => $val[1]); if(isset($val[2])){ // 链接信息 $value['href'] = $val[2]; // 搜索链接信息中的字段信息 preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']); } if(strpos($val[1],'|')){ // 显示格式定义 list($value['title'],$value['format']) = explode('|',$val[1]); } foreach($field as $val){ $array = explode('|',$val); $fields[] = $array[0]; } } // 过滤重复字段信息 $fields = array_unique($fields); // 关键字搜索 $map = $extend_map['where']; $key = $model['search_key']?$model['search_key']:'title'; if(isset($_REQUEST[$key])){ $map[$key] = array('like','%'.$_GET[$key].'%'); unset($_REQUEST[$key]); } // 条件搜索 foreach($_REQUEST as $name=>$val){ if(in_array($name,$fields)){ $map[$name] = $val; } } if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;} //读取模型数据列表 if($model['extend']){ $name = get_table_name($model['id']); $parent = get_table_name($model['extend']); $fix = C("DB_PREFIX"); $key = array_search('id', $fields); if(false === $key){ array_push($fields, "{$fix}{$parent}.id as id"); } else { $fields[$key] = "{$fix}{$parent}.id as id"; } /* 查询记录数 */ $count = D($parent)->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id")->where($map)->count(); // 查询数据 $data = D($parent) ->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id") /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order("{$fix}{$parent}.id DESC") /* 数据分页 */ // ->page($page, $row) 需要查询所有数据 后面用数组分页,原因: 页面要排序 /* 执行查询 */ ->select(); } else { if($model['need_pk']){ in_array('id', $fields) || array_push($fields, 'id'); } $name = parse_name(get_table_name($model['id']), true); $data = D($name) /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order(empty($extend_map['order'])?"id desc":$extend_map['order']) /* 数据分页 */ // ->page($page, $row)//需要查询所有数据 后面用数组分页,原因: 页面要排序 /* 执行查询 */ ->select(); /* 查询记录总数 */ $count = count($data); } //分页 $page = set_pagination($count,$row); if($page) {$this->assign('_page', $page);} $data = $this->parseDocumentList($data,$model['id']); if(isset($extend_map['where']['for_show_pic_list'])){ if($extend_map['where']['for_show_pic_list']=='icon'){ foreach ($data as $key => $value) { $data[$key]['pic_path']=get_cover($value['icon'],'path'); } } if($extend_map['where']['for_show_pic_list']=='novice'){ foreach ($data as $key => $value) { $data[$key]['novice']=arr_count($value['novice']); } } } if($_REQUEST['data_order']!=''){ $data_order=reset(explode(',',$_REQUEST['data_order'])); $data_order_type=end(explode(',',$_REQUEST['data_order'])); $this->assign('userarpu_order',$data_order); $this->assign('userarpu_order_type',$data_order_type); } $data=my_sort($data,$data_order_type,(int)$data_order); $size=$row;//每页显示的记录数 $pnum = ceil(count($data) / $size); //总页数,ceil()函数用于求大于数字的最小整数 //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度) $data = array_slice($data, ($arraypage-1)*$size, $size); $this->assign('model', $model); $this->assign('list_grids', $grids); $this->assign('list_data', $data); $this->meta_title = $model['title'].'列表'; $this->display($model['template_list']); } public function del($model = null, $ids=null,$url=null){ $model = M('Model')->find($model); $model || $this->error('模型不存在!'); $ids = array_unique((array)I('ids',null)); if (empty($ids)) { $this->error('请选择要操作的数据!'); } $Model = D(get_table_name($model['id'])); $map = array('id' => array('in', $ids) ); if($Model->where($map)->delete()){ $this->success('删除成功',$url); } else { $this->error('删除失败!'); } } /** * 设置一条或者多条数据的状态 * @author huajie */ public function setStatus($model='Document'){ return parent::setStatus($model); } public function edit($model = null, $id = 0,$url=null){ //获取模型信息 $model = M('Model')->find($model); $model || $this->error('模型不存在!'); if(IS_POST){ $Model = D(parse_name(get_table_name($model['id']),1)); // 获取模型的字段信息 $Model = $this->checkAttr($Model,$model['id']); if($Model->create() && $Model->save() !== false){ \Think\Log::actionLog(CONTROLLER_NAME.'/edit',$model,1); $this->success('保存'.$model['title'].'成功!',empty($url)? U('lists?model='.$model['name']):$url); } else { $this->error($Model->getError());//,empty($url)? U('lists?model='.$model['name']):$url } } else { $fields = get_model_attribute($model['id']); //获取数据 $data = D(get_table_name($model['id']))->find($id); $data || $this->error('数据不存在!'); $this->assign('model', $model); $this->assign('fields', $fields); $this->assign('data', $data); $this->meta_title = '编辑'.$model['title']; $this->display($model['template_edit']); } } public function add($model = null,$url=''){ //获取模型信息 $model = M('Model')->where(array('status' => 1))->find($model); $model || $this->error('模型不存在!'); if(IS_POST){ $Model = D(parse_name(get_table_name($model['id']),1)); // 获取模型的字段信息 $Model = $this->checkAttr($Model,$model['id']); if($Model->create() && $Model->add()){// \Think\Log::actionLog(CONTROLLER_NAME.'/add',$model,1); $this->success('添加'.$model['title'].'成功!', empty($url)? U('lists?model='.$model['name']):$url); } else { $this->error($Model->getError()); } } else { $fields = get_model_attribute($model['id']); $this->assign('model', $model); $this->assign('fields', $fields); $this->meta_title = '新增'.$model['title']; $this->display($model['template_add']); } } public function remove($model = null,$model1=null, $ids=null,$prefix='tab_'){ $model = M('Model')->find($model); $model || $this->error('模型不存在!'); $ids = array_unique((array)I('request.ids',null)); if ( empty($ids) ) { $this->error('请选择要操作的数据!'); } $Model = M(get_table_name($model['id']),$prefix); $map = array('id' => array('in', $ids) ); if($Model->where($map)->delete()){ $info = D($model1,'Logic'); $info->deletes($ids); $this->success('删除成功'); } else { $this->error('删除失败!'); } } /** * 设置一条或者多条数据的状态 */ public function set_status($Model=CONTROLLER_NAME){ $ids = I('request.ids'); $status = I('request.status'); $msg_type = I('request.msg_type',1); $field = I('request.field','status'); if(empty($ids)){ $this->error('请选择要操作的数据'); } $msg = array( 0=>array('success'=>'推荐成功','error'=>'推荐失败'), 1=>array('success'=>'启用成功','error'=>'启用失败'), 2=>array('success'=>'开启成功','error'=>'开启失败'), 3=>array('success'=>'关闭成功','error'=>'关闭失败'), 4=>array('success'=>'删除成功','error'=>'删除失败'), 5=>array('success'=>'审核成功','error'=>'审核失败'), 6=>array('success'=>'拒绝成功','error'=>'拒绝失败'), 7=>array('success'=>'修复成功','error'=>'修复失败'), 8=>array('success'=>'操作成功','error'=>'操作失败'), 9=>array('success'=>'取消成功','error'=>'取消失败'), 10=>array('success'=>'拉黑成功','error'=>'拉黑失败'), 11=>array('success'=>'锁定成功','error'=>'锁定失败'), 12=>array('success'=>'解锁成功','error'=>'解锁失败'), 13=>array('success'=>'处理成功','error'=>'处理失败'), 14=>array('success'=>'忽略成功','error'=>'忽略失败'), 15=>array('success'=>'驳回成功','error'=>'驳回失败'), 16=>array('success'=>'显示成功','error'=>'显示失败'), ); $map['id'] = array('in',$ids); switch ($status){ case -1 : $this->delete($Model, $map, $msg[$msg_type], $field); break; case 0 : $this->forbid($Model, $map, $msg[$msg_type], $field); break; case 1 : $this->resume($Model, $map, $msg[$msg_type], $field); break; case 2 : $this->reject($Model, $map, $msg[$msg_type],$field); break; default : $this->error('参数错误'); break; } } protected function checkAttr($Model,$model_id){ $fields = get_model_attribute($model_id,false); $validate = $auto = array(); foreach($fields as $key=>$attr){ if($attr['is_must']){// 必填字段 $validate[] = array($attr['name'],'require',$attr['title'].'必须!'); } // 自动验证规则 if(!empty($attr['validate_rule'])) { $validate[] = array($attr['name'],$attr['validate_rule'],$attr['error_info']?$attr['error_info']:$attr['title'].'验证错误',0,$attr['validate_type'],$attr['validate_time']); } // 自动完成规则 if(!empty($attr['auto_rule'])) { $auto[] = array($attr['name'],$attr['auto_rule'],$attr['auto_time'],$attr['auto_type']); }elseif('checkbox'==$attr['type']){ // 多选型 $auto[] = array($attr['name'],'arr2str',3,'function'); }elseif('date' == $attr['type']){ // 日期型 $auto[] = array($attr['name'],'strtotime',3,'function'); }elseif('datetime' == $attr['type']){ // 时间型 $auto[] = array($attr['name'],'strtotime',3,'function'); } } return $Model->validate($validate)->auto($auto); } /** * 显示指定模型列表数据 * @param String $model 模型标识 * @author */ public function flists($model = null, $p = 0,$extend_map = array(),$field=''){ $model || $this->error('模型名标识必须!'); $page = intval($p); $page = $page ? $page : 1; //默认显示第一页数据 //获取模型信息 $model = M('Model')->getByName($model); $model || $this->error('模型不存在!'); //解析列表规则 $fields = array(); $fields[] = $field; $grids = preg_split('/[;\r\n]+/s', trim($model['list_grid'])); foreach ($grids as &$value) { if(trim($value) === ''){ continue; } // 字段:标题:链接 $val = explode(':', $value); // 支持多个字段显示 $field = explode(',', $val[0]); $value = array('field' => $field, 'title' => $val[1]); if(isset($val[2])){ // 链接信息 $value['href'] = $val[2]; // 搜索链接信息中的字段信息 preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']); } if(strpos($val[1],'|')){ // 显示格式定义 list($value['title'],$value['format']) = explode('|',$val[1]); } foreach($field as $val){ $array = explode('|',$val); $fields[] = $array[0]; } } // 过滤重复字段信息 $fields = array_unique($fields); // 关键字搜索 $map = $extend_map; $key = $model['search_key']?$model['search_key']:'title'; if(isset($_REQUEST[$key])){ $map[$key] = array('like','%'.$_GET[$key].'%'); unset($_REQUEST[$key]); } // 条件搜索 foreach($_REQUEST as $name=>$val){ if(in_array($name,$fields)){ $map[$name] = $val; } } if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;} //读取模型数据列表 if($model['extend']){ $name = get_table_name($model['id']); $parent = get_table_name($model['extend']); $fix = C("DB_PREFIX"); $key = array_search('id', $fields); if(false === $key){ array_push($fields, "{$fix}{$parent}.id as id"); } else { $fields[$key] = "{$fix}{$parent}.id as id"; } /* 查询记录数 */ $count = D($parent)->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id")->where($map)->count(); // 查询数据 $data = D($parent) ->join("INNER JOIN {$fix}{$name} ON {$fix}{$parent}.id = {$fix}{$name}.id") /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order("{$fix}{$parent}.id DESC") /* 数据分页 */ ->page($page, $row) /* 执行查询 */ ->select(); } else { if($model['need_pk']){ in_array('id', $fields) || array_push($fields, 'id'); } $name = parse_name(get_table_name($model['id']), true); $data = D($name) /* 查询指定字段,不指定则查询所有字段 */ ->field(empty($fields) ? true : $fields) // 查询条件 ->where($map) /* 默认通过id逆序排列 */ ->order(empty($map['order'])?"id desc":$map['order']) /* 数据分页 */ ->page($page, $row) /* 执行查询 */ ->select(); /* 查询记录总数 */ $count = D($name)->where($map)->count(); } //分页 $page = set_pagination($count,$row); if($page) {$this->assign('_page', $page);} $data = $this->parseDocumentList($data,$model['id']); if(isset($extend_map['for_show_pic_list'])){ if($extend_map['for_show_pic_list']=='icon'){ foreach ($data as $key => $value) { $data[$key]['pic_path']=get_cover($value['icon'],'path'); } } } $this->assign('model', $model); $this->assign('list_grids', $grids); $this->assign('list_data', $data); $this->meta_title = $model['title'].'列表'; $this->display($model['template_list']); } /** * 添加快捷图标 * @author 鹿文学 */ public function addShortcutIcon() { $Kuaijieicon = M('Kuaijieicon'); $result = $Kuaijieicon->where(['url'=>$_REQUEST['url']])->find(); if ($result) { if ($result['status'] == 0) { $data = array('status'=>1,'id'=>$result['id']); $id = $Kuaijieicon->save($data); if($id){ //记录行为 action_log('Kuaijie/edit', 'Kuaijieicon', $result['id'], UID); $this->success('添加成功'); } else { $this->error('添加失败'); } } else { $this->error('已添加过常用设置'); } } else { $data = array('title'=>$_REQUEST['title'],'status'=>1,'url'=>$_REQUEST['url'],'value'=>0); $id = $Kuaijieicon->add($data); if($id){ //记录行为 action_log('Kuaijie/add', 'Kuaijieicon', $id, UID); $this->success('添加成功'); } else { $this->error('添加失败'); } } } /** * 删除快捷图标 * @param integer $id 快捷图标编号 * @author 鹿文学 */ public function delShortcutIcon($id=0) { if (!is_numeric($id) || $id<1) {$this->error('参数错误');} $Kuaijieicon = M('Kuaijieicon'); $data = array('status'=>0,'id'=>$id); $res = $Kuaijieicon->save($data); if($res){ //记录行为 action_log('Kuaijie/del', 'Kuaijieicon', $id, UID); $this->success('删除成功'); } else { $this->error('删除失败'); } } }