/** * 将文章插入数据库 * @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; }
public static function addTags($tags) { self::updateAllCounters(['frequency' => 1], ['in', 'name', $tags]); foreach ($tags as $name) { if (!self::find()->where(['name' => $name])->exists()) { $tag = new Tag(); $tag->name = $name; $tag->frequency = 1; $tag->save(); } } }
public function actionIndex($slug) { $query = Vidmage::find()->joinWith('vidmageTags.tag')->where(['tag.name' => $slug])->orderBy(['id' => SORT_DESC]); $vidmages = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); $tag = Tag::findOne(['name' => $slug]); return $this->render('index', compact('vidmages', 'tag')); }
public function actionIndex($id) { $user = User::findIdentity($id); $comments = Comment::getRecentComments(); $sidebarCategories = Category::getSidebarCategories(); $tags = Tag::getTagList(); return $this->render('index', ['user' => $user, 'comments' => $comments, 'sidebarCategories' => $sidebarCategories, 'tags' => $tags]); }
/** * Finds the Meta model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Tag the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Tag::find()->andWhere('mid=:mid', [':mid' => $id])->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Displays homepage. * * @return mixed */ public function actionIndex() { $query = Page::find()->where(['live' => 1]); $pagination = new Pagination(['defaultPageSize' => 2, 'totalCount' => $query->count()]); $pages = $query->offset($pagination->offset)->limit($pagination->limit)->all(); $comments = Comment::getRecentComments(); $sidebarCategories = Category::getSidebarCategories(); $tags = Tag::getTagList(); return $this->render('index', ['pages' => $pages, 'pagination' => $pagination, 'comments' => $comments, 'sidebarCategories' => $sidebarCategories, 'tags' => $tags]); }
public function init() { parent::init(); $tags = Tag::find()->orderByCount()->all(); if (!empty($tags)) { foreach ($tags as $v) { $this->_htmlStr .= Html::a($v->name, ['site/tag', 'slug' => $v->slug], $this->options); } } }
public function actionTag($slug) { $tag = Tag::find()->andWhere(['slug' => $slug])->one(); if (!$tag) { throw new NotFoundHttpException('页面不存在'); } $pagination = new Pagination(['totalCount' => $tag->getPosts()->count()]); $posts = $tag->getPosts()->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('list', ['posts' => $posts, 'pagination' => $pagination, 'tag' => $tag]); }
/** * Lists the Article models in a specific category $slug. * * @param $slug * @return mixed */ public function actionTag($slug) { $model = Tag::find()->andWhere(['slug' => $slug])->one(); if (!$model) { throw new NotFoundHttpException(Yii::t('frontend', 'Page not found.')); } $query = Article::find()->with('category')->joinWith('tags')->where('{{%tag}}.slug = :slug', [':slug' => $slug])->published(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10]]); $dataProvider->sort = ['defaultOrder' => ['created_at' => SORT_DESC]]; return $this->render('tag', ['model' => $model, 'dataProvider' => $dataProvider, 'menuItems' => self::getMenuItems()]); }
public function actionIndex() { $lastPosts = Post::find()->orderBy('created_at desc')->limit(10)->all(); $featured = Post::featured(); $drafts = Post::drafts(); $mostPopularCategory = Category::find()->joinWith('posts')->select('category.id, post.category_id, count(distinct post.title) as qty, category.title')->groupBy('post.category_id')->orderBy('qty desc')->one(); $mostPopularTag = Tag::find()->joinWith('posts')->select('tags.id, posts_tags.tag_id, count(distinct posts_tags.post_id) as qty, tags.title')->groupBy('posts_tags.tag_id')->orderBy('qty desc')->one(); $postsCount = Post::find()->where('active = 1')->count(); $categoryCount = Category::find()->count(); return $this->render('index', ['lastPosts' => $lastPosts, 'featured' => $featured, 'drafts' => $drafts, 'mostPopularCategory' => $mostPopularCategory, 'mostPopularTag' => $mostPopularTag, 'postsCount' => $postsCount, 'categoryCount' => $categoryCount]); }
/** * Updates an existing Topic model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model->setScenario('update'); $post = Yii::$app->request->post(); if ($model->load($post) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'tags' => Tag::find()->all()]); } }
/** * 将指定文章的所有标签文章数加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; }
public function beforeDelete() { if (parent::beforeDelete()) { $tag = Tag::findOne($this->tag_id); $tag->recount--; $tag->save(); return true; } else { return false; } }
public function actionTag($ids) { $arr_ids = explode('_', $ids); $tags = Tag::find()->all(); $query = Post::find()->joinWith('tags')->where(['tags.id' => $arr_ids]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 10]); $pages->pageSizeParam = false; $posts = $query->offset($pages->offset)->limit($pages->limit)->orderBy('id desc')->all(); return $this->render('category', ['ids' => $arr_ids, 'tags' => $tags, 'posts' => $posts, 'pagination' => $pages]); }
protected function renderContent() { $tags = Tag::findTagWeights($this->maxTags); // $tags = ['test'=>14, 'sss'=>20]; echo Html::begintag('ul', array('class' => 'list-inline')); foreach ($tags as $tag => $weight) { $link = Html::a(Html::encode($tag), array('post/index', 'tags' => $tag)) . "\n"; echo Html::tag('li', $link, array('style' => "font-size:{$weight}px;margin:2px")) . "\n"; } echo Html::endTag('ul'); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
public function search($params) { $query = Tag::find()->where(['store_id' => Yii::$app->user->identity->store_id]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['sort' => SORT_ASC]], 'pagination' => ['pageSize' => 0]]); // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } $dateBegin = strtotime($this->date); $dateEnd = $dateBegin + 86400; // adjust the query by adding the filters $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['>=', 'created_at', $this->date ? $dateBegin : null])->andFilterWhere(['<', 'created_at', $this->date ? $dateEnd : null]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'count' => $this->count, 'topics' => $this->topics, 'shares' => $this->shares, 'labels' => $this->labels, 'created' => $this->created, 'updated' => $this->updated, 'active' => $this->active]); $query->andFilterWhere(['like', 'tag', $this->tag]); return $dataProvider; }
public function actionDelete($id) { $model = Tag::findOne(['id' => $id, 'store_id' => Yii::$app->user->identity->store_id]); if ($model) { if ($model->delete()) { Yii::$app->session->setFlash('success', '成功删除标签“' . $model->name . '”。'); } else { Yii::$app->session->setFlash('danger', '标签删除失败。'); } } if (Yii::$app->request->referrer) { return $this->redirect(Yii::$app->request->referrer); } else { return $this->redirect(['index']); } }
/** * Creates data provider instance with search query applied. * * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => 60]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'frequency' => $this->frequency]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'comment', $this->comment]); return $dataProvider; }
public function saveManyTags($vidmageTags) { var_dump($vidmageTags); foreach ($vidmageTags as $vidmageTagId) { if (!is_numeric($vidmageTagId)) { $tag = new Tag(); $tag->name = $vidmageTagId; $tag->save(); $vidmageTagId = $tag->id; } $vidmageTag = new VidmageTag(); $vidmageTag->vidmage_id = $this->id; $vidmageTag->tag_id = $vidmageTagId; $vidmageTag->save(); } }
public function actionDetail($id) { $postModel = new Comment(); if ($postModel->load(Yii::$app->request->post())) { $postModel->status = Comment::STATUS_INACTIVE; $postModel->post_id = $id; if ($postModel->save()) { $this->added = 1; } } $tags = Tag::findTagWeights(); $cateModel = new Cate(); $cateDataProvider = $cateModel->getCates(); $postData = new Post(); $postDataProvider = $postData->findRecentPosts(); $commentModel = new Comment(); $commentDataProvider = $commentModel->findRecentComments(); return $this->render('detail', ['tags' => $tags, 'added' => $this->added, 'postModel' => $postModel, 'model' => $this->findModel($id), 'postDataProvider' => $postDataProvider, 'cateDataProvider' => $cateDataProvider, 'commentDataProvider' => $commentDataProvider]); }
public function actionNormalize() { /** * @var $tag Tag */ $updated = 0; $deleted = 0; $list = []; foreach (Tag::find()->each() as $tag) { $name = mb_strtolower($tag->name); if (isset($list[$name])) { TagEvent::updateAll(['tag_id' => $list[$name]], ['tag_id' => $tag->id]); $tag->delete(); $deleted++; continue; } $list[$name] = $tag->id; } $this->stdout(Console::wrapText("- {$updated} updated, {$deleted} deleted", 10), Console::BOLD); $this->stdout("\n"); }
/** * 获取文章列表(可以按分类、标签获取文章列表) * @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]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TopTag::find(); $topTagTable = TopTag::tableName(); $tagTable = Tag::tableName(); $query->joinWith(['tag' => function ($query) use($tagTable) { $query->from(['tag' => $tagTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); // enable sorting for the related columns $addSortAttributes = ["tag.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'tag_id' => $this->tag_id]); $query->andFilterWhere(['like', 'tag.name', $this->getAttribute('tag.name')]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $user = Yii::$app->getModule("user")->model("User"); $query = Post::find(); $postTable = Post::tableName(); // set up query with relation to `user.username` $userTable = $user::tableName(); $query->joinWith(['user' => function ($query) use($userTable) { $query->from(['user' => $userTable]); }]); $tagTable = Tag::tableName(); $taggingTable = Tagging::tableName(); $query->leftJoin($taggingTable, "{$taggingTable}.taggable_id = {$postTable}.id AND {$taggingTable}.taggable_type = '" . Tagging::TAGGABLE_POST . "'"); $query->leftJoin($tagTable, "{$tagTable}.id = {$taggingTable}.tag_id"); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); // enable sorting for the related columns $addSortAttributes = ["user.username"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(["{$postTable}.id" => $this->id, 'user_id' => $this->user_id, 'is_public' => $this->is_public, 'is_index' => $this->is_index, 'is_top' => $this->is_top, 'is_pin' => $this->is_pin, 'with_video' => $this->with_video, 'with_photo' => $this->with_photo, 'content_category_id' => $this->content_category_id, 'is_yandex_rss' => $this->is_yandex_rss, 'allow_comment' => $this->allow_comment]); $createdTime = strtotime($this->created_at); $startDay = date("Y-m-d 00:00:00", $createdTime); $endDay = date("Y-m-d 00:00:00", $createdTime + 60 * 60 * 24); if ($this->created_at) { $query->andFilterWhere(['between', 'created_at', $startDay, $endDay]); } $updatedTime = strtotime($this->updated_at); $startDay = date("Y-m-d 00:00:00", $updatedTime); $endDay = date("Y-m-d 00:00:00", $updatedTime + 60 * 60 * 24); if ($this->updated_at) { $query->andFilterWhere(['between', 'updated_at', $startDay, $endDay]); } $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'source_title', $this->source_title])->andFilterWhere(['like', 'source_url', $this->source_url])->andFilterWhere(['like', 'cached_tag_list', $this->cached_tag_list])->andFilterWhere(['like', 'user.username', $this->getAttribute('user.username')])->andFilterWhere(['like', "{$tagTable}.name", $this->getAttribute('tag.name')]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tagging::find(); // set up query with relation to `tag.name` $tagTable = Tag::tableName(); $query->joinWith(['tag' => function ($query) use($tagTable) { $query->from(['tag' => $tagTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query]); // enable sorting for the related columns $addSortAttributes = ["tag.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'tag_id' => $this->tag_id, 'taggable_id' => $this->taggable_id]); $query->andFilterWhere(['like', 'taggable_type', $this->taggable_type]); return $dataProvider; }
public function insertTags($tags, $beforeCount = true, $afterCount = true) { if (!is_array($tags)) { return false; } $this->deleteTags($beforeCount); //先删除标签 //插入新标签 $tagIds = Tag::scanTags($tags); if ($tagIds) { foreach ($tagIds as $v) { $model = new Relationship(); $model->cid = $this->cid; $model->mid = $v; $model->insert(false); if ($afterCount) { //更新标签文章数 Tag::updateAllCounters(['count' => 1], ['mid' => $v]); } } } return true; }
/** * @return \yii\db\ActiveQuery */ public function getTag() { return $this->hasOne(Tag::className(), ['id' => 'tag_id']); }
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <?//= $form->field($model, 'status')->textInput() ?> <div class="row"> <div class="col-md-6"> <?php echo $form->field($model, 'photo')->widget(FileInput::classname(), ['options' => ['accept' => 'image/*'], 'pluginOptions' => ['initialPreview' => $model->photo ? [Html::img($model->photoUrl, ['class' => 'file-preview-image', 'alt' => $model->username, 'title' => $model->username])] : false, 'showUpload' => false, 'allowedFileExtensions' => ['jpg', 'gif', 'png', 'bmp']]]); ?> <?php echo $form->field($model, 'category_id')->dropDownList($model->categoryList()); ?> <?php echo $form->field($model, 'tagIds')->checkboxList(ArrayHelper::map(\common\models\Tag::find()->all(), 'id', 'title')); ?> </div> <div class="col-md-6"> <div class="row"> <div class="col-md-6"> <?php echo $form->field($model, 'first_name')->textInput(); ?> </div> <div class="col-md-6"> <?php echo $form->field($model, 'last_name')->textInput(); ?> </div> </div>