update Device full CRUD

This commit is contained in:
dongpd 2020-10-13 13:40:49 +07:00
parent 64e62ca3ce
commit 03e03c7e13
20 changed files with 1170 additions and 77 deletions

View File

@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/ -- https://www.phpmyadmin.net/
-- --
-- Host: 127.0.0.1 -- 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 -- Server version: 10.4.8-MariaDB
-- PHP Version: 7.1.33 -- PHP Version: 7.1.33
@ -148,14 +148,48 @@ CREATE TABLE `device` (
`id` int(11) NOT NULL, `id` int(11) NOT NULL,
`name` varchar(100) NOT NULL, `name` varchar(100) NOT NULL,
`serial` varchar(50) DEFAULT 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, `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, `area_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL DEFAULT 0, `created_at` int(11) NOT NULL DEFAULT 0,
`modified_at` int(11) NOT NULL DEFAULT 0 `modified_at` int(11) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) 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'), (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'), (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'), (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` ALTER TABLE `device`
ADD PRIMARY KEY (`id`); ADD PRIMARY KEY (`id`);
--
-- Indexes for table `door`
--
ALTER TABLE `door`
ADD PRIMARY KEY (`id`);
-- --
-- Indexes for table `staff` -- Indexes for table `staff`
-- --
@ -358,7 +447,13 @@ ALTER TABLE `department`
-- AUTO_INCREMENT for table `device` -- AUTO_INCREMENT for table `device`
-- --
ALTER 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` -- AUTO_INCREMENT for table `staff`
@ -370,7 +465,7 @@ ALTER TABLE `staff`
-- AUTO_INCREMENT for table `system_logs` -- AUTO_INCREMENT for table `system_logs`
-- --
ALTER 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` -- AUTO_INCREMENT for table `user`

23
assets/DeviceAsset.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace app\assets;
use yii\web\AssetBundle;
class DeviceAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/device.js'
];
public $depends = [
'yii\web\YiiAsset',
'app\assets\AppAsset',
// 'yii\jui\JuiAsset',
'yii\bootstrap\BootstrapAsset',
];
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\assets;
use yii\web\AssetBundle;
class DeviceSearchAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/device_search.js'
];
public $depends = [
'yii\web\YiiAsset',
'app\assets\AppAsset',
// 'yii\jui\JuiAsset',
'yii\bootstrap\BootstrapAsset',
];
}

View File

