This commit is contained in:
2020-02-01 16:47:12 +07:00
commit 4c619ad6e6
16739 changed files with 3329179 additions and 0 deletions

View File

@@ -0,0 +1,353 @@
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\helpers\FileHelper;
use app\models\User;
use app\models\Gate;
use app\models\Camera;
use app\models\Card;
use app\models\Logs;
/**
* CardController implements the CRUD actions for Card model.
*/
class ApiController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionLogin() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$user = User::findOne([
'username' => $post['username'],
'password' => md5($post['password'])
]);
Yii::$app->response->format = "json";
if ($user) {
$gate = Gate::findOne(['user_id' => $user->id]);
if (!$gate) {
return [
'status' => false,
'data' => "Tài khoản chưa được cấu hình phân quyền"
];
} else {
if ($gate->api_config == null || $gate->api_config === "") {
return [
'status' => false,
'data' => "Cổng chưa được cấu hình API"
];
}
}
$cameras = Camera::find()->andWhere(['gate' => $gate->id])->all();
if (count($cameras) == 0) {
return [
'status' => false,
'data' => "Cổng chưa được cấu hình camera"
];
}
return [
'status' => true,
'data' => [
"userID" => $user->id,
"name" => $user->first_name,
"api" => json_decode($gate->api_config, true),
"score" => 8,
"scoreLong" => 6,
"camera1" => $gate->getCameraInfo(1),
"camera2" => $gate->getCameraInfo(2),
"camera3" => $gate->getCameraInfo(3),
"camera4" => $gate->getCameraInfo(4),
"camera5" => $gate->getCameraInfo(5),
"camera6" => $gate->getCameraInfo(6),
"camera7" => $gate->getCameraInfo(7),
"camera8" => $gate->getCameraInfo(8),
"statistic" => Logs::statistics(),
"cascadeConfig" => json_decode($gate->cascade_config, true),
"laneConfig" => json_decode($gate->lane_config, true)
]
];
} else {
return [
'status' => false,
'data' => "Tên tài khoản hoặc mật khẩu không đúng"
];
}
}
}
public function actionCheckCard() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$cardInfo = Card::findOne(['code' => $post['card']]);
Yii::$app->response->format = "json";
if ($cardInfo) {
$lastLog = Logs::find()->andWhere(['card_id' => $cardInfo->id])->orderBy(['time_in' => SORT_DESC])->one();
if ($lastLog) {
if ($lastLog->type == "in") {
return [
'status' => true,
'type' => "out",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'log_id' => $lastLog->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $lastLog->plate_in,
'time' => date("H:i:s d/m/Y", $lastLog->time_in),
'plateImage' => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->plate_image_in)),
'frameImage' => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->frame_image_in)),
'faceImage' => $lastLog->face_image_in === "" ? "" : base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/" . $lastLog->face_image_in)),
'name' => $lastLog->name_in,
'plate' => $cardInfo->plate
];
} else {
return [
'status' => true,
'type' => "in",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $cardInfo->plate
];
}
} else {
return [
'status' => true,
'type' => "in",
'cardID' => sprintf("%06d", $cardInfo->id),
'cardRealID' => $cardInfo->id,
'cardType' => $cardInfo->cardType,
'vehicleType' => $cardInfo->vehicleType,
'plate' => $cardInfo->plate
];
}
} else {
return [
'status' => false
];
}
}
}
public function actionSaveLogs() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$temp = $post['time'];
$temp = explode(" ", $temp);
$DateFolder = str_replace("/", "_", $temp[1]);
$time = date_format(date_create_from_format('H:i:s d/m/Y', $post['time']), 'U');
$targetPathFrame = $RootFolder . "/" . $DateFolder . "/frame/" . $post['type'];
FileHelper::createDirectory($targetPathFrame, 0777);
$FrameFileName = "frame_" . $time . ".png";
file_put_contents($targetPathFrame . "/" . $FrameFileName, base64_decode($post['frameImage']));
$post['frameImage'] = $DateFolder . "/frame/" . $post['type'] . "/" . $FrameFileName;
if ($post['plateImage'] !== "") {
$targetPathPlate = $RootFolder . "/" . $DateFolder . "/plate/" . $post['type'];
FileHelper::createDirectory($targetPathPlate, 0777);
$PlateFileName = "plate_" . $time . ".png";
file_put_contents($targetPathPlate . "/" . $PlateFileName, base64_decode($post['plateImage']));
$post['plateImage'] = $DateFolder . "/plate/" . $post['type'] . "/" . $PlateFileName;
} else {
$post['plateImage'] = null;
}
if (isset($post['faceImage'])) {
$targetPathFace = $RootFolder . "/" . $DateFolder . "/face/" . $post['type'];
FileHelper::createDirectory($targetPathFace, 0777);
$FaceFileName = "face_" . $time . ".png";
file_put_contents($targetPathFace . "/" . $FaceFileName, base64_decode($post['faceImage']));
$post['faceImage'] = $DateFolder . "/face/" . $post['type'] . "/" . $FaceFileName;
}
$post['time'] = $time;
if ($post['type'] === "in") {
$model = new Logs();
Yii::$app->response->format = "json";
$id = $model->create($post);
if ($id) {
return [
'status' => true,
'logID' => $id,
"cost" => "0"
];
} else {
return [
'status' => false
];
}
} else {
$model = Logs::findOne($post['log_id']);
$model->plate_out = $post['plate'];
$model->plate_image_out = $post['plateImage'];
$model->frame_image_out = $post['frameImage'];
$model->face_image_out = isset($post['faceImage']) ? $post['faceImage'] : "";
$model->name_out = isset($post['name']) ? $post['name'] : "";
$model->time_out = $time;
$model->camera_out = $post['camera'];
$model->mod_out = $post['mod'];
Yii::$app->response->format = "json";
if ($model->save()) {
return [
'status' => true,
'logID' => $model->id,
'cost' => $model->cost
];
} else {
return [
'status' => false
];
}
}
}
}
public function actionUpdateLogs() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$model = Logs::findOne($post['logID']);
$model->plate_in = $post['plate_in'];
$model->plate_out = $post['plate_out'];
$model->save();
return true;
}
}
public function actionStatistics() {
Yii::$app->response->format = "json";
return Logs::statistics();
}
public function convert($code) {
$temp = explode("-", $code)[0];
$hex = dechex($temp);
$t = [];
for ($i = 0; $i < strlen($hex) / 2; $i++) {
$t[] = substr($hex, $i * 2, 2);
}
$t = array_reverse($t);
$t = implode("", $t);
return hexdec($t);
}
public function actionImport() {
$file_type = \PHPExcel_IOFactory::identify("data/test.xlsx");
$objReader = \PHPExcel_IOFactory::createReader($file_type);
$objPHPExcel = $objReader->load("data/test.xlsx");
$sheet_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$objPHPExcel = new \PHPExcel();
$count = 1;
$objPHPExcel->setActiveSheetIndex(0);
$toExcelFile[] = [
"Mã NV",
"Họ tên",
"Đơn vị",
"Mã thẻ gốc",
"Giới tính",
"Mã thẻ đã chuyển đổi"
];
$cards = Card::find()->all();
foreach ($sheet_data as $k => $row) {
$ExportData[] = $row["A"];
$ExportData[] = $row["B"];
$ExportData[] = $row["C"];
$ExportData[] = $row["D"];
$ExportData[] = $row["E"];
$ExportData[] = $this->convert($row["D"]);
$toExcelFile[] = $ExportData;
unset($ExportData);
}
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->getColumnDimension('B')->setWidth(30);
$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="ket-qua.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
public function actionExport() {
$objPHPExcel = new \PHPExcel();
$count = 1;
$objPHPExcel->setActiveSheetIndex(0);
$toExcelFile[] = [
"STT",
"Mã thẻ"
];
$cards = Card::find()->all();
foreach ($cards as $k => $v) {
$ExportData[] = $v->id;
$ExportData[] = $v->code;
$toExcelFile[] = $ExportData;
unset($ExportData);
}
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->getColumnDimension('B')->setWidth(30);
$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="ket-qua.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace app\controllers;
use Yii;
use app\models\Logs;
use app\models\LogsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* LogsController implements the CRUD actions for Logs model.
*/
class LogsController 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 Logs models.
* @return mixed
*/
public function actionIndex($from = "", $to = "") {
if ($from === "") {
$from = "00:00 " . date("d/m/Y");
}
if ($to === "") {
$to = "23:59 " . date("d/m/Y");
}
$this->view->title = "Thống kê";
$searchModel = new LogsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$f = date_format(date_create_from_format('H:i d/m/Y', $from), 'U');
$t = date_format(date_create_from_format('H:i d/m/Y', $to), 'U');
$dataProvider->query->andWhere(["OR", ["BETWEEN", 'time_in', $f, $t], ["BETWEEN", 'time_out', $f, $t]]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'from' => $from,
'to' => $to
]);
}
/**
* Displays a single Logs 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 Logs model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Logs();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Logs 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()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Logs 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 Logs model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Logs the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = Logs::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@@ -0,0 +1,122 @@
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex() {
return $this->render('index');
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin() {
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->redirect(["/dashboard"]);
}
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout() {
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
* @return Response|string
*/
public function actionContact() {
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Displays about page.
*
* @return string
*/
public function actionAbout() {
return $this->render('about');
}
}

View File

@@ -0,0 +1,289 @@
<?php
namespace app\controllers;
use Yii;
use app\models\User;
use app\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\models\AuthItem;
use app\models\AuthAssignment;
/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller {
public function init() {
parent::init();
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
if (!Yii::$app->user->can("administrator")) {
return $this->redirect(["/dashboard"]);
}
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all User models.
* @return mixed
*/
public function actionIndex() {
$this->view->title = "Người dùng";
$this->view->params['breadcrumbs'][] = "Hệ thống";
$this->view->params['breadcrumbs'][] = $this->view->title;
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single User model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id) {
if (!Yii::$app->user->can("administrator")) {
Yii::$app->response->format = "json";
return [
"title" => "403",
"form" => Yii::t("app", "Bạn không có quyền truy cập!")
];
}
$model = $this->findModel($id);
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = "json";
return [
"title" => "<i class='fa fa-user'></i> " . Yii::t("app", "Thông tin người dùng"),
"form" => $this->renderPartial("view", [
"model" => $model
])
];
}
}
/**
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
if (!Yii::$app->user->can("administrator")) {
Yii::$app->response->format = "json";
return [
"title" => "403",
"form" => Yii::t("app", "Bạn không có quyền truy cập!")
];
}
$model = new User();
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
Yii::$app->response->format = "json";
$check = User::findOne(["username" => $post['Username']]);
if ($check) {
return [
'stt' => false,
"reason" => 'username'
];
}
$check2 = User::findOne(['email' => $post['Email']]);
if ($check2) {
return [
'stt' => false,
"reason" => 'email'
];
}
$user_id = $model->create($post);
$auth = Yii::$app->authManager;
foreach ($post['Role'] as $key => $value) {
$role = $auth->getRole($value);
if ($role != null) {
$auth->assign($role, $user_id);
}
}
return [
'stt' => true
];
} else {
Yii::$app->response->format = "json";
return [
"title" => "<i class='fa fa-plus-circle'></i> " . Yii::t("app", "Tạo người dùng mới"),
"form" => $this->renderPartial("form", [
"model" => $model,
"roles" => AuthItem::roleArray(),
"url" => Url::to(['/user/create'])
])
];
}
}
/**
* Updates an existing User 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) {
if (!Yii::$app->user->can("administrator")) {
Yii::$app->response->format = "json";
return [
"title" => "403",
"form" => Yii::t("app", "Bạn không có quyền truy cập!")
];
}
$model = $this->findModel($id);
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
if ($post['Username'] !== $model->username) {
$check = User::findOne(["username" => $post['Username']]);
if ($check)
return false;
}
if ($post['Password'] !== "") {
$model->password = md5($post['Password']);
}
$model->first_name = $post['Name'];
$model->username = $post['Username'];
$model->phone_number = $post['PhoneNumber'];
$model->email = $post['Email'];
$model->quota = $post['Quota'];
$model->save();
AuthAssignment::deleteAll(['user_id' => $id]);
$auth = Yii::$app->authManager;
foreach ($post['Role'] as $key => $value) {
$role = $auth->getRole($value);
if ($role != null) {
$auth->assign($role, $id);
}
}
return true;
} else {
Yii::$app->response->format = "json";
return [
"title" => "<i class='fa fa-edit'></i> " . Yii::t("app", "Sửa thông tin người dùng"),
"form" => $this->renderPartial("form", [
"model" => $model,
"roles" => AuthItem::roleArray(),
"url" => Url::to(['/user/update', 'id' => $id])
])
];
}
}
/**
* Deletes an existing User 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) {
if (!Yii::$app->user->can("administrator")) {
throw new \yii\web\ForbiddenHttpException(Yii::t("app", "Bạn không có quyền truy cập!"));
}
$this->findModel($id)->delete();
AuthAssignment::deleteAll(['user_id' => $id]);
return $this->redirect(['index']);
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = User::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionProfiles() {
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
$model = $this->findModel(Yii::$app->user->id);
$this->view->title = Yii::t("app", "Thông tin cá nhân");
$this->view->params['breadcrumbs'][] = $this->view->title;
return $this->render('profiles', [
"model" => $model
]);
}
public function actionInfo($id) {
if (Yii::$app->request->post()) {
$model = $this->findModel($id);
$post = Yii::$app->request->post();
$model->first_name = $post['Name'];
$model->phone_number = $post['PhoneNumber'];
$model->email = $post['Email'];
return $model->save();
}
}
public function actionChangePassword() {
$model = $this->findModel(Yii::$app->user->id);
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$model->password = md5($post['NewPassword']);
$model->save();
return true;
} else {
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
$this->view->title = Yii::t("app", "Đổi mật khẩu");
$this->view->params['breadcrumbs'][] = $this->view->title;
return $this->render('password', [
"model" => $model
]);
}
}
public function actionAvatar() {
if (Yii::$app->request->post()) {
$model = new \app\models\UploadForm();
$path = "avatar/" . Yii::$app->user->id;
$url = $model->UploadGlobal("image", ["PNG", "JPG", "JPEG", "GIF"], $path);
$UserInfo = User::findOne(Yii::$app->user->id);
$UserInfo->user_image = $url;
$UserInfo->save();
return $url;
}
}
}

View 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);
}
}