From 227705f560bf099b6bf1c380e26af51ec60a4e41 Mon Sep 17 00:00:00 2001 From: molong Date: Tue, 4 Jul 2017 16:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=88X=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Form.php | 32 +++++- application/admin/view/form/index.html | 34 ++++++ application/common/model/Form.php | 60 ++++++---- application/common/model/FormAttr.php | 105 ++++++++++++++++++ .../validate/{Document.php => Form.php} | 8 +- 5 files changed, 209 insertions(+), 30 deletions(-) create mode 100644 application/common/model/FormAttr.php rename application/common/validate/{Document.php => Form.php} (87%) diff --git a/application/admin/controller/Form.php b/application/admin/controller/Form.php index dc68a628..6c815552 100644 --- a/application/admin/controller/Form.php +++ b/application/admin/controller/Form.php @@ -35,10 +35,20 @@ class Form extends Admin { /** * 添加表单 */ - public function add(){ + public function add(\think\Request $request){ if (IS_POST) { - # code... + $result = $this->model->validate('Form')->save($request->post()); + if (false !== $result) { + return $this->success('添加成功!', url('admin/form/index')); + }else{ + return $this->error($this->model->getError()); + } }else{ + $data = array( + 'keyList' => $this->model->addField + ); + $this->assign($data); + $this->setMeta('添加表单'); return $this->fetch('public/edit'); } } @@ -46,10 +56,22 @@ class Form extends Admin { /** * 编辑表单 */ - public function edit(){ + public function edit(\think\Request $request){ if (IS_POST) { - # code... + $result = $this->model->validate('Form')->save($request->post(), array('id'=> $request->post('id'))); + if (false !== $result) { + return $this->success('修改成功!', url('admin/form/index')); + }else{ + return $this->error($this->model->getError()); + } }else{ + $info = $this->model->where('id', $request->param('id'))->find(); + $data = array( + 'info' => $info, + 'keyList' => $this->model->editField + ); + $this->assign($data); + $this->setMeta('编辑表单'); return $this->fetch('public/edit'); } } @@ -77,4 +99,6 @@ class Form extends Admin { public function lists(){ return $this->fetch(); } + + public function attr(){} } \ No newline at end of file diff --git a/application/admin/view/form/index.html b/application/admin/view/form/index.html index 0e155834..ed53d395 100644 --- a/application/admin/view/form/index.html +++ b/application/admin/view/form/index.html @@ -27,8 +27,42 @@ + {notempty name="list"} + {volist name="list" id="item"} + + + + + {$item['id']} + {$item['name']} + + {$item['title']} + + + {$item.create_time|time_format} + + + {if $item['status']} + {$item['status_text']} + {else/} + {$item['status_text']} + {/if} + + + 字段 + {$item['status']|show_status_op} + 编辑 + 删除 + 数据 + + + {/volist} + {else/} + aOh! 暂时还没有创建模型! + {/notempty} + {$page} diff --git a/application/common/model/Form.php b/application/common/model/Form.php index 8daaeedd..e76533d2 100644 --- a/application/common/model/Form.php +++ b/application/common/model/Form.php @@ -22,11 +22,22 @@ class Form extends Base{ 'update_time' => 'integer', ); + public $addField = array( + array('name'=>'name','title'=>'标识','type'=>'text','help'=>''), + array('name'=>'title','title'=>'标题','type'=>'text','help'=>'') + ); + + public $editField = array( + array('name'=>'id','title'=>'ID','type'=>'hidden','help'=>''), + array('name'=>'name','title'=>'标识','type'=>'text','help'=>''), + array('name'=>'title','title'=>'标题','type'=>'text','help'=>''), + array('name' => 'list_grid', 'title'=>'列表定义', 'type' => 'textarea', 'help'=>'') + ); protected static function init(){ self::beforeInsert(function($event){ $data = $event->toArray(); - $tablename = strtolower($data['name']); + $tablename = 'form_' . strtolower($data['name']); //实例化一个数据库操作类 $db = new \com\Datatable(); //检查表是否存在并创建 @@ -43,35 +54,40 @@ class Form extends Base{ $fields = include(APP_PATH.'admin/fields.php'); if (!empty($fields)) { foreach ($fields as $key => $value) { - if ($data['is_doc']) { + if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) { $fields[$key]['form_id'] = $data['id']; }else{ - if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) { - $fields[$key]['form_id'] = $data['id']; - }else{ - unset($fields[$key]); - } + unset($fields[$key]); } } model('FormAttr')->saveAll($fields); } return true; }); - self::beforeUpdate(function($event){ - $data = $event->toArray(); - if (isset($data['attribute_sort']) && $data['attribute_sort']) { - $attribute_sort = json_decode($data['attribute_sort'], true); + // self::beforeUpdate(function($event){ + // $data = $event->toArray(); + // if (isset($data['attribute_sort']) && $data['attribute_sort']) { + // $attribute_sort = json_decode($data['attribute_sort'], true); - if (!empty($attribute_sort)) { - foreach ($attribute_sort as $key => $value) { - db('FormAttr')->where('id', 'IN', $value)->setField('group_id', $key); - foreach ($value as $k => $v) { - db('FormAttr')->where('id', $v)->setField('sort', $k); - } - } - } - } - return true; - }); + // if (!empty($attribute_sort)) { + // foreach ($attribute_sort as $key => $value) { + // db('FormAttr')->where('id', 'IN', $value)->setField('group_id', $key); + // foreach ($value as $k => $v) { + // db('FormAttr')->where('id', $v)->setField('sort', $k); + // } + // } + // } + // } + // return true; + // }); } + + public function getStatusTextAttr($value, $data) { + $status = array( + 0 => '禁用', + 1 => '启用', + ); + return $status[$data['status']]; + } + } \ No newline at end of file diff --git a/application/common/model/FormAttr.php b/application/common/model/FormAttr.php new file mode 100644 index 00000000..18b5c90c --- /dev/null +++ b/application/common/model/FormAttr.php @@ -0,0 +1,105 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +/** +* 设置模型 +*/ +class FormAttr extends Base{ + + protected $type = array( + 'id' => 'integer', + ); + + protected static function init(){ + self::afterInsert(function($data){ + if ($data['form_id']) { + $name = db('Form')->where('id', $data['form_id'])->value('name'); + $db = new \com\Datatable(); + $attr = $data->toArray(); + $model_attr = array( + 'form_id' => $data['form_id'], + 'attr_id' => $data->id, + 'group_id' => 0, + 'is_add_table' => 1, + 'is_show' => $data['is_show'], + 'is_must' => $data['is_must'], + 'sort' => 0, + ); + $attr['after'] = db('FormAttr')->where('name', '<>', $data['name'])->where('form_id', $data['form_id'])->order('id desc')->value('name'); + return $db->columField('form_' . strtolower($name), $attr)->query(); + } + }); + self::beforeUpdate(function($data){ + $attr = $data->toArray(); + $attr['action'] = 'CHANGE'; + $attr['oldname'] = db('FormAttr')->where('id', $attr['id'])->value('name'); + if ($attr['id']) { + $name = db('Form')->where('id', $attr['form_id'])->value('name'); + $db = new \com\Datatable(); + return $db->columField('form_' . strtolower($name), $attr)->query(); + }else{ + return false; + } + }); + } + + protected function getTypeTextAttr($value, $data){ + $type = config('config_type_list'); + $type_text = explode(',', $type[$data['type']]); + return $type_text[0]; + } + + public function getFieldlist($map,$index='id'){ + $list = array(); + $row = db('Attribute')->field('*,remark as help,type,extra as "option"')->where($map)->order('group_id asc, sort asc')->select(); + foreach ($row as $key => $value) { + if (in_array($value['type'],array('checkbox','radio','select','bool'))) { + $value['option'] = parse_field_attr($value['extra']); + } elseif ($value['type'] == 'bind') { + $extra = parse_field_bind($value['extra']); + $option = array(); + foreach ($extra as $k => $v) { + $option[$v['id']] = $v['title_show']; + } + $value['option'] = $option; + } + $list[$value['id']] = $value; + } + return $list; + } + + public function del($id, $model_id){ + $map['id'] = $id; + $info = $this->find($id); + $tablename = db('Form')->where(array('id'=>$model_id))->value('name'); + + //先删除字段表内的数据 + $result = $this->where($map)->delete(); + if ($result) { + $tablename = strtolower($tablename); + //删除模型表中字段 + $db = new \com\Datatable(); + if (!$db->CheckField($tablename,$info['name'])) { + return true; + } + $result = $db->delField($tablename,$info['name'])->query(); + if ($result) { + return true; + }else{ + $this->error = "删除失败!"; + return false; + } + }else{ + $this->error = "删除失败!"; + return false; + } + } +} \ No newline at end of file diff --git a/application/common/validate/Document.php b/application/common/validate/Form.php similarity index 87% rename from application/common/validate/Document.php rename to application/common/validate/Form.php index 788b04d2..5109c5cf 100644 --- a/application/common/validate/Document.php +++ b/application/common/validate/Form.php @@ -10,12 +10,12 @@ namespace app\common\validate; /** -* 设置模型 -*/ -class Document extends Base{ - + * 设置模型 + */ +class Form extends Base { protected $rule = array( 'title' => 'require', + 'name' => 'require|unique:form|/^[a-zA-Z]\w{0,39}$/', ); protected $message = array(