Files
sentcms/application/common/model/Form.php
2020-03-25 10:40:32 +08:00

131 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace app\common\model;
/**
* 表单
*/
class Form extends Base{
protected $auto = ['update_time'];
protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid'=>"id:ID\r\ntitle:标题\r\ncreate_time:添加时间|time_format\r\nupdate_time:更新时间|time_format"];
protected $type = array(
'id' => 'integer',
'create_time' => 'integer',
'update_time' => 'integer',
);
public $addField = [
['name' => 'name', 'title' => '标识', 'type' => 'text', 'help' => ''],
['name' => 'title', 'title' => '标题', 'type' => 'text', 'help' => ''],
['name' => 'show_title', 'title' => '显示标题', 'type' => 'readonly', 'help' => ''],
['name' => 'logo', 'title' => '显示Logo', 'type' => 'images', 'help' => ''],
['name' => 'cover', 'title' => 'banner图片', 'type' => 'images', 'help' => ''],
['name' => 'content', 'title' => '内容', 'type' => 'editor', 'help' => ''],
['name' => 'sub_action', 'title' => '提交后操作', 'type' => 'select', 'option' => ['0' => '提示信息', '1' => '跳转连接'], 'help' => ''],
['name' => 'sub_content', 'title' => '提交后内容', 'type' => 'textarea', 'help' => ''],
['name' => 'sub_tips', 'title' => '提交后提示', 'type' => 'images', 'help' => ''],
['name' => 'relation', 'title' => '相关内容', 'type' => 'text', 'help' => '请用英文“,”隔开如1,2,3'],
['name' => 'relation_title', 'title' => '关联标题', 'type' => 'text', 'help' => ''],
['name' => 'form_title', 'title' => '表单标题', 'type' => 'text', 'help' => ''],
['name' => 'header_info', 'title' => '头部信息', 'type' => 'editor', 'help' => ''],
['name' => 'copyright', 'title' => '版权信息', 'type' => 'editor', 'help' => ''],
];
public $editField = [
['name' => 'id', 'title' => 'ID', 'type' => 'hidden', 'help' => ''],
['name' => 'title', 'title' => '标题', 'type' => 'text', 'help' => ''],
['name' => 'show_title', 'title' => '显示标题', 'type' => 'text', 'help' => ''],
['name' => 'logo', 'title' => '显示Logo', 'type' => 'images', 'help' => ''],
['name' => 'cover', 'title' => 'banner图片', 'type' => 'images', 'help' => ''],
['name' => 'content', 'title' => '内容', 'type' => 'editor', 'help' => ''],
['name' => 'sub_action', 'title' => '提交后操作', 'type' => 'select', 'option' => ['0' => '提示信息', '1' => '跳转连接'], 'help' => ''],
['name' => 'sub_content', 'title' => '提交后内容', 'type' => 'textarea', 'help' => ''],
['name' => 'sub_tips', 'title' => '提交后提示', 'type' => 'images', 'help' => ''],
['name' => 'relation', 'title' => '相关内容', 'type' => 'text', 'help' => '请用英文“,”隔开如1,2,3'],
['name' => 'relation_title', 'title' => '关联标题', 'type' => 'text', 'help' => ''],
['name' => 'list_grid', 'title' => '列表定义', 'type' => 'textarea', 'help' => ''],
['name' => 'form_title', 'title' => '表单标题', 'type' => 'text', 'help' => ''],
['name' => 'header_info', 'title' => '头部信息', 'type' => 'editor', 'help' => ''],
['name' => 'copyright', 'title' => '版权信息', 'type' => 'editor', 'help' => ''],
];
protected static function init(){
self::beforeInsert(function($event){
$data = $event->toArray();
$tablename = 'form_' . strtolower($data['name']);
//实例化一个数据库操作类
$db = new \com\Datatable();
//检查表是否存在并创建
if (!$db->CheckTable($tablename)) {
//创建新表
return $db->initTable($tablename, $data['title'], 'id')->query();
}else{
return false;
}
});
self::afterInsert(function($event){
$data = $event->toArray();
$fields = include(APP_PATH.'admin/fields.php');
if (!empty($fields)) {
foreach ($fields as $key => $value) {
if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) {
$fields[$key]['form_id'] = $data['id'];
}else{
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);
// 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 getGridListAttr($value, $data){
$grid = [];
if ($data['list_grid']) {
$row = explode("\r\n", $data['list_grid']);
foreach ($row as $key => $value) {
$item = explode(":", $value);
$grid[] = ['field' => $item[0], 'title' => $item[1]];
}
}
return $grid;
}
public function getStatusTextAttr($value, $data) {
$status = array(
0 => '禁用',
1 => '启用',
);
return $status[$data['status']];
}
public function attr(){
return $this->hasMany('FormAttr');
}
}