AIParking_Intops_Server/controllers/VehicleController.php
2020-02-10 10:23:13 +07:00

257 lines
8.6 KiB
PHP

<?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->format($row["B"]),
// 'type' => $row["C"],
// 'company' => $row["D"],
// 'vehicle_type' => $row["E"],
// 'driver' => $row["F"],
// 'telephone' => $this->format($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);
$p3 = str_replace("\n", "", $p2);
return str_replace(" ", "", $p3);
}
//
// public function actionExport() {
// set_time_limit(3600);
// $vehicles = Vehicle::find()->orderBy(['plate' => SORT_ASC])->all();
// $connection = Yii::$app->getDb();
// foreach ($vehicles as $k => $v) {
// $command = $connection->createCommand("SELECT * FROM `vehicle` WHERE `plate`<>'{$v->plate}' AND levenshtein('%{$v->plate}', `plate`) BETWEEN 0 AND 1");
// $result = $command->queryAll();
// $temp = [];
// foreach ($result as $key => $value) {
// $temp[] = $value['plate'];
// }
// $v->levenshtein_plate_2 = json_encode($temp);
// $v->save();
// }
// exit();
//
// $objPHPExcel = new \PHPExcel();
// $count = 1;
// $objPHPExcel->setActiveSheetIndex(0);
// $toExcelFile[] = [
// "STT",
// "Biển số",
// "Gần giống",
// "Công ty",
// "Lái xe"
// ];
// foreach ($vehicles as $k => $v) {
// $ExportData[] = $count++;
// $ExportData[] = $v->plate;
// $connection = Yii::$app->getDb();
// $command = $connection->createCommand("SELECT * FROM `vehicle` WHERE levenshtein(':plate_query', `plate`) BETWEEN 0 AND 2 AND `plate`<>':plate_query'", [':plate_query' => $v->plate]);
// $result = $command->queryAll();
// $ExportData[] = count($result);
// $ExportData[] = $v->company;
// $ExportData[] = $v->driver;
// $toExcelFile[] = $ExportData;
// unset($ExportData);
// }
// $activeSheet = $objPHPExcel->getActiveSheet();
// $rowCount = 1;
// for ($i = 0; $i < count($toExcelFile); $i++) {
// $column = 'A';
// $row = $toExcelFile[$i];
// for ($j = 0; $j < count($row); $j++) {
// if (!isset($row[$j]))
// $value = NULL;
// elseif ($row[$j] != "")
// $value = strip_tags($row[$j]);
// else
// $value = "";
// $activeSheet->setCellValue($column . $rowCount, $value);
// $column = chr(ord($column) + 1);
// }
// $rowCount++;
// }
//
// $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// ob_end_clean();
// header('Content-type: application/vnd.ms-excel');
// header('Content-Disposition: attachment; filename="bao-cao.xlsx"');
// header('Cache-Control: max-age=0');
// $objWriter->save('php://output');
// exit();
// }
}