/** * 将文章插入数据库 * @param $title * @param $content * @param $publish_at * @param $tag * @return bool */ public static function insert($title, $content, $publish_at, $tag = '') { //插入标签(搜索的分类) $article = new Article(); $article->title = $title; $article->content = $content; $article->author = 'yang'; $article->status = Article::STATUS_GATHER; $article->publish_at = $publish_at; $res = $article->save(false); if ($tag) { try { $tagModel = Tag::find()->where(['name' => $tag])->one(); if (!$tagModel) { $tagModel = new Tag(); $tagModel->name = $tag; $tagModel->article_count = 0; $tagModel->save(false); } $articleTag = new ArticleTag(); $articleTag->article_id = $article->id; $articleTag->tag_id = $tagModel->id; $articleTag->save(false); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; } } return $res ? true : false; }
/** * 将指定文章的所有标签文章数加1 * @param $article_id * @return bool */ public static function updateTagArticleCounts($article_id) { $articleTag = ArticleTag::find()->where(['article_id' => $article_id])->all(); if (!$articleTag) { return false; } foreach ($articleTag as $v) { Tag::updateAllCounters(['article_count' => 1], ['id' => $v->tag_id]); } return true; }
/** * 获取文章列表(可以按分类、标签获取文章列表) * @return string */ public function actionIndex() { $category_id = Html::encode(Yii::$app->request->get('category_id')); $tag_id = Html::encode(Yii::$app->request->get('tag_id')); $data = Article::find()->where(['status' => Article::STATUS_DISPLAY, 'category_id' => $category_id]); if ($tag_id) { $article_tags = ArticleTag::find()->where(['tag_id' => $tag_id])->all(); $article_ids = ArrayHelper::getColumn($article_tags, 'article_id'); $data = $data->andWhere(['in', 'id', $article_ids]); } $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 20]); //全部文章 $articles = $data->orderBy(['publish_at' => SORT_DESC])->offset($pages->offset)->limit($pages->limit)->all(); //推荐文章 $recommendArticles = Article::getRecommendArticle(); //分类 $categories = Category::find()->where(['status' => Category::STATUS_DISPLAY])->all(); //热门标签 $tags = Tag::find()->orderBy(['article_count' => SORT_DESC, 'id' => SORT_DESC])->limit(10)->all(); return $this->render('index', ['articles' => $articles, 'recommendArticles' => $recommendArticles, 'categories' => $categories, 'tags' => $tags, 'category_id' => $category_id, 'tag_id' => $tag_id]); }
/** * 批量发布文章 * @return string|\yii\web\Response */ public function actionPublish() { if (Yii::$app->request->isPost) { $ids = Yii::$app->request->post('ids'); $url = Yii::$app->request->post('url') ?: '/article/gather'; if ($ids) { $num = 0; foreach ($ids as $id) { $article = Article::find()->where(['in', 'status', [Article::STATUS_GATHER, Article::STATUS_DISPLAY]])->andWhere(['id' => $id])->one(); if (!$article) { continue; } $article->status = Article::STATUS_DISPLAY; if ($article->save(false)) { $num++; } //更改文章标签数量 ArticleTag::updateTagArticleCounts($id); } Yii::$app->session->setFlash('success', '成功发布文章【' . $num . '】篇'); return $this->redirect($url); } else { Yii::$app->session->setFlash('error', '文章不能为空,请选择文章'); return $this->redirect($url); } } else { $searchModel = new ArticleGatherSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('publish', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); } }
/** * 新建文章 * @return bool * @throws Exception */ public function saveArticle() { if (!$this->validate()) { return false; } if (!$this->tag) { return $this->save(); } $res = $this->save(); try { $tags = explode(';', $this->tag); foreach ($tags as $v) { if (!$v) { continue; } $tag = Tag::findOne(['name' => $v]); if (!$tag) { $tag = new Tag(); $tag->name = $v; $tag->article_count = 0; $res = $tag->save(); $id = $tag->id; } else { $id = $tag->id; } $article_tag = new ArticleTag(); $article_tag->article_id = $this->id; $article_tag->tag_id = $id; $article_tag->save(); //更前标签的文章数量 $tag->article_count++; $tag->save(false); } } catch (Exception $e) { throw new Exception($e->getMessage()); } return $res; }