Article.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. use think\Exception;
  11. use think\Model;
  12. class Article extends Model
  13. {
  14. const STATUS_OPEN = 1;
  15. const STATUS_CLOSE = 0;
  16. //description
  17. public function getDescriptionAttr($value, $data)
  18. {
  19. if (empty($data['seo_description'])) {
  20. //尝试从文章内容提取描述,去掉HTML及PHP标记
  21. return strip_tags(subStrCN($data['content'], 200));
  22. }
  23. return $data['seo_description'];
  24. }
  25. //get_seo_title
  26. public function getGetSeoTitleAttr($value, $data)
  27. {
  28. if (empty($data['seo_title'])) {
  29. return $data['title'];
  30. }
  31. return $data['seo_title'];
  32. }
  33. //content
  34. public function getContentAttr($value, $data)
  35. {
  36. if ($this->article_editor == 'markdown') {
  37. return $data['content2'];
  38. }
  39. return $value;
  40. }
  41. /**
  42. * 获取缩略图,尝试从内容中提取
  43. * @return bool|mixed|string
  44. */
  45. public function getThumb()
  46. {
  47. if ($this->thumb) {
  48. return geturl($this->thumb);
  49. } else {
  50. if (empty($this->content)) {
  51. return "";
  52. }
  53. $imgs = matchImg($this->content);
  54. return count($imgs) == 0 ? "" : $imgs[0];
  55. }
  56. }
  57. /**
  58. * 按规则生成路径
  59. * @param $catalogPath [uri使用相对路径,生成文件用物理路径]
  60. * @param $rule [文章路径规则]
  61. * @return mixed
  62. * @throws \Exception
  63. */
  64. public function getRulePath($catalogPath, $rule)
  65. {
  66. if (empty($rule)) {
  67. throw new Exception("null articleRule error");
  68. }
  69. $rule = replaceDS($rule);//处理分隔符
  70. $time = $this->getData('create_time');
  71. $search = ["{cpath}", "{Y}", "{M}", "{D}", "{aid}", "{cid}", "{tid}", "{tpath}"];
  72. $replace = [
  73. PathHelper::getDir($catalogPath),
  74. date('Y', $time),
  75. date('m', $time),
  76. date('d', $time),
  77. $this->filename ?: $this->id,
  78. $this->catalog_id,
  79. $this->article_cate_id,
  80. $this->article_cate_id && $this->cate ? $this->cate->tpath() : "",
  81. ];
  82. $rulePath = replaceDS(str_replace($search, $replace, $rule));
  83. if (ifContain($catalogPath, Templet::getRootDir())) {
  84. //生成操作时,处理路径不在根目录里的
  85. if (!ifContain($rulePath, Templet::getRootDir()))
  86. $rulePath = Templet::getRootDir() . deleteStartDS($rulePath);
  87. } else {
  88. //获取URI时,处理路径{aid}.html
  89. $rulePath = appendStartDS($rulePath);
  90. }
  91. return $rulePath;
  92. }
  93. /**
  94. * 获取文章uri
  95. * @param int $catalogId [文章设置了分类,且分类设置了多个栏目时,可指定栏目Id]
  96. * @return mixed|string
  97. * @throws \Exception
  98. */
  99. public function getUri($catalogId = 0)
  100. {
  101. if ($this->article_cate_id && $this->catalog_id) {
  102. throw new \Exception("文章不能同时发布到栏目和分类");
  103. }
  104. if ($this->article_cate_id) {
  105. if (empty($this->cate) || empty($this->cate->catalogs)) {
  106. throw new \Exception("文章{$this->id}找不到对应的栏目");
  107. }
  108. $catalogs = $this->cate->catalogs;
  109. //有多个栏目时,默认第一个栏目,排序最大的
  110. $catalog = $catalogs[0];
  111. //如果有指定 $catalogId ,则获取对应的栏目
  112. if ($catalogId && count($catalogs) > 0) {
  113. foreach ($catalogs as $catalog_) {
  114. if ($catalog_->id == $catalogId)
  115. $catalog = $catalog_;
  116. }
  117. }
  118. } elseif ($this->catalog_id) {
  119. $catalog = $this->catalog;
  120. } else {
  121. return "";
  122. }
  123. return replaceUrlDS($this->getRulePath($catalog->path, $catalog->article_rule));
  124. }
  125. /**
  126. * 当文章发布到多个栏目时,通过匹配路径获取文章uri
  127. * @param string $path_pattern
  128. * @return mixed|string
  129. */
  130. public function getUriByPattern($path_pattern = "/m/")
  131. {
  132. if ($this->article_cate_id) {
  133. $catalogs = $this->cate->catalogs;
  134. foreach ($catalogs as $catalog) {
  135. if (ifContain($catalog->path, $path_pattern)) {
  136. return $this->getUri($catalog->id);
  137. }
  138. }
  139. }
  140. return $this->getUri();
  141. }
  142. /**
  143. * 标签
  144. * @return array 数组
  145. */
  146. public function getTags()
  147. {
  148. if (empty($this->tag)) {
  149. return [];
  150. }
  151. return explode(',', $this->tag);
  152. }
  153. /**
  154. * 上一篇
  155. */
  156. public function last()
  157. {
  158. $where = [
  159. 'status' => self::STATUS_OPEN,
  160. 'article_cate_id' => $this->article_cate_id,
  161. 'catalog_id' => $this->catalog_id
  162. ];
  163. return $this->where("id", "<", $this->id)->where($where)->order('id desc')->find();
  164. }
  165. /**
  166. * 下一篇
  167. */
  168. public function next()
  169. {
  170. $where = [
  171. 'status' => self::STATUS_OPEN,
  172. 'article_cate_id' => $this->article_cate_id,
  173. 'catalog_id' => $this->catalog_id
  174. ];
  175. return $this->where("id", ">", $this->id)->where($where)->order('id asc')->find();
  176. }
  177. /**
  178. * 获取多个栏目属性集合
  179. */
  180. public function getCatalogValues($field = "title")
  181. {
  182. $catalogs = $this->cate->catalogs;
  183. if ($catalogs) {
  184. $name = [];
  185. foreach ($catalogs as $catalog) {
  186. $name[] = $catalog->getData($field);
  187. }
  188. return implode(',', $name);
  189. }
  190. return "";
  191. }
  192. //关联多栏目(通过分类发布到栏目时)
  193. public function catalogs()
  194. {
  195. if (empty($this->article_cate_id) && empty($this->catalog_id)) {
  196. throw new Exception("请先关联栏目或分类");
  197. }
  198. if (!$this->cate) {
  199. throw new Exception("文章{$this->id}找不到对应的分类");
  200. }
  201. if (!$this->cate->catalogs) {
  202. throw new Exception("文章{$this->id}找不到对应的栏目");
  203. }
  204. return $this->cate->catalogs;
  205. }
  206. //获取一个栏目(多栏目时)
  207. public function getFirstCatalog()
  208. {
  209. if (!$this->cate) {
  210. throw new Exception("文章{$this->id}找不到对应的分类");
  211. }
  212. $catalogs = $this->cate->catalogs;
  213. if (empty($catalogs)) {
  214. throw new Exception("文章{$this->id}找不到对应的栏目");
  215. }
  216. //有多个栏目时,默认第一个栏目,排序最大的
  217. return $catalogs[0];
  218. }
  219. //关联栏目(直接发布到栏目时)
  220. public function catalog()
  221. {
  222. return $this->belongsTo('Catalog');
  223. }
  224. //关联分类
  225. public function cate()
  226. {
  227. return $this->belongsTo('ArticleCate');
  228. }
  229. //发布人
  230. public function admin()
  231. {
  232. return $this->belongsTo('Admin');
  233. }
  234. //修改人
  235. public function editor()
  236. {
  237. return $this->belongsTo('Admin', 'edit_admin_id');
  238. }
  239. }