init
This commit is contained in:
190
controllers/VehicleController.php
Normal file
190
controllers/VehicleController.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use Yii;
|
||||
use app\models\Vehicle;
|
||||
use app\models\VehicleSearch;
|
||||
use yii\web\Controller;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\filters\VerbFilter;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* VehicleController implements the CRUD actions for Vehicle model.
|
||||
*/
|
||||
class VehicleController extends Controller {
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
if (Yii::$app->user->isGuest) {
|
||||
return $this->redirect(['/site/login']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function behaviors() {
|
||||
return [
|
||||
'verbs' => [
|
||||
'class' => VerbFilter::className(),
|
||||
'actions' => [
|
||||
'delete' => ['POST'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all Vehicle models.
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionIndex() {
|
||||
$this->view->title = "Danh sách xe";
|
||||
$this->view->params['breadcrumbs'][] = $this->view->title;
|
||||
$searchModel = new VehicleSearch();
|
||||
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
||||
$dataProvider->query->orderBy(['id' => SORT_DESC]);
|
||||
|
||||
return $this->render('index', [
|
||||
'searchModel' => $searchModel,
|
||||
'dataProvider' => $dataProvider,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a single Vehicle 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 Vehicle model.
|
||||
* If creation is successful, the browser will be redirected to the 'view' page.
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionCreate() {
|
||||
$model = new Vehicle();
|
||||
if (Yii::$app->request->post()) {
|
||||
$post = Yii::$app->request->post();
|
||||
return $model->create([
|
||||
'plate' => $this->format($post['plate']),
|
||||
'type' => $post['type'],
|
||||
'company' => $post['company'],
|
||||
'vehicle_type' => $post['vehicleType'],
|
||||
'driver' => $post['driver'],
|
||||
'telephone' => $this->format($post['telephone']),
|
||||
'indentity_card' => $post['cmt']
|
||||
]);
|
||||
} else {
|
||||
Yii::$app->response->format = "json";
|
||||
return [
|
||||
"title" => "Thêm xe mới",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(['create'])
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an existing Vehicle 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 (Yii::$app->request->post()) {
|
||||
$post = Yii::$app->request->post();
|
||||
$model->plate = $this->format($post['plate']);
|
||||
$model->type = $post['type'];
|
||||
$model->company = $post['company'];
|
||||
$model->vehicle_type = $post['vehicleType'];
|
||||
$model->driver = $post['driver'];
|
||||
$model->telephone = $this->format($post['telephone']);
|
||||
$model->indentity_card = $post['cmt'];
|
||||
$model->modified_at = time();
|
||||
return $model->save();
|
||||
} else {
|
||||
Yii::$app->response->format = "json";
|
||||
return [
|
||||
"title" => "Sửa thông tin xe",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(['update', 'id' => $id])
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an existing Vehicle 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 Vehicle model based on its primary key value.
|
||||
* If the model is not found, a 404 HTTP exception will be thrown.
|
||||
* @param integer $id
|
||||
* @return Vehicle the loaded model
|
||||
* @throws NotFoundHttpException if the model cannot be found
|
||||
*/
|
||||
protected function findModel($id) {
|
||||
if (($model = Vehicle::findOne($id)) !== null) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
|
||||
// public function actionImport() {
|
||||
// $file_type = \PHPExcel_IOFactory::identify("data/data.xlsx");
|
||||
// $objReader = \PHPExcel_IOFactory::createReader($file_type);
|
||||
// $objPHPExcel = $objReader->load("data/data.xlsx");
|
||||
// $sheet_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
|
||||
// $kq = [];
|
||||
// foreach ($sheet_data as $k => $row) {
|
||||
// if ($k > 1) {
|
||||
// $model = new Vehicle();
|
||||
// $kq[] = $model->create([
|
||||
// 'plate' => $this->formatPlate($row["B"]),
|
||||
// 'type' => $row["C"],
|
||||
// 'company' => $row["D"],
|
||||
// 'vehicle_type' => $row["E"],
|
||||
// 'driver' => $row["F"],
|
||||
// 'telephone' => $this->formatPlate($row["G"]),
|
||||
// 'indentity_card' => strval($row["H"])
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
// echo "<pre>";
|
||||
// var_dump($kq);
|
||||
// echo "</pre>";
|
||||
// exit();
|
||||
// }
|
||||
//
|
||||
public function format($plate) {
|
||||
$p1 = str_replace("-", "", $plate);
|
||||
$p2 = str_replace(".", "", $p1);
|
||||
return str_replace(" ", "", $p2);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user