Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/backend/controllers/i18n/
Upload File :
Current File : /var/www/html/ebookingdbkl/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;

/**
 * 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',
                        ],
                        '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 ($tid != null) {
            $lang = Yii::$app->request->post('DmgI18nMessage')['language'];

            /* check if language for the message already exist */
            $messLang = $messModel::find()->where([
                "AND",
                [
                    "id" => $tid
                ],
                [
                    "LIKE",
                    "language",
                    $lang
                ]
            ])
                ->asArray()
                ->one();

            if (empty($messLang)) {
                if ($messModel->load(Yii::$app->request->post())) {

                    $messModel->id = $tid;
                    $messModel->save();
                    /* server temp fix */
                }
            } else {
                Yii::$app->session->setFlash('error', Yii::t('app', 'Language already exist') . "!!!");
            }

            $this->redirect([
                "index"
            ]);
        } else {
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect([
                    'index'
                ]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                    'messModel' => $messModel
                ]);
            }
        }
    }

    /**
     * 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);
        
        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
            ]);
        }
        
        return $this->render('create', [
            'model' => $model,
            'messModel' => new DmgI18nMessage()
        ]);
    }
    

    /**
     * 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 actionDelete($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'
        ]);
    }

    /**
     * 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.');
        }
    }
}