PortalPostModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * @return $this
  80. * @throws \think\Exception
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. * @throws \think\exception\PDOException
  85. */
  86. public function adminAddArticle($data)
  87. {
  88. $data['user_id'] = cmf_get_current_admin_id();
  89. if (!empty($data['more']['thumbnail'])) {
  90. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  91. $data['thumbnail'] = $data['more']['thumbnail'];
  92. }
  93. if (!empty($data['more']['audio'])) {
  94. $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
  95. }
  96. if (!empty($data['more']['video'])) {
  97. $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
  98. }
  99. $this->allowField(true)->data($data, true)->isUpdate(false)->save();
  100. return $this;
  101. }
  102. /**
  103. * 后台管理编辑文章
  104. * @param array $data 文章数据
  105. * @param array|string $categories 文章分类 id
  106. * @return $this
  107. * @throws \think\Exception
  108. */
  109. public function adminEditArticle($data)
  110. {
  111. unset($data['user_id']);
  112. if (!empty($data['more']['thumbnail'])) {
  113. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  114. $data['thumbnail'] = $data['more']['thumbnail'];
  115. }
  116. if (!empty($data['more']['audio'])) {
  117. $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
  118. }
  119. if (!empty($data['more']['video'])) {
  120. $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
  121. }
  122. $this->allowField(true)->isUpdate(true)->data($data, true)->save();
  123. return $this;
  124. }
  125. /**
  126. * 增加标签
  127. * @param $keywords
  128. * @param $articleId
  129. * @throws \think\Exception
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @throws \think\exception\DbException
  133. * @throws \think\exception\PDOException
  134. */
  135. public function addTags($keywords, $articleId)
  136. {
  137. $portalTagModel = new PortalTagModel();
  138. $tagIds = [];
  139. $data = [];
  140. if (!empty($keywords)) {
  141. $oldTagIds = Db::name('portal_tag_post')->where('post_id', $articleId)->column('tag_id');
  142. foreach ($keywords as $keyword) {
  143. $keyword = trim($keyword);
  144. if (!empty($keyword)) {
  145. $findTag = $portalTagModel->where('name', $keyword)->find();
  146. if (empty($findTag)) {
  147. $tagId = $portalTagModel->insertGetId([
  148. 'name' => $keyword
  149. ]);
  150. } else {
  151. $tagId = $findTag['id'];
  152. }
  153. if (!in_array($tagId, $oldTagIds)) {
  154. array_push($data, ['tag_id' => $tagId, 'post_id' => $articleId]);
  155. }
  156. array_push($tagIds, $tagId);
  157. }
  158. }
  159. if (empty($tagIds) && !empty($oldTagIds)) {
  160. Db::name('portal_tag_post')->where('post_id', $articleId)->delete();
  161. }
  162. $sameTagIds = array_intersect($oldTagIds, $tagIds);
  163. $shouldDeleteTagIds = array_diff($oldTagIds, $sameTagIds);
  164. if (!empty($shouldDeleteTagIds)) {
  165. Db::name('portal_tag_post')
  166. ->where('post_id', $articleId)
  167. ->where('tag_id', 'in', $shouldDeleteTagIds)
  168. ->delete();
  169. }
  170. if (!empty($data)) {
  171. Db::name('portal_tag_post')->insertAll($data);
  172. }
  173. } else {
  174. Db::name('portal_tag_post')->where('post_id', $articleId)->delete();
  175. }
  176. }
  177. /**
  178. * @param $data
  179. * @return bool
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\ModelNotFoundException
  182. * @throws \think\exception\DbException
  183. */
  184. public function adminDeletePage($data)
  185. {
  186. if (isset($data['id'])) {
  187. $id = $data['id']; //获取删除id
  188. $res = $this->where('id', $id)->find();
  189. if ($res) {
  190. $res = json_decode(json_encode($res), true); //转换为数组
  191. $recycleData = [
  192. 'object_id' => $res['id'],
  193. 'create_time' => time(),
  194. 'table_name' => 'portal_post#page',
  195. 'name' => $res['post_title'],
  196. ];
  197. Db::startTrans(); //开启事务
  198. $transStatus = false;
  199. try {
  200. Db::name('portal_post')->where('id', $id)->update([
  201. 'delete_time' => time()
  202. ]);
  203. Db::name('recycle_bin')->insert($recycleData);
  204. $transStatus = true;
  205. // 提交事务
  206. Db::commit();
  207. } catch (\Exception $e) {
  208. // 回滚事务
  209. Db::rollback();
  210. }
  211. return $transStatus;
  212. } else {
  213. return false;
  214. }
  215. } elseif (isset($data['ids'])) {
  216. $ids = $data['ids'];
  217. $res = $this->where('id', 'in', $ids)
  218. ->select();
  219. if ($res) {
  220. $res = json_decode(json_encode($res), true);
  221. foreach ($res as $key => $value) {
  222. $recycleData[$key]['object_id'] = $value['id'];
  223. $recycleData[$key]['create_time'] = time();
  224. $recycleData[$key]['table_name'] = 'portal_post';
  225. $recycleData[$key]['name'] = $value['post_title'];
  226. }
  227. Db::startTrans(); //开启事务
  228. $transStatus = false;
  229. try {
  230. Db::name('portal_post')->where('id', 'in', $ids)
  231. ->update([
  232. 'delete_time' => time()
  233. ]);
  234. Db::name('recycle_bin')->insertAll($recycleData);
  235. $transStatus = true;
  236. // 提交事务
  237. Db::commit();
  238. } catch (\Exception $e) {
  239. // 回滚事务
  240. Db::rollback();
  241. }
  242. return $transStatus;
  243. } else {
  244. return false;
  245. }
  246. } else {
  247. return false;
  248. }
  249. }
  250. /**
  251. * 后台管理添加页面
  252. * @param array $data 页面数据
  253. * @return $this
  254. */
  255. public function adminAddPage($data)
  256. {
  257. $data['user_id'] = cmf_get_current_admin_id();
  258. if (!empty($data['more']['thumbnail'])) {
  259. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  260. }
  261. $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  262. $data['post_type'] = 2;
  263. $this->allowField(true)->data($data, true)->save();
  264. return $this;
  265. }
  266. /**
  267. * 后台管理编辑页面
  268. * @param array $data 页面数据
  269. * @return $this
  270. */
  271. public function adminEditPage($data)
  272. {
  273. $data['user_id'] = cmf_get_current_admin_id();
  274. if (!empty($data['more']['thumbnail'])) {
  275. $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
  276. }
  277. $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  278. $data['post_type'] = 2;
  279. $this->allowField(true)->isUpdate(true)->data($data, true)->save();
  280. $routeModel = new RouteModel();
  281. $routeModel->setRoute($data['post_alias'], 'portal/Page/index', ['id' => $data['id']], 2, 5000);
  282. $routeModel->getRoutes(true);
  283. return $this;
  284. }
  285. }