Your IP : 216.73.216.79


Current Path : /var/www/html/jpp/backend/controllers/i18n/
Upload File :
Current File : /var/www/html/jpp/backend/controllers/i18n/DmgI18nController.php

<?php

namespace backend\controllers\i18n;

use Yii;
use backend\models\i18n\DmgI18nSourceMessage;
use backend\models\i18n\DmgI18nSourceMessageSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\i18n\DmgI18nMessage;
use yii\filters\AccessControl;
use common\components\AccessRule;
use yii\helpers\Url;

/**
 * DmgI18nController implements the CRUD actions for DmgI18nSourceMessage model.
 */
class DmgI18nController extends Controller {

    /**
     *
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className()
                ],
                'rules' => [
                    [
                        'actions' => [
                            'index',
                            'create',
                            'update',
                            'update-translation',
                            'view',
                            'delete',
                            'delete2',
                            'addtranslation',
                            'updatetranslation',
                            'deletetranslation'
                        ],
                        'allow' => true,
                        'roles' => [
                            '@'
                        ]
                    ]
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => [
                        'POST'
                    ]
                ]
            ]
        ];
    }

    /**
     * Lists all DmgI18nSourceMessage models.
     *
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new DmgI18nSourceMessageSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        /* message model */
        $messModel = new DmgI18nMessage();

        /* language code */
        $langSwitch = $messModel::$languages;

        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'model' => $messModel, 'lang' => $langSwitch]);
    }

    /**
     * Displays a single DmgI18nSourceMessage model.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', ['model' => $this->findModel($id)]);
    }

    /**
     * Creates a new DmgI18nSourceMessage model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionCreate($tid = null) {
        $model = new DmgI18nSourceMessage();
        $messModel = new DmgI18nMessage();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model, 'messModel' => $messModel]);
    }

    public function actionAddtranslation($id) {
        $model = new DmgI18nMessage();
        $modelParent = DmgI18nSourceMessage::findOne($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->id = $id;
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('addtranslation', ['model' => $model, 'modelParent' => $modelParent]);
    }

    public function actionUpdatetranslation($id, $language, $translation) {
        $model = DmgI18nMessage::findOne(['id' => $id, 'language' => $language, 'translation' => $translation]);
        $modelParent = DmgI18nSourceMessage::findOne($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->id = $id;
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('updatetranslation', ['model' => $model, 'modelParent' => $modelParent]);
    }

    /**
     * Updates an existing DmgI18nSourceMessage model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     * @return mixed
     */
    /* public function actionUpdateTranslation($tid, $lang) {
      $model = DmgI18nMessage::find()->where(['id' => $tid, 'language' => $lang])->one();

      if ($model->load(Yii::$app->request->post())) {

      if ($model->save()) {
      Yii::$app->session->setFlash('success', Yii::t('app', 'Translation Updated'));
      } else {
      Yii::$app->session->setFlash('success', Yii::t('app', 'Failed to save translation'));
      }

      return $this->redirect([
      'index',
      'id' => $model->id
      ]);
      }
      } */

    /**
     * Updates an existing DmgI18nSourceMessage model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $messModel = new DmgI18nMessage();
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model, 'messModel' => $messModel]);
    }

    /**
     * Deletes an existing DmgI18nSourceMessage model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionDelete2($id) {
        $lang = Yii::$app->request->post('lang');
        $trans = Yii::$app->request->post('trans');

        if (!empty($lang) && !empty($trans)) {
            Yii::$app->db->createCommand()
                    ->delete('dmg_i18n_message', 'language = N\'' . $lang . '\' AND translation = N\'' . $trans . '\'')
                    ->execute();

            Yii::$app->session->setFlash('warning', Yii::t('app', 'Data Have Been Deleted'));
            return $this->redirect([
                        'index'
            ]);
        } else {
            $fModel = DmgI18nMessage::find()->where([
                        "id" => $id
                    ])
                    ->asArray()
                    ->all();

            if (count($fModel) > 0) {
                Yii::$app->db->createCommand()
                        ->delete('dmg_i18n_message', 'id = ' . $id . '')
                        ->execute();
            }

            $this->findModel($id)->delete();
        }
        return $this->redirect([
                    'index'
        ]);
    }

    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        if (($model = DmgI18nSourceMessage::findOne($id)) !== null) {
            DmgI18nMessage::deleteAll(['id' => $id]);
            $model->delete();
            $status = 'success';
        }
        return $status;
    }

    public function actionDeletetranslation() {
        $id = $_POST['id'];
        $language = $_POST['language'];
        $translation = $_POST['translation'];
        $status = 'failed';
        $model = DmgI18nMessage::findOne(['id' => $id, 'language' => $language, 'translation' => $translation]);
        if ($model->delete())
            $status = 'success';
        return $status;
    }

    /**
     * Finds the DmgI18nSourceMessage model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     * @return DmgI18nSourceMessage the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = DmgI18nSourceMessage::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}