Your IP : 216.73.216.79


Current Path : /var/www/html/mnrb/backend/modules/feedback/controllers/
Upload File :
Current File : /var/www/html/mnrb/backend/modules/feedback/controllers/FeedbackMainController.php

<?php

namespace backend\modules\feedback\controllers;

use Yii;
use backend\modules\feedback\models\FeedbackMain;
use backend\modules\feedback\models\FeedbackMessages;
use backend\modules\feedback\models\FeedbackMainSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 * FeedbackMainController implements the CRUD actions for FeedbackMain model.
 */
class FeedbackMainController extends Controller {

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

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

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

    /**
     * Displays a single FeedbackMain model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        $modelmain = $this->findModel($id);
        $modelmessages = FeedbackMessages::find()->where(['feedback_id' => $id])->orderBy(['id' => SORT_ASC])->all();
        FeedbackMessages::updateAll(['read_status' => '1'], ['AND',
            ['=', 'feedback_id', $id],
            ['=', 'send_to', 'backend_user']
        ]);

        if (isset($_POST['reply_content']) && !empty($_POST['reply_content'])) {
            //insert into table feedback_message
            $model = new FeedbackMessages();
            $model->feedback_id = $id;
            $model->message = $_POST['reply_content'];
            $model->read_status = '0';
            $model->send_to = 'frontend_user';
            $model->created_at = date("Y-m-d H:i:s");
            $model->created_by = Yii::$app->user->identity->id;
            $model->created_user_type = 'backend_user';
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $id]);
            }
        }
        return $this->render('view', ['modelmain' => $modelmain, 'modelmessages' => $modelmessages]);
    }

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

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

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

    /**
     * Updates an existing FeedbackMain 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);

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

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

    public function actionUpdatestatus($id) {
        $model = $this->findModel($id);
        $model->status = 'closed';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->save()) {
            return $this->redirect(['index']);
        }
    }

    /**
     * Deletes an existing FeedbackMain 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 FeedbackMain model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return FeedbackMain the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = FeedbackMain::findOne($id)) !== null) {
            return $model;
        }

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

    public function actionReport($report_type) {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $sheet->setCellValue('A1', 'TICKET NUMBER');
        $sheet->setCellValue('B1', 'FEEDBACK TYPE');
        $sheet->setCellValue('C1', 'FEEDBACK CATEGORY');
        $sheet->setCellValue('D1', 'FEEDBACK');
        $sheet->setCellValue('E1', 'SEND AT');
        $sheet->setCellValue('F1', 'SEND BY');
        $sheet->setCellValue('G1', 'STATUS');
        $count = 1;
        $report_name = '';
        if ($report_type == 'all') {
            $report_name = 'Report All Feedback';
            $model = FeedbackMain::find()->orderBy(['id' => SORT_DESC])->all();
        }
        if ($report_type == 'closed') {
            $report_name = 'Report Closed Feedback';
            $model = FeedbackMain::find()->where(['=', 'status', 'closed'])->orderBy(['id' => SORT_DESC])->all();
        }
        if ($report_type == 'open') {
            $report_name = 'Report Open Feedback';
            $model = FeedbackMain::find()->where(['=', 'status', 'open'])->orderBy(['id' => SORT_DESC])->all();
        }
        if ($report_type == 'unread') {
            $report_name = 'Report Unread Feedback';
            $model = FeedbackMain::find()->innerJoin('feedback_messages', 'feedback_main.id=feedback_messages.feedback_id')
                            ->where(['=', 'feedback_messages.read_status', '0'])
                            ->andwhere(['=', 'feedback_messages.send_to', 'backend_user'])
                            ->orderBy(['id' => SORT_DESC])->all();
        }

        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, $row->ticket_no);
                $sheet->setCellValue('B' . $count, \backend\modules\refdynawebv2\models\Ref::getDesc('FEEDBACKTYPE', $row->type, 'descr_en'));
                $sheet->setCellValue('C' . $count, \backend\modules\refdynawebv2\models\Ref::getDesc('FEEDBACKCAT', $row->cat, 'descr_en'));
                $sheet->setCellValue('D' . $count, $row->feedback);
                $sheet->setCellValue('E' . $count, $row->created_at);
                $sheet->setCellValue('F' . $count, \backend\modules\fruserdynav2\models\FrontendUser::getname($row->created_by));
                $sheet->setCellValue('G' . $count, \backend\modules\refdynawebv2\models\Ref::getDesc('FEEDBACKSTATUS', $row->status, 'descr_en'));
            }
        }
        $writer = new Xlsx($spreadsheet);
        header('Content-Type: application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $report_name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
        exit();
    }

}