Your IP : 216.73.216.79


Current Path : /var/www/html/mnrb/backend/modules/tender/controllers/
Upload File :
Current File : /var/www/html/mnrb/backend/modules/tender/controllers/TenderController.php

<?php

namespace backend\modules\tender\controllers;

use Yii;
use backend\modules\tender\models\Tender;
use backend\modules\tender\models\TenderSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\Url;

/**
 * TenderController implements the CRUD actions for Tender model.
 */
class TenderController extends Controller {

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

    /**
     * Lists all Tender models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new TenderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /**
     * Displays a single Tender 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 Tender model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Tender();
        $model->scenario = 'create';
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            $model->file_url = UploadedFile::getInstance($model, 'file_url');
            if (!empty($model->file_url)) {
                $file_url = 'uploads/tender/' . date("YmdHis") . '.' . $model->file_url->extension;
                if ($model->file_url->saveAs('../' . $file_url))
                    $model->file_url = $file_url;
            }
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing Tender 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);
        $original_file_url = $model->file_url;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            $model->file_url = UploadedFile::getInstance($model, 'file_url');
            if (!empty($model->file_url)) {
                $file_url = 'uploads/tender/' . date("YmdHis") . '.' . $model->file_url->extension;
                if ($model->file_url->saveAs('../' . $file_url))
                    $model->file_url = $file_url;
            } else
                $model->file_url = $original_file_url;
            if ($model->save()){
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Tender 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 = $_POST['id'];
        $status = 'failed';
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

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

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

}