@ -5,9 +5,14 @@ namespace app\controllers;
use Yii; use Yii;
use app\models\Device; use app\models\Device;
use app\models\DeviceSearch; use app\models\DeviceSearch;
use app\models\Area;
use app\models\common;
use app\models\Door;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use yii\helpers\Html;
use yii\helpers\Url;
/** /**
* DeviceController implements the CRUD actions for Device model. * DeviceController implements the CRUD actions for Device model.
@ -42,43 +47,101 @@ class DeviceController extends Controller {
return $this->render('index', [ return $this->render('index', [
'searchModel' => $searchModel, 'searchModel' => $searchModel,
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
]); 'statusArray' => Device::$statusArray,
} 'typeArray' => Device::$typeArray,
'areaArray' => Area::areaArray()
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]); ]);
} }
public function actionCreate() { public function actionCreate() {
$model = new Device(); $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()) { $searchDevice = json_decode(common::requestToCardService("/SearchDevice", ["NetMask" => "255.255.255.255"]), true);
return $this->redirect(['view', 'id' => $model->id]); $device = false;
foreach ($searchDevice as $key => $value) {
if ($value["IP"] === $data["Ip"])
$device = $value;
} }
if (!$device)
return ["status" => false, "type" => "connect"];
return $this->render('create', [ $requestToCardService = json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => $data["Ip"]]), true);
'model' => $model, 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()
])
];
}
} }
public function actionUpdate($id) { public function actionUpdate($id) {
$model = $this->findModel($id); $model = $this->findModel($id);
Yii::$app->response->format = "json";
if ($model->load(Yii::$app->request->post()) && $model->save()) { if (Yii::$app->request->post()) {
return $this->redirect(['view', 'id' => $model->id]); $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', [ public function actionDelete() {
'model' => $model, if (Yii::$app->request->post()) {
]); $lists = Yii::$app->request->post("lists");
foreach ($lists as $key => $value) {
Device::deleteDevice($value);
}
} }
public function actionDelete($id) {
$this->findModel($id)->delete();
return $this->redirect(['index']);
} }
protected function findModel($id) { protected function findModel($id) {
@ -89,4 +152,174 @@ class DeviceController extends Controller {
throw new NotFoundHttpException('The requested page does not exist.'); 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"]];
}
}
} }

27
helpers/DeviceGrid.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace app\helpers;
class DeviceGrid extends CommonGrid {
public static function status($array) {
return function($model) use ($array) {
if ($model->status)
return "<i class='fa fa-check text-green'></i>";
return "<i class='fa fa-remove text-red'></i>";
};
}
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] : "";
};
}
}

View File

@ -11,51 +11,110 @@ use Yii;
* @property string $name * @property string $name
* @property string $serial * @property string $serial
* @property string $ip_address * @property string $ip_address
* @property string $subnet_mask
* @property string $gateway
* @property string $mac_address
* @property int $status * @property int $status
* @property int $type * @property string $type
* @property string $version
* @property int $area_id * @property int $area_id
* @property int $created_at * @property int $created_at
* @property int $modified_at * @property int $modified_at
*/ */
class Device extends \yii\db\ActiveRecord class Device extends \yii\db\ActiveRecord {
{
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function tableName() public static function tableName() {
{
return 'device'; return 'device';
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rules() public function rules() {
{
return [ return [
[['name', 'ip_address', 'status', 'type', 'area_id'], 'required'], [['name', 'ip_address', 'status', 'type', 'area_id'], 'required'],
[['status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'], [['status', 'area_id', 'created_at', 'modified_at'], 'integer'],
[['name'], 'string', 'max' => 100], [['name', 'version'], 'string', 'max' => 100],
[['serial'], 'string', 'max' => 50], [['serial'], 'string', 'max' => 50],
[['ip_address'], 'string', 'max' => 20], [['ip_address', 'subnet_mask', 'gateway', 'mac_address', 'type'], 'string', 'max' => 20],
]; ];
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function attributeLabels() public function attributeLabels() {
{
return [ return [
'id' => 'ID', 'id' => 'ID',
'name' => 'Name', 'name' => 'Tên thiết bị',
'serial' => 'Serial', 'serial' => 'Serial',
'ip_address' => 'Ip Address', 'ip_address' => 'Địa chỉ IP',
'status' => 'Status', 'subnet_mask' => 'Subnet mask',
'type' => 'Type', 'gateway' => 'Gateway',
'area_id' => 'Area ID', 'mac_address' => 'Mac Address',
'created_at' => 'Created At', 'status' => 'Trạng thái',
'modified_at' => 'Modified At', '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;
}
} }

View File

@ -10,15 +10,14 @@ use app\models\Device;
/** /**
* DeviceSearch represents the model behind the search form of `app\models\Device`. * DeviceSearch represents the model behind the search form of `app\models\Device`.
*/ */
class DeviceSearch extends Device class DeviceSearch extends Device {
{
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rules() public function rules() {
{
return [ return [
[['id', 'status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'], [['id', 'status', 'type', 'area_id'], 'integer'],
[['name', 'serial', 'ip_address'], 'safe'], [['name', 'serial', 'ip_address'], 'safe'],
]; ];
} }
@ -26,8 +25,7 @@ class DeviceSearch extends Device
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function scenarios() public function scenarios() {
{
// bypass scenarios() implementation in the parent class // bypass scenarios() implementation in the parent class
return Model::scenarios(); return Model::scenarios();
} }
@ -39,8 +37,7 @@ class DeviceSearch extends Device
* *
* @return ActiveDataProvider * @return ActiveDataProvider
*/ */
public function search($params) public function search($params) {
{
$query = Device::find(); $query = Device::find();
// add conditions that should always apply here // add conditions that should always apply here
@ -73,4 +70,5 @@ class DeviceSearch extends Device
return $dataProvider; return $dataProvider;
} }
} }

57
models/Door.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "door".
*
* @property int $id
* @property int $device_id
* @property string $name
* @property int $code
* @property int $created_at
* @property int $modified_at
*/
class Door extends \yii\db\ActiveRecord {
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'door';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['device_id', 'name', 'code'], 'required'],
[['device_id', 'code', 'created_at', 'modified_at'], 'integer'],
[['name'], 'string', 'max' => 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;
}
}

View File

@ -183,4 +183,14 @@ class common extends \yii\db\ActiveRecord {
return $model->create($data); 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)
]
]));
}
} }

View File

@ -24,7 +24,7 @@
$items = [ $items = [
['label' => 'Khu vực', 'url' => ['/area'], 'icon' => 'building'], ['label' => 'Khu vực', 'url' => ['/area'], 'icon' => 'building'],
['label' => 'Thiết bị', 'url' => ['/device'], 'icon' => 'database'], ['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']
]; ];
} }
?> ?>

