自定义表单功能(X)
This commit is contained in:
@@ -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(){}
|
||||
}
|
||||
@@ -27,8 +27,42 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{notempty name="list"}
|
||||
{volist name="list" id="item"}
|
||||
<tr>
|
||||
<td>
|
||||
<input class="ids" type="checkbox" name="ids[]" value="{$item['id']}" />
|
||||
</td>
|
||||
<td>{$item['id']}</td>
|
||||
<td>{$item['name']}</td>
|
||||
<td>
|
||||
<a data-id="{$item.id}" href="{:url('model/edit?id='.$item['id'])}">{$item['title']}</a>
|
||||
</td>
|
||||
<td>
|
||||
<span>{$item.create_time|time_format}</span>
|
||||
</td>
|
||||
<td>
|
||||
{if $item['status']}
|
||||
<span class="label label-primary">{$item['status_text']}</span>
|
||||
{else/}
|
||||
<span class="label label-danger">{$item['status_text']}</span>
|
||||
{/if}
|
||||
</td>
|
||||
<td>
|
||||
<a href="{:url('admin/form/attr?form_id='.$item['id'])}">字段</a>
|
||||
<a href="{:url('admin/form/status?id='.$item['id'].'&status='.abs(1-$item['status']))}" class="ajax-get">{$item['status']|show_status_op}</a>
|
||||
<a href="{:url('admin/form/edit?id='.$item['id'])}">编辑</a>
|
||||
<a href="{:url('admin/form/del?id='.$item['id'])}" class="confirm ajax-get">删除</a>
|
||||
<a href="{:url('admin/form/lists?form_id='.$item['id'])}">数据</a>
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
{else/}
|
||||
<td colspan="7" class="text-center">aOh! 暂时还没有创建模型!</td>
|
||||
{/notempty}
|
||||
</tbody>
|
||||
</table>
|
||||
{$page}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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']];
|
||||
}
|
||||
|
||||
}
|
||||
105
application/common/model/FormAttr.php
Normal file
105
application/common/model/FormAttr.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
Reference in New Issue
Block a user