<?php namespace AddonsSurveyController; //命名空间 use HomeControllerAddonsController; //调用插件控制器类 class QuestionController extends AddonsController { //定义问题控制器 var $model; //设置model变量 var $survey_id; //设置survey_id变量 function _initialize() { //初始化函数 parent::_initialize(); //调用父类控制器初始化 $this->model = $this->getModel ( 'survey_question' ); //获取问题方法的模型数据 $param ['survey_id'] = $this->survey_id = intval ( $_REQUEST ['survey_id'] ); //获得并设置问卷id $res ['title'] = '微调研'; //设置数组,并给title赋值 $res ['url'] = addons_url ( 'Survey://Survey/lists' ); //设置数组中的url $res ['class'] = ''; $nav [] = $res; //将数组交给nav $res ['title'] = '问题管理'; //再次设置数据 $res ['url'] = addons_url ( 'Survey://Question/lists', $param ); //设置url $res ['class'] = 'current'; //设置class值 $nav [] = $res; //交给数组$nav,组成二维数组 $this->assign ( 'nav', $nav ); //交给前台模板显示 } // 通用插件的列表模型 public function lists() { //列表方法 $param ['survey_id'] = $this->survey_id; //设置参数数组的问卷id $param ['model'] = $this->model ['id']; //设置model 值 $add_url = U ( 'add', $param ); //U方法获得url $this->assign ( 'add_url', $add_url ); //交给模版显示 $map ['survey_id'] = $this->survey_id; //设置条件数组map session ( 'common_condition', $map ); //设置session parent::common_lists ( $this->model, 0, '', $order = 'sort asc,id asc' ); //获取模型列表上数据并排序 } // 通用插件的编辑模型 public function edit() { $id = I ( 'id' ); //获取id if (IS_POST) { //如果有传参 $Model = D ( parse_name ( get_table_name ( $this->model ['id'] ), 1 ) ); //实例模型 // 获取模型的字段信息 $Model = $this->checkAttr ( $Model, $this->model ['id'] ); if ($Model->create () && $Model->save ()) { //如果数据被添加 $param ['survey_id'] = $this->survey_id; //设置数组survey_id $param ['model'] = $this->model ['id']; //设置模型id $url = U ( 'lists', $param ); //跳转url $this->success ( '保存' . $this->model ['title'] . '成功!', $url ); //编辑成功,提示并跳转 } else { $this->error ( $Model->getError () ); //失败报错 } } parent::common_edit ( $this->model, $id ); //公共编辑方法 } // 通用插件的增加模型 public function add() { if (IS_POST) { //如果有传参 $Model = D ( parse_name ( get_table_name ( $this->model ['id'] ), 1 ) ); //获取并实例化模型 // 获取模型的字段信息 $Model = $this->checkAttr ( $Model, $this->model ['id'] ); if ($Model->create () && $id = $Model->add ()) { //过添加成功, $param ['survey_id'] = $this->survey_id; //定义数组param中问卷id $param ['model'] = $this->model ['id']; //定义模型id $url = U ( 'lists', $param ); //转跳url $this->success ( '添加' . $this->model ['title'] . '成功!', $url ); //成功并转跳 } else { $this->error ( $Model->getError () ); //失败,报错 } exit (); } $normal_tips = '字段类型为单选、多选的参数格式第行一项,每项的值和标题用英文冒号分开。如:<br/>0:男<br/>1:女<br/>2:保密'; //提示字符串 $this->assign ( 'normal_tips', $normal_tips ); //交给模版显示提示 parent::common_add ( $this->model ); //执行父类添加方法 } // 通用插件的删除模型 public function del() { //调用父类的删除方法 parent::common_del ( $this->model ); } }