PortalPostModel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\portal\model;
  12. use app\admin\model\RouteModel;
  13. use think\Model;
  14. /**
  15. * @property mixed id
  16. */
  17. class PortalPostModel extends Model
  18. {
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'portal_post';
  24. protected $type = [
  25. 'more' => 'array',
  26. ];
  27. // 开启自动写入时间戳字段
  28. protected $autoWriteTimestamp = true;
  29. /**
  30. * 关联 user表
  31. * @return \think\model\relation\BelongsTo
  32. */
  33. public function user()
  34. {
  35. return $this->belongsTo('UserModel', 'user_id');
  36. }
  37. /**
  38. * 关联分类表
  39. * @return \think\model\relation\BelongsToMany
  40. */
  41. public function categories()
  42. {
  43. return $this->belongsToMany('PortalCategoryModel', 'portal_category_post', 'category_id', 'post_id');
  44. }
  45. /**
  46. * 关联标签表
  47. * @return \think\model\relation\BelongsToMany
  48. */
  49. public function tags()
  50. {
  51. return $this->belongsToMany('PortalTagModel', 'portal_tag_post', 'tag_id', 'post_id');
  52. }
  53. /**
  54. * post_content 自动转化
  55. * @param $value
  56. * @return string
  57. */
  58. public function getPostContentAttr($value)
  59. {
  60. return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  61. }
  62. /**
  63. * post_content 自动转化
  64. * @param $value
  65. * @return string
  66. */
  67. public function setPostContentAttr($value)
  68. {
  69. return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
  70. }
  71. /**
  72. * published_time 自动完成
  73. * @param $value
  74. * @return false|int
  75. */
  76. public function setPublishedTimeAttr($value)
  77. {
  78. return strtotime($value);
  79. }
  80. /**
  81. * 后台管理添加文章
  82. * @param array $data 文章数据
  83. * @param array|string $categories 文章分类 id
  84. * @return $this
  85. * @throws \think\Exception
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. * @throws \think\exception\PDOException
  90. */
  91. public function adminAddArticle($data, $categories)
  92. {
  93. $data['user_id'] = cmf_get_current_admin_id();
  94. if (!empty($data['more']['thumbnail'])) {
  95. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  96. $data['thumbnail'] = $data['more']['thumbnail'];
  97. }
  98. if (!empty($data['more']['audio'])) {
  99. $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
  100. }
  101. if (!empty($data['more']['video'])) {
  102. $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
  103. }
  104. $this->save($data);
  105. if (is_string($categories)) {
  106. $categories = explode(',', $categories);
  107. }
  108. $this->categories()->save($categories);
  109. $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  110. $keywords = explode(',', $data['post_keywords']);
  111. $this->addTags($keywords, $this->id);
  112. return $this;
  113. }
  114. /**
  115. * 后台管理编辑文章
  116. * @param array $data 文章数据
  117. * @param array|string $categories 文章分类 id
  118. * @return $this
  119. * @throws \think\Exception
  120. */
  121. public function adminEditArticle($data, $categories)
  122. {
  123. unset($data['user_id']);
  124. if (!empty($data['more']['thumbnail'])) {
  125. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  126. $data['thumbnail'] = $data['more']['thumbnail'];
  127. }
  128. if (!empty($data['more']['audio'])) {
  129. $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
  130. }
  131. if (!empty($data['more']['video'])) {
  132. $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
  133. }
  134. unset($data['categories']);
  135. $article = self::find($data['id']);
  136. $article->save($data);
  137. if (is_string($categories)) {
  138. $categories = explode(',', $categories);
  139. }
  140. $oldCategoryIds = $article->categories()->column('category_id');
  141. $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  142. $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  143. $newCategoryIds = array_diff($categories, $sameCategoryIds);
  144. if (!empty($needDeleteCategoryIds)) {
  145. $article->categories()->detach($needDeleteCategoryIds);
  146. }
  147. if (!empty($newCategoryIds)) {
  148. $article->categories()->attach(array_values($newCategoryIds));
  149. }
  150. $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  151. $keywords = explode(',', $data['post_keywords']);
  152. $this->addTags($keywords, $data['id']);
  153. return $this;
  154. }
  155. /**
  156. * 增加标签
  157. * @param $keywords
  158. * @param $articleId
  159. * @throws \think\Exception
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. * @throws \think\exception\DbException
  163. * @throws \think\exception\PDOException
  164. */
  165. public function addTags($keywords, $articleId)
  166. {
  167. $portalTagModel = new PortalTagModel();
  168. $tagIds = [];
  169. $data = [];
  170. if (!empty($keywords)) {
  171. $oldTagIds = PortalTagPostModel::where('post_id', $articleId)->column('tag_id');
  172. foreach ($keywords as $keyword) {
  173. $keyword = trim($keyword);
  174. if (!empty($keyword)) {
  175. $findTag = $portalTagModel->where('name', $keyword)->find();
  176. if (empty($findTag)) {
  177. $tagId = $portalTagModel->insertGetId([
  178. 'name' => $keyword
  179. ]);
  180. } else {
  181. $tagId = $findTag['id'];
  182. }
  183. if (!in_array($tagId, $oldTagIds)) {
  184. array_push($data, ['tag_id' => $tagId, 'post_id' => $articleId]);
  185. }
  186. array_push($tagIds, $tagId);
  187. }
  188. }
  189. if (empty($tagIds) && !empty($oldTagIds)) {
  190. PortalTagPostModel::where('post_id', $articleId)->delete();
  191. }
  192. $sameTagIds = array_intersect($oldTagIds, $tagIds);
  193. $shouldDeleteTagIds = array_diff($oldTagIds, $sameTagIds);
  194. if (!empty($shouldDeleteTagIds)) {
  195. PortalTagPostModel::where('post_id', $articleId)
  196. ->where('tag_id', 'in', $shouldDeleteTagIds)
  197. ->delete();
  198. }
  199. if (!empty($data)) {
  200. PortalTagPostModel::insertAll($data);
  201. }
  202. } else {
  203. PortalTagPostModel::where('post_id', $articleId)->delete();
  204. }
  205. }
  206. /**
  207. * @param $data
  208. * @return bool
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. * @throws \think\exception\DbException
  212. */
  213. public function adminDeletePage($data)
  214. {
  215. if (isset($data['id'])) {
  216. $id = $data['id']; //获取删除id
  217. $res = $this->where('id', $id)->find();
  218. if ($res) {
  219. $res = json_decode(json_encode($res), true); //转换为数组
  220. $recycleData = [
  221. 'object_id' => $res['id'],
  222. 'create_time' => time(),
  223. 'table_name' => 'portal_post#page',
  224. 'name' => $res['post_title'],
  225. ];
  226. PortalPostModel::startTrans(); //开启事务
  227. $transStatus = false;
  228. try {
  229. PortalPostModel::where('id', $id)->update([
  230. 'delete_time' => time()
  231. ]);
  232. RecycleBinModel::insert($recycleData);
  233. $transStatus = true;
  234. // 提交事务
  235. PortalPostModel::commit();
  236. } catch (\Exception $e) {
  237. // 回滚事务
  238. PortalPostModel::rollback();
  239. }
  240. return $transStatus;
  241. } else {
  242. return false;
  243. }
  244. } elseif (isset($data['ids'])) {
  245. $ids = $data['ids'];
  246. $res = $this->where('id', 'in', $ids)
  247. ->select();
  248. if ($res) {
  249. $res = json_decode(json_encode($res), true);
  250. foreach ($res as $key => $value) {
  251. $recycleData[$key]['object_id'] = $value['id'];
  252. $recycleData[$key]['create_time'] = time();
  253. $recycleData[$key]['table_name'] = 'portal_post';
  254. $recycleData[$key]['name'] = $value['post_title'];
  255. }
  256. PortalPostModel::startTrans(); //开启事务
  257. $transStatus = false;
  258. try {
  259. PortalPostModel::where('id', 'in', $ids)
  260. ->update([
  261. 'delete_time' => time()
  262. ]);
  263. RecycleBinModel::insertAll($recycleData);
  264. $transStatus = true;
  265. // 提交事务
  266. PortalPostModel::commit();
  267. } catch (\Exception $e) {
  268. // 回滚事务
  269. PortalPostModel::rollback();
  270. }
  271. return $transStatus;
  272. } else {
  273. return false;
  274. }
  275. } else {
  276. return false;
  277. }
  278. }
  279. /**
  280. * 后台管理添加页面
  281. * @param array $data 页面数据
  282. * @return $this
  283. */
  284. public function adminAddPage($data)
  285. {
  286. $data['user_id'] = cmf_get_current_admin_id();
  287. if (!empty($data['more']['thumbnail'])) {
  288. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  289. }
  290. $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  291. $data['post_type'] = 2;
  292. $this->save($data);
  293. return $this;
  294. }
  295. /**
  296. * 后台管理编辑页面
  297. * @param array $data 页面数据
  298. * @return $this
  299. */
  300. public function adminEditPage($data)
  301. {
  302. $data['user_id'] = cmf_get_current_admin_id();
  303. if (!empty($data['more']['thumbnail'])) {
  304. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  305. }
  306. $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  307. $data['post_type'] = 2;
  308. $thisPage = PortalPostModel::find($data['id']);
  309. $thisPage->save($data);
  310. $routeModel = new RouteModel();
  311. $routeUrl = $data['post_alias'] ? trim($data['post_alias'], '$') . '$' : '';
  312. $routeModel->setRoute($routeUrl, 'portal/Page/index', ['id' => $data['id']], 2, 5000);
  313. $routeModel->getRoutes(true);
  314. return $this;
  315. }
  316. }