Document.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\model;
  10. use think\Model as ThinkModel;
  11. use think\Db;
  12. use app\cms\model\Field as FieldModel;
  13. /**
  14. * 文档模型
  15. * @package app\cms\model
  16. */
  17. class Document extends ThinkModel
  18. {
  19. // 设置当前模型对应的完整数据表名称
  20. protected $name = 'cms_document';
  21. // 自动写入时间戳
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * 获取文档列表
  25. * @param array $map 筛选条件
  26. * @param array $order 排序
  27. * @author 蔡伟明 <314013107@qq.com>
  28. * @return \think\Paginator
  29. * @throws \think\exception\DbException
  30. */
  31. public static function getList($map = [], $order = [])
  32. {
  33. $data_list = self::view('cms_document', true)
  34. ->view("cms_column", ['name' => 'column_name'], 'cms_column.id=cms_document.cid', 'left')
  35. ->view("admin_user", 'username', 'admin_user.id=cms_document.uid', 'left')
  36. ->where($map)
  37. ->order($order)
  38. ->paginate();
  39. return $data_list;
  40. }
  41. /**
  42. * 获取单篇文档
  43. * @param string $id 文档id
  44. * @param string $model 独立模型id
  45. * @param array $map 查询条件
  46. * @author 蔡伟明 <314013107@qq.com>
  47. * @return array|string|ThinkModel|null
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public static function getOne($id = '', $model = '', $map = [])
  53. {
  54. if ($model == '') {
  55. $document = self::get($id);
  56. $extra_table = get_model_table($document['model']);
  57. $data = self::view('cms_document', true);
  58. if ($extra_table != '') {
  59. $data = $data->view($extra_table, true, 'cms_document.id='.$extra_table.'.aid', 'left');
  60. }
  61. return $data->view("cms_column", ['name' => 'column_name', 'list_template', 'detail_template'], 'cms_column.id=cms_document.cid', 'left')
  62. ->view("admin_user", 'username', 'admin_user.id=cms_document.uid', 'left')
  63. ->where('cms_document.id', $id)
  64. ->where($map)
  65. ->find();
  66. } else {
  67. $table = get_model_table($model);
  68. return Db::view($table, true)
  69. ->view("cms_column", ['name' => 'column_name', 'list_template', 'detail_template'], 'cms_column.id='.$table.'.cid', 'left')
  70. ->where($table.'.id', $id)
  71. ->where($map)
  72. ->find();
  73. }
  74. }
  75. /**
  76. * 新增或更新文档
  77. * @author 蔡伟明 <314013107@qq.com>
  78. * @return bool
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. * @throws \think\exception\PDOException
  84. */
  85. public function saveData()
  86. {
  87. $data = request()->post();
  88. $data['uid'] = UID;
  89. // 文档模型
  90. $model = Db::name('cms_model')->where('id', $data['model'])->find();
  91. if ($model['type'] != 2 && empty($data['summary']) && config('cms_config.summary') > 0) {
  92. $data['summary'] = mb_substr(strip_tags($data['content']), 0, config('cms_config.summary'), 'utf-8');
  93. }
  94. // 处理自定义属性
  95. if (isset($data['flag'])) {
  96. $data['flag'] = implode(',', $data['flag']);
  97. } else {
  98. $data['flag'] = '';
  99. }
  100. // 验证基础内容
  101. if ($data['title'] == '') {
  102. $this->error = '标题不能为空';
  103. return false;
  104. }
  105. // 处理特殊字段类型
  106. $fields = FieldModel::where('model', $data['model'])->where('status', 1)->column('name,type');
  107. foreach ($fields as $name => $type) {
  108. if (!isset($data[$name])) {
  109. switch ($type) {
  110. // 开关
  111. case 'switch':
  112. $data[$name] = 0;
  113. break;
  114. case 'checkbox':
  115. $data[$name] = '';
  116. break;
  117. }
  118. } else {
  119. // 如果值是数组则转换成字符串,适用于复选框等类型
  120. if (is_array($data[$name])) {
  121. $data[$name] = implode(',', $data[$name]);
  122. }
  123. switch ($type) {
  124. // 开关
  125. case 'switch':
  126. $data[$name] = 1;
  127. break;
  128. // 日期时间
  129. case 'date':
  130. case 'time':
  131. case 'datetime':
  132. $data[$name] = strtotime($data[$name]);
  133. break;
  134. }
  135. }
  136. }
  137. if (empty($data['id'])) {
  138. if ($model['type'] == 2) {
  139. // 新增独立模型文档
  140. $data['create_time'] = request()->time();
  141. $data['update_time'] = request()->time();
  142. $insert_id = Db::table($model['table'])->insertGetId($data);
  143. if (false === $insert_id) {
  144. $this->error = '新增失败';
  145. return false;
  146. } else {
  147. // 记录行为
  148. action_log('document_add', $model['table'], $insert_id, UID, $data['title']);
  149. return true;
  150. }
  151. } else {
  152. // 新增文档基础内容
  153. if ($document = self::create($data)) {
  154. // 新增文档扩展内容
  155. if ($model['table'] != '') {
  156. $data['aid'] = $document['id'];
  157. if (false === Db::table($model['table'])->insert($data)) {
  158. // 删除已添加的基础内容
  159. self::destroy($document['id']);
  160. $this->error = '新增扩展内容出错';
  161. return false;
  162. }
  163. }
  164. // 记录行为
  165. action_log('document_add', 'cms_document', $document['id'], UID, $document['title']);
  166. return true;
  167. } else {
  168. $this->error = '新增基础内容出错';
  169. return false;
  170. }
  171. }
  172. } else {
  173. // 更新独立模型文档
  174. if ($model['type'] == 2) {
  175. // 新增独立模型文档
  176. $data['update_time'] = request()->time();
  177. if (false === Db::table($model['table'])->update($data)) {
  178. $this->error = '编辑失败';
  179. return false;
  180. } else {
  181. // 记录行为
  182. action_log('document_edit', $model['table'], $data['id'], UID, $data['title']);
  183. return true;
  184. }
  185. } else {
  186. // 更新文档基础内容
  187. if (self::update($data)) {
  188. // 更新文档扩展内容
  189. $data['aid'] = $data['id'];
  190. if (false !== Db::table($model['table'])->update($data)) {
  191. // 记录行为
  192. action_log('document_edit', 'cms_document', $data['id'], UID, $data['title']);
  193. return true;
  194. } else {
  195. $this->error = '更新扩展内容出错';
  196. return false;
  197. }
  198. } else {
  199. $this->error = '更新基础内容出错';
  200. return false;
  201. }
  202. }
  203. }
  204. }
  205. }