View File

@ -0,0 +1,41 @@
<style>
.input-group-addon{
width: 140px;
text-align: left;
}
.input-group{
width: 100%;
}
.select2{
width: 100% !important;
}
</style>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Địa chỉ IP cũ</div>
<input type="text" class="form-control" value="{$model->ip_address|default:""}" name="IpOld" readonly="">
</div>
<span class="help-block hidden"></span>
</div>
<div class="form-group" id="ip">
<div class="input-group">
<div class="input-group-addon">Địa chỉ IP mới</div>
<input type="text" class="form-control" value="{$model->ip_address|default:""}" name="IpNew">
</div>
<span class="help-block hidden"></span>
</div>
<div class="form-group" id="subnet_mask">
<div class="input-group">
<div class="input-group-addon">Subnet Mask</div>
<input type="text" class="form-control" value="{$model->subnet_mask|default:""}" name="SubnetMask">
</div>
<span class="help-block hidden"></span>
</div>
<div class="text-right">
<button class="btn btn-primary" onclick="changeIP(this);" data-href="{$url}">
<i class="fa fa-floppy-o"></i> Lưu
</button>
<button class="btn btn-default" data-dismiss="modal">
<span class="fa fa-remove"></span> Hủy
</button>
</div>

View File

@ -0,0 +1,36 @@
<table class="table table-bordered table-hover table-striped" style="background:#fff;">
<thead>
<tr>
<th style="width: 5%;" class="text-center"><input type='checkbox' value='0' class='checkbox-device' id='checkall-device'></th>
<th>Địa chỉ MAC</th>
<th>Địa chỉ IP</th>
<th>Subnet Mask</th>
<th>Gateway</th>
<th>Serial</th>
<th>Loại thiết bị</th>
<th>Trạng thái</th>
</tr>
</thead>
<tbody>
{foreach from=$datas item=d}
<tr>
<td class="text-center" id="checkbox-{$d.SN}">
{if !in_array($d.IP,$deviceIpLists)}
<input type='checkbox' value='{json_encode($d)}' name='checkbox-device' class='checkbox-device'>
{/if}
</td>
<td>{$d.MAC|default:""}</td>
<td>{$d.IP|default:""}</td>
<td>{$d.NetMask|default:""}</td>
<td>{$d.GATEIPAddress|default:""}</td>
<td>{$d.SN|default:""}</td>
<td>{$d.Device|default:""}</td>
<td class="text-center" id="status-{$d.SN}">
<label class="text-green {if !in_array($d.IP,$deviceIpLists)}hidden{/if}">
<i class="fa fa-check"></i> Đã thêm
</label>
</td>
</tr>
{/foreach}
</tbody>
</table>

56
views/device/form.tpl Normal file
View File

