-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
625ce8f
commit a4c2489
Showing
7 changed files
with
354 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
namespace centigen\i18ncontent\models; | ||
|
||
use Yii; | ||
use yii\db\ActiveRecord; | ||
use yii\helpers\ArrayHelper; | ||
use yii\helpers\StringHelper; | ||
|
||
/** | ||
* This is the model class for table "{{%i18n_source_message}}". | ||
|
@@ -12,9 +15,11 @@ | |
* @property string $message | ||
* | ||
* @property I18nMessage[] $i18nMessages | ||
* @property ActiveRecord[]|array $translations | ||
*/ | ||
class I18nSourceMessage extends \yii\db\ActiveRecord | ||
class I18nSourceMessage extends ActiveRecord | ||
{ | ||
public $newTranslations = []; | ||
/** | ||
* @inheritdoc | ||
*/ | ||
|
@@ -53,4 +58,56 @@ public function getI18nMessages() | |
{ | ||
return $this->hasMany(I18nMessage::className(), ['id' => 'id']); | ||
} | ||
|
||
|
||
/** | ||
* @author Guga Grigolia <[email protected]> | ||
* @inheritdoc | ||
*/ | ||
public function load($postData, $formName = null) | ||
{ | ||
if (!parent::load($postData, $formName)) { | ||
return false; | ||
} | ||
|
||
$className = StringHelper::basename(I18nMessage::className()); | ||
$translations = ArrayHelper::getValue($postData, $className); | ||
$this->newTranslations = []; | ||
|
||
$allValid = true; | ||
if(!empty($translations)){ | ||
foreach ($translations as $loc => $modelData) { | ||
$modelData['language'] = $loc; | ||
|
||
|
||
$translation = $this->findTranslationByLocale($loc); | ||
|
||
$this->newTranslations[] = $translation; | ||
if (!$translation->load($modelData, '')) { | ||
$allValid = false; | ||
} | ||
} | ||
} | ||
|
||
return $allValid; | ||
} | ||
|
||
/** | ||
* Find PageTranslation object from `translations` array by locale | ||
* | ||
* @author Guga Grigolia <[email protected]> | ||
* @param $locale | ||
* @return ActiveRecord | ||
*/ | ||
public function findTranslationByLocale($locale) | ||
{ | ||
$translations = array_merge($this->newTranslations, $this->i18nMessages); | ||
foreach ($translations as $translation) { | ||
if ($translation->locale === $locale) { | ||
return $translation; | ||
} | ||
} | ||
|
||
return new I18nMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace centigen\i18ncontent\models\search; | ||
|
||
use Yii; | ||
use yii\base\Model; | ||
use yii\data\ActiveDataProvider; | ||
use backend\modules\i18n\models\I18nMessage; | ||
|
||
/** | ||
* I18nMessageSearch represents the model behind the search form about `backend\modules\i18n\models\I18nMessage`. | ||
*/ | ||
class I18nSearch extends I18nMessage | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['id'], 'integer'], | ||
[['language', 'translation', 'sourceMessage', 'category'], 'safe'], | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function scenarios() | ||
{ | ||
// bypass scenarios() implementation in the parent class | ||
return Model::scenarios(); | ||
} | ||
|
||
/** | ||
* Creates data provider instance with search query applied | ||
* | ||
* @param array $params | ||
* | ||
* @return ActiveDataProvider | ||
*/ | ||
public function search($params) | ||
{ | ||
$query = I18nMessage::find()->with('sourceMessageModel')->joinWith('sourceMessageModel'); | ||
|
||
$dataProvider = new ActiveDataProvider([ | ||
'query' => $query | ||
]); | ||
|
||
if (!($this->load($params) && $this->validate())) { | ||
return $dataProvider; | ||
} | ||
|
||
|
||
|
||
$query->andFilterWhere([ | ||
'{{%i18n_source_message}}.id' => $this->id | ||
]); | ||
|
||
$query->andFilterWhere(['like', '{{%i18n_message}}.language', $this->language]) | ||
->andFilterWhere(['like', '{{%i18n_message}}.translation', $this->translation]) | ||
->andFilterWhere(['like', '{{%i18n_source_message}}.message', $this->sourceMessage]) | ||
->andFilterWhere(['like', '{{%i18n_source_message}}.category', $this->category]); | ||
|
||
|
||
return $dataProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
use yii\bootstrap\Tabs; | ||
use yii\helpers\Html; | ||
use yii\bootstrap\ActiveForm; | ||
|
||
/* @var $this yii\web\View */ | ||
/* @var $model centigen\i18ncontent\models\I18nSourceMessage */ | ||
/* @var $categories array */ | ||
/* @var $form yii\bootstrap\ActiveForm */ | ||
/* @var $locales array */ | ||
?> | ||
|
||
<div class="article-form"> | ||
|
||
<?php $form = ActiveForm::begin(); ?> | ||
|
||
<?php echo $form->field($model, 'category')->textInput(['maxlength' => 32]) ?> | ||
|
||
<?php echo $form->field($model, 'message')->textarea(['rows' => 6]) ?> | ||
|
||
|
||
<?php | ||
|
||
if (isset($locales)) { | ||
$items = []; | ||
$ind = 0; | ||
foreach ($locales as $key => $locale) { | ||
$title = $locale; | ||
$translationModel = $model->findTranslationByLocale($key); | ||
|
||
$content = $this->render('_tab_content', [ | ||
'form' => $form, | ||
'model' => $translationModel, | ||
'language' => $key, | ||
]); | ||
|
||
$items[] = [ | ||
'label' => $title, | ||
'content' => $content, | ||
'headerOptions' => [ | ||
'title' => $translationModel->hasErrors() ? Yii::t('i18ncontent', 'You have validation errors') : "", | ||
'class' => $translationModel->hasErrors() ? 'has-error' : '', | ||
'data-toggle' => 'tooltip' | ||
], | ||
'options' => [ | ||
'class' => 'fade' . ($ind++ === 0 ? ' in' : '') | ||
] | ||
]; | ||
} | ||
echo '<div class="tab-wrapper">'; | ||
echo Tabs::widget([ | ||
'items' => $items | ||
]); | ||
echo '</div>'; | ||
} else { | ||
echo $this->render('_tab_content', [ | ||
'form' => $form, | ||
'model' => $model | ||
]); | ||
} | ||
|
||
?> | ||
|
||
|
||
<div class="form-group pull-left margin-right-5"> | ||
<?php echo Html::submitButton( | ||
$model->isNewRecord ? Yii::t('i18ncontent', 'Create') : Yii::t('i18ncontent', 'Update'), | ||
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ||
</div> | ||
|
||
<?php ActiveForm::end(); ?> | ||
|
||
<?php if(!$model->isNewRecord): ?> | ||
<div class="form-group"> | ||
<?php echo Html::a(Yii::t('i18ncontent', 'Delete', []), ['delete', 'id' => $model->id], | ||
[ | ||
'class' => 'btn btn-danger', | ||
'data-confirm' => "Are you sure you want to delete this item?", | ||
'data-method'=>"post", | ||
'data-pjax' => "0" | ||
]) ?> | ||
</div> | ||
<?php endif?> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use yii\helpers\Url; | ||
|
||
/* @var $this yii\web\View */ | ||
/* @var $language string */ | ||
/* @var $model centigen\i18ncontent\models\I18nMessage */ | ||
/* @var $form yii\bootstrap\ActiveForm */ | ||
|
||
$className = \yii\helpers\StringHelper::basename(\centigen\i18ncontent\models\I18nMessage::className()); | ||
|
||
?> | ||
<?php echo $form->field($model, 'translation', [ | ||
'inputOptions' => [ | ||
'name' => "{$className}[$language][translation]" | ||
] | ||
])->textarea(['maxlength' => 512]) ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/* @var $this yii\web\View */ | ||
/* @var $model centigen\i18ncontent\models\I18nSourceMessage */ | ||
/* @var $locales array */ | ||
|
||
$this->title = Yii::t('i18ncontent', 'Create {modelClass}', [ | ||
'modelClass' => 'i18n', | ||
]); | ||
$this->params['breadcrumbs'][] = ['label' => Yii::t('i18ncontent', 'i18n'), 'url' => ['index']]; | ||
$this->params['breadcrumbs'][] = $this->title; | ||
?> | ||
<div class="article-create"> | ||
|
||
<?php echo $this->render('_form', [ | ||
'model' => $model, | ||
'locales' => $locales | ||
]) ?> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
use backend\modules\i18n\models\search\I18nMessageSearch; | ||
use yii\helpers\Html; | ||
use yii\grid\GridView; | ||
/** | ||
* @var $this yii\web\View | ||
* @var $searchModel I18nMessageSearch | ||
* @var $languages array | ||
* @var $categories array | ||
* @var $dataProvider yii\data\ActiveDataProvider | ||
*/ | ||
|
||
$this->title = Yii::t('backend', 'I18n Messages'); | ||
$this->params['breadcrumbs'][] = $this->title; | ||
?> | ||
<div class="i18n-message-index"> | ||
|
||
<?php //echo $this->render('_search', ['model' => $searchModel]); ?> | ||
|
||
<p> | ||
<?php echo Html::a(Yii::t('backend', 'Create {modelClass}', [ | ||
'modelClass' => 'I18n Message', | ||
]), ['create'], ['class' => 'btn btn-success']) ?> | ||
</p> | ||
|
||
<?php echo GridView::widget([ | ||
'dataProvider' => $dataProvider, | ||
'filterModel' => $searchModel, | ||
'options' => [ | ||
'class' => 'grid-view table-responsive' | ||
], | ||
'columns' => [ | ||
|
||
'id', | ||
[ | ||
'attribute'=>'language', | ||
'filter'=> $languages | ||
], | ||
[ | ||
'attribute'=>'category', | ||
'filter'=> $categories | ||
], | ||
'sourceMessage', | ||
'translation:ntext', | ||
['class' => 'yii\grid\ActionColumn', 'template'=>'{update} {delete}'], | ||
], | ||
]); ?> | ||
|
||
</div> |