BiFace_Server_Lite/controllers/ListManagementController.php
2020-12-04 11:52:16 +07:00

187 lines
6.9 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;
/**
* 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');
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');
}
$this->view->title = "List management";
$searchModel = new ListManagementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$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 actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
public function actionCreate() {
$model = new ListManagement();
Yii::$app->response->format = "json";
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$listManagement = ListManagement::findOne(['name' => $data['name']]);
$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->image = json_encode($images);
$listManagement->time = time();
return $listManagement->save();
}
} 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']]
]);
return $model->create($data);
}
}
}
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,
"name" => $ListManagement->name,
"gender" => $ListManagement->gender,
"birthday" => date("d/m/Y", $ListManagement->birthday),
"telephone" => $ListManagement->telephone,
"address" => $ListManagement->address,
"image" => "/BiFace/data/uploads/face/" . $images[0]['url']
];
}
}
public function actionUpdate() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$model = $this->findModel($data['id']);
$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'];
return $model->save();
}
}
public function actionDelete() {
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
return $this->findModel($data['id'])->delete();
}
}
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);
return $model->save();
}
}
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();
return ListManagement::deleteAll(["IN", "id", $data['lists']]);
}
}
public function actionUpload() {
if (Yii::$app->request->post()) {
$model = new common();
Yii::$app->response->format = "json";
return [
"dataPath" => "BiFace/",
"url" => $model->UploadFile("AnhNhanVien", ["PNG", "JPG", "JPEG", "GIF"], "temp")
];
}
}
}