Migrate from global db object

As per FreePBX BMO, no longer use global db object
This commit is contained in:
steve-lad 2021-03-13 14:58:30 +01:00
parent cc8eb549d0
commit 7030ec2bb5

View file

@ -46,7 +46,7 @@ class dbinterface
public function HWextension_db_SccpTableData($dataid, $data = array()) public function HWextension_db_SccpTableData($dataid, $data = array())
{ {
// $stmt is a single row fetch, $stmts is a fetchAll. // $stmt is a single row fetch, $stmts is a fetchAll.
global $db; $dbh = \FreePBX::Database();
$stmt = ''; $stmt = '';
$stmts = ''; $stmts = '';
if ($dataid == '') { if ($dataid == '') {
@ -55,9 +55,9 @@ class dbinterface
switch ($dataid) { switch ($dataid) {
case 'SccpExtension': case 'SccpExtension':
if (empty($data['name'])) { if (empty($data['name'])) {
$stmts = $db->prepare('SELECT * FROM sccpline ORDER BY name'); $stmts = $dbh->prepare('SELECT * FROM sccpline ORDER BY name');
} else { } else {
$stmts = $db->prepare('SELECT * FROM sccpline WHERE name = :name'); $stmts = $dbh->prepare('SELECT * FROM sccpline WHERE name = :name');
$stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR); $stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR);
} }
break; break;
@ -78,20 +78,20 @@ class dbinterface
} }
} }
if (!empty($data['name'])) { //either filter by name or by type if (!empty($data['name'])) { //either filter by name or by type
$stmt = $db->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE name = :name ORDER BY name'); $stmt = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE name = :name ORDER BY name');
$stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR); $stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR);
} elseif (!empty($data['type'])) { } elseif (!empty($data['type'])) {
switch ($data['type']) { switch ($data['type']) {
case "cisco-sip": case "cisco-sip":
$stmts = $db->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE TYPE LIKE \'%-sip\' ORDER BY name'); $stmts = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE TYPE LIKE \'%-sip\' ORDER BY name');
break; break;
case "cisco": // Fall through to default intentionally case "cisco": // Fall through to default intentionally
default: default:
$stmts = $db->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE TYPE not LIKE \'%-sip\' ORDER BY name'); $stmts = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE TYPE not LIKE \'%-sip\' ORDER BY name');
break; break;
} }
} else { //no filter and no name provided - return all } else { //no filter and no name provided - return all
$stmts = $db->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE ORDER BY name'); $stmts = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE ORDER BY name');
} }
break; break;
case 'HWSipDevice': case 'HWSipDevice':
@ -104,20 +104,20 @@ class dbinterface
$raw_settings = $this->getDb_model_info($get = "extension", $format_list = "model"); $raw_settings = $this->getDb_model_info($get = "extension", $format_list = "model");
break; break;
case 'get_columns_sccpdevice': case 'get_columns_sccpdevice':
$stmts = $db->prepare('DESCRIBE sccpdevice'); $stmts = $dbh->prepare('DESCRIBE sccpdevice');
break; break;
case 'get_columns_sccpuser': case 'get_columns_sccpuser':
$stmts = $db->prepare('DESCRIBE sccpuser'); $stmts = $dbh->prepare('DESCRIBE sccpuser');
break; break;
case 'get_sccpdevice_byid': case 'get_sccpdevice_byid':
$stmt = $db->prepare('SELECT t1.*, types.dns, types.buttons, types.loadimage, types.nametemplate as nametemplate, $stmt = $dbh->prepare('SELECT t1.*, types.dns, types.buttons, types.loadimage, types.nametemplate as nametemplate,
addon.buttons as addon_buttons FROM sccpdevice AS t1 addon.buttons as addon_buttons FROM sccpdevice AS t1
LEFT JOIN sccpdevmodel as types ON t1.type=types.model LEFT JOIN sccpdevmodel as types ON t1.type=types.model
LEFT JOIN sccpdevmodel as addon ON t1.addon=addon.model WHERE name = :name'); LEFT JOIN sccpdevmodel as addon ON t1.addon=addon.model WHERE name = :name');
$stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR); $stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR);
break; break;
case 'get_sccpuser': case 'get_sccpuser':
$stmt = $db->prepare('SELECT * FROM sccpuser WHERE name = :name'); $stmt = $dbh->prepare('SELECT * FROM sccpuser WHERE name = :name');
$stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR); $stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR);
break; break;
case 'get_sccpdevice_buttons': case 'get_sccpdevice_buttons':
@ -129,7 +129,7 @@ class dbinterface
$sql .= (empty($sql)) ? 'ref = :ref' : ' and ref = :ref'; $sql .= (empty($sql)) ? 'ref = :ref' : ' and ref = :ref';
} }
if (!empty($sql)) { if (!empty($sql)) {
$stmts = $db->prepare('SELECT * FROM sccpbuttonconfig WHERE ' .$sql. ' ORDER BY instance'); $stmts = $dbh->prepare('SELECT * FROM sccpbuttonconfig WHERE ' .$sql. ' ORDER BY instance');
$stmts->bindParam(':buttontype', $data['buttontype'],\PDO::PARAM_STR); $stmts->bindParam(':buttontype', $data['buttontype'],\PDO::PARAM_STR);
$stmts->bindParam(':ref', $data['id'],\PDO::PARAM_STR); $stmts->bindParam(':ref', $data['id'],\PDO::PARAM_STR);
} else { } else {
@ -149,9 +149,9 @@ class dbinterface
public function get_db_SccpSetting() public function get_db_SccpSetting()
{ {
global $db; $dbh = \FreePBX::Database();
try { try {
$stmt = $db->prepare('SELECT keyword, data, type, seq FROM sccpsettings ORDER BY type, seq'); $stmt = $dbh->prepare('SELECT keyword, data, type, seq FROM sccpsettings ORDER BY type, seq');
$stmt->execute(); $stmt->execute();
foreach ($stmt->fetchAll() as $var) { foreach ($stmt->fetchAll() as $var) {
$mysccpvalues[$var['keyword']] = array('keyword' => $var['keyword'], 'data' => $var['data'], 'seq' => $var['seq'], 'type' => $var['type']); $mysccpvalues[$var['keyword']] = array('keyword' => $var['keyword'], 'data' => $var['data'], 'seq' => $var['seq'], 'type' => $var['type']);
@ -161,25 +161,25 @@ class dbinterface
// sccpsettings table does not yet exist. FreePBX is instantiating // sccpsettings table does not yet exist. FreePBX is instantiating
// a SCCP_Manager object from the Installer before the installer can // a SCCP_Manager object from the Installer before the installer can
// create the table so will create here. // create the table so will create here.
$stmt = $db-> prepare('CREATE TABLE IF NOT EXISTS sccpsettings ( $stmt = $dbh-> prepare('CREATE TABLE IF NOT EXISTS sccpsettings (
keyword VARCHAR (50) NOT NULL, keyword VARCHAR (50) NOT NULL,
data VARCHAR (255) NOT NULL, data VARCHAR (255) NOT NULL,
seq TINYINT (1), seq TINYINT (1),
type TINYINT (1) NOT NULL default 0, type TINYINT (1) NOT NULL default 0,
PRIMARY KEY (keyword, seq, type ) PRIMARY KEY (keyword, seq, type )
);'); );');
$check = $stmt->execute(); $stmt->execute();
if (\DB::IsError($check)) { //if ($dbh::IsError($check)) {
die_freepbx("Can not create sccpsettings table, error: $check\n"); // die_freepbx("Can not create sccpsettings table, error: $check\n");
} //}
return array(); return array();
} }
} }
public function get_db_sysvalues() public function get_db_sysvalues()
{ {
global $db; $dbh = \FreePBX::Database();
$stmt = $db->prepare('SHOW VARIABLES LIKE \'%group_concat%\''); $stmt = $dbh->prepare('SHOW VARIABLES LIKE \'%group_concat%\'');
$stmt->execute(); $stmt->execute();
return $stmt->fetch(\PDO::FETCH_ASSOC); return $stmt->fetch(\PDO::FETCH_ASSOC);
} }
@ -190,7 +190,7 @@ class dbinterface
function getDb_model_info($get = 'all', $format_list = 'all', $filter = array()) function getDb_model_info($get = 'all', $format_list = 'all', $filter = array())
{ {
global $db; $dbh = \FreePBX::Database();
$sel_inf = '*, 0 as validate'; $sel_inf = '*, 0 as validate';
if ($format_list === 'model') { if ($format_list === 'model') {
$sel_inf = 'model, vendor, dns, buttons, 0 as validate'; $sel_inf = 'model, vendor, dns, buttons, 0 as validate';
@ -200,12 +200,12 @@ class dbinterface
if (!empty($filter)) { if (!empty($filter)) {
if (!empty($filter['model'])) { if (!empty($filter['model'])) {
if (strpos($filter['model'], 'loadInformation')) { if (strpos($filter['model'], 'loadInformation')) {
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (loadinformationid =' . $filter['model'] . ') ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (loadinformationid =' . $filter['model'] . ') ORDER BY model');
} else { } else {
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (loadinformationid = loadInformation' . $filter['model'] . ') ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (loadinformationid = loadInformation' . $filter['model'] . ') ORDER BY model');
} }
} else { } else {
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
} }
break; break;
} }
@ -213,32 +213,32 @@ class dbinterface
case 'byid': case 'byid':
if (!empty($filter)) { if (!empty($filter)) {
if (!empty($filter['model'])) { if (!empty($filter['model'])) {
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE model = :model ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE model = :model ORDER BY model');
$stmt->bindParam(':model', $filter['model'],\PDO::PARAM_STR); $stmt->bindParam(':model', $filter['model'],\PDO::PARAM_STR);
} else { } else {
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
} }
break; break;
} }
break; break;
case 'extension': case 'extension':
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns = 0) and (enabled = 1) ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns = 0) and (enabled = 1) ORDER BY model');
break; break;
case 'enabled': case 'enabled':
//$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE enabled = 1 ORDER BY model'); //previously this fell through to phones. //$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE enabled = 1 ORDER BY model'); //previously this fell through to phones.
//break; // above includes expansion modules but was not original behaviour so commented out. Falls through to phones. //break; // above includes expansion modules but was not original behaviour so commented out. Falls through to phones.
case 'phones': case 'phones':
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) ORDER BY model '); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) ORDER BY model ');
break; break;
case 'ciscophones': case 'ciscophones':
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND vendor NOT LIKE \'%-sip\' ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND vendor NOT LIKE \'%-sip\' ORDER BY model');
break; break;
case 'sipphones': case 'sipphones':
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND `vendor` LIKE \'%-sip\' ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) AND `vendor` LIKE \'%-sip\' ORDER BY model');
break; break;
case 'all': // Fall through to default case 'all': // Fall through to default
default: default:
$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model'); $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
break; break;
} }
$stmt->execute(); $stmt->execute();
@ -249,16 +249,16 @@ class dbinterface
{ {
// mode clear - Empty table before update // mode clear - Empty table before update
// mode update - update / replace record // mode update - update / replace record
global $db; $dbh = \FreePBX::Database();
$result = false; $result = false;
$delete_value = array(); $delete_value = array();
switch ($table_name) { switch ($table_name) {
case 'sccpsettings': case 'sccpsettings':
if ($mode == 'replace') { // Change mode name to be more transparent if ($mode == 'replace') { // Change mode name to be more transparent
$db->prepare('TRUNCATE sccpsettings')->execute(); $dbh->prepare('TRUNCATE sccpsettings')->execute();
$stmt = $db->prepare('INSERT INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)'); $stmt = $dbh->prepare('INSERT INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)');
} else { } else {
$stmt = $db->prepare('REPLACE INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)'); $stmt = $dbh->prepare('REPLACE INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)');
} }
foreach ($save_value as $key => $dataArr) { foreach ($save_value as $key => $dataArr) {
if (!empty($dataArr) && isset($dataArr['data'])) { if (!empty($dataArr) && isset($dataArr['data'])) {
@ -274,7 +274,7 @@ class dbinterface
$result = $stmt->execute(); $result = $stmt->execute();
} }
if (!empty($delete_value)) { if (!empty($delete_value)) {
$stmt = $db->prepare('DELETE FROM sccpsettings WHERE keyword = :keyword'); $stmt = $dbh->prepare('DELETE FROM sccpsettings WHERE keyword = :keyword');
foreach ($delete_value as $del_key) { foreach ($delete_value as $del_key) {
$stmt->bindParam(':keyword',$del_key,\PDO::PARAM_STR); $stmt->bindParam(':keyword',$del_key,\PDO::PARAM_STR);
$result = $stmt->execute(); $result = $stmt->execute();
@ -302,13 +302,13 @@ class dbinterface
if (!empty($sql_var)) { if (!empty($sql_var)) {
switch ($mode) { switch ($mode) {
case 'delete': case 'delete':
$stmt = $db->prepare('DELETE FROM '. $table_name . ' WHERE ' . $sql_key); $stmt = $dbh->prepare('DELETE FROM '. $table_name . ' WHERE ' . $sql_key);
break; break;
case 'update': case 'update':
$stmt = $db->prepare('UPDATE ' . $table_name . ' SET ' . $sql_var . ' WHERE ' . $sql_key); $stmt = $dbh->prepare('UPDATE ' . $table_name . ' SET ' . $sql_var . ' WHERE ' . $sql_key);
break; break;
case 'replace': case 'replace':
$stmt = $db->prepare('REPLACE INTO ' . $table_name . ' VALUES ' . $sql_var); $stmt = $dbh->prepare('REPLACE INTO ' . $table_name . ' VALUES ' . $sql_var);
break; break;
// no default mode - must be explicit. // no default mode - must be explicit.
} }
@ -319,20 +319,20 @@ class dbinterface
switch ($mode) { switch ($mode) {
case 'delete': case 'delete':
$sql = 'DELETE FROM sccpbuttonconfig WHERE ref = :hwid'; $sql = 'DELETE FROM sccpbuttonconfig WHERE ref = :hwid';
$stmt = $db->prepare($sql); $stmt = $dbh->prepare($sql);
$stmt->bindParam(':hwid', $hwid,\PDO::PARAM_STR); $stmt->bindParam(':hwid', $hwid,\PDO::PARAM_STR);
$result = $stmt->execute(); $result = $stmt->execute();
break; break;
case 'replace': case 'replace':
if (!empty($save_value)) { if (!empty($save_value)) {
$stmt = $db->prepare('UPDATE sccpbuttonconfig SET name =? WHERE ref = ? AND reftype =? AND instance =? AND buttontype =?'); $stmt = $dbh->prepare('UPDATE sccpbuttonconfig SET name =? WHERE ref = ? AND reftype =? AND instance =? AND buttontype =?');
$result= $db->executeMultiple($stmt, $save_value); $result= $dbh->executeMultiple($stmt, $save_value);
} }
break; break;
case 'add': case 'add':
if (!empty($save_value)) { if (!empty($save_value)) {
$stmt = $db->prepare('INSERT INTO sccpbuttonconfig (ref, reftype, instance, buttontype, name, options) VALUES (?,?,?,?,?,?)'); $stmt = $dbh->prepare('INSERT INTO sccpbuttonconfig (ref, reftype, instance, buttontype, name, options) VALUES (?,?,?,?,?,?)');
$result = $db->executeMultiple($stmt, $save_value); $result = $dbh->executeMultiple($stmt, $save_value);
} }
break; break;
// No default case - must be specific in request. // No default case - must be specific in request.
@ -357,14 +357,14 @@ class dbinterface
*/ */
public function validate() public function validate()
{ {
global $db; $dbh = \FreePBX::Database();
$result = 0; $result = 0;
$check_fields = [ $check_fields = [
'430' => ['_hwlang' => "varchar(12)"], '430' => ['_hwlang' => "varchar(12)"],
'431' => ['private'=> "enum('on','off')"], '431' => ['private'=> "enum('on','off')"],
'433' => ['directed_pickup'=>''] '433' => ['directed_pickup'=>'']
]; ];
$stmt = $db->prepare('DESCRIBE sccpdevice'); $stmt = $dbh->prepare('DESCRIBE sccpdevice');
$stmt->execute(); $stmt->execute();
foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $value) { foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $value) {
$id_result[$value['Field']] = $value['Type']; $id_result[$value['Field']] = $value['Type'];