PortalPostModel.php 12 KB

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