Document.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\home;
  10. use app\cms\model\Column as ColumnModel;
  11. use app\cms\model\Document as DocumentModel;
  12. use util\Tree;
  13. use think\Db;
  14. /**
  15. * 文档控制器
  16. * @package app\cms\home
  17. */
  18. class Document extends Common
  19. {
  20. /**
  21. * 文档详情页
  22. * @param null $id 文档id
  23. * @param string $model 独立模型id
  24. * @author 蔡伟明 <314013107@qq.com>
  25. * @return mixed
  26. */
  27. public function detail($id = null, $model = '')
  28. {
  29. if ($id === null) $this->error('缺少参数');
  30. if ($model != '') {
  31. $table = get_model_table($model);
  32. $map = [
  33. $table.'.status' => 1,
  34. $table.'.trash' => 0
  35. ];
  36. } else {
  37. $map = [
  38. 'cms_document.status' => 1,
  39. 'cms_document.trash' => 0
  40. ];
  41. }
  42. $info = DocumentModel::getOne($id, $model, $map);
  43. if (isset($info['tags'])) {
  44. $info['tags'] = explode(',', $info['tags']);
  45. }
  46. $this->assign('document', $info);
  47. $this->assign('breadcrumb', $this->getBreadcrumb($info['cid']));
  48. $this->assign('prev', $this->getPrev($id, $model));
  49. $this->assign('next', $this->getNext($id, $model));
  50. $template = $info['detail_template'] == '' ? 'detail' : substr($info['detail_template'], 0, strpos($info['detail_template'], '.'));
  51. return $this->fetch($template);
  52. }
  53. /**
  54. * 获取栏目面包屑导航
  55. * @param int $id 栏目id
  56. * @author 蔡伟明 <314013107@qq.com>
  57. * @return mixed
  58. */
  59. private function getBreadcrumb($id)
  60. {
  61. $columns = ColumnModel::where('status', 1)->column('id,pid,name,url,target,type');
  62. foreach ($columns as &$column) {
  63. if ($column['type'] == 0) {
  64. $column['url'] = url('cms/column/index', ['id' => $column['id']]);
  65. }
  66. }
  67. return Tree::config(['title' => 'name'])->getParents($columns, $id);
  68. }
  69. /**
  70. * 获取上一篇文档
  71. * @param int $id 当前文档id
  72. * @param string $model 独立模型id
  73. * @author 蔡伟明 <314013107@qq.com>
  74. * @return array|string|\think\Model|null
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. */
  79. private function getPrev($id, $model = '')
  80. {
  81. if ($model == '') {
  82. $cid = Db::name('cms_document')->where('id', $id)->value('cid');
  83. $document = Db::name('cms_document')->where([
  84. ['status', '=', 1],
  85. ['trash', '=', 0],
  86. ['cid', '=', $cid],
  87. ['id', 'lt', $id]
  88. ])->order('id desc')->find();
  89. } else {
  90. $table = get_model_table($model);
  91. $cid = Db::table($table)->where('id', $id)->value('cid');
  92. $document = Db::table($table)->where([
  93. ['status', '=', 1],
  94. ['trash', '=', 0],
  95. ['cid', '=', $cid],
  96. ['id', 'lt', $id]
  97. ])->order('id desc')->find();
  98. }
  99. if ($document) {
  100. $document['url'] = url('cms/document/detail', ['id' => $document['id'], 'model' => $model]);
  101. }
  102. return $document;
  103. }
  104. /**
  105. * 获取下一篇文档
  106. * @param int $id 当前文档id
  107. * @param string $model 独立模型id
  108. * @author 蔡伟明 <314013107@qq.com>
  109. * @return array|string|\think\Model|null
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. private function getNext($id, $model = '')
  115. {
  116. if ($model == '') {
  117. $cid = Db::name('cms_document')->where('id', $id)->value('cid');
  118. $document = Db::name('cms_document')->where([
  119. ['status', '=', 1],
  120. ['trash', '=', 0],
  121. ['cid', '=', $cid],
  122. ['id', 'gt', $id]
  123. ])->find();
  124. } else {
  125. $table = get_model_table($model);
  126. $cid = Db::table($table)->where('id', $id)->value('cid');
  127. $document = Db::table($table)->where([
  128. ['status', '=', 1],
  129. ['trash', '=', 0],
  130. ['cid', '=', $cid],
  131. ['id', 'gt', $id]
  132. ])->find();
  133. }
  134. if ($document) {
  135. $document['url'] = url('cms/document/detail', ['id' => $document['id'], 'model' => $model]);
  136. }
  137. return $document;
  138. }
  139. }