Update edit device to apply current schema

Add defaults if they are unset for the device
If device fields are now an enum, make sure that legacy values are valid or replace with defaults
This commit is contained in:
steve-lad 2021-06-20 16:25:44 +02:00
parent 649b717ce7
commit c271dbf3c2
4 changed files with 44 additions and 8 deletions

View file

@ -382,7 +382,6 @@ trait ajaxHelper {
return $result; return $result;
break; break;
case 'validateMac': case 'validateMac':
dbug('Request', $_REQUEST);
break; break;
} }

View file

@ -88,6 +88,28 @@ trait helperfunctions {
return $def_val; return $def_val;
} }
private function getTableEnums($table, $trim_underscore = true) {
$enumFields = array();
$sccpTableDesc = $this->dbinterface->HWextension_db_SccpTableData("get_columns_{$table}");
foreach ($sccpTableDesc as $data) {
$key = (string) $data['Field'];
// function has 2 roles: return actual table keys (trim_underscore = false)
// return sanitised keys to add defaults (trim_underscore = true)
if ($trim_underscore) {
// Remove any leading (or trailing but should be none) underscore
// These are only used to hide fields from chan-sccp for compatibility
$key = trim($key,'_');
}
$typeArray = explode('(', $data['Type']);
if ($typeArray[0] == 'enum') {
$enumOptions = explode(',', trim($typeArray[1],')'));
$enumFields[$key] = $enumOptions;
}
}
return $enumFields;
}
private function findAllFiles($dir, $file_mask = null, $mode = 'full') { private function findAllFiles($dir, $file_mask = null, $mode = 'full') {
$result = null; $result = null;
if (empty($dir) || (!file_exists($dir))) { if (empty($dir) || (!file_exists($dir))) {

View file

@ -36,11 +36,11 @@ if (!empty($_REQUEST['new_id'])) {
$def_val['addon'] = array("keyword" => 'type', "data" => $_REQUEST['addon'], "seq" => "99"); $def_val['addon'] = array("keyword" => 'type', "data" => $_REQUEST['addon'], "seq" => "99");
} }
} }
if (empty($_REQUEST['id'])) {
// Adding new device to database. Get default values //Get default values. Will use these dor a new device, and modify for an existing.
$def_val = $this->getTableDefaults('sccpdevice'); $def_val = $this->getTableDefaults('sccpdevice');
} else { if (!empty($_REQUEST['id'])) {
// Editing an existing Device // Editing an existing Device. Overwrite any defaults that are already set for this device.
$dev_id = $_REQUEST['id']; $dev_id = $_REQUEST['id'];
$dev_new = $dev_id; $dev_new = $dev_id;
$db_res = $this->dbinterface->HWextension_db_SccpTableData('get_sccpdevice_byid', array("id" => $dev_id)); $db_res = $this->dbinterface->HWextension_db_SccpTableData('get_sccpdevice_byid', array("id" => $dev_id));
@ -61,11 +61,13 @@ if (empty($_REQUEST['id'])) {
$device_warning['Template'] = array('Missing device configuration template : '. $tmp_raw['nametemplate']); $device_warning['Template'] = array('Missing device configuration template : '. $tmp_raw['nametemplate']);
} }
} }
$def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99");
break; break;
case 'name': case 'name':
$key = 'mac'; $key = 'mac'; //This is the key that formShow expects
$val = str_replace(array('SEP','ATA','VG'), '', $val); $val = str_replace(array('SEP','ATA','VG'), '', $val);
$val = implode('.', sscanf($val, '%4s%4s%4s')); // Convert to Cisco display Format $val = implode('.', sscanf($val, '%4s%4s%4s')); // Convert to Cisco display Format
$def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99");
break; break;
case '_hwlang': case '_hwlang':
$tmpar = explode(":", $val); $tmpar = explode(":", $val);
@ -79,7 +81,21 @@ if (empty($_REQUEST['id'])) {
// $val = after('/', $val); // $val = after('/', $val);
// break; // break;
default: default:
// Overwrite existing defaults after checking that data is still valid after schema updates
// Do not strip underscores as these fields are new in the schema and so should be valid.
$enumFields = $this->getTableEnums('sccpdevice', false);
if (array_key_exists($key, $enumFields)){
// This field is (now) an enum. Check the current value is acceptable.
// Quote value as enum values are quoted.
if (in_array("'{$val}'", $enumFields[$key])) {
// The value is valid so will keep
$def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99");
}
// Do not store value and let defaults apply
continue;
}
$def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99"); $def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99");
break;
} }
} }
} }

View file

@ -124,7 +124,6 @@ foreach ($items as $child) {
if (empty($res_id)) { if (empty($res_id)) {
$res_id = $res_name; $res_id = $res_name;
} }
if (!empty($fvalues[$res_n])) { if (!empty($fvalues[$res_n])) {
if (!empty($fvalues[$res_n]['data'])) { if (!empty($fvalues[$res_n]['data'])) {
$value->value = $fvalues[$res_n]['data']; $value->value = $fvalues[$res_n]['data'];