Merge branch 'blog-hashtag-filtr' into 'master'

аякс фильты в блоге



See merge request !44
parents 1b409a9b 2f2aa942
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace common\modules\blog\controllers; namespace common\modules\blog\controllers;
use common\modules\blog\models\SearchPost;
use Yii; use Yii;
use common\components\BaseController; use common\components\BaseController;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
...@@ -28,6 +29,7 @@ class PostController extends BaseController ...@@ -28,6 +29,7 @@ class PostController extends BaseController
'Send-article' => 'Послать статью', 'Send-article' => 'Послать статью',
'Set-cookie' => 'Установить куки', 'Set-cookie' => 'Установить куки',
'Tag-index' => 'Перезагрузка статей с учетом тэгов', 'Tag-index' => 'Перезагрузка статей с учетом тэгов',
'Test' => 'test',
]; ];
} }
/** /**
...@@ -40,12 +42,47 @@ class PostController extends BaseController ...@@ -40,12 +42,47 @@ class PostController extends BaseController
$tag = $this->getCookie(); $tag = $this->getCookie();
$query = Post::find()->where(['active' => 1])->limit(Post::PAGE_SIZE)->orderBy(Post::tableName().'.created_at DESC'); // if(count($tag))
// {
//
// '
// SELECT * FROM `posts`
// WHERE `posts`.id IN (
// SELECT `post_id`
// FROM `posts_tags_assign`
// WHERE `tag_id` IN (1,2)
// GROUP BY post_id HAVING COUNT(post_id)=2
// )
// ';
//
// $subQuery = PostTagAssign::find();
// $subQuery->where(["tag_id" => $tag]);
// $subQuery->groupBy('post_id');
// $subQuery->having(['count(post_id)' => count($tag)]);
//
// $query = Post::find()->where('posts.active=1')->limit(Post::PAGE_SIZE)->orderBy(Post::tableName().'.created_at DESC');
// $query->an
//// $query->andWhere()
//
// }
// else
// {
// $query = Post::find()->where('posts.active=1')->limit(Post::PAGE_SIZE)->orderBy(Post::tableName().'.created_at DESC');
// }
if(count($tag)) if(count($tag))
{ {
// $searchPost = new SearchPost();
// $query = $searchPost->findByTags($tag);
$query = Post::find()->where(['active' => 1])->orderBy(Post::tableName().'.created_at DESC');
$query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]); $query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]);
} }
else
{
$query = Post::find()->where('posts.active=1')->limit(Post::PAGE_SIZE)->orderBy(Post::tableName().'.created_at DESC');
}
return $this->render('index', [ return $this->render('index', [
'models' => $query->all(), 'models' => $query->all(),
...@@ -53,6 +90,15 @@ class PostController extends BaseController ...@@ -53,6 +90,15 @@ class PostController extends BaseController
]); ]);
} }
public function actionTest()
{
$tag = $this->getCookie();
$searhPost = new SearchPost();
$searhPost->findByTags($tag);
}
/** /**
* Displays a single Post model. * Displays a single Post model.
* @param string $url * @param string $url
...@@ -208,10 +254,18 @@ class PostController extends BaseController ...@@ -208,10 +254,18 @@ class PostController extends BaseController
$cookie = $session->get('blog_hashtag'); $cookie = $session->get('blog_hashtag');
$cookie = $cookie ? $cookie : []; $cookie = $cookie ? $cookie : [];
// $session->set('blog_hashtag_lifetime', time() + 3600*24);
$session->set('blog_hashtag_lifetime', time() + 10);
$key = array_search($hashtag, $cookie); $key = array_search($hashtag, $cookie);
if ($key !== false) { if ($key !== false) {
unset($cookie[$key]); unset($cookie[$key]);
$session->set('blog_hashtag', $cookie); $cookie_ = [];
foreach ($cookie as $c)
{
$cookie_[] = $c;
}
$session->set('blog_hashtag', $cookie_);
return false; return false;
} }
else { else {
...@@ -219,6 +273,8 @@ class PostController extends BaseController ...@@ -219,6 +273,8 @@ class PostController extends BaseController
$session->set('blog_hashtag', $cookie); $session->set('blog_hashtag', $cookie);
return true; return true;
} }
} }
} }
...@@ -229,6 +285,12 @@ class PostController extends BaseController ...@@ -229,6 +285,12 @@ class PostController extends BaseController
if($session->isActive) if($session->isActive)
{ {
$cookie = $session->get('blog_hashtag'); $cookie = $session->get('blog_hashtag');
if($session->get('blog_hashtag_lifetime') < time())
{
$cookie = [];
$session->set('blog_hashtag', $cookie);
}
} }
return empty($cookie) ? [] : $cookie ; return empty($cookie) ? [] : $cookie ;
......
...@@ -69,4 +69,100 @@ class SearchPost extends Post ...@@ -69,4 +69,100 @@ class SearchPost extends Post
return $dataProvider; return $dataProvider;
} }
/**
* @param array $tags
*/
public function findByTags(array $tags, $limit = Post::PAGE_SIZE)
{
$subQuery = PostTagAssign::find();
$subQuery->select('post_id');
$subQuery->where(["tag_id" => $tags]);
$subQuery->groupBy('post_id');
$subQuery->having(['count(post_id)' => count($tags)]);
$query = Post::find()->where('posts.active=1');
if($limit) $query->limit($limit);
$query->orderBy(Post::tableName().'.created_at DESC');
$query->andWhere(['id' => $subQuery]);
$models = $query->all();
if(count($tags) == 1)
{
foreach ($models as $i => $model)
{
echo $model->id."<br>";
if(count($model->postTagAssigns) > 1)
{
continue;
}
echo $model->id . '<br>';
}
}
return $query;
// if(count($tags) > 1)
// {
// $query->all();
// }
// if(count($tags) > 1)
// {
// $subQuery = PostTagAssign::find();
// $subQuery->select('post_id');
// $subQuery->where(["tag_id" => $tags]);
// $subQuery->groupBy('post_id');
// $subQuery->having(['count(post_id)' => count($tags)]);
//
// $query = Post::find()->where('posts.active=1');
// if($limit) $query->limit($limit);
// $query->orderBy(Post::tableName().'.created_at DESC');
// $query->andWhere(['id' => $subQuery]);
// }
// else
// {
//// print_r($tags);
//// echo "<br>";
// $subQuery = PostTagAssign::find();
// $subQuery->orderBy('tag_id ASC');
// $models = $subQuery->all();
//
// $res = [];
// for($i = 0; $i < count($models); $i++)
// {
//
// if($tags[0] != $models[$i]->tag_id) continue;
//
// $c = 0;
// for ($j = 0; $j < count($models); $j++)
// {
//
// if($models[$j]->tag_id == $tags[0])
// {
// $post_id = $models[$j]->post_id;
// echo $models[$j]->tag_id."<br>";
// $c++;
// }
// }
//
// if($c == 1)
// {
// $res[] = $post_id;
// }
// }
//
//
// print_r($res);
//
// $query = Post::find()->where('posts.active=1');
// if($limit) $query->limit($limit);
// $query->orderBy(Post::tableName().'.created_at DESC');
// $query->andWhere(['id' => $res]);
//
//
// }
return $query;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment