diff --git a/access_control.sql b/access_control.sql index 8400f022..a2edbbfd 100644 --- a/access_control.sql +++ b/access_control.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 --- Generation Time: Oct 10, 2020 at 12:36 PM +-- Generation Time: Oct 13, 2020 at 08:40 AM -- Server version: 10.4.8-MariaDB -- PHP Version: 7.1.33 @@ -148,14 +148,48 @@ CREATE TABLE `device` ( `id` int(11) NOT NULL, `name` varchar(100) NOT NULL, `serial` varchar(50) DEFAULT NULL, - `ip_address` varchar(20) NOT NULL, + `ip_address` varchar(20) DEFAULT NULL, + `subnet_mask` varchar(20) DEFAULT NULL, + `gateway` varchar(20) DEFAULT NULL, + `mac_address` varchar(20) DEFAULT NULL, `status` int(11) NOT NULL, - `type` int(11) NOT NULL, + `type` varchar(20) DEFAULT NULL, + `version` varchar(100) DEFAULT NULL, `area_id` int(11) NOT NULL, `created_at` int(11) NOT NULL DEFAULT 0, `modified_at` int(11) NOT NULL DEFAULT 0 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-- +-- Dumping data for table `device` +-- + +INSERT INTO `device` (`id`, `name`, `serial`, `ip_address`, `subnet_mask`, `gateway`, `mac_address`, `status`, `type`, `version`, `area_id`, `created_at`, `modified_at`) VALUES +(4, 'Thiết bị 1', 'AJNV193560605', '192.168.1.200', '255.255.252.0', '192.168.0.1', '00:17:61:C9:6B:A4', 1, 'C3-200', 'AC Ver 4.3.4 Jan 5 2019', 1, 1602563228, 1602563228); + +-- -------------------------------------------------------- + +-- +-- Table structure for table `door` +-- + +CREATE TABLE `door` ( + `id` int(11) NOT NULL, + `device_id` int(11) NOT NULL, + `name` varchar(100) NOT NULL, + `code` int(11) NOT NULL, + `created_at` int(11) NOT NULL DEFAULT 0, + `modified_at` int(11) NOT NULL DEFAULT 0 +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Dumping data for table `door` +-- + +INSERT INTO `door` (`id`, `device_id`, `name`, `code`, `created_at`, `modified_at`) VALUES +(7, 4, 'Thiết bị 1-1', 1, 1602563228, 1602563228), +(8, 4, 'Thiết bị 1-2', 2, 1602563228, 1602563228); + -- -------------------------------------------------------- -- @@ -240,7 +274,56 @@ INSERT INTO `system_logs` (`id`, `user_id`, `time`, `action`, `description`, `ty (28, 1, 1602325273, 'update', 'Thay đổi đăng ký thẻ: Lê Hải Châu', 'card-register'), (29, 1, 1602325978, 'delete', 'Xóa đăng ký thẻ: 4 nhân viên', 'card-register'), (30, 1, 1602326177, 'register', 'Đăng ký thẻ mới: Phạm Đức Đông', 'card-register'), -(31, 1, 1602326183, 'register', 'Đăng ký thẻ mới: Ngô Văn Dũng', 'card-register'); +(31, 1, 1602326183, 'register', 'Đăng ký thẻ mới: Ngô Văn Dũng', 'card-register'), +(32, 1, 1602478486, 'insert', 'Thêm mới thiết bị: Thiết bị 1', 'device'), +(33, 1, 1602484460, 'insert', 'Thêm mới thiết bị: Thiết bị 2', 'device'), +(34, 1, 1602487344, 'insert', 'Thêm mới thiết bị: Thiết bị 1', 'device'), +(35, 1, 1602487352, 'insert', 'Thêm mới thiết bị: Thiết bị 2', 'device'), +(36, 1, 1602489265, 'update', 'Chỉnh sửa thiết bị: Thiết bị 2232', 'device'), +(37, 1, 1602489271, 'update', 'Chỉnh sửa thiết bị: Thiết bị 2', 'device'), +(38, 1, 1602489594, 'delete', 'Xóa thiết bị: Thiết bị 1', 'device'), +(39, 1, 1602489594, 'delete', 'Xóa thiết bị: Thiết bị 2', 'device'), +(40, 1, 1602489718, 'insert', 'Thêm mới thiết bị: Thiết bị 1', 'device'), +(41, 1, 1602489725, 'insert', 'Thêm mới thiết bị: Thiết bị 2', 'device'), +(42, 1, 1602490553, 'delete', 'Xóa thiết bị: Thiết bị 1', 'device'), +(43, 1, 1602490553, 'delete', 'Xóa thiết bị: Thiết bị 2', 'device'), +(44, 1, 1602490559, 'insert', 'Thêm mới thiết bị: Thiết bị 1', 'device'), +(45, 1, 1602490566, 'insert', 'Thêm mới thiết bị: Thiết bị 2', 'device'), +(46, 1, 1602493420, 'delete', 'Xóa thiết bị: Thiết bị 1', 'device'), +(47, 1, 1602493420, 'delete', 'Xóa thiết bị: Thiết bị 2', 'device'), +(48, 1, 1602561201, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(49, 1, 1602561202, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(50, 1, 1602561313, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(51, 1, 1602561314, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(52, 1, 1602561316, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(53, 1, 1602561418, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(54, 1, 1602561420, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(55, 1, 1602561422, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(56, 1, 1602561547, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(57, 1, 1602561548, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(58, 1, 1602561549, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(59, 1, 1602561666, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(60, 1, 1602561667, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(61, 1, 1602561669, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(62, 1, 1602562223, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(63, 1, 1602562225, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(64, 1, 1602562226, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(65, 1, 1602562546, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(66, 1, 1602562547, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(67, 1, 1602562548, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(68, 1, 1602562578, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(69, 1, 1602562580, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(70, 1, 1602562581, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(71, 1, 1602562649, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(72, 1, 1602562650, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(73, 1, 1602562653, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(74, 1, 1602562727, 'insert', 'Thêm mới thiết bị: 192.168.1.200', 'device'), +(75, 1, 1602562729, 'insert', 'Thêm mới thiết bị: 192.168.1.202', 'device'), +(76, 1, 1602562730, 'insert', 'Thêm mới thiết bị: 192.168.1.203', 'device'), +(77, 1, 1602562860, 'delete', 'Xóa thiết bị: 192.168.1.200', 'device'), +(78, 1, 1602562860, 'delete', 'Xóa thiết bị: 192.168.1.202', 'device'), +(79, 1, 1602562860, 'delete', 'Xóa thiết bị: 192.168.1.203', 'device'), +(80, 1, 1602563228, 'insert', 'Thêm mới thiết bị: Thiết bị 1', 'device'); -- -------------------------------------------------------- @@ -319,6 +402,12 @@ ALTER TABLE `department` ALTER TABLE `device` ADD PRIMARY KEY (`id`); +-- +-- Indexes for table `door` +-- +ALTER TABLE `door` + ADD PRIMARY KEY (`id`); + -- -- Indexes for table `staff` -- @@ -358,7 +447,13 @@ ALTER TABLE `department` -- AUTO_INCREMENT for table `device` -- ALTER TABLE `device` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; + +-- +-- AUTO_INCREMENT for table `door` +-- +ALTER TABLE `door` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9; -- -- AUTO_INCREMENT for table `staff` @@ -370,7 +465,7 @@ ALTER TABLE `staff` -- AUTO_INCREMENT for table `system_logs` -- ALTER TABLE `system_logs` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=32; + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=81; -- -- AUTO_INCREMENT for table `user` diff --git a/assets/DeviceAsset.php b/assets/DeviceAsset.php new file mode 100644 index 00000000..012ad0ce --- /dev/null +++ b/assets/DeviceAsset.php @@ -0,0 +1,23 @@ +render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, - ]); - } - - public function actionView($id) { - return $this->render('view', [ - 'model' => $this->findModel($id), + 'statusArray' => Device::$statusArray, + 'typeArray' => Device::$typeArray, + 'areaArray' => Area::areaArray() ]); } public function actionCreate() { $model = new Device(); + Yii::$app->response->format = "json"; + if (Yii::$app->request->post()) { + $data = Yii::$app->request->post(); + $check = Device::findOne(['ip_address' => $data['Ip']]); + if ($check) + return ["status" => false, "type" => "ip"]; - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id]); + $searchDevice = json_decode(common::requestToCardService("/SearchDevice", ["NetMask" => "255.255.255.255"]), true); + $device = false; + foreach ($searchDevice as $key => $value) { + if ($value["IP"] === $data["Ip"]) + $device = $value; + } + if (!$device) + return ["status" => false, "type" => "connect"]; + + $requestToCardService = json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => $data["Ip"]]), true); + if ($requestToCardService["Status"] === "ERROR") + return ["status" => false, "type" => "connect"]; + + $device_id = $model->create([ + 'name' => $data["Name"], + 'serial' => isset($device['SN']) ? $device['SN'] : "", + 'ip_address' => isset($device['IP']) ? $device['IP'] : "", + 'subnet_mask' => isset($device['NetMask']) ? $device['NetMask'] : "", + 'gateway' => isset($device['GATEIPAddress']) ? $device['GATEIPAddress'] : "", + 'mac_address' => isset($device['MAC']) ? $device['MAC'] : "", + 'status' => 1, + 'type' => isset($device['Device']) ? $device['Device'] : "", + 'version' => isset($device['Ver']) ? $device['Ver'] : "", + 'area_id' => $data["Area"] + ]); + if ($device_id) { + $datas = []; + for ($i = 1; $i <= intval($requestToCardService["LockCount"]); $i++) { + $datas[] = [$device_id, $data["Name"] . "-" . $i, $i, time(), time()]; + } + $door = new Door(); + $door->multiCreate($datas); + common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới thiết bị: " . $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(); + $model->name = $data["Name"]; + $model->area_id = $data["Area"]; + $model->modified_at = time(); + if ($model->save()) { + common::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa thiết bị: " . $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::areaArray() + ]) + ]; } - - 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) { + Device::deleteDevice($value); + } + } } protected function findModel($id) { @@ -89,4 +152,174 @@ class DeviceController extends Controller { throw new NotFoundHttpException('The requested page does not exist.'); } + public function actionCheckConnection() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + return json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => Yii::$app->request->post("Ip")]), true); + } + } + + public function actionExport() { + $objPHPExcel = new \PHPExcel(); + $objPHPExcel->setActiveSheetIndex(0); + $toExcelFile[] = ["Tên thiết bị", "Serial", "Địa chỉ IP", "Loại thiết bị", "Khu vực"]; + $devices = Device::find()->all(); + $areaArray = Area::areaArray(); + $typeArray = Device::$typeArray; + foreach ($devices as $k => $v) { + $ExportData[] = $v->name; + $ExportData[] = $v->serial; + $ExportData[] = $v->ip_address; + $ExportData[] = isset($typeArray[$v->type]) ? $typeArray[$v->type] : ""; + $ExportData[] = isset($areaArray[$v->area_id]) ? $areaArray[$v->area_id] : ""; + $toExcelFile[] = $ExportData; + unset($ExportData); + } + $totals = count($devices) + 1; + $activeSheet = $objPHPExcel->getActiveSheet(); + $activeSheet->getStyle("A1:E" . $totals)->getFont()->setName('Time New Roman')->setSize(10); + $activeSheet->getStyle("A1:E1")->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:E" . $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="device_' . 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 actionChangeIp($id) { + $model = $this->findModel($id); + Yii::$app->response->format = "json"; + if (Yii::$app->request->post()) { + $data = Yii::$app->request->post(); + $OldIpAddress = $model->ip_address; + if ($OldIpAddress !== $data["Ip"]) { + return json_decode(common::requestToCardService("/ModifyIPAddress", [ + "OldIPAddress" => $OldIpAddress, + "NewIPAddress" => $data["Ip"], + "NetMask" => $data["SubnetMask"], + "MAC" => "" + ]), true); + } + return true; + } else { + return [ + "title" => Html::tag("i", "", ["class" => "fa fa-pencil"]) . " Thay đổi địa chỉ IP", + "form" => $this->renderPartial("change-ip", [ + "model" => $model, + "url" => Url::to(["change-ip", "id" => $id]) + ]) + ]; + } + } + + public function actionSearch() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + $datas = json_decode(common::requestToCardService("/SearchDevice", ["NetMask" => Yii::$app->request->post("SubnetMask")]), true); + return [ + "form" => $this->renderPartial("device-lists", [ + "datas" => $datas, + "deviceIpLists" => Device::deviceIpLists() + ]), + "totals" => count($datas) + ]; + } else { + $this->view->title = 'Tìm kiếm thiết bị'; + + return $this->render('search', [ + ]); + } + } + + public function actionProcess() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + + return [ + "title" => Html::tag("i", "", ["class" => "fa fa-plus"]) . " Thêm thiết bị", + "form" => $this->renderPartial("process"), + "lists" => Yii::$app->request->post("lists") + ]; + } + } + + public function actionImport() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + $data = json_decode(Yii::$app->request->post("data"), true); + $check = Device::findOne(['ip_address' => $data["IP"]]); + if ($check) + return ["status" => false, "IP" => $data["IP"]]; + $model = new Device(); + $device_id = $model->create([ + 'name' => isset($data['IP']) ? $data['IP'] : "", + 'serial' => isset($data['SN']) ? $data['SN'] : "", + 'ip_address' => isset($data['IP']) ? $data['IP'] : "", + 'subnet_mask' => isset($data['NetMask']) ? $data['NetMask'] : "", + 'gateway' => isset($data['GATEIPAddress']) ? $data['GATEIPAddress'] : "", + 'mac_address' => isset($data['MAC']) ? $data['MAC'] : "", + 'status' => 1, + 'type' => isset($data['Device']) ? $data['Device'] : "", + 'version' => isset($data['Ver']) ? $data['Ver'] : "", + 'area_id' => 1 + ]); + if ($device_id) { + $checkConnection = json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => $data["IP"]]), true); + $datas = []; + for ($i = 1; $i <= intval($checkConnection["LockCount"]); $i++) { + $datas[] = [$device_id, $data["IP"] . "-" . $i, $i, time(), time()]; + } + $door = new Door(); + $door->multiCreate($datas); + common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới thiết bị: " . $data["IP"], "type" => Yii::$app->controller->id]); + return ["status" => true, "IP" => $data["IP"], "SN" => $data["SN"]]; + } else + return ["status" => false, "IP" => $data["IP"], "SN" => $data["SN"]]; + } + } + } diff --git a/helpers/DeviceGrid.php b/helpers/DeviceGrid.php new file mode 100644 index 00000000..b73bc949 --- /dev/null +++ b/helpers/DeviceGrid.php @@ -0,0 +1,27 @@ +status) + return ""; + return ""; + }; + } + + public static function type($array) { + return function($model) use ($array) { + return isset($array[$model->type]) ? $array[$model->type] : ""; + }; + } + + public static function area($array) { + return function($model) use ($array) { + return isset($array[$model->area_id]) ? $array[$model->area_id] : ""; + }; + } + +} diff --git a/models/Device.php b/models/Device.php index 5e1e7eef..229cb766 100644 --- a/models/Device.php +++ b/models/Device.php @@ -11,51 +11,110 @@ use Yii; * @property string $name * @property string $serial * @property string $ip_address + * @property string $subnet_mask + * @property string $gateway + * @property string $mac_address * @property int $status - * @property int $type + * @property string $type + * @property string $version * @property int $area_id * @property int $created_at * @property int $modified_at */ -class Device extends \yii\db\ActiveRecord -{ +class Device extends \yii\db\ActiveRecord { + /** * {@inheritdoc} */ - public static function tableName() - { + public static function tableName() { return 'device'; } /** * {@inheritdoc} */ - public function rules() - { + public function rules() { return [ - [['name', 'ip_address', 'status', 'type', 'area_id'], 'required'], - [['status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'], - [['name'], 'string', 'max' => 100], - [['serial'], 'string', 'max' => 50], - [['ip_address'], 'string', 'max' => 20], + [['name', 'ip_address', 'status', 'type', 'area_id'], 'required'], + [['status', 'area_id', 'created_at', 'modified_at'], 'integer'], + [['name', 'version'], 'string', 'max' => 100], + [['serial'], 'string', 'max' => 50], + [['ip_address', 'subnet_mask', 'gateway', 'mac_address', 'type'], 'string', 'max' => 20], ]; } /** * {@inheritdoc} */ - public function attributeLabels() - { + public function attributeLabels() { return [ 'id' => 'ID', - 'name' => 'Name', + 'name' => 'Tên thiết bị', 'serial' => 'Serial', - 'ip_address' => 'Ip Address', - 'status' => 'Status', - 'type' => 'Type', - 'area_id' => 'Area ID', - 'created_at' => 'Created At', - 'modified_at' => 'Modified At', + 'ip_address' => 'Địa chỉ IP', + 'subnet_mask' => 'Subnet mask', + 'gateway' => 'Gateway', + 'mac_address' => 'Mac Address', + 'status' => 'Trạng thái', + 'type' => 'Loại thiết bị', + 'version' => 'Phiên bản', + 'area_id' => 'Khu vực', + 'created_at' => 'Thời gian tạo', + 'modified_at' => 'Thời gian sửa', ]; } + + public static $typeArray = [ + 1 => "C3-100", + 2 => "C3-200", + 4 => "C3-400" + ]; + public static $statusArray = [ + 0 => "Ngưng hoạt động", + 1 => "Hoạt động" + ]; + + public function create($data) { + $r = $this->load([ + 'name' => $data['name'], + 'serial' => $data['serial'], + 'ip_address' => $data['ip_address'], + 'subnet_mask' => $data['subnet_mask'], + 'gateway' => $data['gateway'], + 'mac_address' => $data['mac_address'], + 'status' => $data['status'], + 'type' => $data['type'], + 'version' => $data['version'], + 'area_id' => $data['area_id'], + "created_at" => time(), + "modified_at" => time() + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + + public static function deleteDevice($id) { + $model = self::findOne($id); + if ($model) { + Door::deleteAll(['device_id' => $model->id]); + $model->delete(); + common::insertSystemLogs(["action" => "delete", "description" => "Xóa thiết bị: " . $model->name, "type" => "device"]); + } + } + + public static function deviceIpLists() { + $lists = self::find()->all(); + $results = []; + foreach ($lists as $key => $value) { + $results[] = $value->ip_address; + } + return $results; + } + } diff --git a/models/DeviceSearch.php b/models/DeviceSearch.php index 45aae1fb..58de6974 100644 --- a/models/DeviceSearch.php +++ b/models/DeviceSearch.php @@ -10,24 +10,22 @@ use app\models\Device; /** * DeviceSearch represents the model behind the search form of `app\models\Device`. */ -class DeviceSearch extends Device -{ +class DeviceSearch extends Device { + /** * {@inheritdoc} */ - public function rules() - { + public function rules() { return [ - [['id', 'status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'], - [['name', 'serial', 'ip_address'], 'safe'], + [['id', 'status', 'type', 'area_id'], 'integer'], + [['name', 'serial', 'ip_address'], 'safe'], ]; } /** * {@inheritdoc} */ - public function scenarios() - { + public function scenarios() { // bypass scenarios() implementation in the parent class return Model::scenarios(); } @@ -39,8 +37,7 @@ class DeviceSearch extends Device * * @return ActiveDataProvider */ - public function search($params) - { + public function search($params) { $query = Device::find(); // add conditions that should always apply here @@ -68,9 +65,10 @@ class DeviceSearch extends Device ]); $query->andFilterWhere(['like', 'name', $this->name]) - ->andFilterWhere(['like', 'serial', $this->serial]) - ->andFilterWhere(['like', 'ip_address', $this->ip_address]); + ->andFilterWhere(['like', 'serial', $this->serial]) + ->andFilterWhere(['like', 'ip_address', $this->ip_address]); return $dataProvider; } + } diff --git a/models/Door.php b/models/Door.php new file mode 100644 index 00000000..2ce47780 --- /dev/null +++ b/models/Door.php @@ -0,0 +1,57 @@ + 100], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() { + return [ + 'id' => 'ID', + 'device_id' => 'Device ID', + 'name' => 'Name', + 'code' => 'Code', + 'created_at' => 'Created At', + 'modified_at' => 'Modified At', + ]; + } + + public function multiCreate($datas) { + $field = ['device_id', 'name', 'code', 'created_at', 'modified_at']; + static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute(); + return; + } + +} diff --git a/models/common.php b/models/common.php index aa65eb0d..6322b56b 100644 --- a/models/common.php +++ b/models/common.php @@ -183,4 +183,14 @@ class common extends \yii\db\ActiveRecord { return $model->create($data); } + public static function requestToCardService($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) + ] + ])); + } + } diff --git a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/left.php b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/left.php index 3842b598..0a9e6c3e 100644 --- a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/left.php +++ b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/left.php @@ -24,7 +24,7 @@ $items = [ ['label' => 'Khu vực', 'url' => ['/area'], 'icon' => 'building'], ['label' => 'Thiết bị', 'url' => ['/device'], 'icon' => 'database'], - ['label' => 'Tìm kiếm thiết bị', 'url' => ['/dashboard'], 'icon' => 'search'] + ['label' => 'Tìm kiếm thiết bị', 'url' => ['/device/search'], 'icon' => 'search'] ]; } ?> diff --git a/views/device/change-ip.tpl b/views/device/change-ip.tpl new file mode 100644 index 00000000..8d734ce6 --- /dev/null +++ b/views/device/change-ip.tpl @@ -0,0 +1,41 @@ + +
+
+
Địa chỉ IP cũ
+ +
+ +
+
+
+
Địa chỉ IP mới
+ +
+ +
+
+
+
Subnet Mask
+ +
+ +
+
+ + +
\ No newline at end of file diff --git a/views/device/device-lists.tpl b/views/device/device-lists.tpl new file mode 100644 index 00000000..efd952cd --- /dev/null +++ b/views/device/device-lists.tpl @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + {foreach from=$datas item=d} + + + + + + + + + + + {/foreach} + +
Địa chỉ MACĐịa chỉ IPSubnet MaskGatewaySerialLoại thiết bịTrạng thái
+ {if !in_array($d.IP,$deviceIpLists)} + + {/if} + {$d.MAC|default:""}{$d.IP|default:""}{$d.NetMask|default:""}{$d.GATEIPAddress|default:""}{$d.SN|default:""}{$d.Device|default:""} + +
\ No newline at end of file diff --git a/views/device/form.tpl b/views/device/form.tpl new file mode 100644 index 00000000..a13215e9 --- /dev/null +++ b/views/device/form.tpl @@ -0,0 +1,56 @@ + +
+
+
Tên thiết bị *
+ +
+ +
+
+
+
Khu vực
+ +
+
+
+
+
Địa chỉ IP *
+ ip_address!=""}readonly=""{/if}> +
+ +
+ + +
+
+ + Kiểm tra kết nối + +
+
+ + +
+
\ No newline at end of file diff --git a/views/device/index.tpl b/views/device/index.tpl index 3ceb7308..45c4ff01 100644 --- a/views/device/index.tpl +++ b/views/device/index.tpl @@ -2,15 +2,12 @@ {use class="yii\helpers\Url"} {use class="yii\grid\GridView"} {use class="yii\widgets\Pjax" type="block"} -{*use class="app\assets\DepartmentAsset"} -{DepartmentAsset::register($this)|void*} +{use class="app\assets\DeviceAsset"} +{DeviceAsset::register($this)|void} {block name='content'}
- -