Document.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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\Column as ColumnModel;
  13. use app\cms\model\Document as DocumentModel;
  14. use app\cms\model\Field as FieldModel;
  15. use think\Db;
  16. use util\Tree;
  17. /**
  18. * 文档控制器
  19. * @package app\cms\admin
  20. */
  21. class Document extends Admin
  22. {
  23. /**
  24. * 文档列表
  25. * @author 蔡伟明 <314013107@qq.com>
  26. */
  27. public function index()
  28. {
  29. cookie('__forward__', $_SERVER['REQUEST_URI']);
  30. // 查询
  31. $map = $this->getMap();
  32. $map[] = ['cms_document.trash', '=', 0];
  33. // 排序
  34. $order = $this->getOrder('update_time desc');
  35. // 数据列表
  36. $data_list = DocumentModel::getList($map, $order);
  37. // 使用ZBuilder快速创建数据表格
  38. return ZBuilder::make('table')
  39. ->setSearch(['title' => '标题', 'cms_column.name' => '栏目名称']) // 设置搜索框
  40. ->addColumns([ // 批量添加数据列
  41. ['id', 'ID'],
  42. ['title', '标题'],
  43. ['column_name', '栏目名称'],
  44. ['view', '点击量'],
  45. ['username', '发布人'],
  46. ['update_time', '更新时间', 'datetime'],
  47. ['sort', '排序', 'text.edit'],
  48. ['status', '状态', 'switch'],
  49. ['right_button', '操作', 'btn']
  50. ])
  51. ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
  52. ->addRightButtons(['edit', 'delete']) // 批量添加右侧按钮
  53. ->addOrder(['column_name' => 'cms_document.cid'])
  54. ->addOrder('id,title,view,username,update_time')
  55. ->addFilter(['column_name' => 'cms_column.name', 'username' => 'admin_user'])
  56. ->setRowList($data_list) // 设置表格数据
  57. ->fetch(); // 渲染模板
  58. }
  59. /**
  60. * 添加文档
  61. * @param int $cid 栏目id
  62. * @param string $model 模型id
  63. * @author 蔡伟明 <314013107@qq.com>
  64. * @return mixed
  65. * @throws \think\Exception
  66. */
  67. public function add($cid = 0, $model = '')
  68. {
  69. // 保存文档数据
  70. if ($this->request->isAjax()) {
  71. $DocumentModel = new DocumentModel();
  72. if (false === $DocumentModel->saveData()) {
  73. $this->error($DocumentModel->getError());
  74. }
  75. $this->success('新增成功', cookie('__forward__'));
  76. }
  77. // 第二步,填写文档信息
  78. if ($cid > 0) {
  79. cookie('__forward__', url('add', ['cid' => $cid]));
  80. // 获取栏目数据
  81. $column = ColumnModel::getInfo($cid);
  82. // 独立模型只取该模型的字段,不包含系统字段
  83. $where = [];
  84. if (get_model_type($column['model']) == 2) {
  85. $where[] = ['model', '=', $column['model']];
  86. } else {
  87. $where[] = ['model', 'in', [0, $column['model']]];
  88. }
  89. // 获取文档模型字段
  90. $where[] = ['status', '=', 1];
  91. $where[] = ['show', '=', 1];
  92. $fields = FieldModel::where($where)->order('sort asc,id asc')->column(true);
  93. foreach ($fields as &$value) {
  94. // 解析options
  95. if ($value['options'] != '') {
  96. $value['options'] = parse_attr($value['options']);
  97. }
  98. switch ($value['type']) {
  99. case 'linkage':// 解析联动下拉框异步请求地址
  100. if (!empty($value['ajax_url']) && substr($value['ajax_url'], 0, 4) != 'http') {
  101. $value['ajax_url'] = url($value['ajax_url']);
  102. }
  103. break;
  104. case 'date':
  105. case 'time':
  106. case 'datetime':
  107. $value['value'] = '';
  108. break;
  109. case 'bmap':
  110. $value['level'] = $value['level'] == 0 ? 12 : $value['level'];
  111. break;
  112. case 'colorpicker':
  113. $value['mode'] = 'rgba';
  114. break;
  115. }
  116. }
  117. // 添加额外表单项信息
  118. $extra_field = [
  119. ['name' => 'cid', 'title' => '所属栏目', 'type' => 'static', 'value' => $column['name']],
  120. ['name' => 'cid', 'type' => 'hidden', 'value' => $cid],
  121. ['name' => 'model', 'type' => 'hidden', 'value' => $column['model']]
  122. ];
  123. $fields = array_merge($extra_field, $fields);
  124. // 使用ZBuilder快速创建表单
  125. return ZBuilder::make('form')
  126. ->setFormItems($fields)
  127. ->hideBtn('back')
  128. ->fetch();
  129. }
  130. // 第一步,选择栏目
  131. if ($model == '') {
  132. $columns = ColumnModel::getTreeList(0, false);
  133. } else {
  134. // 获取相同内容模型的栏目
  135. $columns = Db::name('cms_column')->where('model', $model)->order('pid,id')->column('id,name,pid');
  136. $columns = Tree::config(['title' => 'name'])->toList($columns, current($columns)['pid']);
  137. $result = [];
  138. foreach ($columns as $column) {
  139. $result[$column['id']] = $column['title_display'];
  140. }
  141. $columns = $result;
  142. }
  143. return ZBuilder::make('form')
  144. ->addFormItem('select', 'cid', '选择栏目', '请选择栏目', $columns)
  145. ->setBtnTitle('submit', '下一步')
  146. ->hideBtn('back')
  147. ->isAjax(false)
  148. ->fetch();
  149. }
  150. /**
  151. * 编辑文档
  152. * @param null $id 文档id
  153. * @param string $model 模型id
  154. * @author 蔡伟明 <314013107@qq.com>
  155. * @return mixed
  156. * @throws \think\Exception
  157. */
  158. public function edit($id = null, $model = '')
  159. {
  160. if ($id === null) $this->error('参数错误');
  161. // 保存文档数据
  162. if ($this->request->isPost()) {
  163. $DocumentModel = new DocumentModel();
  164. $result = $DocumentModel->saveData();
  165. if (false === $result) {
  166. $this->error($DocumentModel->getError());
  167. }
  168. $this->success('编辑成功', cookie('__forward__'));
  169. }
  170. // 获取数据
  171. $info = DocumentModel::getOne($id, $model);
  172. // 独立模型只取该模型的字段,不包含系统字段
  173. $where = [];
  174. if ($model != '') {
  175. $where[] = ['model', '=', $model];
  176. } else {
  177. $where[] = ['model', 'in', [0, $info['model']]];
  178. }
  179. // 用于查询内容模型栏目
  180. $map = $where;
  181. // 获取文档模型字段
  182. $where[] = ['status', '=', 1];
  183. $where[] = ['show', '=', 1];
  184. $fields = FieldModel::where($where)->order('sort asc,id asc')->column(true);
  185. foreach ($fields as $id => &$value) {
  186. // 解析options
  187. if ($value['options'] != '') {
  188. $value['options'] = parse_attr($value['options']);
  189. }
  190. // 日期时间
  191. switch ($value['type']) {
  192. case 'date':
  193. $info[$value['name']] = format_time($info[$value['name']], 'Y-m-d');
  194. break;
  195. case 'time':
  196. $info[$value['name']] = format_time($info[$value['name']], 'H:i:s');
  197. break;
  198. case 'datetime':
  199. $info[$value['name']] = empty($info[$value['name']]) ? '' : format_time($info[$value['name']]);
  200. break;
  201. case 'bmap':
  202. $value['level'] = $value['level'] == 0 ? 12 : $value['level'];
  203. break;
  204. case 'colorpicker':
  205. $value['mode'] = 'rgba';
  206. break;
  207. }
  208. }
  209. // 获取相同内容模型的栏目
  210. $columns = Db::name('cms_column')->where($map)->whereOr('model', $info['model'])->order('pid,id')->column('id,name,pid');
  211. $columns = Tree::config(['title' => 'name'])->toList($columns, current($columns)['pid']);
  212. $result = [];
  213. foreach ($columns as $column) {
  214. $result[$column['id']] = $column['title_display'];
  215. }
  216. $columns = $result;
  217. // 添加额外表单项信息
  218. $extra_field = [
  219. ['name' => 'id', 'type' => 'hidden'],
  220. ['name' => 'cid', 'title' => '所属栏目', 'type' => 'select', 'options' => $columns],
  221. ['name' => 'model', 'type' => 'hidden']
  222. ];
  223. $fields = array_merge($extra_field, $fields);
  224. // 使用ZBuilder快速创建表单
  225. return ZBuilder::make('form')
  226. ->setFormItems($fields)
  227. ->setFormData($info)
  228. ->fetch();
  229. }
  230. /**
  231. * 删除文档(不是彻底删除,而是移动到回收站)
  232. * @param null $ids 文档id
  233. * @param string $table 数据表
  234. * @author 蔡伟明 <314013107@qq.com>
  235. */
  236. public function delete($ids = null, $table = '')
  237. {
  238. if ($ids === null) $this->error('参数错误');
  239. $document_id = is_array($ids) ? '' : $ids;
  240. $document_title = Db::name($table)->where('id', 'in', $ids)->column('title');
  241. // 移动文档到回收站
  242. if (false === Db::name($table)->where('id', 'in', $ids)->setField('trash', 1)) {
  243. $this->error('删除失败');
  244. }
  245. // 删除并记录日志
  246. action_log('document_trash', $table, $document_id, UID, implode('、', $document_title));
  247. $this->success('删除成功');
  248. }
  249. /**
  250. * 启用文档
  251. * @param array $record 行为日志
  252. * @author 蔡伟明 <314013107@qq.com>
  253. * @throws \think\Exception
  254. * @throws \think\exception\PDOException
  255. */
  256. public function enable($record = [])
  257. {
  258. return $this->setStatus('enable');
  259. }
  260. /**
  261. * 禁用文档
  262. * @param array $record 行为日志
  263. * @author 蔡伟明 <314013107@qq.com>
  264. * @throws \think\Exception
  265. * @throws \think\exception\PDOException
  266. */
  267. public function disable($record = [])
  268. {
  269. return $this->setStatus('disable');
  270. }
  271. /**
  272. * 设置文档状态:删除、禁用、启用
  273. * @param string $type 类型:enable/disable
  274. * @param array $record
  275. * @author 蔡伟明 <314013107@qq.com>
  276. * @throws \think\Exception
  277. * @throws \think\exception\PDOException
  278. */
  279. public function setStatus($type = '', $record = [])
  280. {
  281. $table_token = input('param._t', '');
  282. $table_token == '' && $this->error('缺少参数');
  283. !session('?'.$table_token) && $this->error('参数错误');
  284. $table_data = session($table_token);
  285. $table_name = $table_data['table'];
  286. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  287. $document_id = is_array($ids) ? '' : $ids;
  288. $document_title = Db::name($table_name)->where('id', 'in', $ids)->column('title');
  289. return parent::setStatus($type, ['document_'.$type, 'cms_document', $document_id, UID, implode('、', $document_title)]);
  290. }
  291. /**
  292. * 快速编辑
  293. * @param array $record 行为日志
  294. * @author 蔡伟明 <314013107@qq.com>
  295. * @return mixed
  296. */
  297. public function quickEdit($record = [])
  298. {
  299. $table_token = input('param._t', '');
  300. $table_token == '' && $this->error('缺少参数');
  301. !session('?'.$table_token) && $this->error('参数错误');
  302. $table_data = session($table_token);
  303. $table = $table_data['table'];
  304. $id = input('post.pk', '');
  305. $field = input('post.name', '');
  306. $value = input('post.value', '');
  307. $document = Db::name($table)->where('id', $id)->value($field);
  308. $details = '表名(' . $table . '),字段(' . $field . '),原值(' . $document . '),新值:(' . $value . ')';
  309. return parent::quickEdit(['document_edit', 'cms_document', $id, UID, $details]);
  310. }
  311. }