#1103 - Правки английской версии 0.1

parent b79ad8d2
...@@ -185,7 +185,22 @@ class Post extends \common\components\ActiveRecordModel ...@@ -185,7 +185,22 @@ class Post extends \common\components\ActiveRecordModel
*/ */
public function getPostTags() public function getPostTags()
{ {
return $this->hasMany(PostTag::className(), ['id' => 'tag_id'])->viaTable('posts_tags_assign', ['post_id' => 'id']); $lang = Languages::getCurrent();
$query = $this->hasMany(PostTag::className(), ['id' => 'tag_id'])->viaTable('posts_tags_assign', ['post_id' => 'id']);
if($lang->default)
{
$query = $query->andWhere('lang_id = '.$lang->id.' OR lang_id IS NULL');
}
else
{
$query = $query->andWhere([
'lang_id' => $lang->id
]);
}
return $query;
} }
/** /**
......
...@@ -42,7 +42,8 @@ class PostTag extends \common\components\ActiveRecordModel ...@@ -42,7 +42,8 @@ class PostTag extends \common\components\ActiveRecordModel
{ {
return [ return [
[['name'], 'required'], [['name'], 'required'],
[['created_at', 'updated_at'], 'integer'], [['lang_id'], 'safe'],
[['created_at', 'updated_at', 'lang_id'], 'integer'],
[['name'], 'string', 'max' => 255], [['name'], 'string', 'max' => 255],
]; ];
} }
...@@ -55,6 +56,7 @@ class PostTag extends \common\components\ActiveRecordModel ...@@ -55,6 +56,7 @@ class PostTag extends \common\components\ActiveRecordModel
return [ return [
'id' => 'ID', 'id' => 'ID',
'name' => 'Имя тега', 'name' => 'Имя тега',
'lang_id' => 'Язык',
'created_at' => 'Дата добавления', 'created_at' => 'Дата добавления',
'updated_at' => 'Дата обновления', 'updated_at' => 'Дата обновления',
]; ];
...@@ -83,6 +85,14 @@ class PostTag extends \common\components\ActiveRecordModel ...@@ -83,6 +85,14 @@ class PostTag extends \common\components\ActiveRecordModel
->orderBy(Post::tableName().'.created_at DESC'); ->orderBy(Post::tableName().'.created_at DESC');
} }
/**
* @return \yii\db\ActiveQuery
*/
public function getLang()
{
return $this->hasOne(Languages::className(), ['id' => 'lang_id']);
}
/** /**
* @return \yii\db\ActiveQuery * @return \yii\db\ActiveQuery
*/ */
...@@ -115,4 +125,10 @@ class PostTag extends \common\components\ActiveRecordModel ...@@ -115,4 +125,10 @@ class PostTag extends \common\components\ActiveRecordModel
{ {
return Url::to(['/blog/tag/' . $this->name]); return Url::to(['/blog/tag/' . $this->name]);
} }
public static function find()
{
// use CustomerQuery instead of the default ActiveQuery
return new PostTagQuery(get_called_class());
}
} }
<?php
namespace common\modules\blog\models;
use common\modules\languages\models\Languages;
/**
* This is the ActiveQuery class for [[PostTag]].
*
* @see PostTag
*/
class PostTagQuery extends \yii\db\ActiveQuery
{
public function currentLang()
{
$lang = Languages::getCurrent();
if($lang->default)
{
return $this->andWhere('lang_id = '.$lang->id.' OR lang_id IS NULL');
}
return $this->andWhere([
'lang_id' => $lang->id
]);
}
/**
* @inheritdoc
* @return PostTag[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return PostTag|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
...@@ -5,23 +5,30 @@ use yii\helpers\Url; ...@@ -5,23 +5,30 @@ use yii\helpers\Url;
use common\modules\blog\models\Post; use common\modules\blog\models\Post;
use common\modules\blog\models\PostTag; use common\modules\blog\models\PostTag;
use common\models\Settings; use common\models\Settings;
use common\modules\languages\models\Languages;
?> ?>
<div class="col-md-4 col-xs-4 col-sm-12"> <div class="col-md-4 col-xs-4 col-sm-12">
<aside class="sidebar"> <aside class="sidebar">
<div class="sidebar_module">
<h2><?=\Yii::t('blog', 'Categories');?></h2>
<div class="sidebar_module_body">
<?php $tags = PostTag::find() <?php
$tags = PostTag::find()
->innerJoinWith('posts') ->innerJoinWith('posts')
->groupBy(['posts_tags_assign.tag_id']) ->groupBy(['posts_tags_assign.tag_id'])
->where(['posts.active' => 1]) ->where(['posts.active' => 1])
->currentLang()
->orderBy('RAND()') ->orderBy('RAND()')
->limit(7) ->limit(7)
->all(); ->all();
foreach ($tags as $tag) : ?> ?>
<?php if($tags) : ?>
<div class="sidebar_module">
<h2><?=\Yii::t('blog', 'Categories');?></h2>
<div class="sidebar_module_body">
<?php foreach ($tags as $tag) : ?>
<a href="<?=$tag->url;?>" class="cat_link_mod"><?=$tag->name?></a> <a href="<?=$tag->url;?>" class="cat_link_mod"><?=$tag->name?></a>
...@@ -29,6 +36,8 @@ use common\models\Settings; ...@@ -29,6 +36,8 @@ use common\models\Settings;
</div> </div>
</div> </div>
<?php endif; ?>
<div class="sidebar_module"> <div class="sidebar_module">
<div class="sidebar_module_body"> <div class="sidebar_module_body">
<a href="#feedback" class="sidebar_btn popup-form"> <a href="#feedback" class="sidebar_btn popup-form">
......
<?php <?php
use yii\helpers\Html; use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
use common\modules\languages\models\Languages;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */ /* @var $model common\modules\blog\models\PostTag */
/* @var $form yii\widgets\ActiveForm */ /* @var $form yii\widgets\ActiveForm */
...@@ -14,6 +17,8 @@ use yii\widgets\ActiveForm; ...@@ -14,6 +17,8 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'lang_id')->dropDownList(ArrayHelper::map(Languages::find()->all(), 'id', 'name')) ?>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?> <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div> </div>
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
use common\modules\languages\models\Languages;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $searchModel common\modules\blog\models\SearchPostTag */ /* @var $searchModel common\modules\blog\models\SearchPostTag */
/* @var $dataProvider yii\data\ActiveDataProvider */ /* @var $dataProvider yii\data\ActiveDataProvider */
...@@ -22,7 +24,17 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -22,7 +24,17 @@ $this->params['breadcrumbs'][] = $this->title;
// ['class' => 'yii\grid\SerialColumn'], // ['class' => 'yii\grid\SerialColumn'],
'name', 'name',
[
'attribute' => 'lang_id',
'value' => function($model) {
if($model->lang)
{
return $model->lang->name;
}
return Languages::getCurrent()->name;
}
],
[ [
'class' => 'common\components\ColorActionColumn', 'class' => 'common\components\ColorActionColumn',
'template' => '{update} {delete}', 'template' => '{update} {delete}',
......
...@@ -26,6 +26,10 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -26,6 +26,10 @@ $this->params['breadcrumbs'][] = $this->title;
'model' => $model, 'model' => $model,
'attributes' => [ 'attributes' => [
'name', 'name',
[
'attribute' => 'lang_id',
'value' => $model->lang->name,
]
], ],
]) ?> ]) ?>
......
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160322_103650_blog_tag_language extends Migration
{
public function up()
{
$this->addColumn('posts_tags', 'lang_id', Schema::TYPE_INTEGER . '(11) DEFAULT NULL AFTER name');
}
public function down()
{
$this->dropColumn('posts_tags', 'lang_id');
}
}
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