Your IP : 216.73.216.79


Current Path : /var/www/html/school/backend/controllers/
Upload File :
Current File : /var/www/html/school/backend/controllers/StaffController.php

<?php

namespace backend\controllers;

use backend\models\ProfileForm;
use Yii;
use backend\models\Staff;
use backend\models\StaffSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
//use backend\models\ProfileForm;
use backend\models\Ref;
use backend\models\User;
use kartik\mpdf\Pdf;
use arturoliveira\ExcelView;

/**
 * StaffController implements the CRUD actions for Staff model.
 */
class StaffController extends Controller {

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

    /**
     * Lists all Staff models.
     * @return mixed
     */
    public function actionSupervisorView() {
        $searchModel = new StaffSearch();
        $searchModel->supervisor_id = Yii::$app->user->identity->staff->id;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    public function actionIndex() {
        $searchModel = new StaffSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        if (Yii::$app->request->isGet) {
            if (isset($_GET['secondbutton'])) {
                $dataProviderexcel = $searchModel->searchexcel(Yii::$app->request->queryParams);

                ExcelView::widget([
                    'dataProvider' => $dataProviderexcel,
                    'fullExportType' => 'xlsx', //can change to html,xls,csv and so on
                    'grid_mode' => 'export',
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],
                        'staff_no',
                        'staff_name',
                        'staff_ic',
                        'job_title',
                        'projectnm',
                    ],
                ]);
            }
        }

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

    public function actionMyProfile() {
        $model = new ProfileForm();
        $id = Yii::$app->user->identity->staff->id;
        $staff = Staff::findOne($id);
        $user = \common\models\User::findOne($staff->userid);

        if ($model->load(Yii::$app->request->post())) {

            if ($model->validate()) {
                
                $model->file = UploadedFile::getInstance($model, 'file');
                
                if (!empty($model->file)) {
                    $model->file->saveAs('images/profile_pic/' . $id . '.' . $model->file->extension);
                    $staff->staff_photo = $id . '.' . $model->file->extension;
                }

                $staff->staff_name = $model->staff_name;
                $staff->staff_ic = $model->staff_ic;
                $staff->gender = $model->gender;
                $staff->phone_no = $model->phone_no;
                $staff->mobile_no = $model->mobile_no;
                $staff->office_no = $model->office_no;
                $staff->office_ext_no = $model->office_ext_no;
                $staff->save(false);
                
                $user->email = $model->email;
                $user->save(false);
                Yii::$app->session->setFlash('success', Yii::t('app', 'Profil anda berjaya dikemaskini'));

                // return $this->redirect(['my-profile']);

            }
        }
        
        $model->username = $user->username;
        $model->staff_name = $staff->staff_name;
        $model->staff_ic = $staff->staff_ic; 
        $model->gender = $staff->gender;
        $model->email = $user->email;
        $model->phone_no = $staff->phone_no;
        $model->mobile_no = $staff->mobile_no;
        $model->office_no = $staff->office_no;
        $model->office_ext_no = $staff->office_ext_no;
        $model->staff_photo = $staff->staff_photo;
                
        return $this->render('profile', ['model' => $model, 'user' => $user, 'staff' => $staff]);
    }

    /**
     * Displays a single Staff model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Staff model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Staff();
        $muser = new User();
        $model->scenario = 'manageStaff';
        $model->staff_no = $model->jrtno();
        if ($model->load(Yii::$app->request->post())) {
            $muser->username = $model->staff_no;
            $muser->email = $model->company_email;

            if ($model->user_status !== '1')
                $model->user_status = '0';
            $muser->status = $model->user_status;
            if ($muser->save()) {
                $model->userid = $muser->id;
                if ($model->save())
                    return $this->redirect(['update', 'id' => $model->id]);
            }
        } else {
            $model->user_status = 1;
            return $this->render('create', ['model' => $model, 'svname' => '', 'hodname' => '',]);
        }
    }
    
    public function actionCreatebulksv() {
        $model = new Staff();
        $model->scenario = 'bulksv';
        if ($model->load(Yii::$app->request->post())) {
            $arr = Yii::$app->request->post('Staff');
            $staff_name = explode(",", $arr['staff_name']);
            if (is_array($staff_name)) {
                foreach ($staff_name as $value) {
                    $mbulky = Staff::findOne($value);
                    $mbulky->id = $value;
                    $mbulky->supervisor_id = $arr['supervisor_id'];
                    if ($mbulky->save()) {
                        
                    } else {
                        var_dump($mbulky->errors);
                        exit;
                    }
                }
            } 
            return $this->redirect(['indexbulkysv', 'id' => $model->id]);
        } else {
            $model->user_status = 1;
            return $this->render('createbulksv', ['model' => $model, 'svname' => '', 'hodname' => '',]);
        }
    }
    
    public function actionCreatebulkhod() {
        $model = new Staff();
        $model->scenario = 'bulkhod';
        if ($model->load(Yii::$app->request->post())) {
            $arr = Yii::$app->request->post('Staff');
            $staff_name = explode(",", $arr['staff_name']);
            if (is_array($staff_name)) {
                foreach ($staff_name as $value) {
                    $mbulky = Staff::findOne($value);
                    $mbulky->id = $value;
                    $mbulky->hod_id = $arr['hod_id'];
                    if ($mbulky->save()) {
                        
                    } else {
                        var_dump($mbulky->errors);
                        exit;
                    }
                }
            } 
            return $this->redirect(['indexbulkyhod', 'id' => $model->id]);
        } else {
            $model->user_status = 1;
            return $this->render('createbulkhod', ['model' => $model, 'svname' => '', 'hodname' => '',]);
        }
    }

    public function actionSearchemployee($id) {
        $model = new Staff();

        return $this->renderPartial('searchemployee', [
                    'model' => $model,
                    'id' => $id,
        ]);
    }

    public function actionListemployee($staff_name, $id) {
        $mStaff = Staff::find()
                ->where('staff_name LIKE :query')
                ->addParams([':query' => '%' . $staff_name . '%'])
                ->all();

        if ($id == 'sv') {
            return $this->renderPartial('listsv', [
                        'mStaff' => $mStaff,
            ]);
        } else {
            return $this->renderPartial('listhod', [
                        'mStaff' => $mStaff,
            ]);
        }
    }

    /**
     * Updates an existing Staff model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $model->scenario = 'manageStaff';
//        $svname = Staff::findOne($model->supervisor_id);
//        $hodname = Staff::findOne($model->hod_id);
        if (!empty($model->supervisor_id)) {
            $model->supervisor_name = $model->getStaffName($model->supervisor_id);
        }

        if (!empty($model->hod_id)) {
            $model->hod_name = $model->getStaffName($model->hod_id);
        }

        if ($model->load(Yii::$app->request->post())) {
            $staffphoto = $model->staff_photo;
            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                $model->file->saveAs('img/profile_pic/' . $model->id . '.' . $model->file->extension);
                $model->staff_photo = $model->id . '.' . $model->file->extension;
            } else {
                $model->staff_photo = $staffphoto;
            }

            if (!empty($_POST['Staff']['staff_dob']))
                $model->staff_dob = date('Y-m-d', strtotime($_POST['Staff']['staff_dob']));
            else
                $model->staff_dob = null;

            if (!empty($_POST['Staff']['date_joined']))
                $model->date_joined = date('Y-m-d', strtotime($_POST['Staff']['date_joined']));
            else
                $model->date_joined = null;

            if ($model->save()) {
                return $this->redirect(['update', 'id' => $model->id]);
            }
        }
        return $this->render('update', ['model' => $model]);
        //return $this->render('update', ['model' => $model,'svname' => $svname,'hodname' => $hodname,]);
    }

    /**
     * Deletes an existing Staff model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {

        $model = Staff::findOne($id);

        User::deleteAll('id = ' . $model->userid);

        $this->findModel($id)->delete();

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

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

    public function actionSearchStaff($namein, $staffnoin) {
        $data = Staff::find()->where("staff_name LIKE '%$namein%' AND staff_no LIKE '%$staffnoin%'")->all();

        if (count($data) != 0) {
            foreach ($data as $staff) {
                $datax[] = array(
                    'id' => $staff->id,
                    'userid' => $staff->userid,
                    'staff_no' => $staff->staff_no,
                    'staff_name' => $staff->staff_name,
                );
            }
        } else {
            $datax[] = array(
                'id' => null,
            );
        }

        return json_encode($datax);
    }

    public function actionViewstaff($id) {
        $staffPaymentInfo = \backend\models\StaffPaymentInfo::find()->where(['staff_info_id' => $id])->one();
        $staffPaymentDetail = \backend\models\StaffPaymentDetail::find()->where(['staff_payment_id' => $staffPaymentInfo->id])->all();
        $employment = \backend\models\Employment::find()->where(['staff_info_id' => $id])->one();
        return $this->render('viewStaff', [
                    'model' => $this->findModel($id),
                    'staffPaymentInfo' => $staffPaymentInfo,
                    'staffPaymentDetail' => $staffPaymentDetail,
                    'employment' => $employment,
        ]);
    }

    public function actionPrintpreview($id) {
        $model = $this->findModel($id);
        $staffPaymentInfo = \backend\models\StaffPaymentInfo::find()->where(['staff_info_id' => $id])->one();
        $staffPaymentDetail = \backend\models\StaffPaymentDetail::find()->where(['staff_payment_id' => $staffPaymentInfo->id])->all();
        $employment = \backend\models\Employment::find()->where(['staff_info_id' => $id])->one();

        $content = $this->renderPartial('printViewStaff', [
            'model' => $model,
            'staffPaymentInfo' => $staffPaymentInfo,
            'staffPaymentDetail' => $staffPaymentDetail,
            'employment' => $employment]);

        $pdf = new Pdf;
        $mpdf = $pdf->api; // fetches mpdf api
        $mpdf->WriteHtml($content); // call mpdf write html
        echo $mpdf->Output('test.pdf', 'I'); // call the mpdf api output as needed
    }

}