From c2e025d1d9770307f8ac4e447f1631fc0cf889c6 Mon Sep 17 00:00:00 2001 From: dongpd Date: Fri, 27 Mar 2020 13:52:40 +0700 Subject: [PATCH] update export excel --- .gitignore | 3 +- config/web.php | 9 ++- controllers/ApiController.php | 15 ++-- controllers/DashboardController.php | 104 +++++++++++++++++++++++++++- db/app.db | Bin 81920 -> 81920 bytes helpers/LogsGrid.php | 32 +++++++++ models/FaceLogs.php | 88 +++++++++++++++++++++++ models/FaceLogsSearch.php | 78 +++++++++++++++++++++ views/dashboard/index.tpl | 30 +++++++- 9 files changed, 344 insertions(+), 15 deletions(-) create mode 100644 helpers/LogsGrid.php create mode 100644 models/FaceLogs.php create mode 100644 models/FaceLogsSearch.php diff --git a/.gitignore b/.gitignore index 7f9ce2a2..a5d18839 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /runtime /web/assets -/web/data \ No newline at end of file +/web/data +/nbproject/private/ \ No newline at end of file diff --git a/config/web.php b/config/web.php index eafcb66a..3a079447 100644 --- a/config/web.php +++ b/config/web.php @@ -110,7 +110,14 @@ $config = [ ], ], ], - 'db' => $db + 'db' => $db, + 'urlManager' => [ + 'enablePrettyUrl' => true, + 'showScriptName' => false, + 'enableStrictParsing' => false, + 'rules' => [ + ], + ], ], 'params' => $params, ]; diff --git a/controllers/ApiController.php b/controllers/ApiController.php index 9e81c07b..05854ed8 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -5,14 +5,6 @@ namespace app\controllers; use Yii; use yii\web\Controller; use yii\filters\VerbFilter; -use yii\helpers\FileHelper; -use app\models\User; -use app\models\Gate; -use app\models\Camera; -use app\models\Card; -use app\models\CardGroup; -use app\models\Logs; -use app\models\Config; /** * CardController implements the CRUD actions for Card model. @@ -33,10 +25,13 @@ class ApiController extends Controller { ]; } - public function actionLogin() { + public function actionSaveLogs() { if (Yii::$app->request->post()) { $post = Yii::$app->request->bodyParams; - + $model = new \app\models\FaceLogs(); + $model->create($post); + Yii::$app->response->format = "json"; + return ["stt" => true]; } } diff --git a/controllers/DashboardController.php b/controllers/DashboardController.php index 3601cb0c..282d3198 100644 --- a/controllers/DashboardController.php +++ b/controllers/DashboardController.php @@ -3,8 +3,8 @@ namespace app\controllers; use Yii; -use app\models\Logs; -use app\models\LogsSearch; +use app\models\FaceLogs; +use app\models\FaceLogsSearch; use yii\web\Controller; use yii\filters\VerbFilter; use yii\helpers\Url; @@ -41,9 +41,109 @@ class DashboardController extends Controller { */ public function actionIndex() { $this->view->title = "Bảng tổng hợp"; + $searchModel = new FaceLogsSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, ]); } + public function actionExport() { + $lists = FaceLogs::find()->all(); + $objPHPExcel = new \PHPExcel(); + $count = 1; + $objPHPExcel->setActiveSheetIndex(0); + $toExcelFile[] = [ + "STT", + "camera_id", + "confidence", + "frametime", + "face_id", + "name", + "stt", + "timezone" + ]; + foreach ($lists as $k => $v) { + $ExportData[] = $count++; + $ExportData[] = $v->camera_id; + $ExportData[] = $v->confidence; + $ExportData[] = $v->frametime; + $ExportData[] = $v->face_id; + $ExportData[] = $v->name; + $ExportData[] = $v->stt; + $ExportData[] = $v->timezone; + $toExcelFile[] = $ExportData; + unset($ExportData); + } + $totals = count($lists) + 2; + $maxCol = "H"; + $activeSheet = $objPHPExcel->getActiveSheet(); + $activeSheet->setCellValue("A1", "DANH SÁCH CHẤM CÔNG"); + $activeSheet->mergeCells('A1:' . $maxCol . '1'); + $activeSheet->getRowDimension('1')->setRowHeight(50); + $activeSheet->getStyle("A2:" . $maxCol . "2")->getFont()->setBold(true); + $activeSheet->getStyle("A1")->getFont()->setBold(true)->setName('Time New Roman')->setSize(13); + $activeSheet->getStyle("A2:" . $maxCol . $totals)->getFont()->setName('Time New Roman')->setSize(10); + $activeSheet->getColumnDimension('A')->setWidth(5); + $activeSheet->getColumnDimension('B')->setWidth(15); + $activeSheet->getColumnDimension('C')->setWidth(15); + $activeSheet->getColumnDimension('D')->setWidth(25); + $activeSheet->getColumnDimension('E')->setWidth(10); + $activeSheet->getColumnDimension('F')->setWidth(20); + $activeSheet->getColumnDimension('G')->setWidth(10); + $activeSheet->getColumnDimension('H')->setWidth(15); + $style = array( + 'alignment' => array( + 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER, + 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, + 'wrap' => true + ) + ); + $activeSheet->getStyle("A1:" . $maxCol . "2")->applyFromArray($style); + $activeSheet->getStyle("A2:" . $maxCol . "2")->applyFromArray([ + 'fill' => array( + 'type' => \PHPExcel_Style_Fill::FILL_SOLID, + 'color' => array('rgb' => '00c0ef') + ) + ]); + $rowCount = 2; + 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++; + } + + $activeSheet->getStyle("A2:" . $maxCol . $totals)->applyFromArray([ + 'alignment' => [ + 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER, + 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, + 'wrap' => true + ], + 'borders' => [ + 'allborders' => [ + 'style' => \PHPExcel_Style_Border::BORDER_THIN + ] + ] + ]); + $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); + ob_end_clean(); + header('Content-type: application/vnd.ms-excel'); + header('Content-Disposition: attachment; filename="du-lieu.xlsx"'); + header('Cache-Control: max-age=0'); + $objWriter->save('php://output'); + exit(); + } + } diff --git a/db/app.db b/db/app.db index b94da2d6a344134b68658e24a26c33e12c87b690..3f4ca5b57d6aabe360e65ef6463355e10ef82fce 100644 GIT binary patch delta 855 zcmZo@U~On%oggh}#=yWJ0>r{V%nrmyCh8b7nr%#2BG1Ldzng)7H~%O8+nf0mcJW*2 zG8UI4rllzYRT(g*q!uI=m58b{8*&yG<|M_Jq~;dHmn0_Tq{bUt@TfD(bEYLGr^e^x zrx$NBU}<1u|2~{TpHl3R;um?!80rl|%F&_{MLd;+g0@A$O6%-hs z@XK&>Ml8R8ldqQk|hl$@WJmYI^8mz;_a zOU(lZS4w_<5wc)%eoAUed_ihaaeiKWW^Q6Sl9se0pwT6nxyVAGU`Dp8pdP9f$$B(n zfbm#TT8yx?xTFNZ0U2MFp9i*rfq^j&?Ds@))B-);r{V%mKvy6LpLk4K^k$k>_IM-_5|koBt^P(an4cyZ9}v z8H-C2)6x`ystg!YQVSA`N^(>4O87XKnME0k67#azfHGo?$@#ejiFuVwKn^EoT7G`Y zCIglRHUl>MvIDxW_nvIN#t}HB0si`HId3pI|i6!|(#UT%OB?F~F za=cjNmgl7_m??xD-jt}2mvMN3f^TL?YO;=k%dri|76PS?OgOwIFGImEJ>%i-b{z!| zpbEc-yJuz^7@Ju_U|g|?npIq}b|f#*31C-U;@{3c7wEE1{!BSWRt6~+2{u*+Wfm6a z!&`tR0&Q887gyt_2f~uftPH9^8KCLQ6-plN?oQ5#E4EYwVM!*ilzZmkg}DkxCO+If zF*&Z-LJ)*O76}Qnup}lW$5ow}4Z@o)um?!81I2lOm=B0Sia;(B0%C#f3JQ!*_+_}b zmNW2O;%ne-;B(|j<@M!m;8EjR&h5)A$}GBF!GQ4$`(y(y{>i`D6ImD-n7lV9aAYw8 NSqyhKpW`%Q0|3gNh4BCY diff --git a/helpers/LogsGrid.php b/helpers/LogsGrid.php new file mode 100644 index 00000000..fe8641ae --- /dev/null +++ b/helpers/LogsGrid.php @@ -0,0 +1,32 @@ +app->app_name; + }; + } + + public static function getLayout() { + return "{items}
{summary}
{pager}
"; + } + + public static function actionTemplate() { + return "{update} {delete}"; + } + +} diff --git a/models/FaceLogs.php b/models/FaceLogs.php new file mode 100644 index 00000000..3401f428 --- /dev/null +++ b/models/FaceLogs.php @@ -0,0 +1,88 @@ + 'ID', + 'camera_id' => 'Camera ID', + 'confidence' => 'Confidence', + 'enable_door' => 'Enable Door', + 'encoded_person_image' => 'Encoded Person Image', + 'frametime' => 'Frametime', + 'face_id' => 'Face ID', + 'person_id' => 'Person ID', + 'image' => 'Image', + 'status' => 'Status', + 'stt' => 'Stt', + 'timezone' => 'Timezone', + 'name' => 'Name' + ]; + } + + public function create($data) { + $r = $this->load([ + 'camera_id' => $data['camera_id'], + 'confidence' => $data['confidence'], + 'enable_door' => $data['enable_door'], + 'encoded_person_image' => $data['encoded_person_image'], + 'frametime' => $data['frametime'], + 'face_id' => $data['face_id'], + 'person_id' => $data['person_id'], + 'image' => $data['image'], + 'status' => $data['status'], + 'stt' => $data['stt'], + 'timezone' => $data['timezone'], + 'name' => $data['name'] + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + +} diff --git a/models/FaceLogsSearch.php b/models/FaceLogsSearch.php new file mode 100644 index 00000000..422c42fe --- /dev/null +++ b/models/FaceLogsSearch.php @@ -0,0 +1,78 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + ]); + + $query->andFilterWhere(['like', 'camera_id', $this->camera_id]) + ->andFilterWhere(['like', 'confidence', $this->confidence]) + ->andFilterWhere(['like', 'enable_door', $this->enable_door]) + ->andFilterWhere(['like', 'encoded_person_image', $this->encoded_person_image]) + ->andFilterWhere(['like', 'frametime', $this->frametime]) + ->andFilterWhere(['like', 'face_id', $this->face_id]) + ->andFilterWhere(['like', 'person_id', $this->person_id]) + ->andFilterWhere(['like', 'image', $this->image]) + ->andFilterWhere(['like', 'status', $this->status]) + ->andFilterWhere(['like', 'stt', $this->stt]) + ->andFilterWhere(['like', 'timezone', $this->timezone]) + ->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } + +} diff --git a/views/dashboard/index.tpl b/views/dashboard/index.tpl index 54be249e..446adf55 100644 --- a/views/dashboard/index.tpl +++ b/views/dashboard/index.tpl @@ -4,5 +4,33 @@ {use class="app\assets\DashboardAsset"} {DashboardAsset::register($this)|void} {block name='content'} -
abc
+
+
+ +
+ {GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'layout'=> \app\helpers\LogsGrid::getLayout(), + 'tableOptions' => [ + 'class' => 'table table-striped table-bordered', + 'style' => 'background:#fff;min-width:700px;' + ], + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + 'camera_id', + 'confidence', + 'frametime', + 'face_id', + 'name', + 'stt', + 'timezone' + ] + ])} +
+
{/block} \ No newline at end of file