| Current Path : /var/www/html/kpwkm/backend/modules/draw/controllers/ |
| Current File : /var/www/html/kpwkm/backend/modules/draw/controllers/DrawController.php |
<?php
namespace backend\modules\draw\controllers;
use Yii;
use backend\modules\draw\models\Draw;
use backend\modules\draw\models\DrawSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\filters\AccessControl;
/**
* DrawController implements the CRUD actions for Draw model.
*/
class DrawController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Draw models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new DrawSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Draw 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 Draw model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Draw();
if ($model->load(Yii::$app->request->post())) {
$path = 'uploads/draw-io-' . uniqid() . '.png';
if (file_put_contents($path, file_get_contents($model->img_base64))) {
$model->img_path = $path;
}
if (!$model->save()) {
throw new \Exception('Failed to save image');
} else {
$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 Draw 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())) {
$path = 'uploads/draw-io-' . uniqid() . '.png';
if (file_put_contents($path, file_get_contents($model->img_base64))) {
$model->img_path = $path;
}
if (!$model->save()) {
throw new \Exception('Failed to save image');
} else {
$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 Draw 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 Draw model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Draw the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = Draw::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}