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

parent b79ad8d2
......@@ -185,7 +185,22 @@ class Post extends \common\components\ActiveRecordModel
*/
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
{
return [
[['name'], 'required'],
[['created_at', 'updated_at'], 'integer'],
[['lang_id'], 'safe'],
[['created_at', 'updated_at', 'lang_id'], 'integer'],
[['name'], 'string', 'max' => 255],
];
}
......@@ -55,6 +56,7 @@ class PostTag extends \common\components\ActiveRecordModel
return [
'id' => 'ID',
'name' => 'Имя тега',
'lang_id' => 'Язык',
'created_at' => 'Дата добавления',
'updated_at' => 'Дата обновления',
];
......@@ -83,6 +85,14 @@ class PostTag extends \common\components\ActiveRecordModel
->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
*/
......@@ -115,4 +125,10 @@ class PostTag extends \common\components\ActiveRecordModel
{
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,30 +5,39 @@ use yii\helpers\Url;
use common\modules\blog\models\Post;
use common\modules\blog\models\PostTag;
use common\models\Settings;
use common\modules\languages\models\Languages;
?>
<div class="col-md-4 col-xs-4 col-sm-12">
<aside class="sidebar">
<div class="sidebar_module">
<h2><?=\Yii::t('blog', 'Categories');?></h2>
<div class="sidebar_module_body">
<?php $tags = PostTag::find()
->innerJoinWith('posts')
->groupBy(['posts_tags_assign.tag_id'])
->where(['posts.active' => 1])
->orderBy('RAND()')
->limit(7)
->all();
foreach ($tags as $tag) : ?>
<a href="<?=$tag->url;?>" class="cat_link_mod"><?=$tag->name?></a>
<?php
$tags = PostTag::find()
->innerJoinWith('posts')
->groupBy(['posts_tags_assign.tag_id'])
->where(['posts.active' => 1])
->currentLang()
->orderBy('RAND()')
->limit(7)
->all();
?>
<?php if($tags) : ?>
<div class="sidebar_module">
<h2><?=\Yii::t('blog', 'Categories');?></h2>
<div class="sidebar_module_body">
<?php endforeach; ?>
<?php foreach ($tags as $tag) : ?>
<a href="<?=$tag->url;?>" class="cat_link_mod"><?=$tag->name?></a>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
<div class="sidebar_module">
<div class="sidebar_module_body">
<a href="#feedback" class="sidebar_btn popup-form">
......
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use common\modules\languages\models\Languages;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */
/* @var $form yii\widgets\ActiveForm */
......@@ -14,6 +17,8 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'lang_id')->dropDownList(ArrayHelper::map(Languages::find()->all(), 'id', 'name')) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
......
......@@ -3,6 +3,8 @@
use yii\helpers\Html;
use yii\grid\GridView;
use common\modules\languages\models\Languages;
/* @var $this yii\web\View */
/* @var $searchModel common\modules\blog\models\SearchPostTag */
/* @var $dataProvider yii\data\ActiveDataProvider */
......@@ -22,7 +24,17 @@ $this->params['breadcrumbs'][] = $this->title;
// ['class' => 'yii\grid\SerialColumn'],
'name',
[
'attribute' => 'lang_id',
'value' => function($model) {
if($model->lang)
{
return $model->lang->name;
}
return Languages::getCurrent()->name;
}
],
[
'class' => 'common\components\ColorActionColumn',
'template' => '{update} {delete}',
......
......@@ -26,6 +26,10 @@ $this->params['breadcrumbs'][] = $this->title;
'model' => $model,
'attributes' => [
'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