From 70db83d3abacd4cdc5e1cf75ca1ea47482a37812 Mon Sep 17 00:00:00 2001 From: dongpd Date: Fri, 9 Oct 2020 14:19:15 +0700 Subject: [PATCH] init Area full CRUD --- controllers/AreaController.php | 164 +++++++++++++++--- controllers/DepartmentController.php | 10 +- helpers/CommonGrid.php | 8 +- models/Area.php | 51 ++++++ models/AreaSearch.php | 20 +-- views/area/form.tpl | 14 +- views/area/index.tpl | 18 +- web/js/area.js | 79 ++------- widgets/Department.php | 22 --- widgets/SystemLogsView.php | 25 +++ widgets/Tree.php | 23 +++ widgets/TreeSub.php | 24 +++ .../views/system-logs-view.tpl | 0 .../views/{department.tpl => tree-sub.tpl} | 2 +- {views/department => widgets/views}/tree.tpl | 2 +- 15 files changed, 318 insertions(+), 144 deletions(-) delete mode 100644 widgets/Department.php create mode 100644 widgets/SystemLogsView.php create mode 100644 widgets/Tree.php create mode 100644 widgets/TreeSub.php rename views/department/logs.tpl => widgets/views/system-logs-view.tpl (100%) rename widgets/views/{department.tpl => tree-sub.tpl} (81%) rename {views/department => widgets/views}/tree.tpl (80%) diff --git a/controllers/AreaController.php b/controllers/AreaController.php index 7c0b9af8..18042333 100644 --- a/controllers/AreaController.php +++ b/controllers/AreaController.php @@ -8,6 +8,9 @@ use app\models\AreaSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use yii\helpers\Url; +use yii\helpers\Html; +use app\models\common; /** * AreaController implements the CRUD actions for Area model. @@ -42,43 +45,75 @@ class AreaController extends Controller { return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, - ]); - } - - public function actionView($id) { - return $this->render('view', [ - 'model' => $this->findModel($id), + "areaArray" => Area::areaArray() ]); } public function actionCreate() { $model = new Area(); + Yii::$app->response->format = "json"; + if (Yii::$app->request->post()) { + $data = Yii::$app->request->post(); + $check = Area::findOne(['code' => $data['Code']]); + if ($check) + return ["status" => false, "type" => "code"]; - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id]); + if ($model->create($data)) { + common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới khu vực: " . $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"]), + "areaArray" => Area::areaArray() + ]) + ]; } - - return $this->render('create', [ - 'model' => $model, - ]); } public function actionUpdate($id) { $model = $this->findModel($id); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id]); + Yii::$app->response->format = "json"; + if (Yii::$app->request->post()) { + $data = Yii::$app->request->post(); + $check = Area::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->description = $data["Description"]; + $model->modified_at = time(); + if ($model->save()) { + Area::updateAll(["pid" => $data["Code"]], ["pid" => $oldCode]); + common::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa khu vực: " . $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]), + "areaArray" => Area::areaArrayWithOut($id) + ]) + ]; } - - return $this->render('update', [ - 'model' => $model, - ]); } - public function actionDelete($id) { - $this->findModel($id)->delete(); - - return $this->redirect(['index']); + public function actionDelete() { + if (Yii::$app->request->post()) { + $lists = Yii::$app->request->post("lists"); + foreach ($lists as $key => $value) { + Area::deleteArea($value); + } + } } protected function findModel($id) { @@ -89,4 +124,87 @@ class AreaController extends Controller { throw new NotFoundHttpException('The requested page does not exist.'); } + public function actionExport() { + $objPHPExcel = new \PHPExcel(); + $objPHPExcel->setActiveSheetIndex(0); + $toExcelFile[] = ["Mã khu vực", "Tên khu vực", "khu vực cha", "Chú thích"]; + $areas = Area::find()->all(); + $areaArray = Area::areaArray(); + foreach ($areas as $k => $v) { + $ExportData[] = $v->code; + $ExportData[] = $v->name; + $ExportData[] = isset($areaArray[$v->pid]) ? $areaArray[$v->pid] : ""; + $ExportData[] = $v->description; + $toExcelFile[] = $ExportData; + unset($ExportData); + } + $totals = count($areas) + 1; + $activeSheet = $objPHPExcel->getActiveSheet(); + $activeSheet->getColumnDimension('A')->setWidth(15); + $activeSheet->getColumnDimension('B')->setWidth(50); + $activeSheet->getColumnDimension('C')->setWidth(50); + $activeSheet->getColumnDimension('D')->setWidth(50); + $activeSheet->getStyle("A1:D" . $totals)->getFont()->setName('Time New Roman')->setSize(10); + $activeSheet->getStyle("A1:D1")->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:D" . $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="area_' . date("YmdHis") . '.xlsx"'); + header('Cache-Control: max-age=0'); + common::SaveViaTempFile($objWriter); + exit(); + } + + 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 Area()]) + ]; + } + } + } diff --git a/controllers/DepartmentController.php b/controllers/DepartmentController.php index 5ed26d25..6041b9bd 100644 --- a/controllers/DepartmentController.php +++ b/controllers/DepartmentController.php @@ -5,7 +5,6 @@ namespace app\controllers; use Yii; use app\models\Department; use app\models\DepartmentSearch; -use app\models\SystemLogs; use app\models\common; use yii\web\Controller; use yii\web\NotFoundHttpException; @@ -225,10 +224,7 @@ class DepartmentController extends Controller { 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" => SystemLogs::find()->andWhere(["type" => Yii::$app->controller->id])->orderBy(['time' => SORT_DESC])->limit(30)->all(), - "userArray" => \app\models\User::userArray() - ]) + "form" => \app\widgets\SystemLogsView::widget(['type' => Yii::$app->controller->id]) ]; } } @@ -238,9 +234,7 @@ class DepartmentController extends Controller { Yii::$app->response->format = "json"; return [ "title" => Html::tag("i", "", ["class" => "fa fa-sitemap"]) . " Cây thư mục", - "form" => $this->renderPartial("tree", [ - "root" => Department::findOne(1) - ]) + "form" => \app\widgets\Tree::widget(["model" => new Department()]) ]; } } diff --git a/helpers/CommonGrid.php b/helpers/CommonGrid.php index 7c7ec76d..344e7a39 100644 --- a/helpers/CommonGrid.php +++ b/helpers/CommonGrid.php @@ -73,7 +73,7 @@ class CommonGrid { public static function rows($type) { return function($model, $index, $widget, $grid) use ($type) { return [ - "onclick" => "common.form(this, '{$type}');", + "ondblclick" => "common.form(this, '{$type}');", "style" => "cursor: pointer;", "data" => [ "href" => Url::to(["update", "id" => $model->id]) @@ -82,4 +82,10 @@ class CommonGrid { }; } + public static function pid($array) { + return function($model) use ($array) { + return isset($array[$model->pid]) ? $array[$model->pid] : ""; + }; + } + } diff --git a/models/Area.php b/models/Area.php index 40dfaeea..48cda8af 100644 --- a/models/Area.php +++ b/models/Area.php @@ -51,4 +51,55 @@ class Area extends \yii\db\ActiveRecord { ]; } + public static function areaArray() { + $lists = self::find()->all(); + $results = []; + foreach ($lists as $key => $value) { + $results[$value->code] = $value->name; + } + return $results; + } + + public static function areaArrayWithOut($id) { + $lists = self::find()->andWhere(["<>", "id", $id])->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"], + "description" => $data["Description"], + "created_at" => time(), + "modified_at" => time() + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + + public static function deleteArea($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::deleteArea($value->id); + } + $model->delete(); + common::insertSystemLogs(["action" => "delete", "description" => "Xóa khu vực: " . $model->name, "type" => "area"]); + } + } + } diff --git a/models/AreaSearch.php b/models/AreaSearch.php index eee4990a..85d813b7 100644 --- a/models/AreaSearch.php +++ b/models/AreaSearch.php @@ -10,24 +10,22 @@ use app\models\Area; /** * AreaSearch represents the model behind the search form of `app\models\Area`. */ -class AreaSearch extends Area -{ +class AreaSearch extends Area { + /** * {@inheritdoc} */ - public function rules() - { + public function rules() { return [ - [['id', 'code', 'pid', 'created_at', 'modified_at'], 'integer'], - [['name', 'description'], 'safe'], + [['id', 'code', 'pid'], 'integer'], + [['name', 'description'], 'safe'], ]; } /** * {@inheritdoc} */ - public function scenarios() - { + public function scenarios() { // bypass scenarios() implementation in the parent class return Model::scenarios(); } @@ -39,8 +37,7 @@ class AreaSearch extends Area * * @return ActiveDataProvider */ - public function search($params) - { + public function search($params) { $query = Area::find(); // add conditions that should always apply here @@ -67,8 +64,9 @@ class AreaSearch extends Area ]); $query->andFilterWhere(['like', 'name', $this->name]) - ->andFilterWhere(['like', 'description', $this->description]); + ->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; } + } diff --git a/views/area/form.tpl b/views/area/form.tpl index 6ce4f072..b2a9d639 100644 --- a/views/area/form.tpl +++ b/views/area/form.tpl @@ -12,26 +12,32 @@
-
Tên phòng ban *
+
Tên khu vực *
-
Mã phòng ban *
+
Mã khu vực *
code==1}disabled=""{/if}>
-
Trực thuộc *
+
Khu vực cha *
+
+
+
Chú thích
+ +
+
{/foreach} \ No newline at end of file diff --git a/views/department/tree.tpl b/widgets/views/tree.tpl similarity index 80% rename from views/department/tree.tpl rename to widgets/views/tree.tpl index 8d337a19..2970c88f 100644 --- a/views/department/tree.tpl +++ b/widgets/views/tree.tpl @@ -2,6 +2,6 @@ {$root->code}-{$root->name}
- {\app\widgets\Department::widget(["pid"=>1])} + {\app\widgets\TreeSub::widget(["pid"=>1,"model"=>$model])}
\ No newline at end of file