BiFace_Server_Lite/controllers/DashboardController.php
2020-03-27 15:03:44 +07:00

148 lines
5.0 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use app\models\FaceLogs;
use app\models\FaceLogsSearch;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\helpers\Url;
/**
* ScriptController implements the CRUD actions for Script model.
*/
class DashboardController extends Controller {
public function init() {
parent::init();
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Script models.
* @return mixed
*/
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", "DỮ LIỆU 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');
$objWriter->save('du-lieu.xlsx');
return $this->redirect(Yii::$app->request->hostInfo . "/BiFace_Server_Lite/web/du-lieu.xlsx");
}
}