Catalog.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\common\model;
  9. class Catalog extends Tree
  10. {
  11. protected $autoWriteTimestamp = false;
  12. const TYPE_CATALOG = 0;
  13. const TYPE_ARTICLE_LIST = 1;
  14. const TYPE_DIRECTORY = 2;
  15. const TYPES = [
  16. self::TYPE_DIRECTORY => '目录',
  17. self::TYPE_CATALOG => '栏目',
  18. self::TYPE_ARTICLE_LIST => '文章列表',
  19. ];
  20. //type_text 栏目类型
  21. public function getTypeTextAttr($value, $data)
  22. {
  23. return self::TYPES[$data['type']]??$data['type'];
  24. }
  25. //article_count 文章数量
  26. public function getArticleCountAttr($value, $data)
  27. {
  28. return $this->hasMany('Article')->where('status', Article::STATUS_OPEN)->count();
  29. }
  30. //article_cate_count 分类数量
  31. public function getArticleCateCountAttr($value, $data)
  32. {
  33. return $this->belongsToMany('ArticleCate', 'app\common\model\CateCatalog')->where('status', ArticleCate::STATUS_OPEN)->count();
  34. }
  35. /**
  36. * 栏目下拉数据
  37. */
  38. public function treelistForCatalog()
  39. {
  40. $catalogs = $this->where('type', self::TYPE_DIRECTORY)->order('sort asc,id')->select();
  41. return $this->treelist($catalogs);
  42. }
  43. /**
  44. * 文章下拉框数据
  45. */
  46. public function treelistForArticle()
  47. {
  48. $catalogs = $this->where(['type' => ['in', [self::TYPE_ARTICLE_LIST, self::TYPE_DIRECTORY]], 'status' => self::STATUS_OPEN])->order("tree_path")->select();
  49. $whileflag = true;
  50. while ($whileflag) {
  51. $count = 0;
  52. foreach ($catalogs as $key => $catalog) {
  53. if ($catalog->getData('type') == self::TYPE_DIRECTORY) {
  54. $flag = false;
  55. foreach ($catalogs as $key2 => $catalog2) {
  56. if ($catalog2->pid == $catalog->id) {
  57. $flag = true;
  58. break;
  59. }
  60. }
  61. if (!$flag) {
  62. $count++;
  63. unset($catalogs[$key]);
  64. }
  65. } else if ($catalog->articleCateCount) {
  66. $count++;
  67. unset($catalogs[$key]);
  68. }
  69. }
  70. if ($count == 0) {
  71. $whileflag = false;
  72. }
  73. }
  74. return $this->treelist2($catalogs);
  75. }
  76. /**
  77. * 栏目HTML生成路径
  78. * @return string
  79. */
  80. public function getRealPath()
  81. {
  82. return Templet::getRootDir() . deleteStartDS(replaceDS($this->tpath()));
  83. }
  84. /**
  85. * 发布路径
  86. * 如果存在上级目录,则拼接上级目录
  87. */
  88. public function tpath()
  89. {
  90. $tpath = "";
  91. if ($this->pid) {
  92. $pnode = $this->getDirectParentNode();
  93. if ($pnode && $pnode->tpath()) {
  94. $tpath = $pnode->tpath();
  95. }
  96. }
  97. $enname = $this->path;
  98. if ($tpath && $enname) {
  99. return $tpath . appendStartDS($enname, '/');
  100. } else {
  101. return $enname ?: $tpath;
  102. }
  103. }
  104. /**
  105. * 获取栏目uri
  106. * @return mixed
  107. */
  108. public function getUri()
  109. {
  110. return replaceUrlDS($this->getData('path'));
  111. }
  112. /**
  113. * 获取栏目模板
  114. * @return Templet|null
  115. */
  116. public function getCatalogTemplet()
  117. {
  118. return (new Templet)->setName($this->catalog_templet);
  119. }
  120. /**
  121. * 获取文章模板
  122. * @return Templet|null
  123. */
  124. public function getArticleTemplet()
  125. {
  126. return (new Templet)->setName($this->article_templet);
  127. }
  128. //关联分类
  129. public function articleCates()
  130. {
  131. return $this->belongsToMany('ArticleCate', 'app\common\model\CateCatalog')->where('status', ArticleCate::STATUS_OPEN)->order('sort desc');
  132. }
  133. //分类个数
  134. public function articleCateCount()
  135. {
  136. return $this->belongsToMany('ArticleCate', 'app\common\model\CateCatalog')->where('status', ArticleCate::STATUS_OPEN)->count();
  137. }
  138. /**
  139. * 获取一个分类
  140. * @return mixed
  141. * @throws \Exception
  142. */
  143. public function getFirstArticleCate()
  144. {
  145. $cates = $this->articleCates;
  146. if (empty($cates)) {
  147. throw new \Exception("栏目{$this->id}找不到对应的分类");
  148. }
  149. return current($cates);
  150. }
  151. }