@ -0,0 +1,56 @@
<style>
.input-group-addon{
width: 140px;
text-align: left;
}
.input-group{
width: 100%;
}
.select2{
width: 100% !important;
}
</style>
<div class="form-group" id="name">
<div class="input-group">
<div class="input-group-addon">Tên thiết bị <i class="text-red">*</i></div>
<input type="text" class="form-control" value="{$model->name|default:""}" name="Name">
</div>
<span class="help-block hidden"></span>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Khu vực</div>
<select class="form-control" name="Area" id="Area">
{html_options options=$areaArray selected=$model->area_id|default:1}
</select>
</div>
</div>
<div class="form-group" id="ip">
<div class="input-group">
<div class="input-group-addon">Địa chỉ IP <i class="text-red">*</i></div>
<input type="text" class="form-control" value="{$model->ip_address|default:""}" name="Ip" {if $model->ip_address!=""}readonly=""{/if}>
</div>
<span class="help-block hidden"></span>
</div>
<div class="alert alert-success hidden" id='success'>
<i class="fa fa-check"></i> Kết nối thành công!
</div>
<div class="alert alert-danger hidden" id='error'>
<i class="fa fa-remove"></i> Kết nối thất bại!
</div>
<div class="row">
<div class="col-md-6">
<a href="{yii\helpers\Url::to(['check-connection'])}" onclick="checkConnection(this);
return false;" class="text-bold">
<u>Kiểm tra kết nối</u>
</a>
</div>
<div class="col-md-6 text-right">
<button class="btn btn-primary" onclick="save(this);" data-href="{$url}">
<i class="fa fa-floppy-o"></i> Lưu
</button>
<button class="btn btn-default" data-dismiss="modal">
<span class="fa fa-remove"></span> Hủy
</button>
</div>
</div>

View File

