weiphp 投票插件的主控制器部分代码2

  1 <?php
  2 public function edit() {        //编辑方法
  3         // 获取模型信息
  4         $id = I ( 'id', 0, 'intval' );
  5         
  6         if (IS_POST) {        //判断处理传参
  7             $_POST ['mTime'] = time ();        //获取当前时间戳
  8             
  9             $Model = D ( parse_name ( get_table_name ( $this->model ['id'] ), 1 ) );    //实例化模型
 10             // 获取模型的字段信息
 11             $Model = $this->checkAttr ( $Model, $this->model ['id'] );
 12             if ($Model->create () && $Model->save ()) {
 13                 // 增加选项
 14                 D ( 'Addons://Vote/VoteOption' )->set ( I ( 'post.id' ), I ( 'post.' ) );
 15                 
 16                 // 保存关键词
 17                 D ( 'Common/Keyword' )->set ( I ( 'post.keyword' ), 'Vote', I ( 'post.id' ) );
 18                 
 19                 $this->success ( '保存' . $this->model ['title'] . '成功!', U ( 'lists?model=' . $this->model ['name'] ) );
 20             } else {
 21                 $this->error ( $Model->getError () );
 22             }
 23         } else {
 24             $fields = get_model_attribute ( $this->model ['id'] );        //获取模型信息并缓存
 25             
 26             // 获取数据
 27             $data = M ( get_table_name ( $this->model ['id'] ) )->find ( $id );
 28             $data || $this->error ( '数据不存在!' );        //判断若没有数据,报错
 29             
 30             $token = get_token ();        //获取token
 31             if (isset ( $data ['token'] ) && $token != $data ['token'] && defined ( 'ADDON_PUBLIC_PATH' )) {        //判断数据中token值
 32                 $this->error ( '非法访问!' );
 33             }
 34             
 35             $option_list = M ( 'vote_option' )->where ( 'vote_id=' . $id )->order ( '`order` asc' )->select ();        //获取数据
 36             $this->assign ( 'option_list', $option_list );        //向模版发送option_list数据
 37             
 38             $this->assign ( 'fields', $fields );        //向模版发送fields数据
 39             $this->assign ( 'data', $data );        //向模版发送data数据
 40             $this->meta_title = '编辑' . $this->model ['title'];    //赋值    
 41             $this->display ( T ( 'Addons://Vote@Vote/edit' ) );        //调用edit模版
 42         }
 43     }
 44     public function add() {        //添加方法
 45         if (IS_POST) {
 46             // 自动补充token
 47             $_POST ['token'] = get_token ();
 48             $Model = D ( parse_name ( get_table_name ( $this->model ['id'] ), 1 ) );
 49             // 获取模型的字段信息
 50             $Model = $this->checkAttr ( $Model, $this->model ['id'] );
 51             if ($Model->create () && $vote_id = $Model->add ()) {
 52                 // 增加选项
 53                 D ( 'Addons://Vote/VoteOption' )->set ( $vote_id, I ( 'post.' ) );
 54                 
 55                 // 保存关键词
 56                 D ( 'Common/Keyword' )->set ( I ( 'keyword' ), 'Vote', $vote_id );
 57                 
 58                 $this->success ( '添加' . $this->model ['title'] . '成功!', U ( 'lists?model=' . $this->model ['name'] ) );    //提示成功及相应信息
 59             } else {
 60                 $this->error ( $Model->getError () );    //失败报错
 61             }
 62         } else {
 63             
 64             $vote_fields = get_model_attribute ( $this->model ['id'] );        //获取模型信息并缓存
 65             $this->assign ( 'fields', $vote_fields );        //数据交给模版
 66             // 选项表
 67             $option_fields = get_model_attribute ( $this->option ['id'] );        //获取信息并缓存
 68             $this->assign ( 'option_fields', $option_fields );
 69             
 70             $this->meta_title = '新增' . $this->model ['title'];        //赋值
 71             $this->display ( $this->model ['template_add'] ? $this->model ['template_add'] : T ( 'Addons://Vote@Vote/add' ) );        //判断并选择模版显示
 72         }
 73     }
 74     protected function checkAttr($Model, $model_id) {        //定义方法检查字符串
 75         $fields = get_model_attribute ( $model_id, false );        //获取信息并缓存
 76         $validate = $auto = array ();        //设置数组
 77         foreach ( $fields as $key => $attr ) {        //循环fields数组
 78             if ($attr ['is_must']) { // 必填字段
 79                 $validate [] = array (
 80                         $attr ['name'],
 81                         'require',
 82                         $attr ['title'] . '必须!' 
 83                 );
 84             }
 85             // 自动验证规则
 86             if (! empty ( $attr ['validate_rule'] ) || $attr ['validate_type'] == 'unique') {
 87                 //组装数组
 88                 $validate [] = array (
 89                         $attr ['name'],
 90                         $attr ['validate_rule'],
 91                         $attr ['error_info'] ? $attr ['error_info'] : $attr ['title'] . '验证错误',
 92                         0,
 93                         $attr ['validate_type'],
 94                         $attr ['validate_time'] 
 95                 );
 96             }
 97             // 自动完成规则
 98             if (! empty ( $attr ['auto_rule'] )) {
 99                 //组装数组
100                 $auto [] = array (
101                         $attr ['name'],
102                         $attr ['auto_rule'],
103                         $attr ['auto_time'],
104                         $attr ['auto_type'] 
105                 );
106             } elseif ('checkbox' == $attr ['type']) { // 多选型
107                 $auto [] = array (
108                         $attr ['name'],
109                         'arr2str',
110                         3,
111                         'function' 
112                 );
113             } elseif ('datetime' == $attr ['type']) { // 日期型
114                 $auto [] = array (
115                         $attr ['name'],
116                         'strtotime',
117                         3,
118                         'function' 
119                 );
120             }
121         }
122         return $Model->validate ( $validate )->auto ( $auto );        //返回模型的信息
123     }
原文地址:https://www.cnblogs.com/dennr/p/4612208.html