Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/controllers/program/
Upload File :
Current File : /var/www/html/sprm/backend/controllers/program/ProgramDivisionApplicationController.php

<?php

namespace backend\controllers\program;

use Yii;
use backend\models\program\ProgramDivisionApplication;
use backend\models\program\ProgramDivisionApplicationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;
use kartik\mpdf\Pdf;

/**
 * ProgramDivisionApplicationController implements the CRUD actions for ProgramDivisionApplication model.
 */
class ProgramDivisionApplicationController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete', 'download','updatenotes'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single ProgramDivisionApplication 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 ProgramDivisionApplication model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ProgramDivisionApplication();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = 'admin:' . Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = 'admin:' . Yii::$app->user->identity->id;
        $model->status = 'approve';
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing ProgramDivisionApplication 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 = 'admin:' . Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render('update', ['model' => $model,]);
    }
    
    public function actionUpdatenotes($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = 'admin:' . Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            
            if ($model->status == 'approve' && !empty($model->applicant_email)) {
                $emailFrom = 'noreply.testemailweb@gmail.com';
                $emailSubject = 'Permohonan Program / Ceramah / Taklimat di SPRM';
                $emailContent = '<p>Salam Sejahtera ' . $model->applicant_name . ',</p>
                    <p>Permohonan anda untuk Program / Ceramah / Taklimat di SPRM pada ' . date("d-m-Y", strtotime($model->program_start_date)) . ' telah diluluskan.</p>
                    <p>Catatan : ' . $model->notes . '</p>
                    <p>Terima kasih</p>';
                Yii::$app->mailer->compose()->setFrom($emailFrom)->setTo($model->applicant_email)
                        ->setSubject($emailSubject)->setHtmlBody($emailContent)->send();
                
            } else if ($model->status == 'reject' && !empty($model->applicant_email)) {
                
                $emailFrom = 'noreply.testemailweb@gmail.com';
                $emailSubject = 'Permohonan Program / Ceramah / Taklimat di SPRM';
                $emailContent = '<p>Salam Sejahtera ' . $model->applicant_name . ',</p>
                    <p>Permohonan anda untuk Program / Ceramah / Taklimat di SPRM pada ' . date("d-m-Y", strtotime($model->program_start_date)) . ' telah ditolak.</p>
                    <p>Catatan : ' . $model->notes . '</p>
                    <p>Terima kasih</p>';
                Yii::$app->mailer->compose()->setFrom($emailFrom)->setTo($model->applicant_email)
                        ->setSubject($emailSubject)->setHtmlBody($emailContent)->send();
            }
            
            return $this->redirect(['index']);
        }
        return $this->render('updatenotes', ['model' => $model,]);
    }

    /**
     * Deletes an existing ProgramDivisionApplication 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 ProgramDivisionApplication model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ProgramDivisionApplication the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = ProgramDivisionApplication::findOne($id)) !== null) {
            return $model;
        }
        throw new NotFoundHttpException('The requested page does not exist.');
    }
    
    public function actionDownload($id) {
        
        $model = $this->findModel($id);
        if(!empty($model)){
            
            switch ($model->program_category){
                case 'PROCAT1':case 'PROCAT3':case 'PROCAT10':case 'PROCAT11':case 'PROCAT4':
                    $category = 'PENGUATKUASAAN';
                    break;
                default:
                    $category = 'PENCEGAHAN';
                    break;
            }
            
            switch ($model->status){
                case 'approve':
                    $status = 'Lulus';
                    break;
                case 'reject':
                    $status = 'Tidak Lulus';
                    break;
                default:
                    $status = 'Sedang Diproses';
                    break;
            }
            
            
            $header = '<table id="table-header" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><img src="https://www.sprm.gov.my/admin/files/sprm/assets/images/sprm/favicon.png" height="50"></td>
                        <td align="right"><b>Permohonan Program / Ceramah / Taklimat </b><br/>'.$category.'</td>
                    </tr>
                </table>';
            
            
            $content = '<br><br><table border="0" width="100%" cellpadding="5" cellspacing="0">
                    <tr> 
                        <td valign="top" colspan="3" style="text-align:center;color:white;font-weight:bold;background-color:black;">Maklumat Agensi / Organisasi </td>
                    </tr>
                    <tr>
                        <td valign="top">Nama Agensi / Organisasi</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . strtoupper($model->agency_name) . '</td>
                    </tr>
                    <tr>
                        <td valign="top">No. Telefon Pejabat</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . $model->agency_phone . '</td>
                    </tr>
                    <tr>
                        <td valign="top">Alamat Agensi/Organisasi</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . strtoupper($model->agency_address) . '</td>
                    </tr>
                    <tr> 
                        <td valign="top" colspan="3" style="text-align:center;color:white;font-weight:bold;background-color:black;"> Maklumat Pemohon </td>
                    </tr>
                    <tr>
                        <td valign="top">Nama Pemohon</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">'. \backend\models\Ref::getDesc('PROGRAM_APPTITLE', $model->applicant_title) .' '.strtoupper($model->applicant_name) . '</td>
                    </tr>
                    <tr>
                        <td valign="top">No. Telefon Pemohon</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . $model->applicant_phone . '</td>
                    </tr>
                    <tr>
                        <td valign="top">Emel Pemohon</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . $model->applicant_email . '</td>
                    </tr>
                    <tr> 
                        <td valign="top" colspan="3" style="text-align:center;color:white;font-weight:bold;background-color:black;"> Maklumat Program </td>
                    </tr>
                    <tr>
                        <td valign="top">Jenis Program</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . strtoupper(\backend\models\Ref::getDesc('PROGRAM_TYPE', $model->program_type)) . '</td>
                    </tr>
                    <tr>
                        <td valign="top">Kategori Program</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">' . strtoupper(\backend\models\Ref::getDesc('PROGRAM_CATEGORY', $model->program_category)) . '</td>
                    </tr>
                    <tr>
                        <td valign="top">Tarikh & Masa Mula Program</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">'. $model->program_start_date .' ['. strtoupper($model->program_start_time) . '] </td>
                    </tr>
                    <tr>
                        <td valign="top">Tarikh & Masa Akhir Program</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">'. $model->program_end_date .' ['. strtoupper($model->program_end_time) . '] </td>
                    </tr>
                    <tr> 
                        <td valign="top" colspan="3" style="text-align:center;color:white;font-weight:bold;background-color:black;"> Status Permohonan </td>
                    </tr>
                    <tr>
                        <td valign="top">Status</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">'. strtoupper($status) . ' </td>
                    </tr>
                    <tr>
                        <td valign="top">Catatan</td>
                        <td valign="top">&nbsp;&nbsp;:&nbsp;&nbsp;</td>
                        <td valign="top">'. strtoupper($model->notes) . ' </td>
                    </tr>
                    
                    </table>';
                                
            $footer = '<table id="table-footer" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <tr><td align="center">Suruhanjaya Pencegahan Rasuah Malaysia (SPRM), ' .
                       ' No. 2, Lebuh Wawasan,  Presint 7, 62250 Putrajaya. </td>
                    </tr>
                </table>';
            
            // setup kartik\mpdf\Pdf component
            $pdf = new Pdf([
                'mode' => Pdf::MODE_CORE,
                'filename' => 'Permohonan Program / Ceramah / Taklimat oleh ' . $model->agency_name . '.pdf',
                'format' => Pdf::FORMAT_A4,
                'orientation' => Pdf::ORIENT_PORTRAIT,
                'destination' => Pdf::DEST_BROWSER,
                'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
                'cssInline' => 'body,table td{font-size:11px;Arial, Helvetica, sans-serif;line-height:1.7em;}
                    #table-header{padding-bottom:10px;}
                    #table-footer{padding-top:10px;}
                    #table-product th{background-color:black;color:white;text-align:center;}
                    #table-product tr:nth-child(odd) td{background-color:white;}
                    #table-product tr:nth-child(even) td{background-color:whitesmoke;}',
                'methods' => ['SetHeader' => [$header], 'SetFooter' => $footer],
                'content' => $content,
                'options' => ['setAutoTopMargin' => 'stretch'],
            ]);
            return $pdf->render();
        }
    }

}