152 lines
4.4 KiB
PHP
152 lines
4.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace backend\modules\gis\controllers;
|
||
|
|
|
||
|
|
use common\models\GisGeoFeatures;
|
||
|
|
use backend\modules\gis\models\GisGeoFeaturesSearch;
|
||
|
|
use Yii;
|
||
|
|
use yii\web\Controller;
|
||
|
|
use yii\web\NotFoundHttpException;
|
||
|
|
use yii\filters\VerbFilter;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* GeoFeaturesController implements the CRUD actions for GisGeoFeatures model.
|
||
|
|
*/
|
||
|
|
class GeoFeaturesController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @inheritDoc
|
||
|
|
*/
|
||
|
|
public function behaviors()
|
||
|
|
{
|
||
|
|
return array_merge(
|
||
|
|
parent::behaviors(),
|
||
|
|
[
|
||
|
|
'verbs' => [
|
||
|
|
'class' => VerbFilter::className(),
|
||
|
|
'actions' => [
|
||
|
|
'delete' => ['POST'],
|
||
|
|
],
|
||
|
|
],
|
||
|
|
]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Lists all GisGeoFeatures models.
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function actionIndex()
|
||
|
|
{
|
||
|
|
$searchModel = new GisGeoFeaturesSearch();
|
||
|
|
$dataProvider = $searchModel->search($this->request->queryParams);
|
||
|
|
|
||
|
|
return $this->render('index', [
|
||
|
|
'searchModel' => $searchModel,
|
||
|
|
'dataProvider' => $dataProvider,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Displays a single GisGeoFeatures model.
|
||
|
|
* @param int $id ID
|
||
|
|
* @return string
|
||
|
|
* @throws NotFoundHttpException if the model cannot be found
|
||
|
|
*/
|
||
|
|
public function actionView($id)
|
||
|
|
{
|
||
|
|
return $this->render('view', [
|
||
|
|
'model' => $this->findModel($id),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Creates a new GisGeoFeatures model.
|
||
|
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
||
|
|
* @return string|\yii\web\Response
|
||
|
|
*/
|
||
|
|
public function actionCreate()
|
||
|
|
{
|
||
|
|
$model = new GisGeoFeatures();
|
||
|
|
|
||
|
|
if ($this->request->isPost && $model->load(Yii::$app->request->post())) {
|
||
|
|
$geo = json_decode(Yii::$app->request->post('GisGeoFeatures')['geometry'], true);
|
||
|
|
|
||
|
|
if ($geo && isset($geo['type'], $geo['coordinates'])) {
|
||
|
|
$model->geometry = new \yii\db\Expression("ST_GeomFromGeoJSON('" . json_encode($geo) . "')");
|
||
|
|
$model->type = $geo['type']; // ✅ ดึง type จาก GeoJSON โดยตรง
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($model->save(false)) {
|
||
|
|
return $this->redirect(['view', 'id' => $model->id]);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$model->loadDefaultValues();
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->render('create', [
|
||
|
|
'model' => $model,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Updates an existing GisGeoFeatures model.
|
||
|
|
* If update is successful, the browser will be redirected to the 'view' page.
|
||
|
|
* @param int $id ID
|
||
|
|
* @return string|\yii\web\Response
|
||
|
|
* @throws NotFoundHttpException if the model cannot be found
|
||
|
|
*/
|
||
|
|
public function actionUpdate($id)
|
||
|
|
{
|
||
|
|
$model = $this->findModel($id);
|
||
|
|
|
||
|
|
if ($model->load(Yii::$app->request->post())) {
|
||
|
|
$geo = json_decode(Yii::$app->request->post('GisGeoFeatures')['geometry'], true);
|
||
|
|
|
||
|
|
if ($geo && isset($geo['type'], $geo['coordinates'])) {
|
||
|
|
$model->geometry = new \yii\db\Expression("ST_GeomFromGeoJSON('" . json_encode($geo) . "')");
|
||
|
|
$model->type = $geo['type']; // ✅ ตั้งค่า type เช่นกัน
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($model->save(false)) {
|
||
|
|
return $this->redirect(['view', 'id' => $model->id]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->render('update', [
|
||
|
|
'model' => $model,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Deletes an existing GisGeoFeatures model.
|
||
|
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
||
|
|
* @param int $id ID
|
||
|
|
* @return \yii\web\Response
|
||
|
|
* @throws NotFoundHttpException if the model cannot be found
|
||
|
|
*/
|
||
|
|
public function actionDelete($id)
|
||
|
|
{
|
||
|
|
$this->findModel($id)->delete();
|
||
|
|
|
||
|
|
return $this->redirect(['index']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Finds the GisGeoFeatures model based on its primary key value.
|
||
|
|
* If the model is not found, a 404 HTTP exception will be thrown.
|
||
|
|
* @param int $id ID
|
||
|
|
* @return GisGeoFeatures the loaded model
|
||
|
|
* @throws NotFoundHttpException if the model cannot be found
|
||
|
|
*/
|
||
|
|
protected function findModel($id)
|
||
|
|
{
|
||
|
|
if (($model = GisGeoFeatures::findOne(['id' => $id])) !== null) {
|
||
|
|
return $model;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
||
|
|
}
|
||
|
|
}
|