diff --git a/classes/Device.class.php b/classes/Device.class.php index 999d048ec..ad5dc603b 100644 --- a/classes/Device.class.php +++ b/classes/Device.class.php @@ -1528,17 +1528,25 @@ function Search($indexedbyid=false,$loose=false,$filterrights=false){ function LooseSearch($indexedbyid=false,$filterrights=false){ return $this->Search($indexedbyid,true,$filterrights); } - - function SearchDevicebySerialNo(){ +// Search device by SerialNo param bool, $exact if true a single device with exact match otherwise performs a partial search (LIKE) + function SearchDevicebySerialNo($exact = false){ global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_Device WHERE SerialNo LIKE \"%$this->SerialNo%\" ORDER BY Label;"; - + if($exact){ + $sql = "SELECT * FROM fac_Device WHERE SerialNo = :serial LIMIT 1;"; + $stmt = $dbh->prepare($sql); + $stmt->execute([':serial' => $this->SerialNo]); + }else{ + $sql = "SELECT * FROM fac_Device WHERE SerialNo LIKE :serial ORDER BY Label;"; + $stmt = $dbh->prepare($sql); + $stmt->execute([':serial' => $this->SerialNo]); + } + $deviceList=array(); - foreach($dbh->query($sql) as $deviceRow){ + while($deviceRow=$stmt->fetch(PDO::FETCH_ASSOC)){ $deviceList[$deviceRow["DeviceID"]]=Device::RowToObject($deviceRow); } diff --git a/devices.php b/devices.php index 62354147b..06d5c6618 100644 --- a/devices.php +++ b/devices.php @@ -28,6 +28,23 @@ $taginsert=""; // Ajax functions + // checks if an AJAX request is sent to check for the existence of a serial number triggered by the serialexist button + if (isset($_POST['action']) && $_POST['action'] === 'checkserial' && isset($_POST['serial'])) { + + $dev = new Device(); + $dev->SerialNo = $_POST['serial']; + $devList = $dev->SearchDevicebySerialNo(true); + + if (count($devList) > 0) { + $device = reset($devList); + echo '' + .__('Already used') . ' : ' . htmlspecialchars($device->SerialNo) . ''; + } else { + echo __('No duplicate found'); + } + exit; + } + // SNMP Test if(isset($_POST['snmptest'])){ // Parse through the post data and pull in site defaults if necessary @@ -1140,6 +1157,7 @@ function getHash(){ changingHash=false; } } +