PortalPostModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\logic;
  10. use api\portal\model\PortalPostModel as PortalPost;
  11. use think\Db;
  12. class PortalPostModel extends PortalPost
  13. {
  14. /**
  15. * 获取相关文章
  16. * @param int|string|array $postIds 文章id
  17. * @return array
  18. */
  19. public function getRelationPosts($postIds)
  20. {
  21. $posts = $this->with('articleUser')
  22. ->field('id,post_title,user_id,is_top,post_hits,post_like,comment_count,more')
  23. ->whereIn('id', $postIds)
  24. ->select();
  25. foreach ($posts as $post) {
  26. $post->appendRelationAttr('articleUser', 'user_nickname');
  27. }
  28. return $posts;
  29. }
  30. /**
  31. * 获取用户文章
  32. */
  33. public function getUserArticles($userId, $params)
  34. {
  35. $where = [
  36. 'post_type' => 1,
  37. 'user_id' => $userId
  38. ];
  39. if (!empty($params)) {
  40. $this->paramsFilter($params);
  41. }
  42. return $this->where($where)->select();
  43. }
  44. /**
  45. * 会员添加文章
  46. * @param array $data 文章数据
  47. * @return $this
  48. */
  49. public function addArticle($data)
  50. {
  51. //设置图片附件,写入字段过滤
  52. $dataField = $this->setMoreField($data);
  53. $data = $dataField[0];
  54. array_push($dataField[1],'user_id');
  55. $this->readonly = array_diff(['user_id'],$this->readonly);
  56. $this->allowField($dataField[1])->data($data, true)->isUpdate(false)->save();
  57. $categories = $this->strToArr($data['categories']);
  58. $this->categories()->attach($categories);
  59. if (!empty($data['post_keywords']) && is_string($data['post_keywords'])) {
  60. //加入标签
  61. $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  62. $keywords = explode(',', $data['post_keywords']);
  63. $this->addTags($keywords, $this->id);
  64. }
  65. return $this;
  66. }
  67. /**
  68. * 会员文章编辑
  69. * @param array $data 文章数据
  70. * @param int $id 文章id
  71. * @param int $userId 文章所属用户id [可选]
  72. * @return boolean 成功 true 失败 false
  73. */
  74. public function editArticle($data, $id, $userId = '')
  75. {
  76. if (!empty($userId)) {
  77. $isBelong = $this->isuserPost($id, $userId);
  78. if ($isBelong === false) {
  79. return $isBelong;
  80. }
  81. }
  82. //设置图片附件,写入字段过滤
  83. $dataField = $this->setMoreField($data);
  84. $data = $dataField[0];
  85. $data['id'] = $id;
  86. $this->allowField($dataField[1])->data($data, true)->isUpdate(true)->save();
  87. $categories = $this->strToArr($data['categories']);
  88. $oldCategoryIds = $this->categories()->column('category_id');
  89. $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  90. $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  91. $newCategoryIds = array_diff($categories, $sameCategoryIds);
  92. if (!empty($needDeleteCategoryIds)) {
  93. $this->categories()->detach($needDeleteCategoryIds);
  94. }
  95. if (!empty($newCategoryIds)) {
  96. $this->categories()->attach(array_values($newCategoryIds));
  97. }
  98. if (!isset($data['post_keywords'])) {
  99. $keywords = [];
  100. } elseif (is_string($data['post_keywords'])) {
  101. //加入标签
  102. $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  103. $keywords = explode(',', $data['post_keywords']);
  104. }
  105. $this->addTags($keywords, $data['id']);
  106. return $this;
  107. }
  108. /**
  109. * 根据文章关键字,增加标签
  110. * @param array $keywords 文章关键字数组
  111. * @param int $articleId 文章id
  112. * @return void
  113. */
  114. public function addTags($keywords, $articleId)
  115. {
  116. foreach ($keywords as $key => $value) {
  117. $keywords[$key] = trim($value);
  118. }
  119. $continue = true;
  120. $names = $this->tags()->column('name');
  121. if (!empty($keywords) || !empty($names)) {
  122. if (!empty($names)) {
  123. $sameNames = array_intersect($keywords, $names);
  124. $keywords = array_diff($keywords, $sameNames);
  125. $shouldDeleteNames = array_diff($names, $sameNames);
  126. if (!empty($shouldDeleteNames)) {
  127. $tagIdNames = $this->tags()
  128. ->where('name', 'in', $shouldDeleteNames)
  129. ->column('pivot.id', 'tag_id');
  130. $tagIds = array_keys($tagIdNames);
  131. $tagPostIds = array_values($tagIdNames);
  132. $tagPosts = DB::name('portal_tag_post')->where('tag_id', 'in', $tagIds)
  133. ->field('id,tag_id,post_id')
  134. ->select();
  135. $keepTagIds = [];
  136. foreach ($tagPosts as $key => $tagPost) {
  137. if ($articleId != $tagPost['post_id']) {
  138. array_push($keepTagIds, $tagPost['tag_id']);
  139. }
  140. }
  141. $keepTagIds = array_unique($keepTagIds);
  142. $shouldDeleteTagIds = array_diff($tagIds, $keepTagIds);
  143. DB::name('PortalTag')->delete($shouldDeleteTagIds);
  144. DB::name('PortalTagPost')->delete($tagPostIds);
  145. }
  146. } else {
  147. $tagIdNames = DB::name('portal_tag')->where('name', 'in', $keywords)->column('name', 'id');
  148. if (!empty($tagIdNames)) {
  149. $tagIds = array_keys($tagIdNames);
  150. $this->tags()->attach($tagIds);
  151. $keywords = array_diff($keywords, array_values($tagIdNames));
  152. if (empty($keywords)) {
  153. $continue = false;
  154. }
  155. }
  156. }
  157. if ($continue) {
  158. foreach ($keywords as $key => $value) {
  159. if (!empty($value)) {
  160. $this->tags()->attach(['name' => $value]);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * 设置缩略图,图片,附件
  168. * 懒人方法
  169. * @param $data 表单数据
  170. */
  171. public function setMoreField($data)
  172. {
  173. $allowField = [
  174. 'post_title','post_keywords','post_source',
  175. 'post_excerpt','post_content','more',
  176. 'published_time'
  177. ];
  178. if (!empty($data['more'])) {
  179. $data['more'] = $this->setMoreUrl($data['more']);
  180. }
  181. if (!empty($data['thumbnail'])) {
  182. $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  183. }
  184. return [$data,$allowField];
  185. }
  186. /**
  187. * 获取图片附件url相对地址
  188. * 默认上传名字 *_names 地址 *_urls
  189. * @param $annex 上传附件
  190. * @return array
  191. */
  192. public function setMoreUrl($annex)
  193. {
  194. $more = [];
  195. if (!empty($annex)) {
  196. foreach ($annex as $key => $value) {
  197. $nameArr = $key . '_names';
  198. $urlArr = $key . '_urls';
  199. if (is_string($value[$nameArr]) && is_string($value[$urlArr])) {
  200. $more[$key] = [$value[$nameArr], $value[$urlArr]];
  201. } elseif (!empty($value[$nameArr]) && !empty($value[$urlArr])) {
  202. $more[$key] = [];
  203. foreach ($value[$urlArr] as $k => $url) {
  204. $url = cmf_asset_relative_url($url);
  205. array_push($more[$key], ['url' => $url, 'name' => $value[$nameArr][$k]]);
  206. }
  207. }
  208. }
  209. }
  210. return $more;
  211. }
  212. /**
  213. * 删除文章
  214. * @param $ids int|array 文章id
  215. * @param int $userId 文章所属用户id [可选]
  216. * @return bool|int 删除结果 true 成功 false 失败 -1 文章不存在
  217. */
  218. public function deleteArticle($ids, $userId)
  219. {
  220. $time = time();
  221. $result = false;
  222. $where = [];
  223. if (!empty($userId)) {
  224. if (is_numeric($ids)) {
  225. $article = $this->find($ids);
  226. if (!empty($article)) {
  227. if ($this->isUserPost($ids, $userId) || $userId == 1) {
  228. $where['id'] = $ids;
  229. }
  230. }
  231. } else {
  232. $ids = $this->strToArr($ids);
  233. $articles = $this->where('id', 'in', $ids)->select();
  234. if (!empty($articles)) {
  235. $deleteIds = $this->isUserPosts($ids, $userId);
  236. if (!empty($deleteIds)) {
  237. $where['id'] = ['in', $deleteIds];
  238. }
  239. }
  240. }
  241. } else {
  242. if (is_numeric($ids)) {
  243. $article = $this->find($ids);
  244. if (!empty($article)) {
  245. $where['id'] = $ids;
  246. }
  247. } else {
  248. $ids = $this->strToArr($ids);
  249. $articles = $this->where('id', 'in', $ids)->select();
  250. if (!empty($articles)) {
  251. $where['id'] = ['in', $ids];
  252. }
  253. }
  254. }
  255. if (empty($article) && empty($articles)) {
  256. return -1;
  257. }
  258. if (!empty($where)) {
  259. $result = $this->useGlobalScope(false)
  260. ->where($where)
  261. ->setField('delete_time', $time);
  262. }
  263. if ($result) {
  264. $data = [
  265. 'create_time' => $time,
  266. 'table_name' => 'portal_post'
  267. ];
  268. if (!empty($article)) {
  269. $data['name'] = $article['post_title'];
  270. $article->recycleBin()->save($data);
  271. }
  272. if (!empty($articles)) {
  273. foreach ($articles as $article) {
  274. $data['name'] = $article['post_title'];
  275. $article->recycleBin()->save($data);
  276. }
  277. }
  278. }
  279. return $result;
  280. }
  281. /**
  282. * 判断文章所属用户是否为当前用户,超级管理员除外
  283. * @params int $id 文章id
  284. * @param int $userId 当前用户id
  285. * @return boolean 是 true , 否 false
  286. */
  287. public function isUserPost($id, $userId)
  288. {
  289. $postUserId = $this->useGlobalScope(false)
  290. ->getFieldById($id, 'user_id');
  291. if ($postUserId != $userId || $userId != 1) {
  292. return false;
  293. } else {
  294. return true;
  295. }
  296. }
  297. /**
  298. * 过滤属于当前用户的文章,超级管理员除外
  299. * @params array $ids 文章id的数组
  300. * @param int $userId 当前用户id
  301. * @return array 属于当前用户的文章id
  302. */
  303. public function isUserPosts($ids, $userId)
  304. {
  305. $postIds = $this->useGlobalScope(false)
  306. ->where('user_id', $userId)
  307. ->where('id', 'in', $ids)
  308. ->column('id');
  309. return array_intersect($ids, $postIds);
  310. }
  311. }