I18nMessageController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace common\modules\i18n\backend\controllers;
  3. use common\modules\i18n\models\I18nMessage;
  4. use common\modules\i18n\models\I18nSourceMessage;
  5. use common\modules\i18n\models\search\I18nMessageSearch;
  6. use Yii;
  7. use yii\filters\VerbFilter;
  8. use yii\helpers\ArrayHelper;
  9. use yii\helpers\Url;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. /**
  13. * I18nMessageController implements the CRUD actions for I18nMessage model.
  14. */
  15. class I18nMessageController extends Controller
  16. {
  17. public function behaviors()
  18. {
  19. return [
  20. 'verbs' => [
  21. 'class' => VerbFilter::className(),
  22. 'actions' => [
  23. 'delete' => ['post'],
  24. ],
  25. ],
  26. ];
  27. }
  28. /**
  29. * Lists all I18nMessage models.
  30. * @return mixed
  31. */
  32. public function actionIndex()
  33. {
  34. $searchModel = new I18nMessageSearch();
  35. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  36. Url::remember(Yii::$app->request->getUrl(), 'i18n-messages-filter');
  37. $languages = ArrayHelper::map(
  38. I18nMessage::find()->select('language')->distinct()->all(),
  39. 'language',
  40. 'language'
  41. );
  42. $categories = ArrayHelper::map(
  43. I18nSourceMessage::find()->select('category')->distinct()->all(),
  44. 'category',
  45. 'category'
  46. );
  47. return $this->render('index', [
  48. 'searchModel' => $searchModel,
  49. 'dataProvider' => $dataProvider,
  50. 'languages' => $languages,
  51. 'categories' => $categories
  52. ]);
  53. }
  54. /**
  55. * Creates a new I18nMessage model.
  56. * If creation is successful, the browser will be redirected to the 'view' page.
  57. * @return mixed
  58. */
  59. public function actionCreate()
  60. {
  61. $model = new I18nMessage();
  62. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  63. return $this->redirect(['index']);
  64. } else {
  65. return $this->render('create', [
  66. 'model' => $model,
  67. ]);
  68. }
  69. }
  70. /**
  71. * Updates an existing I18nMessage model.
  72. * If update is successful, the browser will be redirected to the 'view' page.
  73. * @param integer $id
  74. * @param string $language
  75. * @return mixed
  76. */
  77. public function actionUpdate($id, $language)
  78. {
  79. $model = $this->findModel($id, $language);
  80. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  81. return $this->redirect(Url::previous('i18n-messages-filter') ?: ['index']);
  82. } else {
  83. return $this->render('update', [
  84. 'model' => $model,
  85. ]);
  86. }
  87. }
  88. /**
  89. * Deletes an existing I18nMessage model.
  90. * If deletion is successful, the browser will be redirected to the 'index' page.
  91. * @param integer $id
  92. * @param string $language
  93. * @return mixed
  94. */
  95. public function actionDelete($id, $language)
  96. {
  97. $this->findModel($id, $language)->delete();
  98. return $this->redirect(['index']);
  99. }
  100. /**
  101. * Finds the I18nMessage model based on its primary key value.
  102. * If the model is not found, a 404 HTTP exception will be thrown.
  103. * @param integer $id
  104. * @param string $language
  105. * @return I18nMessage the loaded model
  106. * @throws NotFoundHttpException if the model cannot be found
  107. */
  108. protected function findModel($id, $language)
  109. {
  110. if (($model = I18nMessage::findOne(['id' => $id, 'language' => $language])) !== null) {
  111. return $model;
  112. } else {
  113. throw new NotFoundHttpException('The requested page does not exist.');
  114. }
  115. }
  116. }