Your IP : 216.73.216.79


Current Path : /var/www/html/protegev2/backend/modules/manual/controllers/
Upload File :
Current File : /var/www/html/protegev2/backend/modules/manual/controllers/ManualController.php

<?php

namespace backend\modules\manual\controllers;

use Yii;
use backend\modules\manual\models\Manual;
use backend\modules\manual\models\ManualSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\Url;

/**
 * ManualController implements the CRUD actions for Manual model.
 */
class ManualController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    }
    public function actionList()
    {
        $searchModel = new ManualSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    }

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

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

        if ($model->load(Yii::$app->request->post()))  {
            $model->role = json_encode($model->role);
            $model->file_url = UploadedFile::getInstance($model, 'file_url');
              if (!empty($model->file_url)) {
                  $file_url = 'uploads/' . 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 Manual 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);
        $originalFile = $model->file_url;
        $model->role = json_decode($model->role);
        if ($model->load(Yii::$app->request->post())) {
          $model->role = json_encode($model->role);
          $model->file_url = UploadedFile::getInstance($model, 'file_url');
            if (!empty($model->file_url)) {
                $file_url = 'uploads/' . date('YmdHis') . '.' . $model->file_url->extension;
                if ($model->file_url->saveAs('../' . $file_url))
                    $model->file_url = $file_url;
                    $model->save();
                  }else{$model->file_url = $originalFile;}
                  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 Manual 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'];
         $model = $this->findModel($id);
         if (!empty($model))
             $model->delete();
         return 'success';
     }

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

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}