Optimise locale (language) management

Optimise function calls
Use array functions instead of loops
findInstalledlangs is currently called by the construct - as this is called frequently, maximise its efficiency
This commit is contained in:
steve-lad 2021-07-15 12:12:07 +02:00
parent a4f8fa4600
commit 1b32e0977e
7 changed files with 36 additions and 52 deletions

View file

@ -84,12 +84,11 @@ namespace FreePBX\modules;
class Sccp_manager extends \FreePBX_Helpers implements \BMO { class Sccp_manager extends \FreePBX_Helpers implements \BMO {
/* Field Values for type seq */ /* Field Values for type seq */
private $SCCP_LANG_DICTIONARY = 'be-sccp.jar'; // CISCO LANG file search in /tftp-path
private $pagedata = null; private $pagedata = null;
private $sccp_driver_ver = '11.4'; // Ver fore SCCP.CLASS.PHP private $sccp_driver_ver = '11.4'; // Ver fore SCCP.CLASS.PHP
public $sccp_manager_ver = '14.1.0.0'; public $sccp_manager_ver = '14.1.0.0';
public $sccp_branch = 'm'; // Ver fore SCCP.CLASS.PHP public $sccp_branch = 'm'; // Ver fore SCCP.CLASS.PHP
private $tftpLang = array(); private $installedLangs = array();
private $hint_context = array('default' => '@ext-local'); /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Get it from Config !!! private $hint_context = array('default' => '@ext-local'); /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Get it from Config !!!
private $val_null = 'NONE'; /// REPLACE to null Field private $val_null = 'NONE'; /// REPLACE to null Field
@ -140,7 +139,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
$this->sccpvalues = $this->dbinterface->get_db_SccpSetting(); //Initialise core settings $this->sccpvalues = $this->dbinterface->get_db_SccpSetting(); //Initialise core settings
$this->initializeSccpPath(); //Set required Paths $this->initializeSccpPath(); //Set required Paths
$this->updateTimeZone(); // Get timezone from FreePBX $this->updateTimeZone(); // Get timezone from FreePBX
$this->initTftpLang(); $this->findInstLangs();
$this->saveSccpSettings(); $this->saveSccpSettings();
} }
@ -173,7 +172,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
'h_show' => $show_Header, 'h_show' => $show_Header,
'form_prefix' => $form_prefix, 'form_prefix' => $form_prefix,
'fvalues' => $form_values, 'fvalues' => $form_values,
'tftpLang' => $this->tftpLang, 'installedLangs' => $this->installedLangs,
'chanSccpHelp' => $this->sccpHelpInfo, 'chanSccpHelp' => $this->sccpHelpInfo,
'sccp_defaults' => $this->sccpvalues 'sccp_defaults' => $this->sccpvalues
) )
@ -749,34 +748,26 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
/** /**
* Retrieve Active Codecs * Retrieve Active Codecs
* return finds Languageg pack * return finds Language pack
*/ */
private function initTftpLang() { private function findInstLangs() {
//locales and languages are installed in the tftp_lang_path
$result = array(); $result = array();
if (empty($this->sccppath["tftp_path"]) || empty($this->sccppath["tftp_lang_path"])) { $langDir = $this->sccppath["tftp_lang_path"];
return $result; $localeJar = 'be-sccp.jar'; // This jar should exist if the locale is populated
} $langArr = $this->extconfigs->getExtConfig('sccp_lang');
$dir = $this->sccppath["tftp_lang_path"]; $localeArray = array_combine(array_keys($langArr),array_column($langArr, 'locale'));
$cdir = scandir($dir); foreach (array_diff(scandir($langDir),array('.', '..')) as $subDir) {
foreach ($cdir as $key => $value) { if (is_dir($langDir . DIRECTORY_SEPARATOR . $subDir)) {
if (!in_array($value, array(".", ".."))) { $filename = $langDir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . $localeJar;
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { if (file_exists($filename)) {
$filename = $dir . DIRECTORY_SEPARATOR . $value . DIRECTORY_SEPARATOR . $this->SCCP_LANG_DICTIONARY; $result = array_merge(array_intersect($localeArray,array($subDir)),$result);
if (file_exists($filename)) {
$lang_ar = $this->extconfigs->getExtConfig('sccp_lang');
foreach ($lang_ar as $lang_key => $lang_value) {
if ($lang_value['locale'] == $value) {
$result[$lang_key] = $value;
}
}
// $result[] = $value;
}
} }
} }
} }
$this->tftpLang = $result; $this->installedLangs = $result;
} }
/* /*
@ -785,11 +776,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
private function initializeTFtpLanguagePath() { private function initializeTFtpLanguagePath() {
$dir = $this->sccppath["tftp_lang_path"]; $dir = $this->sccppath["tftp_lang_path"];
foreach ($this->extconfigs->getExtConfig('sccp_lang') as $lang_key => $lang_value) { foreach ($this->extconfigs->getExtConfig('sccp_lang') as $langKey => $langValueArr) {
$filename = $dir . DIRECTORY_SEPARATOR . $lang_value['locale']; $localeDir = $dir . DIRECTORY_SEPARATOR . $langValueArr['locale'];
if (!file_exists($filename)) { if (!is_dir($localeDir)) {
if (!mkdir($filename, 0777, true)) { if (!mkdir($localeDir, 0755, true)) {
die('Error creating tftp language directory'); die("Error creating $localeDir directory");
} }
} }
} }

View file

@ -1,14 +1,9 @@
<?php <?php
/**
*
*/
namespace FreePBX\modules\Sccp_manager; namespace FreePBX\modules\Sccp_manager;
class extconfigs class extconfigs
{ {
public function __construct($parent_class = null) public function __construct($parent_class = null)
{ {
$this->paren_class = $parent_class; $this->paren_class = $parent_class;

View file

@ -482,7 +482,7 @@ class formcreate
<?php <?php
} }
function addElementSL($child, $fvalues, $sccp_defaults,$npref, $tftpLang) { function addElementSL($child, $fvalues, $sccp_defaults,$npref, $installedLangs) {
// Input element Select SLS - System Language // Input element Select SLS - System Language
$res_n = (string)$child ->name; $res_n = (string)$child ->name;
$res_id = $npref.$res_n; $res_id = $npref.$res_n;
@ -502,7 +502,7 @@ class formcreate
$select_opt= $syslangs; $select_opt= $syslangs;
break; break;
case 'SLT': case 'SLT':
$select_opt= $tftpLang; $select_opt= $installedLangs;
break; break;
case 'SLZ': case 'SLZ':
$timeZoneOffsetList = array('-12' => 'GMT -12', '-11' => 'GMT -11', '-10' => 'GMT -10', '-09' => 'GMT -9', $timeZoneOffsetList = array('-12' => 'GMT -12', '-11' => 'GMT -11', '-10' => 'GMT -10', '-09' => 'GMT -9',

View file

@ -397,19 +397,17 @@ trait ajaxHelper {
$save_codec = array(); $save_codec = array();
$count_mods = 0; $count_mods = 0;
$dbSaveArray = array(); $dbSaveArray = array();
$integer_msg = _("%s must be a non-negative integer");
$errors = array(); $errors = array();
$i = 0; $i = 0;
$action = isset($request['sccp_createlangdir']) ? $request['sccp_createlangdir'] : '';
if (isset($request["{$hdr_prefix}createlangdir"]) && ($request["{$hdr_prefix}createlangdir"] == 'yes')) {
$this->initializeTFtpLanguagePath();
}
// if uncheck all codecs, audiocodecs key is missing so nothing changes in db. // if uncheck all codecs, audiocodecs key is missing so nothing changes in db.
// Unsetting all codecs will now return to chan-sccp defaults. // Unsetting all codecs will now return to chan-sccp defaults.
if (!isset($request['audiocodecs'])) { if (!isset($request['audiocodecs'])) {
$request['audiocodecs'] = array_fill_keys(explode(',',$this->sccpvalues['allow']['systemdefault']),true); $request['audiocodecs'] = array_fill_keys(explode(',',$this->sccpvalues['allow']['systemdefault']),true);
} }
if ($action == 'yes') {
$this->initializeTFtpLanguagePath();
}
foreach ($request as $key => $value) { foreach ($request as $key => $value) {
// Originally saved all to sccpvalues. Now will save to db defaults if appropriate // Originally saved all to sccpvalues. Now will save to db defaults if appropriate
// TODO: Need to verify the tables defined in showGroup - some options maybe // TODO: Need to verify the tables defined in showGroup - some options maybe

View file

@ -271,7 +271,7 @@ trait helperfunctions {
} }
public function getFilesFromProvisioner($type = "",$name = "",$device = "") { public function getFilesFromProvisioner($type = '',$name = '',$device = '') {
$filesToGet = array(); $filesToGet = array();
$provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/"; $provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/";
if (!$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) { if (!$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) {
@ -280,15 +280,15 @@ trait helperfunctions {
} }
switch ($type) { switch ($type) {
case 'firmware': case 'firmware':
if (!is_dir("{$this->sccppath['tftp_path']}/firmware/{$device}")) { if (!is_dir("{$this->sccppath['tftp_firmware_path']}/{$device}")) {
mkdir("{$this->sccppath['tftp_path']}/firmware/{$device}", 0755); mkdir("{$this->sccppath['tftp_firmware_path']}/{$device}", 0755);
} }
$firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']"); $firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']");
$result = $firmwareDir[0]->xpath("//Directory[@name={$device}]"); $result = $firmwareDir[0]->xpath("//Directory[@name={$device}]");
$filesToGet = (array)$result[0]->FileName; $filesToGet = (array)$result[0]->FileName;
foreach ($filesToGet as $srcFile) { foreach ($filesToGet as $srcFile) {
file_put_contents("{$this->sccppath['tftp_path']}/firmware/{$device}/{$srcFile}", file_put_contents("{$this->sccppath['tftp_firmware_path']}/{$device}/{$srcFile}",
file_get_contents("{$provisionerUrl}" . (string)$result[0]->DirectoryPath . $srcFile)); file_get_contents($provisionerUrl . (string)$result[0]->DirectoryPath . $srcFile));
} }
return "thanks for trying Diederik :-)"; return "thanks for trying Diederik :-)";
break; break;
@ -298,8 +298,6 @@ trait helperfunctions {
break; break;
} }
} }
file_get_contents(https://github.com/dkgroot/provision_sccp/raw/master/tftpboot/firmware/7911/SCCP11.9-4-2SR3-1S.loads): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
File:/var/www/html/admin/modules/sccp_manager/sccpManTraits/helperFunctions.php:291
public function initVarfromXml() { public function initVarfromXml() {
if ((array) $this->xml_data) { if ((array) $this->xml_data) {

View file

@ -162,7 +162,8 @@
<div class="col-md-12"> <div class="col-md-12">
<?php echo "Sccp_Manager will try to download the firmware and template files from Provision_Sccp on GitHub.<br> <?php echo "Sccp_Manager will try to download the firmware and template files from Provision_Sccp on GitHub.<br>
This site, dkgroot/provision_sccp, is unrelated to Sccp_Manager, and the files found cannot be warrantied<br> This site, dkgroot/provision_sccp, is unrelated to Sccp_Manager, and the files found cannot be warrantied<br>
If you accept this, please select the device type that you want to try to get files for and then Get Files From Provisioner<br><br>" ?> If you accept this, please select the device type that you want to try to get files for and then Get Files From Provisioner<br><br>
Please be patient - this may take some time depending on your internet link<br><br>" ?>
</div> </div>
</div> </div>
</div> </div>

View file

@ -81,7 +81,7 @@ foreach ($items as $child) {
case 'SLA': case 'SLA':
case 'SLZ': case 'SLZ':
case 'SL': case 'SL':
\FreePbx::sccp_manager()->formcreate->addElementSL($child, $fvalues, $sccp_defaults,$npref, $tftpLang); \FreePbx::sccp_manager()->formcreate->addElementSL($child, $fvalues, $sccp_defaults,$npref, $installedLangs);
break; break;
case 'SDM': case 'SDM':
case 'SDMS': case 'SDMS':
@ -105,3 +105,4 @@ if ($h_show==1) {
echo '</div>'; echo '</div>';
} }
?> ?>
installedLangs