Your IP : 216.73.216.79


Current Path : /var/www/html/maiwp/backend/modules/chat.backup/controllers/
Upload File :
Current File : /var/www/html/maiwp/backend/modules/chat.backup/controllers/ChatQuestionController.php

<?php

namespace backend\modules\chat\controllers;

use Yii;
use backend\modules\chat\models\ChatQuestion;
use backend\modules\chat\models\ChatQuestionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\modules\chat\models\ChatAnswerBot;
use backend\modules\chat\models\ChatQuestionTag;

/**
 * ChatQuestionController implements the CRUD actions for ChatQuestion model.
 */
class ChatQuestionController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single ChatQuestion model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->question_id]);
        }

        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing ChatQuestion model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $model_answer_bot = ChatAnswerBot::find()
                ->where(['=', 'question_id', $model->question_id])
                ->one();

        if (!$model_answer_bot) {
            $model_answer_bot = new ChatAnswerBot();
            $model_question_tag = new ChatQuestionTag();
        } else {
            $model_question_tag = ChatQuestionTag::find()
                    ->where(['=', 'question_tag_id', $model_answer_bot->question_tag_id])
                    ->one();
        }


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            //Check Question Tag
            $questionTagId = 0;
            $questionTagPost = Yii::$app->request->post('ChatQuestionTag');
            $model_question_tag = ChatQuestionTag::find()
                    ->where(['=', 'question_tag_name', $questionTagPost['question_tag_name']])
                    ->andWhere(['=', 'question_tag_language', $model->question_language])
                    ->one();

            //Question Tag not exists
            if (!$model_question_tag) {
                $model_question_tag = new ChatQuestionTag();
                $model_question_tag->load(Yii::$app->request->post());
                $model_question_tag->question_tag_language = $model->question_language;
                $model_question_tag->save();
                $questionTagId = $model_question_tag->question_tag_id;
            } else {
                $questionTagId = $model_question_tag->question_tag_id;
            }

            //Save Answer Bot
            $model_answer_bot->load(Yii::$app->request->post());
            $model_answer_bot->question_tag_id = $questionTagId;
            $model_answer_bot->question_id = $id;
            $model_answer_bot->answer_bot_language = $model->question_language;
            $model_answer_bot->save();

            //Mark Question as reviewed
            $model->question_review = 1;
            $model->save();

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

        return $this->render('update', [
                    'model' => $model,
                    'model_question_tag' => $model_question_tag,
                    'model_answer_bot' => $model_answer_bot,
        ]);
    }

    /**
     * Deletes an existing ChatQuestion model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

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

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

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

}