Files
sentcms/app/model/Model.php
2020-03-27 19:01:10 +08:00

116 lines
4.9 KiB
PHP

<?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\model;
/**
* 设置模型
*/
class Model extends \think\Model{
protected $auto = ['update_time'];
protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间\r\nupdate_time:更新时间"];
protected $type = array(
'id' => 'integer'
);
protected static function onAfterInsert($model){
$data = $model->getDate();
$fields = [
'title'=> ['name' => 'title', 'title' => '标题', 'type' => 'text', 'length' => 200, 'extra' => '', 'remark' => '标题', 'is_show' => 1, 'is_must' => 1, 'value'=>''],
'category_id' => ['name' => 'category_id', 'title' => '栏目', 'type' => 'bind', 'length' => 10, 'extra' => 'category', 'remark' => '栏目', 'is_show' => 1, 'is_must' => 1, 'value'=>'0'],
'uid' => ['name' => 'uid', 'title' => '用户UID', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '用户UID', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'cover_id' => ['name' => 'cover_id', 'title' => '内容封面', 'type' => 'image', 'length' => 10, 'extra' => '', 'remark' => '内容封面', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'description' => ['name' => 'description', 'title' => '内容描述', 'type' => 'textarea', 'length' => '', 'extra' => '', 'remark' => '内容描述', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'content' => ['name' => 'content', 'title' => '内容', 'type' => 'editor', 'length' => '', 'extra' => '', 'remark' => '内容', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'status' => ['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' => ['name' => 'is_top', 'title' => '是否置顶', 'type' => 'bool', 'length' => 2, 'extra' => '', 'remark' => '是否置顶', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'view' => ['name' => 'view', 'title' => '浏览数量', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '浏览数量', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'update_time' => ['name' => 'update_time', 'title' => '更新时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '更新时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'create_time' => ['name' => 'create_time', 'title' => '添加时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '添加时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
];
if (!empty($fields)) {
foreach ($fields as $key => $value) {
if ($data['is_doc']) {
$fields[$key]['model_id'] = $data['id'];
} else {
if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) {
$fields[$key]['model_id'] = $data['id'];
} else {
unset($fields[$key]);
}
}
}
(new Attribute())->saveAll($fields);
}
return true;
}
protected static function onAfterUpdate($model){
$data = $model->getDate();
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('Attribute')->where('id', 'IN', $value)->setField('group_id', $key);
foreach ($value as $k => $v) {
db('Attribute')->where('id', $v)->setField('sort', $k);
}
}
}
}
return true;
}
protected function setAttributeSortAttr($value) {
return $value ? json_encode($value) : '';
}
public function setNameAttr($value) {
return strtolower($value);
}
public function getStatusTextAttr($value, $data) {
$status = array(
0 => '禁用',
1 => '启用',
);
return $status[$data['status']];
}
public function del() {
$id = input('id', '', 'trim,intval');
$tablename = $this->where('id', $id)->value('name');
//删除数据表
$db = new \com\Datatable();
if ($db->CheckTable($tablename)) {
//检测表是否存在
$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;
} else {
$this->error = "模型删除失败!";
return false;
}
}
public function attribute() {
return $this->hasMany('Attribute');
}
}