Document.php 4.8 KB

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