@ -2,15 +2,12 @@
{use class="yii\helpers\Url"} {use class="yii\helpers\Url"}
{use class="yii\grid\GridView"} {use class="yii\grid\GridView"}
{use class="yii\widgets\Pjax" type="block"} {use class="yii\widgets\Pjax" type="block"}
{*use class="app\assets\DepartmentAsset"} {use class="app\assets\DeviceAsset"}
{DepartmentAsset::register($this)|void*} {DeviceAsset::register($this)|void}
{block name='content'} {block name='content'}
<div class="device-index"> <div class="device-index">
<div class="" style="font-size: 15px;"> <div class="" style="font-size: 15px;">
<label class="action-button" onclick="common.form(this, '');" data-href="{Url::to(['tree'])}"> <label class="action-button" onclick="common.form(this, 'device');" data-href="{Url::to(['create'])}">
<i class="fa fa-sitemap fa-1-5x"></i> Cây thư mục
</label>
<label class="action-button" onclick="common.form(this, 'department');" data-href="{Url::to(['create'])}">
<i class="fa fa-plus-square fa-1-5x"></i> Thêm <i class="fa fa-plus-square fa-1-5x"></i> Thêm
</label> </label>
<label class="action-button" onclick="_form(this);" data-href="{Url::to(['update'])}"> <label class="action-button" onclick="_form(this);" data-href="{Url::to(['update'])}">
@ -19,8 +16,8 @@
<label class="action-button" onclick="_delete(this);" data-href="{Url::to(['delete'])}"> <label class="action-button" onclick="_delete(this);" data-href="{Url::to(['delete'])}">
<i class="fa fa-trash fa-1-5x"></i> Xóa <i class="fa fa-trash fa-1-5x"></i> Xóa
</label> </label>
<label class="action-button" onclick="$('#file').trigger('click');"> <label class="action-button" onclick="_form(this);" data-href="{Url::to(['change-ip'])}">
<i class="fa fa-upload fa-1-5x"></i> Nhập <i class="fa fa-pencil fa-1-5x"></i> Thay đổi địa chỉ IP
</label> </label>
<label class="action-button" onclick="_export(this);" data-href="{Url::to(['export'])}"> <label class="action-button" onclick="_export(this);" data-href="{Url::to(['export'])}">
<i class="fa fa-download fa-1-5x"></i> Xuất <i class="fa fa-download fa-1-5x"></i> Xuất
@ -50,18 +47,36 @@
'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'] 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%']
], ],
[ [
'header' => "<input type='checkbox' value='0' class='checkbox-department' id='checkall-department'>", 'header' => "<input type='checkbox' value='0' class='checkbox-device' id='checkall-device'>",
'format' => 'raw', 'format' => 'raw',
'contentOptions' => ['class' => 'text-center'], 'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'],
'value' => \app\helpers\CommonGrid::checkbox("device") 'value' => \app\helpers\CommonGrid::checkbox("device", false)
], ],
'name', 'name',
'serial', 'serial',
'ip_address', 'ip_address',
'status', [
'type', 'attribute' => 'status',
'area_id', 'format' => 'raw',
'filter' => $statusArray,
'contentOptions' => ['class' => 'text-center'],
'value' => \app\helpers\DeviceGrid::status($statusArray)
],
[
'attribute' => 'type',
'format' => 'raw',
'filter' => $typeArray,
'contentOptions' => ['class' => 'text-center'],
'value' => \app\helpers\DeviceGrid::type($typeArray)
],
[
'attribute' => 'area_id',
'format' => 'raw',
'filter' => $areaArray,
'contentOptions' => ['class' => 'text-center'],
'value' => \app\helpers\DeviceGrid::area($areaArray)
],
[ [
'attribute' => 'created_at', 'attribute' => 'created_at',
'value' => \app\helpers\CommonGrid::createdAt() 'value' => \app\helpers\CommonGrid::createdAt()

13
views/device/process.tpl Normal file
View File

@ -0,0 +1,13 @@
<div class="well" style="height: 300px;overflow-y: scroll;" id="logs-response">
</div>
<div class="progress">
<div class="progress-bar progress-bar-primary progress-bar-striped" id='progress' role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
0%
</div>
</div>
<div class="text-right">
<button class="btn btn-default" data-dismiss="modal" disabled="" id="close-modal">
<span class="fa fa-remove"></span> Đóng
</button>
</div>

47
views/device/search.tpl Normal file
View File

@ -0,0 +1,47 @@
{extends file=$smarty.current_dir|cat:'/../extends.tpl'}
{use class="yii\helpers\Url"}
{use class="yii\grid\GridView"}
{use class="app\assets\DeviceSearchAsset"}
{DeviceSearchAsset::register($this)|void}
{block name='content'}
<div class="device-index">
<input type='hidden' name='import_url' value="{Url::to(['import'])}">
<div class="" style="font-size: 15px;">
<label class="action-button" onclick="_search(this);" data-href='{Url::to(['search'])}'>
<i class="fa fa-search fa-1-5x"></i> Tìm kiếm theo TCP/IP
</label>
<label class="action-button-disabled" id='add-button-disabled'>
<i class="fa fa-plus fa-1-5x"></i> Thêm thiết bị
</label>
<label class="action-button hidden" id='add-button-enabled' onclick="_process(this);" data-href='{Url::to(['process'])}'>
<i class="fa fa-plus fa-1-5x"></i> Thêm thiết bị
</label>
</div>
<div id="table-device">
<table class="table table-bordered table-hover table-striped" style="background:#fff;">
<thead>
<tr>
<th></th>
<th>Địa chỉ MAC</th>
<th>Địa chỉ IP</th>
<th>Subnet Mask</th>
<th>Gateway</th>
<th>Serial</th>
<th>Loại thiết bị</th>
<th>Trạng thái</th>
</tr>
</thead>
</table>
</div>
<div class="row" style="padding: 0 10px;">
<div class="col-md-6">
<input type='checkbox' value='0' class='checkbox-use-subnetmask' checked="">
Sử dụng Subnet Mask
<input type='text' value="255.255.255.255" name='SubnetMask'>
</div>
<div class="col-md-6 text-right">
<i class="fa fa-info-circle"></i> Tìm thấy <b id='totals'>0</b> thiết bị
</div>
</div>
</div>
{/block}

View File

@ -252,6 +252,12 @@ table > tbody > tr.success >td>a {
background: #b2d7ec; background: #b2d7ec;
border: 1px solid #3c8dbc; border: 1px solid #3c8dbc;
} }
.action-button-disabled{
padding: 10px;
margin: 0;
font-weight: inherit;
border: 1px solid #ecf0f5;
}
.modal-header{ .modal-header{
padding: 10px; padding: 10px;
background: #b2d7ec; background: #b2d7ec;
@ -259,3 +265,6 @@ table > tbody > tr.success >td>a {
.fa-1-5x { .fa-1-5x {
font-size: 1.3em !important; font-size: 1.3em !important;
} }
.alert{
padding: 5px;
}

View File

@ -242,6 +242,9 @@ common.form = function (e, obj, bigSize) {
if (obj === 'card-register') { if (obj === 'card-register') {
$('#Staff').select2(); $('#Staff').select2();
} }
if (obj === 'device') {
$('#Area').select2();
}
}, },
error: function (jqXHR, textStatus, errorThrown) { error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false); common.modalBlock(false);
@ -287,7 +290,12 @@ common.validateEmail = function (Email) {
return false; return false;
} }
}; };
common.validateIp = function (Ip) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(Ip)) {
return true;
}
return false;
}
/** /**
* ========================= * =========================
*/ */

