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 {
/* Field Values for type seq */
private $SCCP_LANG_DICTIONARY = 'be-sccp.jar'; // CISCO LANG file search in /tftp-path
private $pagedata = null;
private $sccp_driver_ver = '11.4'; // Ver fore SCCP.CLASS.PHP
public $sccp_manager_ver = '14.1.0.0';
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 $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->initializeSccpPath(); //Set required Paths
$this->updateTimeZone(); // Get timezone from FreePBX
$this->initTftpLang();
$this->findInstLangs();
$this->saveSccpSettings();
}
@ -173,7 +172,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
'h_show' => $show_Header,
'form_prefix' => $form_prefix,
'fvalues' => $form_values,
'tftpLang' => $this->tftpLang,
'installedLangs' => $this->installedLangs,
'chanSccpHelp' => $this->sccpHelpInfo,
'sccp_defaults' => $this->sccpvalues
)
@ -749,34 +748,26 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
/**
* 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();
if (empty($this->sccppath["tftp_path"]) || empty($this->sccppath["tftp_lang_path"])) {
return $result;
}
$dir = $this->sccppath["tftp_lang_path"];
$langDir = $this->sccppath["tftp_lang_path"];
$localeJar = 'be-sccp.jar'; // This jar should exist if the locale is populated
$langArr = $this->extconfigs->getExtConfig('sccp_lang');
$localeArray = array_combine(array_keys($langArr),array_column($langArr, 'locale'));
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
$filename = $dir . DIRECTORY_SEPARATOR . $value . DIRECTORY_SEPARATOR . $this->SCCP_LANG_DICTIONARY;
foreach (array_diff(scandir($langDir),array('.', '..')) as $subDir) {
if (is_dir($langDir . DIRECTORY_SEPARATOR . $subDir)) {
$filename = $langDir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . $localeJar;
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;
$result = array_merge(array_intersect($localeArray,array($subDir)),$result);
}
}
}
}
$this->tftpLang = $result;
$this->installedLangs = $result;
}
/*
@ -785,11 +776,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
private function initializeTFtpLanguagePath() {
$dir = $this->sccppath["tftp_lang_path"];
foreach ($this->extconfigs->getExtConfig('sccp_lang') as $lang_key => $lang_value) {
$filename = $dir . DIRECTORY_SEPARATOR . $lang_value['locale'];
if (!file_exists($filename)) {
if (!mkdir($filename, 0777, true)) {
die('Error creating tftp language directory');
foreach ($this->extconfigs->getExtConfig('sccp_lang') as $langKey => $langValueArr) {
$localeDir = $dir . DIRECTORY_SEPARATOR . $langValueArr['locale'];
if (!is_dir($localeDir)) {
if (!mkdir($localeDir, 0755, true)) {
die("Error creating $localeDir directory");
}
}
}

View file

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

View file

@ -482,7 +482,7 @@ class formcreate
<?php
}
function addElementSL($child, $fvalues, $sccp_defaults,$npref, $tftpLang) {
function addElementSL($child, $fvalues, $sccp_defaults,$npref, $installedLangs) {
// Input element Select SLS - System Language
$res_n = (string)$child ->name;
$res_id = $npref.$res_n;
@ -502,7 +502,7 @@ class formcreate
$select_opt= $syslangs;
break;
case 'SLT':
$select_opt= $tftpLang;
$select_opt= $installedLangs;
break;
case 'SLZ':
$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();
$count_mods = 0;
$dbSaveArray = array();
$integer_msg = _("%s must be a non-negative integer");
$errors = array();
$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.
// Unsetting all codecs will now return to chan-sccp defaults.
if (!isset($request['audiocodecs'])) {
$request['audiocodecs'] = array_fill_keys(explode(',',$this->sccpvalues['allow']['systemdefault']),true);
}
if ($action == 'yes') {
$this->initializeTFtpLanguagePath();
}
foreach ($request as $key => $value) {
// 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

View file

@ -271,7 +271,7 @@ trait helperfunctions {
}
public function getFilesFromProvisioner($type = "",$name = "",$device = "") {
public function getFilesFromProvisioner($type = '',$name = '',$device = '') {
$filesToGet = array();
$provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/";
if (!$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) {
@ -280,15 +280,15 @@ trait helperfunctions {
}
switch ($type) {
case 'firmware':
if (!is_dir("{$this->sccppath['tftp_path']}/firmware/{$device}")) {
mkdir("{$this->sccppath['tftp_path']}/firmware/{$device}", 0755);
if (!is_dir("{$this->sccppath['tftp_firmware_path']}/{$device}")) {
mkdir("{$this->sccppath['tftp_firmware_path']}/{$device}", 0755);
}
$firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']");
$result = $firmwareDir[0]->xpath("//Directory[@name={$device}]");
$filesToGet = (array)$result[0]->FileName;
foreach ($filesToGet as $srcFile) {
file_put_contents("{$this->sccppath['tftp_path']}/firmware/{$device}/{$srcFile}",
file_get_contents("{$provisionerUrl}" . (string)$result[0]->DirectoryPath . $srcFile));
file_put_contents("{$this->sccppath['tftp_firmware_path']}/{$device}/{$srcFile}",
file_get_contents($provisionerUrl . (string)$result[0]->DirectoryPath . $srcFile));
}
return "thanks for trying Diederik :-)";
break;
@ -298,8 +298,6 @@ trait helperfunctions {
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() {
if ((array) $this->xml_data) {

View file

@ -162,7 +162,8 @@
<div class="col-md-12">
<?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>
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>

View file

@ -81,7 +81,7 @@ foreach ($items as $child) {
case 'SLA':
case 'SLZ':
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;
case 'SDM':
case 'SDMS':
@ -105,3 +105,4 @@ if ($h_show==1) {
echo '</div>';
}
?>
installedLangs