diff --git a/.gitignore b/.gitignore index 78859a01..35905b2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /runtime /web/assets /web/data/uploads +/vendor/ \ No newline at end of file diff --git a/assets/DepartmentAsset.php b/assets/DepartmentAsset.php new file mode 100644 index 00000000..92571c24 --- /dev/null +++ b/assets/DepartmentAsset.php @@ -0,0 +1,23 @@ + 'yii\db\Connection', - 'dsn' => 'mysql:host=localhost;dbname=traffic', + 'dsn' => 'mysql:host=localhost;dbname=access_control', 'username' => 'root', 'password' => '', 'charset' => 'utf8', diff --git a/controllers/DepartmentController.php b/controllers/DepartmentController.php new file mode 100644 index 00000000..0f0af742 --- /dev/null +++ b/controllers/DepartmentController.php @@ -0,0 +1,236 @@ +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)) { + Department::insertSystemLogs(["action" => "insert", "description" => "Thêm mới phòng ban: " . $data["Name"]]); + 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"]; + $model->modified_at = time(); + if ($model->save()) { + Department::updateAll(["pid" => $data["Code"]], ["pid" => $oldCode]); + Department::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa phòng ban: " . $data["Name"]]); + 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::departmentArray() + ]) + ]; + } + } + + 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'); + $objWriter->save('php://output'); + 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); + Department::insertSystemLogs(["action" => "import", "description" => "Nhập dữ liệu: " . count($post) . " phòng ban mới"]); + 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" => $this->renderPartial("logs", [ + "logs" => LogsDepartment::find()->orderBy(['time' => SORT_DESC])->limit(30)->all(), + "userArray" => \app\models\User::userArray() + ]) + ]; + } + } + +} diff --git a/helpers/DepartmentGrid.php b/helpers/DepartmentGrid.php new file mode 100644 index 00000000..19680f33 --- /dev/null +++ b/helpers/DepartmentGrid.php @@ -0,0 +1,36 @@ +pid]) ? $departmentArray[$model->pid] : ""; + }; + } + + public static function checkbox() { + return function($model) { + if ($model->id == 1) + return ""; + return ""; + }; + } + + public static function rows() { + return function($model, $index, $widget, $grid) { + return [ + "onclick" => "common.form(this, 'department');", + "style" => "cursor: pointer;", + "data" => [ + "href" => Url::to(["update", "id" => $model->id]) + ] + ]; + }; + } + +} diff --git a/models/Department.php b/models/Department.php new file mode 100644 index 00000000..5c895df9 --- /dev/null +++ b/models/Department.php @@ -0,0 +1,111 @@ + 100], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() { + return [ + 'id' => 'ID', + 'code' => 'Mã phòng ban', + 'pid' => 'Trực thuộc', + 'name' => 'Tên phòng ban', + 'created_at' => 'Thời gian tạo', + 'modified_at' => 'Thời gian sửa', + ]; + } + + public static function departmentArray() { + $lists = self::find()->all(); + $results = []; + foreach ($lists as $key => $value) { + $results[$value->code] = $value->name; + } + return $results; + } + + public function create($data) { + $r = $this->load([ + "code" => $data["Code"], + "pid" => $data["Pid"], + "name" => $data["Name"], + "created_at" => time(), + "modified_at" => time() + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + + public function multiCreate($datas) { + $field = ['code', 'pid', 'name', 'created_at', 'modified_at']; + static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute(); + return; + } + + public static function deleteDepartment($id) { + if ($id == 1) + return; + $model = self::findOne($id); + if ($model) { + $childs = self::find()->andWhere(["pid" => $model->code])->all(); + foreach ($childs as $key => $value) { + self::deleteDepartment($value->id); + } + $model->delete(); + self::insertSystemLogs(["action" => "delete", "description" => "Xóa phòng ban: " . $model->name]); + } + } + + public static function insertSystemLogs($data) { + $model = new LogsDepartment(); + return $model->create($data); + } + + public function checkStatusImport($data) { + if ($this->findOne(["code" => $data["A"]])) + return ["status" => false, "description" => "Mã phòng ban đã tồn tại"]; + if (!$this->findOne(["name" => $data["C"]])) + return ["status" => true, "description" => "Phòng ban trực thuộc không tồn tại"]; + return ["status" => true, "description" => ""]; + } + +} diff --git a/models/DepartmentSearch.php b/models/DepartmentSearch.php new file mode 100644 index 00000000..5ada033e --- /dev/null +++ b/models/DepartmentSearch.php @@ -0,0 +1,71 @@ + $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, + 'code' => $this->code, + 'pid' => $this->pid, + 'created_at' => $this->created_at, + 'modified_at' => $this->modified_at, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } + +} diff --git a/models/LogsDepartment.php b/models/LogsDepartment.php new file mode 100644 index 00000000..923da0c8 --- /dev/null +++ b/models/LogsDepartment.php @@ -0,0 +1,67 @@ + 10], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() { + return [ + 'id' => 'ID', + 'user_id' => 'User ID', + 'time' => 'Time', + 'action' => 'Action', + 'description' => 'Description', + ]; + } + + public function create($data) { + $r = $this->load([ + 'user_id' => Yii::$app->user->id, + 'time' => time(), + 'action' => $data['action'], + 'description' => $data['description'] + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + +} diff --git a/models/User.php b/models/User.php index 4e59824e..dee3188c 100644 --- a/models/User.php +++ b/models/User.php @@ -201,4 +201,13 @@ class User extends ActiveRecord implements \yii\web\IdentityInterface { return $this->user_image == null ? $directoryAsset . "/img/user2-160x160.jpg" : Yii::getAlias("@images_folder") . $this->user_image; } + public static function userArray() { + $lists = self::find()->all(); + $results = []; + foreach ($lists as $key => $value) { + $results[$value->id] = $value->first_name; + } + return $results; + } + } diff --git a/models/common.php b/models/common.php index 5f8ed625..1815bf2a 100644 --- a/models/common.php +++ b/models/common.php @@ -56,13 +56,13 @@ class common extends \yii\db\ActiveRecord { } public function formatTime($time, $format = "d/m/Y") { -// $now = time(); -// $range = $now - $time; -// if ($range > 60 * 60 * 12) { + $now = time(); + $range = $now - $time; + if ($range > 60 * 60 * 12) { return date($format, $time); -// } else { -// return Yii::$app->formatter->asRelativeTime($time); -// } + } else { + return Yii::$app->formatter->asRelativeTime($time); + } } //Upload diff --git a/nbproject/project.xml b/nbproject/project.xml index 70fd6221..bec7282a 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -3,7 +3,7 @@ org.netbeans.modules.php.project - traffic + AccessControl diff --git a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php index a558ac5f..90229451 100644 --- a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php +++ b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php @@ -11,18 +11,42 @@ use yii\widgets\ActiveForm; params['hideInfomation']) { - echo Html::a('AIPAIParking', Yii::$app->homeUrl, ['class' => 'logo']); + echo Html::a('BIAccess Control', Yii::$app->homeUrl, ['class' => 'logo']); } else { echo Html::a('BI', Yii::$app->homeUrl, ['class' => 'logo']); } ?>