123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- <?php
- namespace app\portal\model;
- use app\admin\model\RouteModel;
- use think\Model;
- use think\Db;
- class PortalPostModel extends Model
- {
- protected $type = [
- 'more' => 'array',
- ];
-
- protected $autoWriteTimestamp = true;
-
- public function user()
- {
- return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
- }
-
- public function categories()
- {
- return $this->belongsToMany('PortalCategoryModel', 'portal_category_post', 'category_id', 'post_id');
- }
-
- public function tags()
- {
- return $this->belongsToMany('PortalTagModel', 'portal_tag_post', 'tag_id', 'post_id');
- }
-
- public function getPostContentAttr($value)
- {
- return cmf_replace_content_file_url(htmlspecialchars_decode($value));
- }
-
- public function setPostContentAttr($value)
- {
- return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
- }
-
- public function setPublishedTimeAttr($value)
- {
- return strtotime($value);
- }
-
- public function adminAddArticle($data, $categories)
- {
- $data['user_id'] = cmf_get_current_admin_id();
- if (!empty($data['more']['thumbnail'])) {
- $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
- $data['thumbnail'] = $data['more']['thumbnail'];
- }
- if (!empty($data['more']['audio'])) {
- $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
- }
- if (!empty($data['more']['video'])) {
- $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
- }
- $this->allowField(true)->data($data, true)->isUpdate(false)->save();
- if (is_string($categories)) {
- $categories = explode(',', $categories);
- }
- $this->categories()->save($categories);
- $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
- $keywords = explode(',', $data['post_keywords']);
- $this->addTags($keywords, $this->id);
- return $this;
- }
-
- public function adminEditArticle($data, $categories)
- {
- unset($data['user_id']);
- if (!empty($data['more']['thumbnail'])) {
- $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
- $data['thumbnail'] = $data['more']['thumbnail'];
- }
- if (!empty($data['more']['audio'])) {
- $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
- }
- if (!empty($data['more']['video'])) {
- $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
- }
- $this->allowField(true)->isUpdate(true)->data($data, true)->save();
- if (is_string($categories)) {
- $categories = explode(',', $categories);
- }
- $oldCategoryIds = $this->categories()->column('category_id');
- $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
- $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
- $newCategoryIds = array_diff($categories, $sameCategoryIds);
- if (!empty($needDeleteCategoryIds)) {
- $this->categories()->detach($needDeleteCategoryIds);
- }
- if (!empty($newCategoryIds)) {
- $this->categories()->attach(array_values($newCategoryIds));
- }
- $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
- $keywords = explode(',', $data['post_keywords']);
- $this->addTags($keywords, $data['id']);
- return $this;
- }
-
- public function addTags($keywords, $articleId)
- {
- $portalTagModel = new PortalTagModel();
- $tagIds = [];
- $data = [];
- if (!empty($keywords)) {
- $oldTagIds = Db::name('portal_tag_post')->where('post_id', $articleId)->column('tag_id');
- foreach ($keywords as $keyword) {
- $keyword = trim($keyword);
- if (!empty($keyword)) {
- $findTag = $portalTagModel->where('name', $keyword)->find();
- if (empty($findTag)) {
- $tagId = $portalTagModel->insertGetId([
- 'name' => $keyword
- ]);
- } else {
- $tagId = $findTag['id'];
- }
- if (!in_array($tagId, $oldTagIds)) {
- array_push($data, ['tag_id' => $tagId, 'post_id' => $articleId]);
- }
- array_push($tagIds, $tagId);
- }
- }
- if (empty($tagIds) && !empty($oldTagIds)) {
- Db::name('portal_tag_post')->where('post_id', $articleId)->delete();
- }
- $sameTagIds = array_intersect($oldTagIds, $tagIds);
- $shouldDeleteTagIds = array_diff($oldTagIds, $sameTagIds);
- if (!empty($shouldDeleteTagIds)) {
- Db::name('portal_tag_post')
- ->where('post_id', $articleId)
- ->where('tag_id', 'in', $shouldDeleteTagIds)
- ->delete();
- }
- if (!empty($data)) {
- Db::name('portal_tag_post')->insertAll($data);
- }
- } else {
- Db::name('portal_tag_post')->where('post_id', $articleId)->delete();
- }
- }
-
- public function adminDeletePage($data)
- {
- if (isset($data['id'])) {
- $id = $data['id'];
- $res = $this->where('id', $id)->find();
- if ($res) {
- $res = json_decode(json_encode($res), true);
- $recycleData = [
- 'object_id' => $res['id'],
- 'create_time' => time(),
- 'table_name' => 'portal_post#page',
- 'name' => $res['post_title'],
- ];
- Db::startTrans();
- $transStatus = false;
- try {
- Db::name('portal_post')->where('id', $id)->delete();
-
-
-
-
-
- $transStatus = true;
-
- Db::commit();
- } catch (\Exception $e) {
-
- Db::rollback();
- }
- return $transStatus;
- } else {
- return false;
- }
- } elseif (isset($data['ids'])) {
- $ids = $data['ids'];
- $res = $this->where('id', 'in', $ids)
- ->select();
- if ($res) {
- $res = json_decode(json_encode($res), true);
- foreach ($res as $key => $value) {
- $recycleData[$key]['object_id'] = $value['id'];
- $recycleData[$key]['create_time'] = time();
- $recycleData[$key]['table_name'] = 'portal_post';
- $recycleData[$key]['name'] = $value['post_title'];
- }
- Db::startTrans();
- $transStatus = false;
- try {
- Db::name('portal_post')->where('id', 'in', $ids)->delete();
-
-
-
-
-
-
- $transStatus = true;
-
- Db::commit();
- } catch (\Exception $e) {
-
- Db::rollback();
- }
- return $transStatus;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
-
- public function adminAddPage($data){
- $data['user_id'] = cmf_get_current_admin_id();
- if (!empty($data['more']['thumbnail'])) {
- $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
- }
- $data['post_status'] = empty($data['post_status']) ? 0 : 1;
- $data['post_type'] = 2;
-
- $this->allowField(true)->data($data, true)->save();
- return $this;
- }
-
- public function adminEditPage($data)
- {
- $data['user_id'] = cmf_get_current_admin_id();
- if (!empty($data['more']['thumbnail'])) {
- $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
- }
- $data['post_status'] = empty($data['post_status']) ? 0 : 1;
- $data['post_type'] = 2;
- $this->allowField(true)->isUpdate(true)->data($data, true)->save();
- $routeModel = new RouteModel();
- $routeModel->setRoute($data['post_alias'], 'portal/Page/index', ['id' => $data['id']], 2, 5000);
- $routeModel->getRoutes(true);
- return $this;
- }
- }
|