Merge branch 'Fix-dbinterface-class' into Issue-106

This commit is contained in:
steve-lad 2021-03-13 15:50:17 +01:00
commit fdf6c2ae83
7 changed files with 249 additions and 256 deletions

View file

@ -169,6 +169,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
$this->xml_data = simplexml_load_file($xml_vars); $this->xml_data = simplexml_load_file($xml_vars);
$this->initVarfromXml(); // Overwrite Exist $this->initVarfromXml(); // Overwrite Exist
} }
if (get_class($freepbx) === 'FreePBX') {
// only save settings when building a new FreePBX object
$this->saveSccpSettings();
}
} }
/* /*
@ -642,6 +647,8 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
} }
public function ajaxRequest($req, &$setting) { public function ajaxRequest($req, &$setting) {
// Called first by BMO. Must return true or request will be aborted.
// See https://wiki.freepbx.org/display/FOP/BMO+Ajax+Calls
switch ($req) { switch ($req) {
case 'backupsettings': case 'backupsettings':
case 'savesettings': case 'savesettings':
@ -670,11 +677,13 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
case 'delete_dialplan': case 'delete_dialplan':
return true; return true;
break; break;
} default:
return false; return false;
} }
}
// !TODO!: this should go into it's only ajam.html.php file (see: dahdiconfig) // !TODO!: this should go into it's only ajax.html.php file (see: dahdiconfig)
// ajaxHandler is called after ajaxRequest returns true
public function ajaxHandler() { public function ajaxHandler() {
$request = $_REQUEST; $request = $_REQUEST;
$msg = array(); $msg = array();
@ -822,7 +831,6 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
$res = $this->updateSccpButtons($hw_list); $res = $this->updateSccpButtons($hw_list);
$msg .= $res['Response'] . ' raw: ' . $res['data'] . ' '; $msg .= $res['Response'] . ' raw: ' . $res['data'] . ' ';
return array('status' => true, 'message' => 'Update Butons Labels Complite ' . $msg, 'reload' => true); return array('status' => true, 'message' => 'Update Butons Labels Complite ' . $msg, 'reload' => true);
case 'model_add': case 'model_add':
$save_settings = array(); $save_settings = array();
$key_name = array('model', 'vendor', 'dns', 'buttons', 'loadimage', 'loadinformationid', 'nametemplate'); $key_name = array('model', 'vendor', 'dns', 'buttons', 'loadimage', 'loadinformationid', 'nametemplate');
@ -846,7 +854,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
return $save_settings; return $save_settings;
break; break;
case 'model_enabled': case 'model_enabled':
$model_set = '1'; $model_set = '1'; // fall through intentionally
case 'model_disabled': case 'model_disabled':
if ($request['command'] == 'model_disabled') { if ($request['command'] == 'model_disabled') {
$model_set = '0'; $model_set = '0';
@ -859,7 +867,6 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
} }
} }
return array('status' => true, 'table_reload' => true); return array('status' => true, 'table_reload' => true);
break; break;
case 'model_delete': case 'model_delete':
if (!empty($request['model'])) { if (!empty($request['model'])) {
@ -1161,7 +1168,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
$save_settings = array(); $save_settings = array();
$save_codec = array(); $save_codec = array();
$name_dev = ''; $name_dev = '';
$db_field = $this->dbinterface->HWextension_db_SccpTableData("get_colums_sccpdevice"); $db_field = $this->dbinterface->HWextension_db_SccpTableData("get_columns_sccpdevice");
$hw_id = (empty($get_settings['sccp_deviceid'])) ? 'new' : $get_settings['sccp_deviceid']; $hw_id = (empty($get_settings['sccp_deviceid'])) ? 'new' : $get_settings['sccp_deviceid'];
$hw_type = (empty($get_settings['sccp_device_typeid'])) ? 'sccpdevice' : $get_settings['sccp_device_typeid']; $hw_type = (empty($get_settings['sccp_device_typeid'])) ? 'sccpdevice' : $get_settings['sccp_device_typeid'];
$update_hw = ($hw_id == 'new') ? 'update' : 'clear'; $update_hw = ($hw_id == 'new') ? 'update' : 'clear';
@ -1284,7 +1291,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
} }
$this->dbinterface->write("sccpdevice", $save_settings, 'replace'); $this->dbinterface->write("sccpdevice", $save_settings, 'replace');
$save_buttons = $this->getPhoneButtons($get_settings, $name_dev, $hw_type); $save_buttons = $this->getPhoneButtons($get_settings, $name_dev, $hw_type);
$this->dbinterface->write("sccpbuttons", $save_buttons, $update_hw, '', $name_dev); $this->dbinterface->write("sccpbuttons", $save_buttons, $update_hw, 'add', $name_dev); //was empty so would fall through to INSERT
$this->createSccpDeviceXML($name_dev); $this->createSccpDeviceXML($name_dev);
if ($hw_id == 'new') { if ($hw_id == 'new') {
$this->srvinterface->sccpDeviceReset($name_dev); $this->srvinterface->sccpDeviceReset($name_dev);
@ -1403,7 +1410,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
); );
*/ */
$name_dev = ''; $name_dev = '';
$db_field = $this->dbinterface->HWextension_db_SccpTableData("get_colums_sccpuser"); $db_field = $this->dbinterface->HWextension_db_SccpTableData("get_columns_sccpuser");
// $hw_id = (empty($get_settings['sccp_deviceid'])) ? 'new' : $get_settings['sccp_deviceid']; // $hw_id = (empty($get_settings['sccp_deviceid'])) ? 'new' : $get_settings['sccp_deviceid'];
// $update_hw = ($hw_id == 'new') ? 'update' : 'clear'; // $update_hw = ($hw_id == 'new') ? 'update' : 'clear';
$hw_prefix = 'SEP'; $hw_prefix = 'SEP';
@ -1476,17 +1483,14 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
} }
} }
$this->dbinterface->write("sccpuser", $save_settings, 'replace', 'name'); $this->dbinterface->write("sccpuser", $save_settings, 'replace', 'name');
$this->dbinterface->write("sccpbuttons", $save_buttons, 'clear', '', $name_dev); $this->dbinterface->write("sccpbuttons", $save_buttons, 'delete', '', $name_dev); //standardise to delete
return $save_buttons; return $save_buttons;
// Why is there a second return here???????
return $save_settings; return $save_settings;
} }
public function getSccpSettingFromDB() { public function getSccpSettingFromDB() {
$raw_data = $this->dbinterface->get_db_SccpSetting(); $this->sccpvalues = $this->dbinterface->get_db_SccpSetting();
foreach ($raw_data as $var) {
$this->sccpvalues[$var['keyword']] = array('keyword' => $var['keyword'], 'data' => $var['data'], 'seq' => $var['seq'], 'type' => $var['type']);
}
return; return;
} }
@ -1864,17 +1868,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
// global $db; // global $db;
// global $amp_conf; // global $amp_conf;
$save_settings = array(); // $save_settings = array();
if (empty($save_value)) { if (empty($save_value)) {
foreach ($this->sccpvalues as $key => $val) { $this->dbinterface->write('sccpsettings', $this->sccpvalues, 'replace'); //Change to replace as clearer
if ((trim($val['data']) !== '') or ($val['data'] == '0')) {
$save_settings[] = array($key, $db->escapeSimple($val['data']), $val['seq'], $val['type']);
}
}
$this->dbinterface->write('sccpsettings', $save_settings, 'clear');
} else { } else {
$this->dbinterface->write('sccpsettings', $save_value, 'update'); $this->dbinterface->write('sccpsettings', $save_value, 'update');
return true;
} }
return true; return true;
} }
@ -2136,9 +2134,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
} else { } else {
$dir_list = $this->findAllFiles($dir, $file_ext, 'fileonly'); $dir_list = $this->findAllFiles($dir, $file_ext, 'fileonly');
} }
$raw_settings = $this->dbinterface->getDb_model_info($get, $format_list, $filter); $raw_settings = $this->dbinterface->getDb_model_info($get, $format_list, $filter);
if ($validate) { if ($validate) {
for ($i = 0; $i < count($raw_settings); $i++) { for ($i = 0; $i < count($raw_settings); $i++) {
$raw_settings[$i]['validate'] = '-;-'; $raw_settings[$i]['validate'] = '-;-';

View file

@ -21,7 +21,7 @@ class dbinterface
public function info() public function info()
{ {
$Ver = '13.0.2'; $Ver = '13.0.2'; // This should be updated
return array('Version' => $Ver, return array('Version' => $Ver,
'about' => 'Data access interface ver: ' . $Ver); 'about' => 'Data access interface ver: ' . $Ver);
} }
@ -31,39 +31,39 @@ class dbinterface
*/ */
public function get_db_SccpTableByID($dataid, $data = array(), $indexField = '') public function get_db_SccpTableByID($dataid, $data = array(), $indexField = '')
{ {
$resut = array(); $result = array();
$raw = $this->HWextension_db_SccpTableData($dataid, $data); $raw = $this->HWextension_db_SccpTableData($dataid, $data);
if (empty($raw) || empty($indexField)) { if (empty($raw) || empty($indexField)) {
return $raw; return $raw;
} }
foreach ($raw as $value) { foreach ($raw as $value) {
$id = $value[$indexField]; $id = $value[$indexField];
$resut[$id] = $value; $result[$id] = $value;
} }
return $resut; return $result;
} }
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.
$dbh = \FreePBX::Database();
$stmt = '';
$stmts = '';
if ($dataid == '') { if ($dataid == '') {
return false; return false;
} }
switch ($dataid) { switch ($dataid) {
case "SccpExtension": case 'SccpExtension':
if (empty($data['name'])) { if (empty($data['name'])) {
$sql = "SELECT * FROM `sccpline` ORDER BY `name`"; $stmts = $dbh->prepare('SELECT * FROM sccpline ORDER BY name');
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
} else { } else {
$sql = "SELECT * FROM `sccpline` WHERE `name`='" . $data['name']. "'"; $stmts = $dbh->prepare('SELECT * FROM sccpline WHERE name = :name');
; $stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR);
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
} }
break; break;
case "SccpDevice": case 'SccpDevice':
$filtred ='';
$singlrow = false;
if (empty($data['fields'])) { if (empty($data['fields'])) {
$fld = '`name`,`name` as `mac`, `type`, `button`, `addon`, `_description` as description'; $fld = 'name, name as mac, type, button, addon, _description as description';
} else { } else {
switch ($data['fields']) { switch ($data['fields']) {
case "all": case "all":
@ -77,216 +77,213 @@ class dbinterface
break; break;
} }
} }
if (!empty($data['name'])) { if (!empty($data['name'])) { //either filter by name or by type
$filtred = "`name`='" . $data['name']. "'"; $stmt = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE name = :name ORDER BY name');
; $stmt->bindParam(':name', $data['name'],\PDO::PARAM_STR);
$singlrow = true; } elseif (!empty($data['type'])) {
}
if (!empty($data['type'])) {
switch ($data['type']) { switch ($data['type']) {
case "cisco-sip": case "cisco-sip":
$filtred = "`TYPE` LIKE '%-sip'"; $stmts = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE TYPE LIKE \'%-sip\' ORDER BY name');
break; break;
case "cisco": case "cisco": // Fall through to default intentionally
default: default:
$filtred = "`TYPE` not LIKE '%-sip'"; $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
if (empty($filtred)) { $stmts = $dbh->prepare('SELECT ' . $fld . ' FROM sccpdeviceconfig WHERE ORDER BY name');
$sql = "SELECT ". $fld ." FROM `sccpdeviceconfig` ORDER BY `name`";
} else {
$sql = "SELECT ". $fld ." FROM `sccpdeviceconfig` WHERE ".$filtred." ORDER BY `name`";
}
if ($singlrow) {
$raw_settings = sql($sql, "getRow", DB_FETCHMODE_ASSOC);
} else {
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
} }
break; break;
case "HWSipDevice": case 'HWSipDevice':
$raw_settings = $this->getDb_model_info($get = "sipphones", $format_list = "model"); $raw_settings = $this->getDb_model_info($get = "sipphones", $format_list = "model");
break; break;
case "HWDevice": case 'HWDevice':
$raw_settings = $this->getDb_model_info($get = "ciscophones", $format_list = "model"); $raw_settings = $this->getDb_model_info($get = "ciscophones", $format_list = "model");
break; break;
case "HWextension": case 'HWextension':
$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_colums_sccpdevice": case 'get_columns_sccpdevice':
$sql = "DESCRIBE sccpdevice"; $stmts = $dbh->prepare('DESCRIBE sccpdevice');
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
break; break;
case "get_colums_sccpuser": case 'get_columns_sccpuser':
$sql = "DESCRIBE sccpuser"; $stmts = $dbh->prepare('DESCRIBE sccpuser');
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
break; break;
case "get_sccpdevice_byid": case 'get_sccpdevice_byid':
$sql = '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="' . $data['id'] . '";'; LEFT JOIN sccpdevmodel as addon ON t1.addon=addon.model WHERE name = :name');
$raw_settings = sql($sql, "getRow", DB_FETCHMODE_ASSOC); $stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR);
break; break;
case "get_sccpuser": case 'get_sccpuser':
$sql = "SELECT * FROM `sccpuser` "; $stmt = $dbh->prepare('SELECT * FROM sccpuser WHERE name = :name');
if (!empty($data['id'])) { $stmt->bindParam(':name', $data['id'],\PDO::PARAM_STR);
$sql .= 'WHERE name="' . $data['id'] . '" ';
}
$sql .= "ORDER BY `name`;";
$raw_settings = sql($sql, "getRow", DB_FETCHMODE_ASSOC);
break; break;
case "get_sccpdevice_buttons": case 'get_sccpdevice_buttons':
$sql = ''; $sql = '';
if (!empty($data['buttontype'])) { if (!empty($data['buttontype'])) {
$sql .= 'buttontype="' . $data['buttontype'] . '" '; $sql .= 'buttontype = :buttontype';
} }
if (!empty($data['id'])) { if (!empty($data['id'])) {
$sql .= (empty($sql)) ? 'ref="' . $data['id'] . '" ' : 'and ref="' . $data['id'] . '" '; $sql .= (empty($sql)) ? 'ref = :ref' : ' and ref = :ref';
} }
if (!empty($sql)) { if (!empty($sql)) {
$sql = 'SELECT * FROM sccpbuttonconfig WHERE ' .$sql. 'ORDER BY `instance`;'; $stmts = $dbh->prepare('SELECT * FROM sccpbuttonconfig WHERE ' .$sql. ' ORDER BY instance');
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC); $stmts->bindParam(':buttontype', $data['buttontype'],\PDO::PARAM_STR);
$stmts->bindParam(':ref', $data['id'],\PDO::PARAM_STR);
} else { } else {
$raw_settings = array(); $raw_settings = array();
} }
break; break;
} }
if (!empty($stmt)) {
$stmt->execute();
$raw_settings = $stmt->fetch(\PDO::FETCH_ASSOC);
} elseif (!empty($stmts)) {
$stmts->execute();
$raw_settings = $stmts->fetchAll(\PDO::FETCH_ASSOC);
}
return $raw_settings; return $raw_settings;
} }
public function get_db_SccpSetting() public function get_db_SccpSetting()
{ {
$sql = "SELECT `keyword`, `data`, `type`, `seq` FROM `sccpsettings` ORDER BY `type`, `seq`"; $dbh = \FreePBX::Database();
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC); try {
return $raw_settings; $stmt = $dbh->prepare('SELECT keyword, data, type, seq FROM sccpsettings ORDER BY type, seq');
$stmt->execute();
foreach ($stmt->fetchAll() as $var) {
$mysccpvalues[$var['keyword']] = array('keyword' => $var['keyword'], 'data' => $var['data'], 'seq' => $var['seq'], 'type' => $var['type']);
}
return $mysccpvalues;
} catch(\PDOException $e) {
// sccpsettings table does not yet exist. FreePBX is instantiating
// a SCCP_Manager object from the Installer before the installer can
// create the table so will create here.
$stmt = $dbh-> prepare('CREATE TABLE IF NOT EXISTS sccpsettings (
keyword VARCHAR (50) NOT NULL,
data VARCHAR (255) NOT NULL,
seq TINYINT (1),
type TINYINT (1) NOT NULL default 0,
PRIMARY KEY (keyword, seq, type )
);');
$stmt->execute();
//if ($dbh::IsError($check)) {
// die_freepbx("Can not create sccpsettings table, error: $check\n");
//}
return array();
}
} }
public function get_db_sysvalues() public function get_db_sysvalues()
{ {
$sql = "SHOW VARIABLES LIKE '%group_concat%'"; $dbh = \FreePBX::Database();
$raw_settings = sql($sql, "getRow", DB_FETCHMODE_ASSOC); $stmt = $dbh->prepare('SHOW VARIABLES LIKE \'%group_concat%\'');
return $raw_settings; $stmt->execute();
return $stmt->fetch(\PDO::FETCH_ASSOC);
} }
/* /*
* Get Sccp Device Model information * Get Sccp Device Model information
*/ */
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();
switch ($format_list) { $sel_inf = '*, 0 as validate';
case "model": if ($format_list === 'model') {
$sel_inf = "model, vendor, dns, buttons"; $sel_inf = 'model, vendor, dns, buttons, 0 as validate';
break;
case "all":
default:
$sel_inf = "*";
break;
} }
$sel_inf .= ", '0' as 'validate'";
switch ($get) { switch ($get) {
case "byciscoid": case 'byciscoid':
if (!empty($filter)) { if (!empty($filter)) {
if (!empty($filter['model'])) { if (!empty($filter['model'])) {
if (strpos($filter['model'], 'loadInformation')) { if (strpos($filter['model'], 'loadInformation')) {
$sql = "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 {
$sql = "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 {
// $sql = "SELECT ".$filter['model']; $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel ORDER BY model ";
} }
break; break;
} }
break; break;
case "byid": case 'byid':
if (!empty($filter)) { if (!empty($filter)) {
if (!empty($filter['model'])) { if (!empty($filter['model'])) {
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (`model` ='" . $filter['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);
} else { } else {
// $sql = "SELECT ".$filter['model']; $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel ORDER BY model ";
} }
break; break;
} }
break; break;
case "extension": case 'extension':
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (dns = 0) and (enabled > 0) 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':
case "phones": //$stmt = $db->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE enabled = 1 ORDER BY model'); //previously this fell through to phones.
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (dns > 0) and (enabled > 0) ORDER BY model "; //break; // above includes expansion modules but was not original behaviour so commented out. Falls through to phones.
// $sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (enabled > 0) ORDER BY model "; case 'phones':
$stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel WHERE (dns > 0) and (enabled = 1) ORDER BY model ');
break; break;
case "ciscophones": case 'ciscophones':
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (dns > 0) and (enabled > 0) 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');
// $sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (enabled > 0) ORDER BY model ";
break; break;
case "sipphones": case 'sipphones':
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (dns > 0) and (enabled > 0) 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');
// $sql = "SELECT " . $sel_inf . " FROM sccpdevmodel WHERE (enabled > 0) ORDER BY model ";
break; break;
case "all": case 'all': // Fall through to default
default: default:
$sql = "SELECT " . $sel_inf . " FROM sccpdevmodel ORDER BY model "; $stmt = $dbh->prepare('SELECT ' . $sel_inf . ' FROM sccpdevmodel ORDER BY model');
break; break;
} }
$raw_settings = sql($sql, "getAll", DB_FETCHMODE_ASSOC); $stmt->execute();
return $raw_settings; return $stmt->fetchAll(\PDO::FETCH_ASSOC);
} }
function write($db_name = "", $save_value = array(), $mode = 'update', $key_fld = "", $hwid = "") function write($table_name = "", $save_value = array(), $mode = 'update', $key_fld = "", $hwid = "")
{ {
// mode clear - Empty tabele before update // mode clear - Empty table before update
// mode update - update / replace record // mode update - update / replace record
global $db; $dbh = \FreePBX::Database();
// global $amp_conf; $result = false;
$result = "Error";
$delete_value = array(); $delete_value = array();
switch ($db_name) { switch ($table_name) {
case 'sccpsettings': case 'sccpsettings':
foreach ($save_value as $key_v => $data) { if ($mode == 'replace') { // Change mode name to be more transparent
if (!empty($data)) { $dbh->prepare('TRUNCATE sccpsettings')->execute();
if (isset($data[1])) { $stmt = $dbh->prepare('INSERT INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)');
if ($data[1] == $this->val_null) {
$delete_value[] = $save_value[$key_v]['keyword'];
unset($save_value[$key_v]);
}
}
if (isset($data['data'])) {
if ($data['data'] == $this->val_null) {
$delete_value[] = $save_value[$key_v]['keyword'];
unset($save_value[$key_v]);
}
}
}
}
if ($mode == 'clear') {
$sql = 'truncate `sccpsettings`';
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt = $db->prepare('INSERT INTO `sccpsettings` (`keyword`, `data`, `seq`, `type`) VALUES (?,?,?,?)');
$result = $db->executeMultiple($stmt, $save_value);
} else { } else {
if (!empty($delete_value)) { $stmt = $dbh->prepare('REPLACE INTO sccpsettings (keyword, data, seq, type) VALUES (:keyword,:data,:seq,:type)');
$stmt = $db->prepare('DELETE FROM `sccpsettings` WHERE `keyword`=?');
$result = $db->executeMultiple($stmt, $delete_value);
} }
if (!empty($save_value)) { foreach ($save_value as $key => $dataArr) {
$stmt = $db->prepare('REPLACE INTO `sccpsettings` (`keyword`, `data`, `seq`, `type`) VALUES (?,?,?,?)'); if (!empty($dataArr) && isset($dataArr['data'])) {
$result = $db->executeMultiple($stmt, $save_value); if ($dataArr['data'] == $this->val_null) {
$delete_value[] = $save_value[$key]['keyword'];
break;
}
}
$stmt->bindParam(':keyword',$dataArr['keyword'],\PDO::PARAM_STR);
$stmt->bindParam(':data',$dataArr['data'],\PDO::PARAM_STR);
$stmt->bindParam(':seq',$dataArr['seq'],\PDO::PARAM_INT);
$stmt->bindParam(':type',$dataArr['type'],\PDO::PARAM_INT);
$result = $stmt->execute();
}
if (!empty($delete_value)) {
$stmt = $dbh->prepare('DELETE FROM sccpsettings WHERE keyword = :keyword');
foreach ($delete_value as $del_key) {
$stmt->bindParam(':keyword',$del_key,\PDO::PARAM_STR);
$result = $stmt->execute();
} }
} }
break; break;
case 'sccpdevmodel': case 'sccpdevmodel': // Fall through to next intentionally
case 'sccpdevice': case 'sccpdevice': // Fall through to next intentionally
case 'sccpuser': case 'sccpuser':
$sql_db = $db_name;
$sql_key = ""; $sql_key = "";
$sql_var = ""; $sql_var = "";
foreach ($save_value as $key_v => $data) { foreach ($save_value as $key_v => $data) {
@ -294,60 +291,58 @@ class dbinterface
$sql_var .= ', '; $sql_var .= ', ';
} }
if ($data === $this->val_null) { if ($data === $this->val_null) {
$sql_var .= '`' . $key_v . '`=NULL'; $sql_var .= $key_v . '= NULL';
} else { } else {
$sql_var .= '`' . $key_v . '`="' . $data . '"'; $sql_var .= $key_v . ' = \'' . $data . '\''; //quote data as normally is string
} }
if ($key_fld == $key_v) { if ($key_v === $key_fld) {
$sql_key = '`' . $key_v . '`="' . $data . '"'; $sql_key = $key_v . ' = \'' . $data . '\''; //quote data as normally is string
} }
} }
if (!empty($sql_var)) { if (!empty($sql_var)) {
if ($mode == 'delete') { switch ($mode) {
$req = 'DELETE FROM `' . $sql_db . '` WHERE ' . $sql_key . ';'; case 'delete':
} else { $stmt = $dbh->prepare('DELETE FROM '. $table_name . ' WHERE ' . $sql_key);
if ($mode == 'update') { break;
$req = 'UPDATE `' . $sql_db . '` SET ' . $sql_var . ' WHERE ' . $sql_key . ';'; case 'update':
} else { $stmt = $dbh->prepare('UPDATE ' . $table_name . ' SET ' . $sql_var . ' WHERE ' . $sql_key);
$req = 'REPLACE INTO `' . $sql_db . '` SET ' . $sql_var . ';'; break;
case 'replace':
$stmt = $dbh->prepare('REPLACE INTO ' . $table_name . ' VALUES ' . $sql_var);
break;
// no default mode - must be explicit.
} }
} }
}
$stmt = $db->prepare($req);
$result = $stmt->execute(); $result = $stmt->execute();
break; break;
case 'sccpbuttons': case 'sccpbuttons':
if (($mode == 'clear') || ($mode == 'delete')) { switch ($mode) {
$sql = 'DELETE FROM `sccpbuttonconfig` WHERE ref="' . $hwid . '";'; case 'delete':
$stmt = $db->prepare($sql); $sql = 'DELETE FROM sccpbuttonconfig WHERE ref = :hwid';
$stmt->execute(); $stmt = $dbh->prepare($sql);
} $stmt->bindParam(':hwid', $hwid,\PDO::PARAM_STR);
if ($mode == 'delete') { $result = $stmt->execute();
break; break;
case 'replace':
if (!empty($save_value)) {
$stmt = $dbh->prepare('UPDATE sccpbuttonconfig SET name =? WHERE ref = ? AND reftype =? AND instance =? AND buttontype =?');
$result= $dbh->executeMultiple($stmt, $save_value);
} }
if (empty($save_value)) {
break; break;
case 'add':
if (!empty($save_value)) {
$stmt = $dbh->prepare('INSERT INTO sccpbuttonconfig (ref, reftype, instance, buttontype, name, options) VALUES (?,?,?,?,?,?)');
$result = $dbh->executeMultiple($stmt, $save_value);
} }
if ($mode == 'replace') {
$sql = 'UPDATE `sccpbuttonconfig` SET `name`=? WHERE `ref`= ? AND `reftype`=? AND `instance`=? AND `buttontype`=?;';
// $sql = 'INSERT INTO `sccpbuttonconfig` (`ref`, `reftype`,`instance`, `buttontype`, `name`, `options`) VALUES (?,?,?,?,?,?);';
// die(print_r($save_value,1));
$stmt = $db->prepare($sql);
$result= $db->executeMultiple($stmt, $save_value);
} else {
$sql = 'INSERT INTO `sccpbuttonconfig` (`ref`, `reftype`,`instance`, `buttontype`, `name`, `options`) VALUES (?,?,?,?,?,?);';
// die(print_r($save_value,1));
$stmt = $db->prepare($sql);
$result = $db->executeMultiple($stmt, $save_value);
}
break; break;
// No default case - must be specific in request.
}
} }
return $result; return $result;
} }
/* /*
* My be Replace by SccpTables ??! * Maybe Replace by SccpTables ??!
* *
*/ */
public function dump_sccp_tables($data_path, $database, $user, $pass) public function dump_sccp_tables($data_path, $database, $user, $pass)
@ -362,35 +357,28 @@ class dbinterface
*/ */
public function validate() public function validate()
{ {
global $db; $dbh = \FreePBX::Database();
$check_fields = array('430' => array('_hwlang' => "varchar(12)"), '431' => array('private'=> "enum('on','off')"), '433' => array('directed_pickup'=>'') );
$sql = "DESCRIBE `sccpdevice`;";
$raw_result = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
$result = 0; $result = 0;
foreach ($raw_result as $value) { $check_fields = [
'430' => ['_hwlang' => "varchar(12)"],
'431' => ['private'=> "enum('on','off')"],
'433' => ['directed_pickup'=>'']
];
$stmt = $dbh->prepare('DESCRIBE sccpdevice');
$stmt->execute();
foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $value) {
$id_result[$value['Field']] = $value['Type']; $id_result[$value['Field']] = $value['Type'];
} }
foreach ($check_fields as $key => $value) { foreach ($check_fields as $key => $value) {
$sub_result = true; if (!empty(array_intersect_assoc($value, $id_result))) {
foreach ($value as $skey => $svalue) {
if (!empty($svalue)) {
if (empty($id_result[$skey])) {
$sub_result = false;
} else {
if (strtolower($id_result[$skey]) != strtolower($svalue)) {
$sub_result = false;
}
}
} else {
if (!empty($id_result[$skey])) {
$sub_result = false;
}
}
}
if ($sub_result) {
$result = $key; $result = $key;
} else { } else {
break; // no match but maybe checking against an empty string so just need to check key does not exist
foreach ($value as $skey => $svalue) {
if (empty($svalue) && (!isset($id_result[$skey]))) {
$result = $key;
}
}
} }
} }

View file

@ -692,7 +692,7 @@ $(document).ready(function () {
//}); //});
// Bootstrap table Enabled / Disabled buttons ( class="btn-tab-select") // Bootstrap table Enable / Disable buttons ( class="btn-tab-select")
$("table").on('check-all.bs.table', function (rows) { $("table").on('check-all.bs.table', function (rows) {
var id_fld = $(this).data('id'); var id_fld = $(this).data('id');
$(".btn-tab-select").each(function () { $(".btn-tab-select").each(function () {

View file

@ -17,6 +17,7 @@ global $astman;
global $version; global $version;
global $srvinterface; global $srvinterface;
global $mobile_hw; global $mobile_hw;
global $useAmiForSoftKeys;
$mobile_hw = '0'; $mobile_hw = '0';
$class = "\\FreePBX\\Modules\\Sccp_manager\\srvinterface"; $class = "\\FreePBX\\Modules\\Sccp_manager\\srvinterface";
@ -26,7 +27,6 @@ if (!class_exists($class, false)) {
if (class_exists($class, false)) { if (class_exists($class, false)) {
$srvinterface = new $class(); $srvinterface = new $class();
} }
function Get_DB_config($sccp_compatible) function Get_DB_config($sccp_compatible)
{ {
global $mobile_hw; global $mobile_hw;
@ -483,16 +483,16 @@ function InstallDB_sccpsettings()
{ {
global $db; global $db;
outn("<li>" . _("Creating sccpsettings table...") . "</li>"); outn("<li>" . _("Creating sccpsettings table...") . "</li>");
$sql = "CREATE TABLE IF NOT EXISTS `sccpsettings` ( $stmt = $db-> prepare('CREATE TABLE IF NOT EXISTS sccpsettings (
`keyword` VARCHAR (50) NOT NULL default '', keyword VARCHAR (50) NOT NULL,
`data` VARCHAR (255) NOT NULL default '', data VARCHAR (255) NOT NULL,
`seq` TINYINT (1), seq TINYINT (1),
`type` TINYINT (1) NOT NULL default '0', type TINYINT (1) NOT NULL,
PRIMARY KEY (`keyword`,`seq`,`type`) PRIMARY KEY (keyword, seq, type )
);"; );');
$check = $db->query($sql); $check = $stmt->execute();
if (DB::IsError($check)) { if (DB::IsError($check)) {
die_freepbx("Can not create sccpsettings table, error:$check\n"); die_freepbx("Can not create sccpsettings table, error: $check\n");
} }
return true; return true;
} }

View file

@ -56,6 +56,14 @@ if (!empty($version)) {
sql("DELETE FROM kvstore WHERE module = 'sccpsettings'"); sql("DELETE FROM kvstore WHERE module = 'sccpsettings'");
sql("DELETE FROM kvstore WHERE module = 'Sccp_manager'"); sql("DELETE FROM kvstore WHERE module = 'Sccp_manager'");
} }
// By accessing the database, we have recreated sccpsettings table so now delete
// Need to rewrite this uninstaller.
$sql = "DROP TABLE IF EXISTS sccpsettings";
$result = $db->query($sql);
if (DB::IsError($result)) {
die_freepbx($result->getDebugInfo());
}
unset($result);
/* Comment: Maybe save in sccpsettings, if the chan_sccp tables already existed in the database or if they were created by install.php */ /* Comment: Maybe save in sccpsettings, if the chan_sccp tables already existed in the database or if they were created by install.php */
/* So that you know if it is safe to drop/delete them */ /* So that you know if it is safe to drop/delete them */

View file

@ -13,12 +13,13 @@
<div class="display no-border"> <div class="display no-border">
<div id="toolbar-model"> <div id="toolbar-model">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target=".add_new_model"><i class="fa fa-bolt"></i> <?php echo _("Add model"); ?></button> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target=".add_new_model"><i class="fa fa-bolt"></i> <?php echo _("Add model"); ?>
</button>
<button data-id="model_disabled" class="btn btn-danger sccp_update btn-tab-select" data-type="sccp_model" data-table="table-models" disabled data-section="all"> <button data-id="model_disabled" class="btn btn-danger sccp_update btn-tab-select" data-type="sccp_model" data-table="table-models" disabled data-section="all">
<i class="glyphicon glyphicon-remove"></i> <span><?php echo _('Disabled') ?></span> <i class="glyphicon glyphicon-remove"></i> <span><?php echo _('Disable') ?></span>
</button> </button>
<button data-id="model_enabled" class="btn btn-danger sccp_update btn-tab-select" data-table="table-models" data-type="sccp_model" disabled data-section="all"> <button data-id="model_enabled" class="btn btn-danger sccp_update btn-tab-select" data-table="table-models" data-type="sccp_model" disabled data-section="all">
<i class="glyphicon glyphicon-active"></i> <span><?php echo _('Enabled') ?></span> <i class="glyphicon glyphicon-active"></i> <span><?php echo _('Enable') ?></span>
</button> </button>
<div class="btn-group"> <div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">