ArticleCate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. use file\PathHelper;
  10. class ArticleCate extends Tree
  11. {
  12. // 关闭自动写入update_time字段
  13. protected $updateTime = false;
  14. //status_text 分类状态
  15. public function getStatusTextAttr($value, $data)
  16. {
  17. $status = [0 => '隐藏', 1 => '显示'];
  18. return $status[$data['status']];
  19. }
  20. //article_count 文章数量
  21. public function getArticleCountAttr($value, $data)
  22. {
  23. return $this->hasMany('Article')->where('status', Article::STATUS_OPEN)->count();
  24. }
  25. /**
  26. * 发布路径
  27. * 如果存在上级英文名,则拼接上级目录
  28. * 如果没有分类英文名称,则使用分类拼音
  29. * @param bool $auto 是否自动生成英文别名
  30. * @return mixed|string
  31. */
  32. public function tpath($auto = false)
  33. {
  34. $tpath = "";
  35. if ($this->pid) {
  36. $pnode = $this->getDirectParentNode();
  37. if ($pnode && $pnode->tpath()) {
  38. $tpath = $pnode->tpath();
  39. }
  40. }
  41. $enname = $this->en_name;
  42. if (!$this->en_name && $auto) {
  43. $enname = toPinyin($this->getData('title'));
  44. }
  45. if ($tpath && $enname) {
  46. return $tpath . appendStartDS($enname, '/');
  47. } else {
  48. return $enname ?: $tpath;
  49. }
  50. }
  51. /**
  52. * 按规则生成路径
  53. * @param $catalogPath [uri使用相对路径,生成文件用物理路径]
  54. * @param $rule [列表路径规则]
  55. * @param $cid [栏目id]
  56. * @param $tid [分类id]
  57. * @param $page [第几页]
  58. * @return mixed
  59. * @throws \Exception
  60. */
  61. public static function getRulePath($catalogPath, $rule, $cid, $tid, $page = 1)
  62. {
  63. if (empty($rule)) {
  64. throw new \Exception("null articlesRule error");
  65. }
  66. $rule = replaceDS($rule);//处理分隔符
  67. $search = ["{cpath}", "{cid}", "{tid}", "{page}", "{tpath}"];
  68. $replace = [
  69. PathHelper::getDir($catalogPath),
  70. $cid,
  71. $tid,
  72. $page,
  73. $tid && ifContain($rule, "{tpath}") ? self::get($tid)->tpath() : "",
  74. ];
  75. $rulePath = replaceDS(str_replace($search, $replace, $rule));
  76. if (ifContain($catalogPath, Templet::getRootDir())) {
  77. //生成操作时,处理路径不在根目录里的
  78. if (!ifContain($rulePath, Templet::getRootDir()))
  79. $rulePath = Templet::getRootDir() . deleteStartDS($rulePath);
  80. } else {
  81. //获取URI时,处理路径{aid}.html
  82. $rulePath = appendStartDS($rulePath);
  83. }
  84. return $rulePath;
  85. }
  86. /**
  87. * 获取分类uri
  88. * @param int $catalogId [分类设置了多个栏目时,默认选择首个栏目]
  89. * @return mixed
  90. * @throws \Exception
  91. */
  92. public function getUri($catalogId = 0)
  93. {
  94. $catalogs = $this->catalogs;
  95. if (!$catalogs) {
  96. throw new \Exception("分类{$this->id}找不到对应的栏目");
  97. }
  98. $catalog = $catalogs[0];
  99. if (count($catalogs) > 0) {
  100. foreach ($catalogs as $catalog_) {
  101. if ($catalog_->id == $catalogId)
  102. $catalog = $catalog_;
  103. }
  104. }
  105. return replaceUrlDS(self::getRulePath($catalog->getUri(), $catalog->articlelist_rule, $catalog->id, $this->id));
  106. }
  107. /**
  108. * 分类设置了多个栏目时,通过指定路径获取uri
  109. * @param string $path_pattern
  110. * @return mixed|string
  111. */
  112. public function getUriByPattern($path_pattern = "/m/")
  113. {
  114. $catalogs = $this->catalogs;
  115. foreach ($catalogs as $catalog) {
  116. if (ifContain($catalog->path, $path_pattern)) {
  117. return $this->getUri($catalog->id);
  118. }
  119. }
  120. return $this->getUri();
  121. }
  122. /**
  123. * 获取多个栏目属性集合
  124. */
  125. public function getCatalogValues($field = "title")
  126. {
  127. $catalogs = $this->catalogs;
  128. if ($catalogs) {
  129. $name = [];
  130. foreach ($catalogs as $catalog) {
  131. $name[] = $catalog->getData($field);
  132. }
  133. return implode(',', $name);
  134. }
  135. return "";
  136. }
  137. //获取一个栏目
  138. public function getFirstCatalog()
  139. {
  140. $catalogs = $this->catalogs;
  141. if (empty($catalogs)) {
  142. throw new \Exception("分类{$this->id}找不到对应的栏目");
  143. }
  144. //有多个栏目时,默认第一个栏目,排序最大的
  145. return $catalogs[0];
  146. }
  147. //关联文章
  148. public function articles()
  149. {
  150. return $this->hasMany('Article')->where('status', Article::STATUS_OPEN);
  151. }
  152. //关联栏目
  153. public function catalogs()
  154. {
  155. return $this->belongsToMany('Catalog', 'app\common\model\CateCatalog')->order("sort desc,id");
  156. }
  157. }