BiFace_Server_Lite/controllers/ListManagementController.php

438 lines
19 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use app\models\ListManagement;
use app\models\ListManagementSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\CaptureLogs;
use app\models\common;
use yii\helpers\FileHelper;
/**
* ListMamagementController implements the CRUD actions for ListMamagement model.
*/
class ListManagementController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionIndex($from = "", $to = "", $name = "", $type = "all", $gender = "all") {
$f = date_format(date_create_from_format('H:i d/m/Y', "00:00 " . date("d/m/Y")), 'U');
$t = date_format(date_create_from_format('H:i d/m/Y', "23:59 " . date("d/m/Y")), 'U');
$this->view->title = "List management";
$searchModel = new ListManagementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if ($from !== "" && $to !== "") {
$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(["BETWEEN", "time", $f, $t]);
}
if ($name !== "")
$dataProvider->query->andWhere(["LIKE", "name", $name]);
if ($type !== "all")
$dataProvider->query->andWhere(["type" => $type]);
if ($gender !== "all")
$dataProvider->query->andWhere(["gender" => $gender]);
$dataProvider->query->orderBy(["time" => SORT_DESC]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'f' => $f,
't' => $t,
'typeArray' => ListManagement::$typeArray,
'genderArray' => ListManagement::$genderArray
]);
}
public function actionAdd() {
$model = new ListManagement();
Yii::$app->response->format = "json";
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$check = ListManagement::findOne(['code' => $data['code']]);
if ($check)
return ["status" => false];
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $data['image'], "type" => "raw"]
]
]), true);
$data['image'] = json_encode([
["url" => $data['image'], "features" => $features['results'][0]['feature']]
]);
$model->create($data);
file_get_contents("http://localhost:2305/update-feature");
return ["status" => true];
}
}
public function actionCreate() {
$model = new ListManagement();
Yii::$app->response->format = "json";
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$listManagement = ListManagement::findOne(['code' => $data['code']]);
$url = CaptureLogs::findOne($data['id'])->image;
if ($listManagement) {
$images = json_decode($listManagement->image, true);
$add = true;
foreach ($images as $key => $value) {
if ($value['url'] === $url)
$add = false;
}
if ($add) {
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $url, "type" => "raw"]
]
]), true);
$images[] = ["url" => $url, "features" => $features['results'][0]['feature']];
$listManagement->type = $data['type'];
$listManagement->name = $data['name'];
$listManagement->gender = $data['gender'];
$listManagement->birthday = $data['birthday'] === "" ? 0 : date_format(date_create_from_format('d/m/Y', $data['birthday']), 'U');
$listManagement->telephone = $data['telephone'];
$listManagement->address = $data['address'];
$listManagement->image = json_encode($images);
$listManagement->save();
file_get_contents("http://localhost:2305/update-feature");
return ["status" => true];
}
} else {
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $url, "type" => "raw"]
]
]), true);
$data['image'] = json_encode([
["url" => $url, "features" => $features['results'][0]['feature']]
]);
$model->create($data);
file_get_contents("http://localhost:2305/update-feature");
return ["status" => true];
}
}
}
public function actionFormUpdate() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$ListManagement = $this->findModel($data['id']);
$images = json_decode($ListManagement->image, true);
Yii::$app->response->format = "json";
return [
"type" => $ListManagement->type,
"code" => $ListManagement->code,
"name" => $ListManagement->name,
"gender" => $ListManagement->gender,
"birthday" => date("d/m/Y", $ListManagement->birthday),
"telephone" => $ListManagement->telephone,
"address" => $ListManagement->address,
"image" => count($images) > 0 ? "/data/uploads/face/" . $images[0]['url'] : "/images/user2-160x160.jpg"
];
}
}
public function actionUpdate() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$model = $this->findModel($data['id']);
$check = ListManagement::findOne(['code' => $data['code']]);
if ($check && $check->id != $model->id)
return false;
if ($data['image'] !== "") {
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $data['image'], "type" => "raw"]
]
]), true);
$images = json_decode($model->image, true);
$images[] = ["url" => $data['image'], "features" => $features['results'][0]['feature']];
$model->image = json_encode($images);
}
$model->code = $data['code'];
$model->type = $data['type'];
$model->name = $data['name'];
$model->gender = $data['gender'];
$model->birthday = $data['birthday'] === "" ? 0 : date_format(date_create_from_format('d/m/Y', $data['birthday']), 'U');
$model->telephone = $data['telephone'];
$model->address = $data['address'];
$model->save();
file_get_contents("http://localhost:2305/update-feature");
return true;
}
}
public function actionDelete() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$model = $this->findModel($data['id']);
$images = json_decode($model->image, true);
foreach ($images as $k => $v) {
unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url']);
}
$model->delete();
file_get_contents("http://localhost:2305/update-feature");
return true;
}
}
public function actionDeleteFeature() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$model = $this->findModel($data['id']);
$images = json_decode($model->image, true);
$features = [];
foreach ($images as $key => $value) {
if ($value['url'] !== $data['image'])
$features[] = $value;
}
$model->image = json_encode($features);
$model->save();
file_get_contents("http://localhost:2305/update-feature");
return true;
}
}
protected function findModel($id) {
if (($model = ListManagement::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionBatchDelete() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
Yii::$app->response->format = "json";
foreach ($data['lists'] as $key => $value) {
$staff = ListManagement::findOne($value);
if ($staff) {
$images = json_decode($staff->image, true);
foreach ($images as $k => $v) {
unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url']);
}
$staff->delete();
}
}
file_get_contents("http://localhost:2305/update-feature");
return true;
}
}
public function actionUpload() {
if (Yii::$app->request->post()) {
$model = new common();
Yii::$app->response->format = "json";
$url = $model->UploadFile("AnhNhanVien", ["PNG", "JPG", "JPEG", "GIF"], "temp");
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/" . $url, "type" => "crop"]
]
]), true);
unlink("/var/www/html/BiFace_Server_Lite/web/" . $url);
if ($features['results'][0]['crop'] === "") {
return ["status" => false];
} else {
$key = common::generateRandomString();
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$targetPath = $RootFolder . "/face";
$fileName = "face_" . $key . "_" . time() . ".png";
FileHelper::createDirectory($targetPath, 0777);
file_put_contents($targetPath . "/" . $fileName, base64_decode($features['results'][0]['crop']));
return ["status" => true, "url" => $fileName, "dataPath" => "data/uploads/face/"];
}
}
}
public function actionFormSync() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$results = json_decode(file_get_contents("http://" . $data['ip'] . "/api/data", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode(ListManagement::nameArray())
]
])), true);
Yii::$app->response->format = "json";
return [
"title" => $data['ip'],
"form" => $this->renderPartial("list", [
"results" => $results,
"typeArray" => ListManagement::$typeArray,
"ip" => $data['ip']
])
];
}
}
public function actionSyncFromServer() {
if (Yii::$app->request->isAjax) {
$server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
$ip = "dev-dc.beetai.com";
if ($server_ip)
$ip = $server_ip->data;
$device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']);
$id_camera = 209;
if ($device_id)
$id_camera = intval($device_id->data);
$datas = json_decode(file_get_contents("https://" . $ip . "/api/oem/get_all_image", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
"id_camera" => $id_camera,
"ids_staff" => []
])
]
])), true);
Yii::$app->response->format = "json";
return [
"title" => "<i class='fa fa-download'></i> Đồng bộ từ máy chủ",
"form" => $this->renderPartial("list-server", [
"datas" => $datas['data']
])
];
}
}
public function actionSyncFeature() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
$server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
$ip = "dev-dc.beetai.com";
if ($server_ip)
$ip = $server_ip->data;
$device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']);
$id_camera = 209;
if ($device_id)
$id_camera = intval($device_id->data);
$res = json_decode(file_get_contents("https://" . $ip . "/api/oem/get_all_image", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
"id_camera" => $id_camera,
"ids_staff" => [strval(Yii::$app->request->post("id"))]
])
]
])), true);
$data = $res['data'][0];
$ft = [];
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$targetPath = $RootFolder . "/face";
FileHelper::createDirectory($targetPath, 0777);
foreach ($data['images'] as $key => $value) {
$key = common::generateRandomString();
$fileName = "face_" . $key . "_" . time() . ".png";
file_put_contents($targetPath . "/" . $fileName, file_get_contents($value));
$features = json_decode(common::requestToEngine("/get-feature", [
"image_paths" => [
["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"]
]
]), true);
$ft[] = ["url" => $fileName, "features" => $features['results'][0]['feature']];
}
$model = ListManagement::findOne(['code' => $data['idStaff']]);
if ($model) {
$model->name = $data['name'];
$model->address = $data['department'];
$model->image = json_encode($ft);
$model->last_modified = time();
$model->save();
} else {
$model = new ListManagement();
$model->create([
'code' => strval($data['idStaff']),
'type' => "wl",
'name' => $data['name'],
'image' => json_encode($ft),
'gender' => "",
'birthday' => "",
'telephone' => "",
'address' => $data['department']
]);
}
return ["status" => true];
}
}
public function actionSyncFeatureFromDevice() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
$data = Yii::$app->request->post();
$results = json_decode(file_get_contents("http://" . $data['ip'] . "/api/full-data", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([$data['id']])
]
])), true);
foreach ($results as $key => $value) {
$images = [];
foreach ($value['image'] as $k => $v) {
$key = common::generateRandomString();
$RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$targetPath = $RootFolder . "/face";
$fileName = "face_" . $key . "_" . time() . ".png";
FileHelper::createDirectory($targetPath, 0777);
file_put_contents($targetPath . "/" . $fileName, base64_decode($v['url']));
$images[] = ["url" => $fileName, "features" => $v['features']];
}
$model = ListManagement::findOne(['code' => $value['code']]);
if ($model) {
$model->type = $value['type'];
$model->name = $value['name'];
$model->image = json_encode($images);
$model->gender = $value['gender'];
$model->birthday = date("d/m/Y", $value['birthday']);
$model->telephone = $value['telephone'];
$model->address = $value['address'];
$model->save();
} else {
$model = new ListManagement();
$model->create([
'code' => $value['code'],
'type' => $value['type'],
'name' => $value['name'],
'image' => json_encode($images),
'gender' => $value['gender'],
'birthday' => date("d/m/Y", $value['birthday']),
'telephone' => $value['telephone'],
'address' => $value['address'],
'time' => $value['time']
]);
}
}
return ["status" => true];
}
}
public function actionUpdateFeature() {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = "json";
file_get_contents("http://localhost:2305/update-feature");
return ["status" => true];
}
}
}