Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
taskonsite-архив-перенесен
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages
Packages
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitry Korolev
taskonsite-архив-перенесен
Commits
ad819030
Commit
ad819030
authored
Jun 23, 2016
by
Александр Кузнецов
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'blog-hashtag-filtr' into 'master'
аякс фильты в блоге See merge request !44
parents
1b409a9b
2f2aa942
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
2 deletions
+160
-2
common/modules/blog/controllers/PostController.php
common/modules/blog/controllers/PostController.php
+64
-2
common/modules/blog/models/SearchPost.php
common/modules/blog/models/SearchPost.php
+96
-0
No files found.
common/modules/blog/controllers/PostController.php
View file @
ad819030
...
...
@@ -2,6 +2,7 @@
namespace
common\modules\blog\controllers
;
use
common\modules\blog\models\SearchPost
;
use
Yii
;
use
common\components\BaseController
;
use
yii\web\NotFoundHttpException
;
...
...
@@ -28,6 +29,7 @@ class PostController extends BaseController
'Send-article'
=>
'Послать статью'
,
'Set-cookie'
=>
'Установить куки'
,
'Tag-index'
=>
'Перезагрузка статей с учетом тэгов'
,
'Test'
=>
'test'
,
];
}
/**
...
...
@@ -40,12 +42,47 @@ class PostController extends BaseController
$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
))
{
// $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
]);
}
else
{
$query
=
Post
::
find
()
->
where
(
'posts.active=1'
)
->
limit
(
Post
::
PAGE_SIZE
)
->
orderBy
(
Post
::
tableName
()
.
'.created_at DESC'
);
}
return
$this
->
render
(
'index'
,
[
'models'
=>
$query
->
all
(),
...
...
@@ -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.
* @param string $url
...
...
@@ -208,10 +254,18 @@ class PostController extends BaseController
$cookie
=
$session
->
get
(
'blog_hashtag'
);
$cookie
=
$cookie
?
$cookie
:
[];
// $session->set('blog_hashtag_lifetime', time() + 3600*24);
$session
->
set
(
'blog_hashtag_lifetime'
,
time
()
+
10
);
$key
=
array_search
(
$hashtag
,
$cookie
);
if
(
$key
!==
false
)
{
unset
(
$cookie
[
$key
]);
$session
->
set
(
'blog_hashtag'
,
$cookie
);
$cookie_
=
[];
foreach
(
$cookie
as
$c
)
{
$cookie_
[]
=
$c
;
}
$session
->
set
(
'blog_hashtag'
,
$cookie_
);
return
false
;
}
else
{
...
...
@@ -219,6 +273,8 @@ class PostController extends BaseController
$session
->
set
(
'blog_hashtag'
,
$cookie
);
return
true
;
}
}
}
...
...
@@ -229,6 +285,12 @@ class PostController extends BaseController
if
(
$session
->
isActive
)
{
$cookie
=
$session
->
get
(
'blog_hashtag'
);
if
(
$session
->
get
(
'blog_hashtag_lifetime'
)
<
time
())
{
$cookie
=
[];
$session
->
set
(
'blog_hashtag'
,
$cookie
);
}
}
return
empty
(
$cookie
)
?
[]
:
$cookie
;
...
...
common/modules/blog/models/SearchPost.php
View file @
ad819030
...
...
@@ -69,4 +69,100 @@ class SearchPost extends Post
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment