Your IP : 216.73.216.79


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

<?php

namespace backend\controllers\staff;

use Yii;
use backend\models\staff\Staff;
use backend\models\staff\StaffSearch;
use backend\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * 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 actionIndex()
    {

        $user_info = \backend\models\StaffDetail::find()->where(['userid' => Yii::$app->user->identity->id])->one();
        $role = \backend\models\StaffAccess::find()->where(['staff_info_id' => $user_info->id])->one();
       if($role->role_code == 'ADM'){

        $searchModel = new StaffSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

       } else {

        $searchModel = new StaffSearch();
        $dataProvider = $searchModel->search1(Yii::$app->request->queryParams);

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

       }

    }

    /**
     * Displays a single Staff 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 Staff model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */

public function actionCreate()
    {
        $model = new Staff();
       

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

           // $model->status = $status;

             $model->staff_gambar = UploadedFile::getInstances($model, 'staff_gambar');

                // echo '<pre>'; var_dump(__DIR__); echo '</pre>';die();
                foreach ($model->staff_gambar as $key => $file) {
                $file->saveAs( 'images/' . $file->baseName . '.' . $file->extension);
                //Upload files to server
                $model->staff_gambar =  $file->baseName . '.' . $file->extension;//Save file names in database- '**' is for separating images;

                }

                if(empty($model->staff_gambar))
                {
                  $model->staff_gambar =  Yii::$app->request->post()['hidden_logo'];
                }

            $model->save(false);

            return $this->redirect(['view', 'id' => $model->staff_id]);

        } else {

            return $this->render('create', [
                'model' => $model,

            ]);
        }
    }
    /**
     * Updates an existing Staff 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())) {

			$image = $model->staff_gambar;
			$uploadimage = UploadedFile::getInstance($model, 'staff_gambar');
            $model->staff_gambar = $uploadimage;
					
			//echo $image;
			//echo '<br>';
			//echo $uploadimage;
			//echo '<br>';
			//echo $model->staff_gambar;
			//echo '<br>';
			//print_r(UploadedFile::getInstance($model, 'staff_gambar'));
			
			

            if($uploadimage == null){
				echo $image."_test";
                $model->staff_gambar = $image;
				
//die;
                if ($model->save(false)) {
                        return $this->redirect(['index']);
                }
            }else{
							//	echo $uploadimage."_test";

//die;
                if ($model->upload()) {
					

                    // file is uploaded successfully
                    if ($model->save(false)) {
                        return $this->redirect(['index']);
                    }
                }
            }
  
        }
		  return $this->render('update', [
            'model' => $model,
        ]);
    }  

	
	/*public function actionUpdate($id)
    {
        $model = $this->findModel($id);

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



            $model->staff_gambar = UploadedFile::getInstances($model, 'staff_gambar');

            // print_r($model->layout_skin_logo[0]->name);

            foreach ($model->staff_gambar as $key => $file) {
            $file->saveAs( 'images/' . $file->baseName . '.' . $file->extension);
            //Upload files to server
            $model->staff_gambar =  $file->baseName . '.' . $file->extension;//Save file names in database- '**' is for separating images

            }

            if(empty($model->staff_gambar))
            {
              $model->staff_gambar =  Yii::$app->request->post()['hidden_logo'];
            }

            $model->save(false);

            return $this->redirect(['update', 'id' => $model->staff_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }*/
    /**
     * Deletes an existing Staff 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 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;
        }

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