Your IP : 216.73.216.79


Current Path : /var/www/html/aipa/frontend/controllers/
Upload File :
Current File : /var/www/html/aipa/frontend/controllers/SiteController.php

<?php

namespace frontend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use kartik\mpdf\Pdf;
use frontend\models\Delegation;
use frontend\models\Attendees;
use yii\web\Response;
use Da\QrCode\QrCode;
use yii\helpers\Url;
use backend\modules\activities\models\Activities;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class SiteController extends Controller {
    /* system parameter * */

    public $DYNAWEB_DEFAULT_ROUTE_PAGE;
    public $DYNAWEB_DEFAULT_ROUTE_PARAMS;

    public function beforeAction($action) {
        return parent::beforeAction($action);
    }

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup', 'error', 'stub'],
                'rules' => [
                    [
                        'actions' => ['signup', 'error', 'stub'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout', 'error'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex($frontendpage = null, $params = null) {
        $this->DYNAWEB_DEFAULT_ROUTE_PAGE = $frontendpage;
        $this->DYNAWEB_DEFAULT_ROUTE_PARAMS = $params;
        return $this->render('index');
    }

    public function actionPage() {
        return $this->render('page'); // placeholder
    }

    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionError() {
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            return $exception->getMessage();
        }
    }

    public function actionViewpdf($doc_id) {
        if (($model = \common\models\CourseDocument::findOne($doc_id)) !== null) {
            $pdf = new Pdf([
                'mode' => Pdf::MODE_BLANK,
                'filename' => $model->doc_title . '.pdf',
                'format' => Pdf::FORMAT_A4,
                'orientation' => Pdf::ORIENT_PORTRAIT,
                'destination' => Pdf::DEST_DOWNLOAD,
                'content' => $model->doc_content,
            ]);
            return $pdf->render();
        }
    }

    public function actionTranslate($language) {
        $cookies = Yii::$app->response->cookies;
        Yii::$app->language = $language;
        $cookies->add(new \yii\web\Cookie(['name' => 'language', 'value' => $language]));
        return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    }

    public function actionGetparticipant($type) {
        $output = '<option value="">- Please choose -</option>';
        $model = Delegation::find()
                ->where(['=', 'created_by', Yii::$app->user->identity->id])
                ->andwhere(['=', 'created_user_type', 'frontend_user'])
                ->andwhere(['=', 'delegation_type', $type])
                ->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $output .= '<option value="' . $row->delegation_id . '">' . $row->delegation_name . '</option>';
            }
        }
        return $output;
    }

    public function actionGenerateqr($url) {
        header("Content-type: image/png");
        $qr = Yii::$app->get('qr');
        Yii::$app->response->format = Response::FORMAT_RAW;
        Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
        return $qr->setText($url)->writeString();
    }

    public function actionPrintqr($code) {
        $content = '';
        $title = '';

        if (($model = Attendees::findOne(['attendees_code' => $code])) !== null) {
            $url = 'https://' . $_SERVER['SERVER_NAME'] . '/portal-main/attendees-mobile-menu?code=' . $model->attendees_code;
            $qrcode = '<img src="' . Url::to(['site/generateqr', 'url' => $url]) . '" style="height:200px;"/>';

            $title = strtoupper($model->user->delegation_name);
            $content = '<div style="text-align:center;">
                    ' . $qrcode . '
                    <h3>' . strtoupper($model->user->delegation_name) . '</h3>
                    <h3>' . strtoupper(\backend\modules\refdynawebv2\models\Ref::getDesc('ATTENDEES_TYPE', $model->attendees_type)) . '</h3>
                    <h3>' . strtoupper(\backend\modules\refdynawebv2\models\Ref::getDesc('COUNTRY', $model->user->delegation_country)) . '</h3> 
                    <h3>' . strtoupper($model->activity->activity_name) . '</h3>
                </div>';
        }

        $pdf = new Pdf([
            'mode' => Pdf::MODE_BLANK,
            'filename' => $title . '.pdf',
            'format' => Pdf::FORMAT_A4,
            'orientation' => Pdf::ORIENT_PORTRAIT,
            'destination' => Pdf::DEST_BROWSER,
            'content' => $content,
        ]);
        return $pdf->render();
    }

    public function actionDownloaddelegationpdf($activity_id) {
        $title = Activities::getname($activity_id) . ' ' . Yii::t('app', 'Attendees');
        $content = '<div style="text-align:center;">
            <!-- <img src="https://' . $_SERVER['SERVER_NAME'] . '/assets/img/logo-aipa.png" style="width:50%;"> -->
            <h4 style="line-height:1.5;margin:40px 0;"><b>' . strtoupper($title) . '</b></h4>
        </div>';

        $model = Delegation::find()
                ->innerJoin('attendees', 'attendees.attendees_user_id=delegation.delegation_id')
                ->where(['=', 'attendees_activity_id', $activity_id])
                ->andwhere(['=', 'delegation.created_by', Yii::$app->user->identity->id])
                ->andwhere(['=', 'delegation.created_user_type', 'frontend_user'])
                //->andWhere(['=', 'delegation_data_status', 'confirmed'])
                ->orderby(['delegation_name' => SORT_ASC])
                ->all();

        if (!empty($model)) {
            $content .= '<table class="table table-borderedx">
                <thead>
                    <tr>
                        <th style="text-align:center;width:10%">NO.</th>
                        <th style="width:30%">NAME</th>
                        <th style="width:20%">CATEGORY</th>
                        <th style="width:20%">CONTACT PHONE NUMBER</th>
                        <th style="width:20%">CONTACT EMAIL</th>
                    </tr>
                </thead>
                <tbody></tbody>';
            $count = 0;
            foreach ($model as $row) {
                $count++;
                $content .= '<tr>
                        <td style="text-align:center;">' . $count . '</td>
                        <td>' . strtoupper($row->delegation_salutation . ' ' . $row->delegation_name) . '</td>
                        <td>' . strtoupper(\backend\modules\refdynawebv2\models\Ref::getDesc('PARTICIPANT_CATEGORY', $row->delegation_category)) . '</td>
                        <td>' . $row->delegation_contact_phone_country_code . '' . $row->delegation_contact_phone . '</td>
                        <td>' . $row->delegation_contact_email . '</td>
                    </tr>';
            }
            $content .= '</tbody></table>';
        } else {
            $content .= 'No attendees yet';
        }

        $pdf = new Pdf([
            'mode' => Pdf::MODE_BLANK,
            'filename' => $title . '.pdf',
            'format' => Pdf::FORMAT_A4,
            'orientation' => Pdf::ORIENT_PORTRAIT,
            'destination' => Pdf::DEST_BROWSER,
            'content' => $content,
            'cssInline' => '.table th{background-color:white;border:0 !important;}
                .table thead, .table thead tr, .table thead tr th{border:0 !important;}
                .table td{padding:10px;vertical-align:top;line-height:1.5;}',
        ]);
        return $pdf->render();
    }

    public function actionDownloaddelegationexcel($activity_id) {

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $sheet->setCellValue('A1', 'TITLE');
        $sheet->setCellValue('B1', 'NAME AS PER PASSPORT');
        $sheet->setCellValue('C1', 'PARTICIPANT CATEGORY');
        $sheet->setCellValue('D1', 'CONTACT PHONE NUMBER');
        $sheet->setCellValue('E1', 'CONTACT EMAIL');

        $model = Delegation::find()
                ->innerJoin('attendees', 'attendees.attendees_user_id=delegation.delegation_id')
                ->where(['=', 'attendees_activity_id', $activity_id])
                ->andwhere(['=', 'delegation.created_by', Yii::$app->user->identity->id])
                ->andwhere(['=', 'delegation.created_user_type', 'frontend_user'])
                //->andWhere(['=', 'delegation_data_status', 'confirmed'])
                ->orderby(['delegation_name' => SORT_ASC])
                ->all();
        $count = 1;
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                $sheet->setCellValue('A' . $count, strtoupper($row->delegation_salutation));
                $sheet->setCellValue('B' . $count, strtoupper($row->delegation_name));
                $sheet->setCellValue('C' . $count, strtoupper(\backend\modules\refdynawebv2\models\Ref::getDesc('PARTICIPANT_CATEGORY', $row->delegation_category)));

                $sheet->setCellValue('D' . $count, $row->delegation_contact_phone_country_code . $row->delegation_contact_phone);
                $sheet->setCellValue('E' . $count, $row->delegation_contact_email);
            }
        }

        $filename = Activities::getname($activity_id) . '.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        die();
    }
    
    public function actionApi($code) {
        if (($model = \backend\modules\api\models\Api::findOne(['api_code' => $code])) !== null) {
            header('Content-Type: application/json');
            eval('?>' . $model->api_content);
            die();
        }
    }
}