From c271dbf3c26416bdba64ffac86c37b979a4b821a Mon Sep 17 00:00:00 2001 From: steve-lad <72376554+steve-lad@users.noreply.github.com> Date: Sun, 20 Jun 2021 16:25:44 +0200 Subject: [PATCH] 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 --- sccpManTraits/ajaxHelper.php | 1 - sccpManTraits/helperFunctions.php | 22 ++++++++++++++++++++++ views/form.adddevice.php | 28 ++++++++++++++++++++++------ views/formShow.php | 1 - 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/sccpManTraits/ajaxHelper.php b/sccpManTraits/ajaxHelper.php index 8864f36..1dfcbf6 100644 --- a/sccpManTraits/ajaxHelper.php +++ b/sccpManTraits/ajaxHelper.php @@ -382,7 +382,6 @@ trait ajaxHelper { return $result; break; case 'validateMac': - dbug('Request', $_REQUEST); break; } diff --git a/sccpManTraits/helperFunctions.php b/sccpManTraits/helperFunctions.php index 7b25332..2aeb76e 100644 --- a/sccpManTraits/helperFunctions.php +++ b/sccpManTraits/helperFunctions.php @@ -88,6 +88,28 @@ trait helperfunctions { 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') { $result = null; if (empty($dir) || (!file_exists($dir))) { diff --git a/views/form.adddevice.php b/views/form.adddevice.php index 2244b7c..369e381 100644 --- a/views/form.adddevice.php +++ b/views/form.adddevice.php @@ -36,11 +36,11 @@ if (!empty($_REQUEST['new_id'])) { $def_val['addon'] = array("keyword" => 'type', "data" => $_REQUEST['addon'], "seq" => "99"); } } -if (empty($_REQUEST['id'])) { - // Adding new device to database. Get default values - $def_val = $this->getTableDefaults('sccpdevice'); -} else { - // Editing an existing Device + +//Get default values. Will use these dor a new device, and modify for an existing. +$def_val = $this->getTableDefaults('sccpdevice'); +if (!empty($_REQUEST['id'])) { + // Editing an existing Device. Overwrite any defaults that are already set for this device. $dev_id = $_REQUEST['id']; $dev_new = $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']); } } + $def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99"); break; case 'name': - $key = 'mac'; + $key = 'mac'; //This is the key that formShow expects $val = str_replace(array('SEP','ATA','VG'), '', $val); $val = implode('.', sscanf($val, '%4s%4s%4s')); // Convert to Cisco display Format + $def_val[$key] = array("keyword" => $key, "data" => $val, "seq" => "99"); break; case '_hwlang': $tmpar = explode(":", $val); @@ -79,7 +81,21 @@ if (empty($_REQUEST['id'])) { // $val = after('/', $val); // break; 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"); + break; } } } diff --git a/views/formShow.php b/views/formShow.php index cb6d45d..ebe9647 100644 --- a/views/formShow.php +++ b/views/formShow.php @@ -124,7 +124,6 @@ foreach ($items as $child) { if (empty($res_id)) { $res_id = $res_name; } - if (!empty($fvalues[$res_n])) { if (!empty($fvalues[$res_n]['data'])) { $value->value = $fvalues[$res_n]['data'];