| Current Path : /var/www/html/ebook/backend/modules/draw/controllers/ |
| Current File : /var/www/html/ebook/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;
/**
* DrawController implements the CRUD actions for Draw model.
*/
class DrawController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'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');
}
return $this->redirect(['view', 'id' => $model->draw_id]);
}
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');
}
return $this->redirect(['view', 'id' => $model->draw_id]);
}
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)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* 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.');
}
}