| Current Path : /var/www/html/mycoastv2/backend/modules/data/controllers/ |
| Current File : /var/www/html/mycoastv2/backend/modules/data/controllers/DataInfoController.php |
<?php
namespace backend\modules\data\controllers;
use Yii;
use backend\modules\data\models\DataInfo;
use backend\modules\data\models\DataInfoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use backend\modules\refdynawebv2\models\Ref;
/**
* DataInfoController implements the CRUD actions for DataInfo model.
*/
class DataInfoController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all DataInfo models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new DataInfoSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single DataInfo 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 DataInfo model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new DataInfo();
$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->data_info_image = UploadedFile::getInstance($model, 'data_info_image');
if (!empty($model->data_info_image)) {
$data_info_image = date("YmdHis") . '.' . $model->data_info_image->extension;
if ($model->data_info_image->saveAs('../admin/uploads/' . $data_info_image))
$model->data_info_image = $data_info_image;
}
if ($model->save())
return $this->redirect(['index']);
}
return $this->render('create', ['model' => $model,]);
}
/**
* Updates an existing DataInfo 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);
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$original_img = $model->data_info_image;
if ($model->load(Yii::$app->request->post())) {
$model->data_info_image = UploadedFile::getInstance($model, 'data_info_image');
if (!empty($model->data_info_image)) {
$data_info_image = date("YmdHis") . '.' . $model->data_info_image->extension;
if ($model->data_info_image->saveAs('../admin/uploads/' . $data_info_image))
$model->data_info_image = $data_info_image;
}else
$model->data_info_image = $original_img;
if ($model->save())
return $this->redirect(['index']);
}
return $this->render('update', ['model' => $model,]);
}
/**
* Deletes an existing DataInfo 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 DataInfo model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return DataInfo the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = DataInfo::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
public function actionGetdistrict($parent_code = '') {
$output = '<option value="">-- Please choose --</option>';
if (($model2 = Ref::findOne(['code' => $parent_code, 'cat' => 'STATE'])) !== null) {
$model = Ref::find()->where(['param1' => $model2->id])->orderBy(['descr_en' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
$output .= '<option value="' . $row->code . '">' . $row->descr_en . '</option>';
}
}
}
return $output;
}
}