public function down() { $this->dropForeignKey('fk_message_source_message', Message::tableName()); $this->dropTable(Message::tableName()); $this->dropTable(SourceMessage::tableName()); return true; }
/** * @var AfterSaveEvent $event */ public function afterSave($event) { /** @var ActiveRecord $owner */ $owner = $event->sender; foreach ($this->attributes as $attribute) { $category = str_replace(['{Class}', '{attribute}'], [strtolower($owner->formName()), $attribute], $this->category); $condition = ['category' => $category, 'message' => static::getOldAttribute($event, $attribute)]; $data = ['category' => $category, 'message' => $owner->getAttribute($attribute)]; switch ($event->name) { case ActiveRecord::EVENT_AFTER_INSERT: case ActiveRecord::EVENT_AFTER_UPDATE: $model = SourceMessage::findOne($condition) ?: new SourceMessage($data); $model->save(false); break; case ActiveRecord::EVENT_AFTER_DELETE: SourceMessage::deleteAll($condition); break; } } }
/** * @return \yii\db\ActiveQuery */ public function getSource() { return $this->hasOne(SourceMessage::className(), ['id' => 'id'])->from(['source' => SourceMessage::tableName()]); }
<?php use yii\helpers\Html; /** * @var yii\web\View $this * @var yii\data\ActiveDataProvider $dataProvider * @var bariew\i18nModule\models\MessageSearch $searchModel */ $this->title = Yii::t('modules/i18n', "Translations"); ?> <h1> <?php echo Html::encode($this->title); echo Html::a(Yii::t('modules/i18n', 'Create Translation Source'), ['create'], ['class' => 'btn btn-success pull-right']); ?> </h1> <?php echo \yii\grid\GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['attribute' => 'sourceCategory', 'filter' => \bariew\i18nModule\models\SourceMessage::categoryList()], ['attribute' => 'language', 'filter' => $searchModel::languageList()], 'sourceMessage', ['attribute' => 'translation', 'format' => 'raw', 'value' => function ($model) { return "<div contentEditable='true'\n style='background-color: white;border: 1px solid #ddd;min-height: 20px'\n class='form-control translate-live-input'\n >{$model->translation}</div>"; }], ['attribute' => 'translationUpdate', 'label' => Yii::t('modules/i18n', 'Update'), 'filter' => ['is not null' => Yii::t('modules/i18n', 'Translated'), 'is null' => Yii::t('modules/i18n', 'Not translated')], 'format' => 'raw', 'value' => function ($model) { return \yii\helpers\Html::button("<i class='glyphicon glyphicon-ok'></i>", ['class' => 'btn btn-success', 'data-id' => "{$model->id}-{$model->language}", 'data-url' => \yii\helpers\Url::toRoute(['fast-update', 'id' => $model->id]), 'onclick' => "\n \$(this).parents('tr').fadeOut();\n \$.post(\$(this).data('url'), {\n translation : \$(this).parents('tr').find('.translate-live-input').text(),\n language : '" . $model->language . "',\n _csrf : '" . Yii::$app->request->csrfToken . "'\n })"]) . ' ' . \yii\helpers\Html::button("<i class='glyphicon glyphicon-remove'></i>", ['class' => 'btn btn-danger class' . $model->id, 'data-id' => "{$model->id}-{$model->language}", 'data-url' => \yii\helpers\Url::toRoute(['delete', 'id' => $model->id]), 'onclick' => "\n \$('.btn-danger.class" . $model->id . "').parents('tr').fadeOut();\n \$.post(\$(this).data('url'), {_csrf : '" . Yii::$app->request->csrfToken . "'})"]); }]]]);
/** * Deletes all source and translation messages. * @param $id * @throws \Exception */ public function actionDelete($id) { SourceMessage::findOne($id)->delete(); }