Clean up dbinterface class

Change db search criteria for sip / sccp to improve performance
Use Toast for Save and continue success message
Improve db write performance by simplifying formatting
This commit is contained in:
stevenA 2022-01-13 10:21:50 +01:00
parent b7654a8c75
commit 67428fb3be
6 changed files with 35 additions and 35 deletions

View file

@ -376,7 +376,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
public function processPageData() { public function processPageData() {
foreach ($this->pagedata as &$page) { foreach ($this->pagedata as &$page) {
// own version of load_view - simplifies passing variables // own version of load_view - simplifies passing variables as in object context
ob_start(); ob_start();
include($page['page']); include($page['page']);
$page['content'] = ob_get_contents(); $page['content'] = ob_get_contents();

View file

@ -52,9 +52,7 @@ $(document).ready(function () {
success: function (data) { success: function (data) {
if (data.status === true) { if (data.status === true) {
if (data.message) { if (data.message) {
bs_alert(data.message,data.status); fpbxToast(_(data.message),'', 'success');
} else {
fpbxToast(_('Data saved'),'', 'success');
} }
} else { } else {
bs_alert(data.message,data.status); bs_alert(data.message,data.status);

View file

@ -126,7 +126,7 @@ abstract class Message
protected function setSanitizedKey($key, $value) protected function setSanitizedKey($key, $value)
{ {
$key = strtolower((string) $key); //$key = strtolower((string) $key);
$_string_key = array('actionid', 'descr'); $_string_key = array('actionid', 'descr');
if (array_search($key, $_string_key) !== false) { if (array_search($key, $_string_key) !== false) {
$this->keys[$key] = (string) $this->sanitizeInput($value, 'string'); $this->keys[$key] = (string) $this->sanitizeInput($value, 'string');
@ -257,6 +257,7 @@ abstract class IncomingMessage extends Message
public function __construct($rawContent) public function __construct($rawContent)
{ {
parent::__construct(); parent::__construct();
dbug($rawContent);
$this->rawContent = $rawContent; $this->rawContent = $rawContent;
$lines = explode(Message::EOL, $rawContent); $lines = explode(Message::EOL, $rawContent);
foreach ($lines as $line) { foreach ($lines as $line) {
@ -269,6 +270,7 @@ abstract class IncomingMessage extends Message
} catch (AMIException $e) { } catch (AMIException $e) {
throw new AMIException("Error: '" . $e . "'\n Dump RawContent:\n" . $this->rawContent . "\n"); throw new AMIException("Error: '" . $e . "'\n Dump RawContent:\n" . $this->rawContent . "\n");
} }
dbug($this->keys);
} }
} }
} }

View file

@ -95,7 +95,7 @@ class aminterface
public function info() public function info()
{ {
$Ver = '13.0.4'; $Ver = '16.0.0.1';
if ($this->_config['enabled']){ if ($this->_config['enabled']){
return array('Version' => $Ver, return array('Version' => $Ver,
'about' => 'AMI data ver: ' . $Ver, 'test' => get_declared_classes()); 'about' => 'AMI data ver: ' . $Ver, 'test' => get_declared_classes());

View file

@ -22,7 +22,7 @@ class dbinterface
public function info() public function info()
{ {
$Ver = '14.0.0.1'; // This should be updated $Ver = '16.0.0.1'; // This should be updated
return array('Version' => $Ver, return array('Version' => $Ver,
'about' => 'Data access interface ver: ' . $Ver); 'about' => 'Data access interface ver: ' . $Ver);
} }
@ -70,12 +70,12 @@ class dbinterface
switch ($data['type']) { switch ($data['type']) {
case "cisco-sip": case "cisco-sip":
$stmts = $this->db->prepare("SELECT name, type, button, addon, description, 'not connected' AS status, '- -' AS address, 'N' AS new_hw $stmts = $this->db->prepare("SELECT name, type, button, addon, description, 'not connected' AS status, '- -' AS address, 'N' AS new_hw
FROM sccpdeviceconfig WHERE type LIKE '%-sip' ORDER BY name"); FROM sccpdeviceconfig WHERE RIGHT(type,4) = '-sip' ORDER BY name");
break; break;
case "sccp": // Fall through to default intentionally case "sccp": // Fall through to default intentionally
default: default:
$stmts = $this->db->prepare("SELECT name, type, button, addon, description, 'not connected' AS status, '- -' AS address, 'N' AS new_hw $stmts = $this->db->prepare("SELECT name, type, button, addon, description, 'not connected' AS status, '- -' AS address, 'N' AS new_hw
FROM sccpdeviceconfig WHERE type not LIKE '%-sip' ORDER BY name"); FROM sccpdeviceconfig WHERE RIGHT(type,4) != '-sip' ORDER BY name");
break; break;
} }
break; break;
@ -101,11 +101,11 @@ class dbinterface
} elseif (!empty($data['type'])) { } elseif (!empty($data['type'])) {
switch ($data['type']) { switch ($data['type']) {
case "cisco-sip": case "cisco-sip":
$stmts = $this->db->prepare("SELECT {$fld} FROM sccpdeviceconfig WHERE TYPE LIKE '%-sip' ORDER BY name"); $stmts = $this->db->prepare("SELECT {$fld} FROM sccpdeviceconfig WHERE RIGHT(type,4) = '-sip' ORDER BY name");
break; break;
case "cisco": // Fall through to default intentionally case "cisco": // Fall through to default intentionally
default: default:
$stmts = $this->db->prepare("SELECT {$fld} FROM sccpdeviceconfig WHERE TYPE not LIKE '%-sip' ORDER BY name"); $stmts = $this->db->prepare("SELECT {$fld} FROM sccpdeviceconfig WHERE RIGHT(type,4) != '-sip' ORDER BY name");
break; break;
} }
} else { //no filter and no name provided - return all } else { //no filter and no name provided - return all
@ -239,10 +239,10 @@ class dbinterface
$stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns != 0) and (enabled = 1) ORDER BY model"); $stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns != 0) and (enabled = 1) ORDER BY model");
break; break;
case 'ciscophones': case 'ciscophones':
$stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND vendor NOT LIKE '%-sip' ORDER BY model"); $stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND RIGHT(vendor,4) != '-sip' ORDER BY model");
break; break;
case 'sipphones': case 'sipphones':
$stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND vendor LIKE '%-sip' ORDER BY model"); $stmt = $this->db->prepare("SELECT {$sel_inf} FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND RIGHT(vendor,4) = '-sip' ORDER BY model");
break; break;
case 'all': // Fall through to default case 'all': // Fall through to default
default: default:
@ -253,7 +253,7 @@ class dbinterface
return $stmt->fetchAll(\PDO::FETCH_ASSOC); return $stmt->fetchAll(\PDO::FETCH_ASSOC);
} }
function write($table_name = "", $save_value = array(), $mode = 'update', $key_fld = "", $hwid = "") function write(string $table_name, $save_value = array(), string $mode = 'update', $key_fld = "", $hwid = "")
{ {
// mode clear - Empty table before update // mode clear - Empty table before update
// mode update - update / replace record // mode update - update / replace record
@ -281,36 +281,36 @@ class dbinterface
case 'sccpdevmodel': // Fall through to next intentionally case 'sccpdevmodel': // Fall through to next intentionally
case 'sccpdevice': // Fall through to next intentionally case 'sccpdevice': // Fall through to next intentionally
case 'sccpuser': case 'sccpuser':
$sql_key = ""; $sql_key = '';
$sql_var = ""; $stmt = '';
foreach ($save_value as $key_v => $data) { $formattedSQL = array_reduce(
if (!empty($sql_var)) { array_keys($save_value), // pass in the array_keys instead of the array here
$sql_var .= ', '; function ($carry, $key) use ($save_value) { // ... then 'use' the actual array here
} return "${carry}${key} = '${save_value[$key]}', ";
if ($data === $this->val_null) { },
$sql_var .= $key_v . '= NULL'; );
} else { if (isset($formattedSQL)) { // if array is empty returns null
$sql_var .= $key_v . ' = \'' . $data . '\''; //quote data as normally is string $formattedSQL = rtrim($formattedSQL,', '); // Remove the trailing ',' and any spaces.
}
if ($key_v === $key_fld) {
$sql_key = $key_v . ' = \'' . $data . '\''; //quote data as normally is string
}
}
if (!empty($sql_var)) {
switch ($mode) { switch ($mode) {
case 'delete': case 'delete':
if (array_key_exists($key_fld, $save_value)) {
$sql_key = "${key_fld} = '${save_value[$key_fld]}'"; //quote data as normally is string
$stmt = $this->db->prepare("DELETE FROM {$table_name} WHERE {$sql_key}"); $stmt = $this->db->prepare("DELETE FROM {$table_name} WHERE {$sql_key}");
}
break; break;
case 'update': case 'update':
$stmt = $this->db->prepare("UPDATE {$table_name} SET {$sql_var} WHERE {$sql_key}"); if (array_key_exists($key_fld, $save_value)) {
$sql_key = "${key_fld} = '${save_value[$key_fld]}'"; //quote data as normally is string
$stmt = $this->db->prepare("UPDATE {$table_name} SET {$formattedSQL} WHERE {$sql_key}");
}
break; break;
case 'replace': case 'replace':
$stmt = $this->db->prepare("REPLACE INTO {$table_name} SET {$sql_var}"); $stmt = $this->db->prepare("REPLACE INTO {$table_name} SET {$formattedSQL}");
break; break;
// no default mode - must be explicit. // no default mode - must be explicit.
} }
} }
$result = $stmt->execute(); $result = (!empty($stmt)) ? $stmt->execute() : false;
break; break;
case 'sccpbuttons': case 'sccpbuttons':
switch ($mode) { switch ($mode) {