Model.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\admin;
  10. use app\admin\controller\Admin;
  11. use app\common\builder\ZBuilder;
  12. use app\cms\model\Model as DocumentModel;
  13. use app\admin\model\Menu as MenuModel;
  14. use think\Db;
  15. use think\facade\Cache;
  16. /**
  17. * 内容模型控制器
  18. * @package app\cms\admin
  19. */
  20. class Model extends Admin
  21. {
  22. /**
  23. * 内容模型列表
  24. * @author 蔡伟明 <314013107@qq.com>
  25. */
  26. public function index()
  27. {
  28. // 查询
  29. $map = $this->getMap();
  30. // 数据列表
  31. $data_list = DocumentModel::where($map)->order('sort,id desc')->paginate();
  32. // 字段管理按钮
  33. $btnField = [
  34. 'title' => '字段管理',
  35. 'icon' => 'fa fa-fw fa-navicon',
  36. 'href' => url('field/index', ['id' => '__id__'])
  37. ];
  38. // 使用ZBuilder快速创建数据表格
  39. return ZBuilder::make('table')
  40. ->setSearch(['name' => '标识', 'title' => '标题']) // 设置搜索框
  41. ->addColumns([ // 批量添加数据列
  42. ['id', 'ID'],
  43. ['icon', '图标', 'icon'],
  44. ['title', '标题'],
  45. ['name', '标识'],
  46. ['table', '附加表'],
  47. ['type', '模型', 'text', '', ['系统', '普通', '独立']],
  48. ['create_time', '创建时间', 'datetime'],
  49. ['sort', '排序', 'text.edit'],
  50. ['status', '状态', 'switch'],
  51. ['right_button', '操作', 'btn']
  52. ])
  53. ->addFilter('type', ['系统', '普通', '独立'])
  54. ->addTopButtons('add,enable,disable') // 批量添加顶部按钮
  55. ->addRightButtons(['edit', 'custom' => $btnField, 'delete' => ['data-tips' => '删除模型将同时删除该模型下的所有字段,且无法恢复。']]) // 批量添加右侧按钮
  56. ->setRowList($data_list) // 设置表格数据
  57. ->fetch(); // 渲染模板
  58. }
  59. /**
  60. * 新增内容模型
  61. * @author 蔡伟明 <314013107@qq.com>
  62. * @return mixed
  63. * @throws \think\Exception
  64. */
  65. public function add()
  66. {
  67. // 保存数据
  68. if ($this->request->isPost()) {
  69. $data = $this->request->post();
  70. if ($data['table'] == '') {
  71. $data['table'] = config('database.prefix') . 'cms_document_' . $data['name'];
  72. } else {
  73. $data['table'] = str_replace('#@__', config('database.prefix'), $data['table']);
  74. }
  75. // 验证
  76. $result = $this->validate($data, 'Model');
  77. if(true !== $result) $this->error($result);
  78. // 严格验证附加表是否存在
  79. if (table_exist($data['table'])) {
  80. $this->error('附加表已存在');
  81. }
  82. if ($model = DocumentModel::create($data)) {
  83. // 创建附加表
  84. if (false === DocumentModel::createTable($model)) {
  85. $this->error('创建附加表失败');
  86. }
  87. // 创建菜单节点
  88. $map = [
  89. 'module' => 'cms',
  90. 'title' => '内容管理'
  91. ];
  92. $menu_data = [
  93. "module" => "cms",
  94. "pid" => Db::name('admin_menu')->where($map)->value('id'),
  95. "title" => $data['title'],
  96. "url_type" => "module_admin",
  97. "url_value" => "cms/content/{$data['name']}",
  98. "url_target" => "_self",
  99. "icon" => "fa fa-fw fa-list",
  100. "online_hide" => "0",
  101. "sort" => "100",
  102. ];
  103. MenuModel::create($menu_data);
  104. // 记录行为
  105. action_log('model_add', 'cms_model', $model['id'], UID, $data['title']);
  106. Cache::clear();
  107. $this->success('新增成功', 'index');
  108. } else {
  109. $this->error('新增失败');
  110. }
  111. }
  112. $type_tips = '此选项添加后不可更改。如果为 <code>系统模型</code> 将禁止删除,对于 <code>独立模型</code>,将强制创建字段id,cid,uid,model,title,create_time,update_time,sort,status,trash,view';
  113. // 显示添加页面
  114. return ZBuilder::make('form')
  115. ->addFormItems([
  116. ['text', 'name', '模型标识', '由小写字母、数字或下划线组成,不能以数字开头'],
  117. ['text', 'title', '模型标题', '可填写中文'],
  118. ['text', 'table', '附加表', '创建后不可更改。由小写字母、数字或下划线组成,如果不填写默认为 <code>'. config('database.prefix') . 'cms_document_模型标识</code>,如果需要自定义,请务必填写系统表前缀,<code>#@__</code>表示当前系统表前缀'],
  119. ['radio', 'type', '模型类别', $type_tips, ['系统模型', '普通模型', '独立模型(不使用主表)'], 1],
  120. ['icon', 'icon', '图标'],
  121. ['radio', 'status', '立即启用', '', ['否', '是'], 1],
  122. ['text', 'sort', '排序', '', 100],
  123. ])
  124. ->fetch();
  125. }
  126. /**
  127. * 编辑内容模型
  128. * @param null $id 模型id
  129. * @author 蔡伟明 <314013107@qq.com>
  130. * @return mixed
  131. * @throws \think\Exception
  132. */
  133. public function edit($id = null) {
  134. if ($id === null) $this->error('参数错误');
  135. // 保存数据
  136. if ($this->request->isPost()) {
  137. $data = $this->request->post();
  138. // 验证
  139. $result = $this->validate($data, 'Model.edit');
  140. if(true !== $result) $this->error($result);
  141. if (DocumentModel::update($data)) {
  142. cache('cms_model_list', null);
  143. cache('cms_model_title_list', null);
  144. // 记录行为
  145. action_log('model_edit', 'cms_model', $id, UID, "ID({$id}),标题({$data['title']})");
  146. $this->success('编辑成功', 'index');
  147. } else {
  148. $this->error('编辑失败');
  149. }
  150. }
  151. $list_model_type = ['系统模型', '普通模型', '独立模型(不使用主表)'];
  152. // 模型信息
  153. $info = DocumentModel::get($id);
  154. $info['type'] = $list_model_type[$info['type']];
  155. // 显示编辑页面
  156. return ZBuilder::make('form')
  157. ->addFormItems([
  158. ['hidden', 'id'],
  159. ['hidden', 'name'],
  160. ['static', 'name', '模型标识'],
  161. ['static', 'type', '模型类别'],
  162. ['static', 'table', '附加表'],
  163. ['text', 'title', '模型标题', '可填写中文'],
  164. ['icon', 'icon', '图标'],
  165. ['radio', 'status', '立即启用', '', ['否', '是']],
  166. ['text', 'sort', '排序'],
  167. ])
  168. ->setFormData($info)
  169. ->fetch();
  170. }
  171. /**
  172. * 删除内容模型
  173. * @param null $ids 内容模型id
  174. * @author 蔡伟明 <314013107@qq.com>
  175. * @throws \think\Exception
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @throws \think\exception\DbException
  179. * @throws \think\exception\PDOException
  180. */
  181. public function delete($ids = null)
  182. {
  183. if ($ids === null) $this->error('参数错误');
  184. $model = DocumentModel::where('id', $ids)->find();
  185. if ($model['type'] == 0) {
  186. $this->error('禁止删除系统模型');
  187. }
  188. // 删除表和字段信息
  189. if (DocumentModel::deleteTable($ids)) {
  190. // 删除主表中的文档
  191. if (false === Db::name('cms_document')->where('model', $ids)->delete()) {
  192. $this->error('删除主表文档失败');
  193. }
  194. // 删除菜单节点
  195. $map = [
  196. 'module' => 'cms',
  197. 'url_value' => "cms/content/{$model['name']}"
  198. ];
  199. if (false === Db::name('admin_menu')->where($map)->delete()) {
  200. $this->error('删除菜单节点失败');
  201. }
  202. // 删除字段数据
  203. if (false !== Db::name('cms_field')->where('model', $ids)->delete()) {
  204. cache('cms_model_list', null);
  205. cache('cms_model_title_list', null);
  206. return parent::delete();
  207. } else {
  208. $this->error('删除内容模型字段失败');
  209. }
  210. } else {
  211. $this->error('删除内容模型表失败');
  212. }
  213. }
  214. }