diff --git a/application/admin/controller/Content.php b/application/admin/controller/Content.php index 2eb0e9a8..7a70b937 100644 --- a/application/admin/controller/Content.php +++ b/application/admin/controller/Content.php @@ -196,11 +196,13 @@ class Content extends Admin { } //获得数组的第一条数组 - $first_key = array_keys($field_group); - $rows = model('Attribute')->where($map)->order('group_id asc, sort asc')->select(); + $rows = model('Attribute')->getFieldlist($map, 'id'); if (!empty($rows)) { foreach ($rows as $key => $value) { - $fields[$field_group[$value['group_id']]][] = $value; + $list[$value['group_id']][] = $value; + } + foreach ($field_group as $key => $value) { + $fields[$value] = isset($list[$key]) ? $list[$key] : array(); } }else{ $fields = array(); diff --git a/application/admin/controller/Model.php b/application/admin/controller/Model.php index fbee84c1..7fe0ce72 100644 --- a/application/admin/controller/Model.php +++ b/application/admin/controller/Model.php @@ -53,7 +53,7 @@ class Model extends Admin { action_log('add_model', 'model', $result, session('auth_user.uid')); $this->success('创建成功!', url('admin/model/index')); }else{ - return $this->error($this->model->getError()); + return $this->error($this->model->getError() ? $this->model->getError() : '模型标识为保留名称!'); } }else{ $this->setMeta('新增模型'); @@ -78,12 +78,16 @@ class Model extends Admin { }else{ $info = $this->model->where('id', $request->param('id'))->find(); + $field_group = parse_config_attr($info['attribute_group']); //获取字段列表 $rows = db('Attribute')->where('model_id', $request->param('id'))->where('is_show', 1)->order('group_id asc, sort asc')->select(); if ($rows) { // 梳理属性的可见性 foreach ($rows as $key => $field) { - $fields[$field['group_id']][] = $field; + $list[$field['group_id']][] = $field; + } + foreach ($field_group as $key => $value) { + $fields[$value] = isset($list[$key]) ? $list[$key] : array(); } }else{ $fields = array(); diff --git a/application/admin/fields.php b/application/admin/fields.php new file mode 100644 index 00000000..2ad08806 --- /dev/null +++ b/application/admin/fields.php @@ -0,0 +1,13 @@ + array('name' => 'title', 'title' => '标题', 'type' => 'text', 'length' => 200, 'extra' => '', 'remark' => '标题', 'is_show' => 1, 'is_must' => 1, 'value'=>''), + 'category_id' => array('name' => 'category_id', 'title' => '栏目', 'type' => 'bind', 'length' => 10, 'extra' => 'category', 'remark' => '栏目', 'is_show' => 1, 'is_must' => 1, 'value'=>'0'), + 'uid' => array('name' => 'uid', 'title' => '用户UID', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '用户UID', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'), + 'cover_id' => array('name' => 'cover_id', 'title' => '内容封面', 'type' => 'image', 'length' => 10, 'extra' => '', 'remark' => '内容封面', 'is_show' => 1, 'is_must' => 0, 'value'=>''), + 'description' => array('name' => 'description', 'title' => '内容描述', 'type' => 'textarea', 'length' => '', 'extra' => '', 'remark' => '内容描述', 'is_show' => 1, 'is_must' => 0, 'value'=>''), + 'status' => array('name' => 'status', 'title' => '数据状态', 'type' => 'select', 'length' => 2, 'extra' => "-1:删除\r\n0:禁用\r\n1:正常\r\n2:待审核\r\n3:草稿", 'remark' => '数据状态', 'is_show' => 1, 'is_must' => 1, 'value'=>'1'), + 'is_top' => array('name' => 'is_top', 'title' => '是否置顶', 'type' => 'bool', 'length' => 2, 'extra' => '', 'remark' => '是否置顶', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'), + 'view' => array('name' => 'view', 'title' => '浏览数量', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '浏览数量', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'), + 'update_time' => array('name' => 'update_time', 'title' => '更新时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '更新时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'), + 'create_time' => array('name' => 'create_time', 'title' => '添加时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '添加时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'), +); \ No newline at end of file diff --git a/application/admin/view/model/add.html b/application/admin/view/model/add.html index c3f825aa..9c92b6b7 100644 --- a/application/admin/view/model/add.html +++ b/application/admin/view/model/add.html @@ -29,6 +29,16 @@ (请输入模型的名称) +
+ +
+ + (文档模型默认导入部分必要字段) +
+
diff --git a/application/admin/view/model/edit.html b/application/admin/view/model/edit.html index 85eea342..dafd7bbd 100644 --- a/application/admin/view/model/edit.html +++ b/application/admin/view/model/edit.html @@ -17,7 +17,6 @@
@@ -45,14 +44,6 @@
- -
- -
- - (用于表单显示的名称) -
-
@@ -65,7 +56,7 @@
{volist name="fields" id="field"}
-
{:parse_field_attr($info['attribute_group'])[$key]}
+
{$key}
{foreach name="field" item="item" key="k"} @@ -105,8 +96,6 @@ (默认列表模板的高级搜索项)
-
-
diff --git a/application/common/model/Attribute.php b/application/common/model/Attribute.php index 415c9eee..c7d53100 100644 --- a/application/common/model/Attribute.php +++ b/application/common/model/Attribute.php @@ -59,7 +59,7 @@ class Attribute extends Base{ public function getFieldlist($map,$index='id'){ $list = array(); - $row = db('Attribute')->field('*,remark as help,type,extra as "option"')->where($map)->select(); + $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']); diff --git a/application/common/model/Model.php b/application/common/model/Model.php index 275afbc4..c4d89461 100644 --- a/application/common/model/Model.php +++ b/application/common/model/Model.php @@ -15,7 +15,7 @@ namespace app\common\model; class Model extends Base { protected $auto = ['update_time']; - protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid'=>'id:ID']; + 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', @@ -32,7 +32,22 @@ class Model extends Base { if (!$db->CheckTable($tablename)) { //创建新表 return $db->initTable($tablename, $data['title'], 'id')->query(); - }; + }else{ + return false; + } + }); + self::afterInsert(function($event){ + $data = $event->toArray(); + if ($data['is_doc']) { + $fields = include(APP_PATH.'admin/fields.php'); + if (!empty($fields)) { + foreach ($fields as $key => $value) { + $fields[$key]['model_id'] = $data['id']; + } + model('Attribute')->saveAll($fields); + } + } + return true; }); self::beforeUpdate(function($event){ $data = $event->toArray(); @@ -76,12 +91,13 @@ class Model extends Base { $db = new \com\Datatable(); if ($db->CheckTable($tablename)) { //检测表是否存在 - $result = $db->del_table($tablename)->query(); + $result = $db->delTable($tablename)->query(); if (!$result) { return false; $this->error = "数据表删除失败!"; } } + db('Attribute')->where('model_id', $id)->delete(); //删除字段信息 $result = $this->where('id', $id)->delete(); if ($result) { return true; diff --git a/application/common/validate/Attribute.php b/application/common/validate/Attribute.php index aeaf1f99..8f2f34c4 100644 --- a/application/common/validate/Attribute.php +++ b/application/common/validate/Attribute.php @@ -20,7 +20,6 @@ class Attribute extends Base { 'type' => 'require', 'length' => 'requireIn:type,textarea,editor|integer', 'remark' => 'require', - 'value' => 'requireIf:is_must,1' ); protected $message = array( @@ -30,7 +29,6 @@ class Attribute extends Base { 'title.require' => '字段标题不能为空!', 'type.require' => '类型不能为空!', 'remark.require' => '描述不能为空!', - 'value' => '必填字段默认值必须!' ); protected $scene = array( diff --git a/application/index/controller/Content.php b/application/index/controller/Content.php index 45d96739..100f80e5 100644 --- a/application/index/controller/Content.php +++ b/application/index/controller/Content.php @@ -58,20 +58,21 @@ class Content extends Fornt { } $cate = $this->getCategory($id); - - if ($this->modelInfo['extend'] == 1) { - //获得当前栏目的所有子栏目 + $map = array(); + $attr = db('Attribute')->where('model_id', $this->modelInfo['id'])->column('name'); + if (in_array('category_id', $attr)) { $ids = get_category_child($id); $map['category_id'] = array('IN', $ids); - $map['model_id'] = $this->modelInfo['id']; + } + if (in_array('status', $attr)) { $map['status'] = array('GT', 0); } - - if ($this->modelInfo['extend'] > 1) { - $order = "id desc"; - } else { + if (in_array('is_top', $attr)) { $order = "is_top desc,id desc"; + }else{ + $order = "id desc"; } + $list = $this->model->where($map)->order($order)->paginate(15); $data = array( diff --git a/core/extend/com/Datatable.php b/core/extend/com/Datatable.php index b2090fe3..1b6b98a5 100644 --- a/core/extend/com/Datatable.php +++ b/core/extend/com/Datatable.php @@ -60,24 +60,13 @@ class Datatable { * @param string $table 表名 * @return void 空 */ - public function initTable($table = '', $comment = '', $pk = '', $time = true) { + public function initTable($table = '', $comment = '', $pk = 'id') { $this->table = $this->getTablename($table, true); - if ($pk) { - $sql[] = $this->generateField('id', 'int', 11, '', '主键', true); - } - if ($time) { - //初始化表内含创建时间和更新时间两个字段 - $sql[] = $this->generateField('create_time', 'int', 11, 0, '创建时间', false); - $sql[] = $this->generateField('update_time', 'int', 11, 0, '创建时间', false); - } + $sql = $this->generateField($pk, 'int', 11, '', '主键', true); $primary = $pk ? "PRIMARY KEY (`" . $pk . "`)" : ''; - if ($primary) { - $generatesql = implode(',', $sql) . ','; - } else { - $generatesql = implode(',', $sql); - } + $generatesql = $sql . ','; $create = "CREATE TABLE IF NOT EXISTS `" . $this->table . "`(" . $generatesql @@ -122,7 +111,7 @@ class Datatable { $field_attr['length'] = ""; } $field_attr['is_null'] = $attr['is_must'] ? 'NOT NULL' : 'NULL'; - $field_attr['default'] = $attr['value'] != '' ? 'DEFAULT "' . $attr['default'] . '"' : 'DEFAULT null'; + $field_attr['default'] = $attr['value'] != '' ? 'DEFAULT "' . $attr['value'] . '"' : ''; $field_attr['comment'] = (isset($attr['remark']) && $attr['remark']) ? $attr['remark'] : $attr['title']; $field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : ' AFTER `id`'; diff --git a/template/default/content/article/detail.html b/template/default/content/article/detail.html index 60f001c2..43f7d70b 100644 --- a/template/default/content/article/detail.html +++ b/template/default/content/article/detail.html @@ -14,7 +14,7 @@
{$info['title']}
发布时间:{$info['create_time']|date='Y-m-d H:i:s',###} - 访问量:{$info['view']} + 访问量:{$info['view']|default=0}
diff --git a/template/default/content/article/list.html b/template/default/content/article/list.html index 44cb213c..b6c3a3e6 100644 --- a/template/default/content/article/list.html +++ b/template/default/content/article/list.html @@ -22,14 +22,14 @@