From 1f73ade1968cea727757cff0aff1fed860b99dcc Mon Sep 17 00:00:00 2001 From: stevenA Date: Tue, 4 Jan 2022 14:41:43 +0100 Subject: [PATCH] Fix Issue #54 Do not try to write SEP file if no line defined. Save settings and warn user via toast Fix feedback to ajax by separately submitting data before return Fix freepbx overwriting data and causing fail, by separating json into two blocks and only parsing the first Fix return to tab on save (set location hash) when tabs changed in settings Fix return to calling page on save when device updated --- Sccp_manager.class.php | 5 +++++ assets/js/sccp_manager.js | 36 ++++++++++++++++--------------- module.xml | 2 +- sccpManTraits/ajaxHelper.php | 42 +++++++++++++++++++++++++++--------- sccpManagerUpdater.json | 4 ++-- views/form.adddevice.php | 1 + 6 files changed, 60 insertions(+), 30 deletions(-) diff --git a/Sccp_manager.class.php b/Sccp_manager.class.php index 31317b6..b732f17 100644 --- a/Sccp_manager.class.php +++ b/Sccp_manager.class.php @@ -870,6 +870,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO { $sccp_native = false; $tmp_bind = $this->sipconfigs->getSipConfig(); $dev_ext_config = $this->dbinterface->getSccpDeviceTableData("SccpDevice", array('name' => $dev_id, 'fields' => 'sip_ext')); + if (empty($dev_ext_config)){ + // TODO: Placeholder. Have no associated sip line so cannot generate SEP Xml for SIP. + // Need to return and inform user + return false; + } $data_value = array_merge($data_value, $dev_ext_config); $data_tmp = explode(';', $dev_ext_config['sip_lines']); $data_value['sbind'] = array(); diff --git a/assets/js/sccp_manager.js b/assets/js/sccp_manager.js index 97c4046..b9a462e 100644 --- a/assets/js/sccp_manager.js +++ b/assets/js/sccp_manager.js @@ -54,7 +54,7 @@ $(document).ready(function () { if (data.message) { bs_alert(data.message,data.status); } else { - fpbxToast(_('Data saved'),_('Data saved'), 'success'); + fpbxToast(_('Data saved'),'', 'success'); } } else { bs_alert(data.message,data.status); @@ -86,27 +86,27 @@ $(document).ready(function () { url: 'ajax.php?module=sccp_manager&command=' + snd_command, data: vdata, success: function (data) { + // FreePbx handles first and returns its own values removing data sent + // so ajaxHandler echos data before return, and appends the ";#;" separator + // FreePbx own data is the second json after the separator + // without this this function fails as have 2 json fields. + data = JSON.parse(data.split(';#;')[0]); if (data.status === true) { if (data.table_reload === true) { $('table').bootstrapTable('refresh'); } - if (data.hash != null) { - location.hash = data.hash; - } - if (data.href != null) { - location.href = data.href; - } - if (data.path != null) { - location.path = data.path; - } - if (data.search != null) { - location.search = data.search; - } + var newLocation = location.href; + newLocation = ('path' in data && data.path !== '') ? data.path : location.pathname; + newLocation += ('search' in data && data.search !== '') ? `${data.search}` : `${location.search}`; + // location.hash is set by (".change-tab") at line 198 for settings + newLocation += ('hash' in data && data.hash !== '' ) ? data.hash : location.hash; if (data.message) { - fpbxToast(_('Configuration saved. Reloading Module'),_('Configuration saved'), 'success'); + fpbxToast(_(data.message),'', data.toastFlag); + // If posting warning, allow time to read + var toastDelay = (data.toastFlag == 'success') ? 500 : 1500; if (data.reload === true) { //Need setTimout or reload will kill Toast - setTimeout(function(){location.reload();},500); + setTimeout(function(){location.replace(newLocation);},toastDelay); } } } else { @@ -128,7 +128,6 @@ $(document).ready(function () { del_dynamic_table($(this), $(this).data('for')); }); - $(".table").on('click', '.btn-item-delete', function (e) { var dev_cmd = ''; var dev_id = $(this).data('id'); @@ -194,7 +193,10 @@ $(document).ready(function () { }); // --------------------------------------- - +// Set location.hash when changing tabs so that can return to same tab after reload. + $(".change-tab").click(function(){ + window.location.hash = '#' + $(this).attr('data-name'); + }); $('.btnMultiselect').click(function (e) { var kid = $(this).data('id'); diff --git a/module.xml b/module.xml index 0be2d2e..27212c8 100644 --- a/module.xml +++ b/module.xml @@ -1,7 +1,7 @@ sccp_manager SCCP Manager - 14.3.0.25 + 14.3.0.26 setup SCCP Connectivity Steve Lad, Alex GP diff --git a/sccpManTraits/ajaxHelper.php b/sccpManTraits/ajaxHelper.php index 8d18d7d..22bdf06 100644 --- a/sccpManTraits/ajaxHelper.php +++ b/sccpManTraits/ajaxHelper.php @@ -517,10 +517,18 @@ trait ajaxHelper { } // rewrite sccp.conf $this->createDefaultSccpConfig($this->sccpvalues, $this->sccppath["asterisk"]); - $save_settings[] = array('status' => true, ); $this->createDefaultSccpXml(); - return $save_settings; + $toastFlag = 'success'; + $msg = 'Data saved'; + $search = '?display=sccpsettings'; + $hash = ''; + + echo json_encode(array('status' => true, 'message' => $msg, 'reload' => true, 'toastFlag' => $toastFlag, 'search' => $search, 'hash' => $hash)). ";#;" ; + ob_flush(); + flush(); + + return $true; } public function getMyConfig($var = null, $id = "noid") { @@ -646,6 +654,7 @@ trait ajaxHelper { } function saveSccpDevice($get_settings, $validateonly = false) { + dbug($get_settings); $hdr_prefix = 'sccp_hw_'; $hdr_arprefix = 'sccp_hw-ar_'; $hdr_vendPrefix = 'vendorconfig_'; @@ -761,15 +770,28 @@ trait ajaxHelper { $this->dbinterface->write('sccpbuttons', $save_buttons, $update_hw, '', $name_dev); // Create new XML for this device, and then reset or restart the device // so that it loads the file from TFT. - $this->createSccpDeviceXML($name_dev); - if ($hw_id == 'new') { - $this->aminterface->sccpDeviceReset($name_dev, 'reset'); - } else { - $this->aminterface->sccpDeviceReset($name_dev, 'restart'); - } $msg = "Device Saved"; - - return array('status' => true, 'message' => $msg, 'reload' => true); + $toastFlag = 'success'; + if (!$this->createSccpDeviceXML($name_dev)){ + // will only be false if creating SIP SEP with no line. + $msg = "Device Saved but SEP config file not created as no SIP line attached to this device"; + $toastFlag = 'warning'; + }; + $hash = '#sipdevice'; + if ($get_settings['command'] != 'save_sip_device') { + $hash = '#sccpdevice'; + // cannot restart SIP device via chan-sccp. + if ($hw_id == 'new') { + $this->aminterface->sccpDeviceReset($name_dev, 'reset'); + } else { + $this->aminterface->sccpDeviceReset($name_dev, 'restart'); + } + } + $search = '?display=sccp_phone'; + echo json_encode(array('status' => true, 'message' => $msg, 'reload' => true, 'toastFlag' => $toastFlag, 'search' => $search, 'hash' => $hash)). ";#;" ; + ob_flush(); + flush(); + return true; } } diff --git a/sccpManagerUpdater.json b/sccpManagerUpdater.json index 0074a5c..897611d 100644 --- a/sccpManagerUpdater.json +++ b/sccpManagerUpdater.json @@ -2,7 +2,7 @@ "rawname": "sccp_manager", "repo": "stable", "name": "Sccp Manager", - "version": "14.3.0.25", + "version": "14.3.0.26", "publisher": "Steve Lad", "license": "GPL", "licenselink": "http://www.gnu.org/licenses/gpl.txt", @@ -15,7 +15,7 @@ "supported": { "version": "13.0" }, - "location": "https://github.com/chan-sccp/sccp_manager/archive/refs/tags/v14.3.0.25.tar.gz", + "location": "https://github.com/chan-sccp/sccp_manager/archive/refs/tags/v14.3.0.26.tar.gz", "md5sum": "0da5aac57e47f27e8fad4cfb8c2b5629", "packaged": "1641125511" } diff --git a/views/form.adddevice.php b/views/form.adddevice.php index a1d46c9..faaf46c 100644 --- a/views/form.adddevice.php +++ b/views/form.adddevice.php @@ -101,6 +101,7 @@ if (!empty($def_val['type'])) {
+