246
web/js/device.js Normal file
View File

@ -0,0 +1,246 @@
$(function () {
common.checkboxInit("device");
});
function validate() {
var error = 0;
var Name = $("input[name='Name']").val();
if (Name === "") {
common.error("name", "Tên thiết bị không được để trống");
error++;
} else {
common.success("name");
}
var Ip = $("input[name='Ip']").val();
if (Ip === "") {
common.error("ip", "Địa chỉ IP không được để trống");
error++;
} else if (!common.validateIp(Ip)) {
common.error("ip", "Định dạng địa chỉ IP không đúng");
error++;
} else {
common.success("ip");
}
return error == 0 ? true : false;
}
function checkConnection(e) {
var Ip = $("input[name='Ip']").val();
if (Ip === "") {
common.error("ip", "Địa chỉ IP không được để trống");
} else if (!common.validateIp(Ip)) {
common.error("ip", "Định dạng địa chỉ IP không đúng");
} else {
common.success("ip");
common.modalBlock(true);
$.ajax({
url: $(e).attr('href'),
type: 'POST',
data: {
Ip: $("input[name='Ip']").val()
},
success: function (data) {
common.modalBlock(false);
$(".alert").addClass("hidden");
if (data.Status === "SUCCESS") {
$("#success").removeClass("hidden");
} else {
$("#error").removeClass("hidden");
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
}
});
}
}
function save(e) {
if (validate()) {
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
data: {
Name: $("input[name='Name']").val(),
Area: $("select[name='Area']").val(),
Ip: $("input[name='Ip']").val()
},
success: function (data) {
common.modalBlock(false);
if (data.status) {
notification.success("Đã lưu thông tin", 1000);
$.pjax.reload({container: '#device-list'});
$("#device-list").on('pjax:success', function () {
common.checkboxInit("device");
});
$("#myModal").modal("hide");
} else {
if (data.type === "ip") {
common.error("ip", "Địa chỉ ip đã tồn tại");
} else if (data.type === "connect") {
$(".alert").addClass("hidden");
$("#error").removeClass("hidden");
} else {
notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000);
}
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
}
});
}
}
function _form(e) {
var lists = [];
$.each($("input[name='checkbox-device']:checked"), function () {
lists.push($(this).val());
});
if (lists.length == 0) {
alert("Vui lòng lựa chọn đối tượng để thay đổi!");
return;
}
if (lists.length > 1) {
alert("Tác vụ này không thể lựa chọn nhiều hơn một đối tượng!");
return;
}
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href') + "?id=" + lists[0],
type: 'POST',
success: function (data) {
common.modalBlock(false);
common.modalOpen(data.form, false, data.title);
$('#Area').select2();
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
common.ajaxError();
}
});
}
function _delete(e) {
var lists = [];
$.each($("input[name='checkbox-device']:checked"), function () {
lists.push($(this).val());
});
if (lists.length == 0) {
alert("Vui lòng lựa chọn đối tượng để xóa!");
return;
}
if (lists.length == 1 && lists[0] === "1") {
alert("Danh mục gốc không thể xóa!");
return;
}
if (confirm("Bạn có chắc chắn muốn xóa các đối tượng đã lựa chọn không?")) {
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
data: {
lists: lists
},
success: function (data) {
common.modalBlock(false);
notification.danger("Đã xóa dữ liệu", 1000);
$.pjax.reload({container: '#device-list'});
$("#device-list").on('pjax:success', function () {
common.checkboxInit("device");
});
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
common.ajaxError();
}
});
}
}
function _export(e) {
window.location = $(e).attr("data-href");
}
function _logs(e) {
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
success: function (data) {
common.modalBlock(false);
common.modalOpen(data.form, true, data.title);
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
common.ajaxError();
}
});
}
function validateChangeIP() {
var error = 0;
var Ip = $("input[name='IpNew']").val();
if (Ip === "") {
common.error("ip", "Địa chỉ IP mới không được để trống");
error++;
} else if (!common.validateIp(Ip)) {
common.error("ip", "Định dạng địa chỉ IP không đúng");
error++;
} else {
common.success("ip");
}
var SubnetMask = $("input[name='SubnetMask']").val();
if (SubnetMask === "") {
common.error("subnet_mask", "Subnet mask không được để trống");
error++;
} else if (!common.validateIp(SubnetMask)) {
common.error("subnet_mask", "Định dạng Subnet mask không đúng");
error++;
} else {
common.success("subnet_mask");
}
return error == 0 ? true : false;
}
function changeIP(e) {
if (validateChangeIP()) {
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
data: {
Ip: $("input[name='IpNew']").val(),
SubnetMask: $("input[name='SubnetMask']").val()
},
success: function (data) {
common.modalBlock(false);
console.log(data);
return;
if (data.status) {
notification.success("Đã lưu thông tin", 1000);
$.pjax.reload({container: '#device-list'});
$("#device-list").on('pjax:success', function () {
common.checkboxInit("device");
});
$("#myModal").modal("hide");
} else {
if (data.type === "ip") {
common.error("ip", "Địa chỉ ip đã tồn tại");
} else {
notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000);
}
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
}
});
}
}

