123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 小夏 < 449134904@qq.com>
- // +----------------------------------------------------------------------
- namespace app\portal\model;
- use app\admin\model\RouteModel;
- use think\Model;
- use think\Db;
- /**
- * @property mixed id
- */
- class PortalPostModel extends Model
- {
- protected $type = [
- 'more' => 'array',
- ];
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- /**
- * 关联 user表
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
- }
- /**
- * 关联分类表
- * @return \think\model\relation\BelongsToMany
- */
- public function categories()
- {
- return $this->belongsToMany('PortalCategoryModel', 'portal_category_post', 'category_id', 'post_id');
- }
- /**
- * 关联标签表
- * @return \think\model\relation\BelongsToMany
- */
- public function tags()
- {
- return $this->belongsToMany('PortalTagModel', 'portal_tag_post', 'tag_id', 'post_id');
- }
- /**
- * post_content 自动转化
- * @param $value
- * @return string
- */
- public function getPostContentAttr($value)
- {
- return cmf_replace_content_file_url(htmlspecialchars_decode($value));
- }
- /**
- * post_content 自动转化
- * @param $value
- * @return string
- */
- public function setPostContentAttr($value)
- {
- return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
- }
- /**
- * published_time 自动完成
- * @param $value
- * @return false|int
- */
- public function setPublishedTimeAttr($value)
- {
- return strtotime($value);
- }
- /**
- * 后台管理添加文章
- * @param array $data 文章数据
- * @param array|string $categories 文章分类 id
- * @return $this
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- 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;
- }
- /**
- * 后台管理编辑文章
- * @param array $data 文章数据
- * @param array|string $categories 文章分类 id
- * @return $this
- * @throws \think\Exception
- */
- 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;
- }
- /**
- * 增加标签
- * @param $keywords
- * @param $articleId
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- 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();
- }
- }
- /**
- * @param $data
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function adminDeletePage($data)
- {
- if (isset($data['id'])) {
- $id = $data['id']; //获取删除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();
-
- // Db::name('portal_post')->where('id', $id)->update([
- // 'delete_time' => time()
- // ]);
- // Db::name('recycle_bin')->insert($recycleData);
- $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();
-
- // Db::name('portal_post')->where('id', 'in', $ids)
- // ->update([
- // 'delete_time' => time()
- // ]);
- // Db::name('recycle_bin')->insertAll($recycleData);
- $transStatus = true;
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return $transStatus;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /**
- * 后台管理添加页面
- * @param array $data 页面数据
- * @return $this
- */
- 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;
- /*var_dump($data);
- die;*/
- $this->allowField(true)->data($data, true)->save();
- return $this;
- }
- /**
- * 后台管理编辑页面
- * @param array $data 页面数据
- * @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;
- }
- }
|