1 <?php 2 3 namespace Addons\Vote\Controller; //命名空间,设置投票vote 4 5 use Home\Controller\AddonsController; //引入系统插件控制器 6 7 class VoteController extends AddonsController { //投票的主控制器 8 protected $model; //设置字段model 9 protected $option; //设置字段option 10 public function __construct() { //构造函数 11 parent::__construct (); //调用父类的构造函数 12 $this->model = M ( 'Model' )->getByName ( $_REQUEST ['_controller'] ); //查询model表,获取该插件基本列表信息 13 $this->model || $this->error ( '模型不存在!' ); //判断模型是否存在,若不存在抛出错误提示 14 15 $this->assign ( 'model', $this->model ); //将model数据传到模板,初始化的作用 16 17 $this->option = M ( 'Model' )->getByName ( 'vote_option' ); //查vote_option获取投票模型字段信息 18 $this->assign ( 'option', $this->option ); //option数据传到模板,初始化的作用 19 } 20 /** 21 * 显示指定模型列表数据 22 */ 23 public function lists() { //列表控制方法 24 $page = I ( 'p', 1, 'intval' ); // 默认显示第一页数据 25 26 // 解析列表规则 27 $list_data = $this->_list_grid ( $this->model ); //_list_grid方法解析列表规则 28 $grids = $list_data ['list_grids']; //给子数组别名 29 $fields = $list_data ['fields']; //给子数组别名 30 31 // 关键字搜索 32 $map ['token'] = get_token (); //获取token 33 $key = $this->model ['search_key'] ? $this->model ['search_key'] : 'title'; //判断并赋值 34 if (isset ( $_REQUEST [$key] )) { //判断并将字符转换为 HTML 实体 35 $map [$key] = array ( 36 'like', 37 '%' . htmlspecialchars ( $_REQUEST [$key] ) . '%' 38 ); 39 unset ( $_REQUEST [$key] ); 40 } 41 // 条件搜索 42 foreach ( $_REQUEST as $name => $val ) { //循环 43 if (in_array ( $name, $fields )) { //组装$map数组 44 $map [$name] = $val; //把$val传值给$map [$name] 45 } 46 } 47 $row = empty ( $this->model ['list_row'] ) ? 20 : $this->model ['list_row']; //判断并设置model ['list_row'] 48 49 // 读取模型数据列表 50 51 empty ( $fields ) || in_array ( 'id', $fields ) || array_push ( $fields, 'id' ); //判断数组中字段 52 $name = parse_name ( get_table_name ( $this->model ['id'] ), true ); //获取表名,并转换格式 53 $data = M ( $name )->field ( empty ( $fields ) ? true : $fields )->where ( $map )->order ( 'id DESC' )->page ( $page, $row )->select (); //查表,并id升序排序 54 55 /* 查询记录总数 */ 56 $count = M ( $name )->where ( $map )->count (); 57 58 // 分页 59 if ($count > $row) { //若总数大于基本分页数 60 $page = new \Think\Page ( $count, $row ); //实例化分也类 61 $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' ); 62 $this->assign ( '_page', $page->show () ); 63 } 64 65 $this->assign ( 'list_grids', $grids ); //传参list_grids 66 $this->assign ( 'list_data', $data ); //传参list_data 67 $this->meta_title = $this->model ['title'] . '列表'; //给meta_title赋值 68 $this->display ( T ( 'Addons://Vote@Vote/lists' ) ); //调用模板显示 69 } 70 public function del() { //列表中的删除方法 71 $ids = I ( 'id', 0 ); //对传参进行赋值 72 if (empty ( $ids )) { //判断$ids是否为空 73 $ids = array_unique ( ( array ) I ( 'ids', 0 ) ); //若$ids不为空,将数组降维 74 } 75 if (empty ( $ids )) { 76 $this->error ( '请选择要操作的数据!' ); //若数组为空,提示错误 77 } 78 79 $Model = M ( get_table_name ( $this->model ['id'] ) ); //实例化model表中对应id的一条数据 80 //组装数组 81 $map = array ( 82 'id' => array ( 83 'in', 84 $ids 85 ) 86 ); 87 $map ['token'] = get_token (); 88 if ($Model->where ( $map )->delete ()) { //删除数据操作并做判断是否删除成功 89 $this->success ( '删除成功' ); 90 } else { 91 $this->error ( '删除失败!' ); 92 } 93 }