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

Blog hashtag filtr



See merge request !43
parents 6467955a 5f012166
...@@ -19,7 +19,7 @@ class GeneratorAsset extends AssetBundle ...@@ -19,7 +19,7 @@ class GeneratorAsset extends AssetBundle
'css/style_add_22.05.16.css', 'css/style_add_22.05.16.css',
]; ];
public $js = [ public $js = [
// 'js/generator.js', 'js/generator.js',
]; ];
public $depends = [ public $depends = [
'yii\web\JqueryAsset', 'yii\web\JqueryAsset',
......
/** /**
* Created by andre on 21.06.2016. * Created by andre on 21.06.2016.
*/ */
$('form.bids-form').on('beforeSubmit', function(e) { $('.checkbox_btn').on('change', function(e) {
var form = $(this), xhr = new XMLHttpRequest, filebool = false, file, data = new FormData();
form.find('input, textarea').each(function(){ var $this = $(this);
data.append($(this).attr('name'), $(this).val()); var action = $this.attr("action");
});
if(form.find('input[type=file]').length) {
file = form.find('input[type=file]')[0].files[0];
filebool = !filebool;
data.append("Bid[file]", file);
}
xhr.open("POST", form.attr('action'), true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(data);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4){
try
{
var response = JSON.parse(xhr.responseText);
}
catch(e)
{
var response = xhr.responseText;
}
form.find('.has-error').removeClass('has-error'); $.ajax({
type: "get",
url: action,
success: function(msg){
$this.prop("checked", msg);
if(response.success) var action_form = $this.closest('form').attr('action');
{
if(form.find('.message-box').length > 0)
{
form.find('.message-box').fadeIn('fast');
form.find('.content').css('visibility','hidden');
setTimeout(function(){ $.ajax({
form.find('.message-box').fadeOut('fast'); type: "post",
form.find('.content').css('visibility','visible'); url: action_form,
}, 2000); success: function(msg){
$('section.blog article').detach();
$('section.blog h1').after(msg.posts);
$('section.blog .loaded').html('');
} }
form.find('input:not(.not_clear), textarea').val('');
form.find('#files-zone').html('');
$('.dz-preview.dz-processing').remove();
dataLayer.push({
'event': 'UA_event',
'Catagory': form.data('tag'),
'Action': form.data('title'),
'Label': 'Успешно'
}); });
} }
else
{
errors = [];
$.each(response, function(key, value) {
form.find('.field-'+key).addClass('has-error');
errors = errors.concat(value);
});
dataLayer.push({
'event': 'UA_event',
'Catagory': 'Ошибки',
'Action': form.data('form'),
'Label': errors
}); });
} })
} \ No newline at end of file
}
}).on('submit', function(e){
return false;
});
\ No newline at end of file
...@@ -26,6 +26,8 @@ class PostController extends BaseController ...@@ -26,6 +26,8 @@ class PostController extends BaseController
'Tag' => 'Просмотр тега', 'Tag' => 'Просмотр тега',
'View' => 'Просмотр записи', 'View' => 'Просмотр записи',
'Send-article' => 'Послать статью', 'Send-article' => 'Послать статью',
'Set-cookie' => 'Установить куки',
'Tag-index' => 'Перезагрузка статей с учетом тэгов',
]; ];
} }
...@@ -37,7 +39,14 @@ class PostController extends BaseController ...@@ -37,7 +39,14 @@ class PostController extends BaseController
{ {
$this->meta_title = Yii::t('blog', 'Art Project Blog'); $this->meta_title = Yii::t('blog', 'Art Project Blog');
$query = Post::find()->where(['active' => 1])->limit(Post::PAGE_SIZE)->orderBy('created_at DESC'); $tag = $this->getCookie();
$query = Post::find()->where(['active' => 1])->limit(Post::PAGE_SIZE)->orderBy(Post::tableName().'.created_at DESC');
if(count($tag))
{
$query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]);
}
return $this->render('index', [ return $this->render('index', [
'models' => $query->all(), 'models' => $query->all(),
...@@ -93,13 +102,13 @@ class PostController extends BaseController ...@@ -93,13 +102,13 @@ class PostController extends BaseController
if(Yii::$app->request->isAjax) if(Yii::$app->request->isAjax)
{ {
$offset = Yii::$app->request->post('offset'); $offset = Yii::$app->request->post('offset');
$tag = Yii::$app->request->post('tag'); $tag = $this->getCookie();
Yii::$app->response->format = Response::FORMAT_JSON; Yii::$app->response->format = Response::FORMAT_JSON;
$query = Post::find()->where(['active' => 1])->orderBy(Post::tableName().'.created_at DESC'); $query = Post::find()->where(['active' => 1])->orderBy(Post::tableName().'.created_at DESC');
if($tag) if(count($tag))
{ {
$query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]); $query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]);
} }
...@@ -187,4 +196,73 @@ class PostController extends BaseController ...@@ -187,4 +196,73 @@ class PostController extends BaseController
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
} }
} }
/**
* Запишет или удалит хэштэг в куки
* используется на странице блога в списке категорий, для отбора статей по хэштегам
*/
public function actionSetCookie($hashtag)
{
$session = Yii::$app->session;
if($session->isActive) {
$cookie = $session->get('blog_hashtag');
$cookie = $cookie ? $cookie : [];
$key = array_search($hashtag, $cookie);
if ($key !== false) {
unset($cookie[$key]);
$session->set('blog_hashtag', $cookie);
return false;
}
else {
$cookie[] = $hashtag;
$session->set('blog_hashtag', $cookie);
return true;
}
}
}
public function getCookie()
{
$session = Yii::$app->session;
if($session->isActive)
{
$cookie = $session->get('blog_hashtag');
}
return empty($cookie) ? [] : $cookie ;
}
/**
* @param $url
* @return string
* @throws NotFoundHttpException
*
* редерит список статей блога с отбором по хэштегам
*/
public function actionTagIndex()
{
$tag = $this->getCookie();
Yii::$app->response->format = Response::FORMAT_JSON;
$query = Post::find()->where(['active' => 1])->orderBy(Post::tableName().'.created_at DESC');
if(count($tag))
{
$query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]);
}
$models = $query->limit(Post::PAGE_SIZE)->offset($offset)->all();
$count = $query->count();
return [
'posts' => $this->renderPartial('_load', [
'models' => $models,
]),
'count' => (int)$count,
];
}
} }
...@@ -20,17 +20,18 @@ use common\modules\languages\models\Languages; ...@@ -20,17 +20,18 @@ use common\modules\languages\models\Languages;
->currentLang() ->currentLang()
->orderBy('RAND()') ->orderBy('RAND()')
->limit(7) ->limit(7)
->orderBy('name')
->all(); ->all();
?> ?>
<?php if($tags) : ?> <?php if($tags) : ?>
<div class="sidebar_module"> <div class="sidebar_module">
<h2><?=\Yii::t('blog', 'Categories');?></h2> <h2><?=\Yii::t('blog', 'Categories');?></h2>
<!-- add 22.05.16 --> <!-- add 22.05.16 -->
<div class="categories_form"> <div class="categories_form">
<form> <form action="/blog/post/tag-index">
<? $cookie = \Yii::$app->controller->getCookie() ?>
<?php foreach ($tags as $tag) : ?> <?php foreach ($tags as $tag) : ?>
<input type="checkbox" class="checkbox_btn" id="checkbox-<?= $tag->id ?>"> <input <?= in_array($tag->id, $cookie) ? "checked" : "" ?> action="<?= \yii\helpers\Url::to(["/blog/post/set-cookie", "hashtag" => $tag->id]) ?>" type="checkbox" class="checkbox_btn" id="checkbox-<?= $tag->id ?>">
<label for="checkbox-<?= $tag->id ?>"><?= $tag->name ?></label> <label for="checkbox-<?= $tag->id ?>"><?= $tag->name ?></label>
<?php endforeach; ?> <?php endforeach; ?>
</form> </form>
......
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