97
web/js/device_search.js Normal file
View File

@ -0,0 +1,97 @@
$(function () {
$('.checkbox-use-subnetmask').iCheck({
checkboxClass: 'icheckbox_flat-red'
});
});
var progress = 0;
var totals = 0;
function _search(e) {
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
data: {
SubnetMask: $("input[name='SubnetMask']").val()
},
success: function (data) {
common.modalBlock(false);
$("#table-device").html(data.form);
$("#totals").html(data.totals);
$("#add-button-enabled").removeClass("hidden");
$("#add-button-disabled").addClass("hidden");
common.checkboxInit("device");
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
}
});
}
function _process(e) {
var lists = [];
$.each($("input[name='checkbox-device']:checked"), function () {
lists.push($(this).val());
});
if (lists.length == 0) {
alert("Vui lòng lựa chọn đối tượng!");
return;
}
common.modalBlock(true);
$.ajax({
url: $(e).attr('data-href'),
type: 'POST',
data: {
lists: lists
},
success: function (data) {
common.modalBlock(false);
common.modalOpen(data.form, false, data.title);
$("#close-modal").attr("disabled", true);
$("#modalHeader").find("button").remove();
totals = data.lists.length;
for (var i = 0; i < totals; i++) {
_import(data.lists[i]);
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
common.ajaxError();
}
});
}
function _import(data) {
$.ajax({
url: $("input[name='import_url']").val(),
type: 'POST',
data: {
data: data
},
success: function (data) {
var html = "";
if (data.status) {
html = "<span class='text-green'><i class='fa fa-check'></i> Thêm thiết bị <b>" + data.IP + "</b> thành công.</span><br>";
} else {
html = "<span class='text-red'><i class='fa fa-remove'></i> Thêm thiết bị <b>" + data.IP + "</b> thất bại.</span><br>";
}
$("#logs-response").append(html);
$("#checkbox-" + data.SN).html("");
$("#status-" + data.SN).find("label").removeClass("hidden");
progress++;
var percent = parseInt(progress / totals * 100);
$("#progress").attr("aria-valuenow", percent);
$("#progress").attr("style", "width: " + percent + "%");
$("#progress").html(percent + "%");
if (percent >= 100) {
progress = 0;
$("#close-modal").attr("disabled", false);
}
},
error: function (jqXHR, textStatus, errorThrown) {
common.ajaxError();
}
});
}