<% ' Universal save endpoint for all network devices (IDF, Server, Switch, Camera) ' Routes to appropriate table based on device type ' Get device type and ID Dim deviceType, deviceId, isDelete deviceType = Trim(Request.Form("type")) deviceId = Trim(Request.Form("id")) isDelete = Trim(Request.Form("delete")) ' Validate device type If deviceType <> "idf" And deviceType <> "server" And deviceType <> "switch" And deviceType <> "camera" And deviceType <> "accesspoint" Then Response.Write("
Error: Invalid device type
") Response.Write("Back to Network Devices
") objConn.Close Response.End End If ' Validate device ID If deviceId = "" Then deviceId = "0" If Not IsNumeric(deviceId) Then Response.Write("
Error: Invalid device ID
") Response.Write("Back to Network Devices
") objConn.Close Response.End End If ' Map type to table and fields Dim tableName, idField, nameField, redirectUrl Select Case deviceType Case "idf" tableName = "idfs" idField = "idfid" nameField = "idfname" redirectUrl = "network_devices.asp?filter=IDF" Case "server" tableName = "servers" idField = "serverid" nameField = "servername" redirectUrl = "network_devices.asp?filter=Server" Case "switch" tableName = "switches" idField = "switchid" nameField = "switchname" redirectUrl = "network_devices.asp?filter=Switch" Case "camera" tableName = "cameras" idField = "cameraid" nameField = "cameraname" redirectUrl = "network_devices.asp?filter=Camera" Case "accesspoint" tableName = "accesspoints" idField = "apid" nameField = "apname" redirectUrl = "network_devices.asp?filter=Access Point" End Select ' Handle DELETE request If isDelete = "1" Then ' Soft delete - set isactive = 0 Dim strDelete strDelete = "UPDATE " & tableName & " SET isactive = 0 WHERE " & idField & " = " & deviceId objConn.Execute(strDelete) objConn.Close Response.Redirect(redirectUrl) Response.End End If ' Get form data Dim deviceName, description, maptop, mapleft, isactiveForm deviceName = Trim(Request.Form(nameField)) description = Trim(Request.Form("description")) maptop = Trim(Request.Form("maptop")) mapleft = Trim(Request.Form("mapleft")) isactiveForm = Trim(Request.Form("isactive")) ' Handle isactive - checkbox: checked=1, unchecked=empty string ' Default to 0 (inactive) if not provided (unchecked) If isactiveForm = "1" Then isactiveForm = "1" Else isactiveForm = "0" End If ' Validate name field (required for all) If deviceName = "" Then Response.Write("
Error: " & UCase(Left(nameField, 1)) & Mid(nameField, 2) & " is required
") Response.Write("Go back
") objConn.Close Response.End End If ' Validate field lengths If Len(deviceName) > 100 Or Len(description) > 255 Then Response.Write("
Error: Field length exceeded
") Response.Write("Go back
") objConn.Close Response.End End If ' Handle NULL values for optional numeric fields If maptop = "" Or Not IsNumeric(maptop) Then maptop = "NULL" Else maptop = CLng(maptop) If mapleft = "" Or Not IsNumeric(mapleft) Then mapleft = "NULL" Else mapleft = CLng(mapleft) ' Escape single quotes for SQL deviceName = Replace(deviceName, "'", "''") description = Replace(description, "'", "''") ' Build SQL based on device type Dim strSQL If deviceType = "idf" Then ' IDF - no model/vendor If deviceId = "0" Then ' INSERT strSQL = "INSERT INTO idfs (idfname, description, maptop, mapleft, isactive) " & _ "VALUES ('" & deviceName & "', '" & description & "', " & maptop & ", " & mapleft & ", " & isactiveForm & ")" Else ' UPDATE strSQL = "UPDATE idfs SET " & _ "idfname = '" & deviceName & "', " & _ "description = '" & description & "', " & _ "maptop = " & maptop & ", " & _ "mapleft = " & mapleft & ", " & _ "isactive = " & isactiveForm & " " & _ "WHERE idfid = " & deviceId End If ElseIf deviceType = "server" Or deviceType = "switch" Or deviceType = "accesspoint" Then ' Server/Switch/Access Point - has modelid but NO idfid Dim modelid, serialnumber, ipaddress modelid = Trim(Request.Form("modelid")) serialnumber = Trim(Request.Form("serialnumber")) ipaddress = Trim(Request.Form("ipaddress")) ' Handle new model creation If modelid = "new" Then Dim newmodelnumber, newvendorid, newmodelnotes, newmodeldocpath, newvendorname newmodelnumber = Trim(Request.Form("newmodelnumber")) newvendorid = Trim(Request.Form("newvendorid")) newmodelnotes = Trim(Request.Form("newmodelnotes")) newmodeldocpath = Trim(Request.Form("newmodeldocpath")) newvendorname = Trim(Request.Form("newvendorname")) ' Validate required fields for new model If newmodelnumber = "" Then Response.Write("
Error: Model number is required
") Response.Write("Go back
") objConn.Close Response.End End If If newvendorid = "" Then Response.Write("
Error: Vendor is required for new model
") Response.Write("Go back
") objConn.Close Response.End End If ' Handle new vendor creation (nested) If newvendorid = "new" Then If newvendorname = "" Then Response.Write("
Error: Vendor name is required
") Response.Write("Go back
") objConn.Close Response.End End If ' Escape and insert new vendor Dim escapedVendorName, sqlNewVendor escapedVendorName = Replace(newvendorname, "'", "''") sqlNewVendor = "INSERT INTO vendors (vendor, isactive) VALUES ('" & escapedVendorName & "', 1)" On Error Resume Next objConn.Execute sqlNewVendor If Err.Number <> 0 Then Response.Write("
Error creating vendor: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If ' Get newly created vendor ID Dim rsNewVendor Set rsNewVendor = objConn.Execute("SELECT LAST_INSERT_ID() AS newid") newvendorid = rsNewVendor("newid") rsNewVendor.Close Set rsNewVendor = Nothing On Error Goto 0 End If ' Escape and insert new model Dim escapedModelNumber, escapedModelNotes, escapedModelDocPath, sqlNewModel escapedModelNumber = Replace(newmodelnumber, "'", "''") escapedModelNotes = Replace(newmodelnotes, "'", "''") escapedModelDocPath = Replace(newmodeldocpath, "'", "''") sqlNewModel = "INSERT INTO models (modelnumber, vendorid, notes, documentationpath, isactive) " & _ "VALUES ('" & escapedModelNumber & "', " & newvendorid & ", '" & escapedModelNotes & "', '" & escapedModelDocPath & "', 1)" On Error Resume Next objConn.Execute sqlNewModel If Err.Number <> 0 Then Response.Write("
Error creating model: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If ' Get newly created model ID Dim rsNewModel Set rsNewModel = objConn.Execute("SELECT LAST_INSERT_ID() AS newid") modelid = rsNewModel("newid") rsNewModel.Close Set rsNewModel = Nothing On Error Goto 0 ' Handle NULL/empty modelid ElseIf modelid = "" Or Not IsNumeric(modelid) Then modelid = "NULL" Else modelid = CLng(modelid) End If ' Escape strings serialnumber = Replace(serialnumber, "'", "''") ipaddress = Replace(ipaddress, "'", "''") ' Validate lengths If Len(serialnumber) > 100 Or Len(ipaddress) > 45 Then Response.Write("
Error: Field length exceeded
") Response.Write("Go back
") objConn.Close Response.End End If If deviceId = "0" Then ' INSERT strSQL = "INSERT INTO " & tableName & " (" & nameField & ", modelid, serialnumber, ipaddress, description, maptop, mapleft, isactive) " & _ "VALUES ('" & deviceName & "', " & modelid & ", '" & serialnumber & "', '" & ipaddress & "', '" & description & "', " & maptop & ", " & mapleft & ", " & isactiveForm & ")" Else ' UPDATE strSQL = "UPDATE " & tableName & " SET " & _ nameField & " = '" & deviceName & "', " & _ "modelid = " & modelid & ", " & _ "serialnumber = '" & serialnumber & "', " & _ "ipaddress = '" & ipaddress & "', " & _ "description = '" & description & "', " & _ "maptop = " & maptop & ", " & _ "mapleft = " & mapleft & ", " & _ "isactive = " & isactiveForm & " " & _ "WHERE " & idField & " = " & deviceId End If ElseIf deviceType = "camera" Then ' Camera - has modelid, idfid, AND macaddress Dim cameraModelid, cameraIdfid, cameraSerial, cameraMac, cameraIP cameraModelid = Trim(Request.Form("modelid")) cameraIdfid = Trim(Request.Form("idfid")) cameraSerial = Trim(Request.Form("serialnumber")) cameraMac = Trim(Request.Form("macaddress")) cameraIP = Trim(Request.Form("ipaddress")) ' Handle new IDF creation for camera If cameraIdfid = "new" Then Dim cameraNewidfname, cameraNewidfdescription cameraNewidfname = Trim(Request.Form("newidfname")) cameraNewidfdescription = Trim(Request.Form("newidfdescription")) ' Validate required fields for new IDF If cameraNewidfname = "" Then Response.Write("
Error: IDF name is required
") Response.Write("Go back
") objConn.Close Response.End End If ' Escape and insert new IDF Dim cameraEscapedIdfName, cameraEscapedIdfDesc, cameraSqlNewIdf cameraEscapedIdfName = Replace(cameraNewidfname, "'", "''") cameraEscapedIdfDesc = Replace(cameraNewidfdescription, "'", "''") cameraSqlNewIdf = "INSERT INTO idfs (idfname, description, isactive) VALUES ('" & cameraEscapedIdfName & "', '" & cameraEscapedIdfDesc & "', 1)" On Error Resume Next objConn.Execute cameraSqlNewIdf If Err.Number <> 0 Then Response.Write("
Error creating IDF: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If ' Get newly created IDF ID Dim rsNewIdfCamera Set rsNewIdfCamera = objConn.Execute("SELECT LAST_INSERT_ID() AS newid") cameraIdfid = CLng(rsNewIdfCamera("newid")) rsNewIdfCamera.Close Set rsNewIdfCamera = Nothing On Error Goto 0 End If ' Validate required idfid for cameras If cameraIdfid = "" Or Not IsNumeric(cameraIdfid) Or CLng(cameraIdfid) < 1 Then Response.Write("
Error: IDF location is required for cameras
") Response.Write("Go back
") objConn.Close Response.End End If ' Handle new model creation for camera If cameraModelid = "new" Then Dim cameraNewmodelnumber, cameraNewvendorid, cameraNewmodelnotes, cameraNewmodeldocpath, cameraNewvendorname cameraNewmodelnumber = Trim(Request.Form("newmodelnumber")) cameraNewvendorid = Trim(Request.Form("newvendorid")) cameraNewmodelnotes = Trim(Request.Form("newmodelnotes")) cameraNewmodeldocpath = Trim(Request.Form("newmodeldocpath")) cameraNewvendorname = Trim(Request.Form("newvendorname")) ' Validate required fields for new model If cameraNewmodelnumber = "" Then Response.Write("
Error: Model number is required
") Response.Write("Go back
") objConn.Close Response.End End If If cameraNewvendorid = "" Then Response.Write("
Error: Vendor is required for new model
") Response.Write("Go back
") objConn.Close Response.End End If ' Handle new vendor creation (nested) If cameraNewvendorid = "new" Then If cameraNewvendorname = "" Then Response.Write("
Error: Vendor name is required
") Response.Write("Go back
") objConn.Close Response.End End If ' Escape and insert new vendor Dim cameraEscapedVendorName, cameraSqlNewVendor cameraEscapedVendorName = Replace(cameraNewvendorname, "'", "''") cameraSqlNewVendor = "INSERT INTO vendors (vendor, isactive) VALUES ('" & cameraEscapedVendorName & "', 1)" On Error Resume Next objConn.Execute cameraSqlNewVendor If Err.Number <> 0 Then Response.Write("
Error creating vendor: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If ' Get newly created vendor ID Dim rsNewVendorCamera Set rsNewVendorCamera = objConn.Execute("SELECT LAST_INSERT_ID() AS newid") cameraNewvendorid = rsNewVendorCamera("newid") rsNewVendorCamera.Close Set rsNewVendorCamera = Nothing On Error Goto 0 End If ' Escape and insert new model Dim cameraEscapedModelNumber, cameraEscapedModelNotes, cameraEscapedModelDocPath, cameraSqlNewModel cameraEscapedModelNumber = Replace(cameraNewmodelnumber, "'", "''") cameraEscapedModelNotes = Replace(cameraNewmodelnotes, "'", "''") cameraEscapedModelDocPath = Replace(cameraNewmodeldocpath, "'", "''") cameraSqlNewModel = "INSERT INTO models (modelnumber, vendorid, notes, documentationpath, isactive) " & _ "VALUES ('" & cameraEscapedModelNumber & "', " & cameraNewvendorid & ", '" & cameraEscapedModelNotes & "', '" & cameraEscapedModelDocPath & "', 1)" On Error Resume Next objConn.Execute cameraSqlNewModel If Err.Number <> 0 Then Response.Write("
Error creating model: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If ' Get newly created model ID Dim rsNewModelCamera Set rsNewModelCamera = objConn.Execute("SELECT LAST_INSERT_ID() AS newid") cameraModelid = rsNewModelCamera("newid") rsNewModelCamera.Close Set rsNewModelCamera = Nothing On Error Goto 0 ' Handle NULL modelid ElseIf cameraModelid = "" Or Not IsNumeric(cameraModelid) Then cameraModelid = "NULL" Else cameraModelid = CLng(cameraModelid) End If ' Escape strings cameraSerial = Replace(cameraSerial, "'", "''") cameraMac = Replace(cameraMac, "'", "''") cameraIP = Replace(cameraIP, "'", "''") ' Validate lengths If Len(cameraSerial) > 100 Or Len(cameraMac) > 17 Or Len(cameraIP) > 45 Then Response.Write("
Error: Field length exceeded
") Response.Write("Go back
") objConn.Close Response.End End If If deviceId = "0" Then ' INSERT strSQL = "INSERT INTO cameras (cameraname, modelid, idfid, serialnumber, macaddress, ipaddress, description, maptop, mapleft, isactive) " & _ "VALUES ('" & deviceName & "', " & cameraModelid & ", " & cameraIdfid & ", '" & cameraSerial & "', '" & cameraMac & "', '" & cameraIP & "', '" & description & "', " & maptop & ", " & mapleft & ", " & isactiveForm & ")" Else ' UPDATE strSQL = "UPDATE cameras SET " & _ "cameraname = '" & deviceName & "', " & _ "modelid = " & cameraModelid & ", " & _ "idfid = " & cameraIdfid & ", " & _ "serialnumber = '" & cameraSerial & "', " & _ "macaddress = '" & cameraMac & "', " & _ "ipaddress = '" & cameraIP & "', " & _ "description = '" & description & "', " & _ "maptop = " & maptop & ", " & _ "mapleft = " & mapleft & ", " & _ "isactive = " & isactiveForm & " " & _ "WHERE cameraid = " & deviceId End If End If ' Execute SQL On Error Resume Next objConn.Execute(strSQL) If Err.Number <> 0 Then Response.Write("
Error saving device: " & Server.HTMLEncode(Err.Description) & "
") Response.Write("Go back
") objConn.Close Response.End End If On Error Goto 0 ' Success - redirect to list objConn.Close Response.Redirect(redirectUrl) %>