/** * 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; }
/** * Deletes an existing Tag model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $tag = $this->findModel($id); $taggings = Tagging::find()->where(['taggable_type' => Tagging::TAGGABLE_POST, 'tag_id' => $id])->all(); foreach ($taggings as $tagging) { $post = Post::findOne($tagging->taggable_id); if ($post) { $cached_tag_list = []; $newTags = $post->getTags(); foreach ($newTags as $newTag) { if (strcmp($tag->name, $newTag->name) !== 0) { $cached_tag_list[] = $newTag->name; } } $post->cached_tag_list = implode(', ', $cached_tag_list); $post->save(true, ['cached_tag_list']); } } Tagging::deleteAll(['tag_id' => $tag->id]); $tag->delete(); return $this->redirect(['index']); }
/** * @return \yii\db\ActiveQuery */ public function getTaggings() { return $this->hasMany(Tagging::className(), ['tag_id' => 'id']); }
/** * @param mixed $id Tag id OR new tag name * * @return boolean */ public function addTag($id) { if (is_numeric($id)) { $tag = Tag::find()->where(['id' => $id])->one(); } elseif (strpos($id, '{new}') !== false) { $name = mb_substr($id, 5, mb_strlen($id, 'UTF-8') - 5, 'UTF-8'); $tag = new Tag(); $tag->name = $name; if (!$tag->save()) { return false; } } if (!empty($tag)) { $tagging = new Tagging(); $tagging->taggable_id = $this->id; $tagging->taggable_type = Tagging::TAGGABLE_VIDEO; $tagging->tag_id = $tag->id; return $tagging->save(); } return false; }
/** * Url: /search?t={$t} * Url: /search?q={$q} * @param bool|string $t Tag name slug * @param bool|string $q Search words * @return mixed Content * @internal param string $date Searching by date */ public function actionSearch($t = false, $q = false) { $t = str_replace('-+-', '-#%#-', $t); $t = str_replace('+', ' ', $t); $t = str_replace('-#%#-', '+', $t); $postTable = Post::tableName(); $query = Post::find()->where(['is_public' => 1]); if (isset($t) && trim($t) != '') { $taggingTable = Tagging::tableName(); $tagTable = Tag::tableName(); $query->innerJoin($taggingTable, "{$postTable}.id = {$taggingTable}.taggable_id"); $query->innerJoin($tagTable, "{$taggingTable}.tag_id = {$tagTable}.id"); $query->andWhere(["{$taggingTable}.taggable_type" => Tagging::TAGGABLE_POST, "{$tagTable}.name" => $t]); } elseif (isset($q) && trim($q) != '') { $search = addslashes($q); $query->andWhere(['or', "MATCH (content) AGAINST ('{$search}')", ['like', 'title', $q]]); } $query->orderBy(["{$postTable}.created_at" => SORT_DESC]); if (!isset($_GET['page']) || $_GET['page'] == 1) { Yii::$app->session['news_post_time_last'] = 1; } $newsDataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 15]]); return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | Поиск по сайту', 'columnFirst' => ['news' => ['view' => '@frontend/views/search/search_posts', 'data' => compact('newsDataProvider')]], 'columnSecond' => ['blog_column' => SiteBlock::getBlogPosts(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]); }