Your IP : 216.73.216.79


Current Path : /var/www/html/akm-ipw/backend/modules/chat/controllers/
Upload File :
Current File : /var/www/html/akm-ipw/backend/modules/chat/controllers/ChatMenuController.php

<?php

namespace backend\modules\chat\controllers;

use Yii;
use backend\modules\chat\models\ChatMenu;
use backend\modules\chat\models\ChatMenuSearch;
use backend\modules\chat\models\ChatAnswerBot;
use backend\modules\chat\models\ChatQuestionTag;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use yii\helpers\Url;

/**
 * ChatMenuController implements the CRUD actions for ChatMenu model.
 */
class ChatMenuController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all ChatMenu models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new ChatMenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /**
     * Displays a single ChatMenu 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 ChatMenu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ChatMenu();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->sorting = 0;
        if ($model->load(Yii::$app->request->post())) {
            $model->thumbnail = UploadedFile::getInstance($model, 'thumbnail');
            if (!empty($model->thumbnail)) {
                $thumbnail = date("YmdHis") . '.' . $model->thumbnail->extension;
                if ($model->thumbnail->saveAs('../uploads/' . $thumbnail))
                    $model->thumbnail = $thumbnail;
            }
            if ($model->save()) {

                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {

                        $link_text = $_POST['new_link_text_' . $newdatano];
                        $link_answer = $_POST['new_link_answer_' . $newdatano];
                        $question_tag_id = 0;

                        if (($modelQuestion = ChatQuestionTag::findOne(['question_tag_name' => $link_text])) !== null) {
                            $question_tag_id = $modelQuestion->question_tag_id;
                        } else {
                            $modelQuestion = new ChatQuestionTag();
                            $modelQuestion->menu_chat_id = $model->id;
                            $modelQuestion->create_dttm = date("Y-m-d H:i:s");
                            $modelQuestion->question_tag_language = 'MY';
                            $modelQuestion->question_tag_name = $link_text;

                            $modelQuestion->status = $_POST['new_link_status_' . $newdatano];
                            $modelQuestion->sorting = $_POST['new_link_sorting_' . $newdatano];
                            $modelQuestion->menu_name = $_POST['new_link_menu_name_' . $newdatano];
                            $modelQuestion->type = $_POST['new_type_' . $newdatano];

                            if ($modelQuestion->save(false))
                                $question_tag_id = $modelQuestion->question_tag_id;
                        }


                        $modelAnswer = new ChatAnswerBot();
                        $modelAnswer->php_status = 0;
                        $modelAnswer->create_dttm = date("Y-m-d H:i:s");
                        $modelAnswer->answer_bot_language = 'MY';
                        $modelAnswer->question_id = 0;
                        $modelAnswer->answer_bot_description = $link_answer;
                        $modelAnswer->question_tag_id = $question_tag_id;
                        $modelAnswer->save(false);
                    }
                }
                $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,]);
    }

    /**
     * Updates an existing ChatMenu 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->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $original_thumbnail = $model->thumbnail;
        if ($model->load(Yii::$app->request->post())) {
            $model->thumbnail = UploadedFile::getInstance($model, 'thumbnail');
            if (!empty($model->thumbnail)) {
                $thumbnail = date("YmdHis") . '.' . $model->thumbnail->extension;
                if ($model->thumbnail->saveAs('../uploads/' . $thumbnail))
                    $model->thumbnail = $thumbnail;
            } else {
                $model->thumbnail = $original_thumbnail;
            }
            if ($model->save()) {
                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {
                        $link_text = $_POST['new_link_text_' . $newdatano];
                        $link_answer = $_POST['new_link_answer_' . $newdatano];
                        $question_tag_id = 0;

                        if (($modelQuestion = ChatQuestionTag::findOne(['question_tag_name' => $link_text])) !== null) {
                            $question_tag_id = $modelQuestion->question_tag_id;
                        } else {
                            $modelQuestion = new ChatQuestionTag();
                            $modelQuestion->menu_chat_id = $model->id;
                            $modelQuestion->create_dttm = date("Y-m-d H:i:s");
                            $modelQuestion->question_tag_language = 'MY';
                            $modelQuestion->question_tag_name = $link_text;

                            $modelQuestion->status = $_POST['new_link_status_' . $newdatano];
                            $modelQuestion->sorting = $_POST['new_link_sorting_' . $newdatano];
                            $modelQuestion->menu_name = $_POST['new_link_menu_name_' . $newdatano];
                            $modelQuestion->type = $_POST['new_type_' . $newdatano];


                            if ($modelQuestion->save(false))
                                $question_tag_id = $modelQuestion->question_tag_id;
                        }
                        $modelAnswer = new ChatAnswerBot();
                        $modelAnswer->php_status = 0;
                        $modelAnswer->create_dttm = date("Y-m-d H:i:s");
                        $modelAnswer->answer_bot_language = 'MY';
                        $modelAnswer->question_id = 0;
                        $modelAnswer->answer_bot_description = $link_answer;
                        $modelAnswer->question_tag_id = $question_tag_id;
                        $modelAnswer->save(false);
                    }
                }

                if (!empty($_POST['olddata'])) {
                    foreach ($_POST['olddata'] as $olddatano) {
                        $old_link_text = $_POST['old_link_text_' . $olddatano];
                        $old_link_answer = $_POST['old_link_answer_' . $olddatano];

                        if (($modelQuestion = ChatQuestionTag::findOne([$olddatano])) !== null) {
                            $modelQuestion->question_tag_name = $old_link_text;
                            $modelQuestion->status = $_POST['old_link_status_' . $olddatano];
                            $modelQuestion->sorting = $_POST['old_link_sorting_' . $olddatano];
                            $modelQuestion->menu_name = $_POST['old_link_menu_name_' . $olddatano];
                            $modelQuestion->type = $_POST['old_type_' . $olddatano];
                            $modelQuestion->save(false);
                        }

                        ChatAnswerBot::updateAll(['answer_bot_description' => $old_link_answer], ['question_tag_id' => $olddatano]);
                    }
                }
                $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,]);
    }

    /**
     * Deletes an existing ChatMenu 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 = $_POST['id'];
        $status = 'failed';
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

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

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionDeletelink($id) {
        if (($modelQuestion = ChatQuestionTag::findOne(['question_tag_id' => $id])) !== null) {
            $modelQuestion->menu_chat_id = NULL;
            $modelQuestion->save(false);
        }
    }

    public function actionSorting($data = '') {
        $count = 0;
        $data_arr = explode(',', $data);
        if (!empty($data_arr)) {
            foreach ($data_arr as $slider_id) {
                $model = ChatMenu::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->sorting = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }

}