user->isGuest) return $this->redirect(['/site/login']); } /** * {@inheritdoc} */ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } public function actionIndex() { $this->view->title = 'Phòng ban'; $searchModel = new DepartmentSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, "departmentArray" => Department::departmentArray() ]); } public function actionCreate() { $model = new Department(); Yii::$app->response->format = "json"; if (Yii::$app->request->post()) { $data = Yii::$app->request->post(); $check = Department::findOne(['code' => $data['Code']]); if ($check) return ["status" => false, "type" => "code"]; if ($model->create($data)) { common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới phòng ban: " . $data["Name"], "type" => Yii::$app->controller->id]); return ["status" => true]; } else return ["status" => false, "type" => "error"]; } else { return [ "title" => Html::tag("i", "", ["class" => "fa fa-plus-square"]) . " Thêm", "form" => $this->renderPartial("form", [ "model" => $model, "url" => Url::to(["create"]), "departmentArray" => Department::departmentArray() ]) ]; } } public function actionUpdate($id) { $model = $this->findModel($id); Yii::$app->response->format = "json"; if (Yii::$app->request->post()) { $data = Yii::$app->request->post(); $check = Department::findOne(['code' => $data['Code']]); if ($check && $check->id != $id) return ["status" => false, "type" => "code"]; $oldCode = $model->code; $model->name = $data["Name"]; $model->code = $data["Code"]; $model->pid = $data["Pid"] !== "" ? $data["Pid"] : 0; $model->modified_at = time(); if ($model->save()) { Department::updateAll(["pid" => $data["Code"]], ["pid" => $oldCode]); common::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa phòng ban: " . $data["Name"], "type" => Yii::$app->controller->id]); return ["status" => true]; } else return ["status" => false, "type" => "error"]; } else { return [ "title" => Html::tag("i", "", ["class" => "fa fa-edit"]) . " Tùy chỉnh", "form" => $this->renderPartial("form", [ "model" => $model, "url" => Url::to(["update", "id" => $id]), "departmentArray" => Department::departmentArrayWithOut($id) ]) ]; } } public function actionDelete() { if (Yii::$app->request->post()) { $lists = Yii::$app->request->post("lists"); foreach ($lists as $key => $value) { Department::deleteDepartment($value); } } } protected function findModel($id) { if (($model = Department::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); } public function actionExport() { $objPHPExcel = new \PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $toExcelFile[] = ["Mã phòng ban", "Tên phòng ban", "Trực thuộc"]; $departments = Department::find()->all(); $departmentArray = Department::departmentArray(); foreach ($departments as $k => $v) { $ExportData[] = $v->code; $ExportData[] = $v->name; $ExportData[] = isset($departmentArray[$v->pid]) ? $departmentArray[$v->pid] : ""; $toExcelFile[] = $ExportData; unset($ExportData); } $totals = count($departments) + 1; $activeSheet = $objPHPExcel->getActiveSheet(); $activeSheet->getColumnDimension('A')->setWidth(15); $activeSheet->getColumnDimension('B')->setWidth(50); $activeSheet->getColumnDimension('C')->setWidth(50); $activeSheet->getStyle("A1:C" . $totals)->getFont()->setName('Time New Roman')->setSize(10); $activeSheet->getStyle("A1:C1")->applyFromArray([ 'fill' => array( 'type' => \PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '7ac3ec') ) ]); $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++; } $activeSheet->getStyle("A1:C" . $totals)->applyFromArray([ 'alignment' => [ 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, ], '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="department_' . date("YmdHis") . '.xlsx"'); header('Cache-Control: max-age=0'); common::SaveViaTempFile($objWriter); exit(); } public function actionUpload() { if (Yii::$app->request->post()) { $common = new common(); $fileUploads = $common->UploadFile("file", ["XLS", "XLSX"], "excel"); $file_type = \PHPExcel_IOFactory::identify($fileUploads); $objReader = \PHPExcel_IOFactory::createReader($file_type); $objPHPExcel = $objReader->load($fileUploads); $sheet_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); Yii::$app->response->format = 'json'; return [ "title" => Html::tag("i", "", ["class" => "fa fa-upload"]) . " Nhập", "form" => $this->renderPartial("import", [ "data" => $sheet_data, "model" => new Department() ]) ]; } } public function actionImport() { if (Yii::$app->request->post()) { $post = Yii::$app->request->post("lists"); $datas = []; foreach ($post as $key => $value) { $val = json_decode($value, true); $parent = Department::findOne(["name" => $val["C"]]); $datas[] = [$val["A"], $parent ? $parent->code : 1, $val["B"], time(), time()]; } $model = new Department(); $model->multiCreate($datas); common::insertSystemLogs(["action" => "import", "description" => "Nhập dữ liệu: " . count($post) . " phòng ban mới", "type" => Yii::$app->controller->id]); return; } } public function actionLogs() { if (Yii::$app->request->isAjax) { Yii::$app->response->format = "json"; return [ "title" => Html::tag("i", "", ["class" => "fa fa-file"]) . " Ghi nhận hệ thống", "form" => \app\widgets\SystemLogsView::widget(['type' => Yii::$app->controller->id]) ]; } } public function actionTree() { if (Yii::$app->request->isAjax) { Yii::$app->response->format = "json"; return [ "title" => Html::tag("i", "", ["class" => "fa fa-sitemap"]) . " Cây thư mục", "form" => \app\widgets\Tree::widget(["model" => new Department()]) ]; } } public function actionTest() { $timezone = [ "DeviceIP" => "192.168.1.200", "TimezoneId" => 1, "SunTime1" => 0, "SunTime2" => 0, "SunTime3" => 0, "MonTime1" => 0, "MonTime2" => 0, "MonTime3" => 0, "TueTime1" => 0, "TueTime2" => 0, "TueTime3" => 0, "WedTime1" => 0, "WedTime2" => 0, "WedTime3" => 0, "ThuTime1" => 0, "ThuTime2" => 0, "ThuTime3" => 0, "FriTime1" => $this->convertTime("8:30", "12:30"), "FriTime2" => $this->convertTime("13:30", "18:30"), "FriTime3" => 0, "SatTime1" => 0, "SatTime2" => 0, "SatTime3" => 0, "Hol1Time1" => 0, "Hol1Time2" => 0, "Hol1Time3" => 0, "Hol2Time1" => 0, "Hol2Time2" => 0, "Hol2Time3" => 0, "Hol3Time1" => 0, "Hol3Time2" => 0, "Hol3Time3" => 0 ]; $timezoneRq = $this->requestToMCard("/SetDeviceData/timezone", $timezone); $user = [ "DeviceIP" => "192.168.1.200", "CardNo" => 16673827, "Pin" => 69, "Password" => "", "Group" => "", "StartTime" => "", "EndTime" => "" ]; $userRq = $this->requestToMCard("/SetDeviceData/user", $user); $userAuthor = [ "DeviceIP" => "192.168.1.200", "Pin" => 69, "AuthorizeTimezoneId" => 1, "AuthorizeDoorId" => 2 ]; $userAuthorRq = $this->requestToMCard("/SetDeviceData/userauthorize", $userAuthor); return var_dump($timezoneRq, $userRq, $userAuthorRq); } public function requestToMCard($path, $data) { return file_get_contents("http://192.168.2.119:2001" . $path, false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode($data) ] ])); } public function convertTime($from, $to) { $fTemp = explode(":", $from); $front = dechex(intval($fTemp[0]) * 100 + intval($fTemp[1])); $tTemp = explode(":", $to); $back = dechex(intval($tTemp[0]) * 100 + intval($tTemp[1])); return hexdec("0" . $front . "0" . $back); } }