Menu="WebGui"
Title="Language"
Icon="icon-language"
Tag="icon-language"
---
<?PHP
/* Copyright 2005-2022, Lime Technology
 * Copyright 2012-2022, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";

$installed = ['English'];
$plugins   = '/var/log/plugins';

$keys = parse_ini_file('webGui/include/languages.key',false,INI_SCANNER_RAW);
$head = '<i class="fa fa-fw fa-trash" title="_(Remove the installed language)_" style="cursor:pointer" onclick="remove(&quot;';
$tail = '&quot;)"></i>';

foreach (glob("$plugins/lang-*.xml",GLOB_NOSORT) as $xml_file) $installed[] = language('LanguageLocal',$xml_file).$head.str_replace('lang-','',basename($xml_file,'.xml')).$tail;
?>
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.switchbutton.css')?>">
<style>
select.wide{min-width:<?=$themes2?'314':'300'?>px}
div.notice{margin-top:70px;margin-bottom:30px}
div#selectlang{margin-top:20px}
label.button{font-family:clear-sans;font-size:1.1rem;font-weight:bold;letter-spacing:1.8px;text-transform:uppercase;min-width:86px;margin:0;padding:8px;text-align:center;text-decoration:none;white-space:nowrap;cursor:pointer;outline:none;border-radius:4px;border:none;color:#ff8c2f;background:-webkit-gradient(linear,left top,right top,from(#e22828),to(#ff8c2f)) 0 0 no-repeat,-webkit-gradient(linear,left top,right top,from(#e22828),to(#ff8c2f)) 0 100% no-repeat,-webkit-gradient(linear,left bottom,left top,from(#e22828),to(#e22828)) 0 100% no-repeat,-webkit-gradient(linear,left bottom,left top,from(#ff8c2f),to(#ff8c2f)) 100% 100% no-repeat;background:linear-gradient(90deg,#e22828 0,#ff8c2f) 0 0 no-repeat,linear-gradient(90deg,#e22828 0,#ff8c2f) 0 100% no-repeat,linear-gradient(0deg,#e22828 0,#e22828) 0 100% no-repeat,linear-gradient(0deg,#ff8c2f 0,#ff8c2f) 100% 100% no-repeat;background-size:100% 2px,100% 2px,2px 100%,2px 100%}
label.button:hover{color:#f2f2f2;background:-webkit-gradient(linear,left top,right top,from(#e22828),to(#ff8c2f));background:linear-gradient(90deg,#e22828 0,#ff8c2f)}
</style>

<script src="<?autov('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script>
function execute(cmd,title,language,name,filedata) {
  var keys = {};
  <?foreach ($keys as $key => $name) echo "keys['$key'] = '".strtok($keys[$key],' (')."'\n";?>
  $.post("/webGui/include/FileUpload.php",{cmd:cmd,filename:language,name:encodeURIComponent(name),filedata:filedata},function(list){
    if (list.indexOf('Error 500')==-1) {
      var installed = [];
      if (list.length) {
        var name = list.split(',');
        for (var i=0; i < name.length; i++) installed.push(keys[name[i]]+(name[i]!='en_US'? '<?=$head?>'+name[i]+'<?=$tail?>' : ''));
      }
      $('.installed').html(installed.join(', '));
      swal({title:title,text:"_(See)_ <a href='/Settings/DisplaySettings'>_(Display Settings)_</a> _(to select your language)_",type:'success',html:true,confirmButtonText:"_(Ok)_"});
      $('#uploadbutton').prop('disabled',true);
    }
  });
}
function remove(language) {
  execute('rm', "_(Language support deleted)_", language, '', '');
}
function upload(form) {
  var language = form.language;
  if (!language.value) {
    swal({title:"_(Missing Language)_",text:"_(Please select a language before proceeding)_!",type:'error',html:true,confirmButtonText:"_(Ok)_"});
    return;
  }
  execute('add', "_(Language support added)_", language.value, language.options[language.selectedIndex].text, form.file.value);
}
function getZIPfile(event,form) {
  var input = event.target;
  var reader = new FileReader();
  reader.onload = function(){
    var name = input.files[0].name;
    if (name.slice(-4)=='.zip') {
      form.file.value = reader.result;
      $('#uploadbutton').prop('disabled',false);
      for (var n = 0; n < form.language.length; n++) if (form.language.options[n].text.indexOf(name.slice(0,-4))>0) {form.language.selectedIndex = n; break;}
    } else {
     swal({title:"_(Wrong File Extension)_",text:"_(Only ZIP files are allowed)_",type:'error',html:true,confirmButtonText:"_(Ok)_"});
    }
  };
  reader.readAsDataURL(input.files[0]);
}
function installXML(name) {
  var file = name.trim();
  if (file) openPlugin('language install '+file, "_(Install Language)_");
}
$(function() {
  $('input.view').switchButton({labels_placement:'left', off_label:"_(User)_", on_label:"_(Developer)_"});
  $('input.view').change(function(){$('.user,.developer').toggle('slow');});
});
</script>
<span class="status vhshift"><input type="checkbox" class="view"></span>

<div markdown="1" class="user">
<div class="notice">_(Use this to install official language packs)_</div>
_(Installed languages)_:
: <span class="installed"><?=implode(', ',$installed)?></span>

_(Enter URL of language pack XML file)_
: &nbsp;

<dl>
<form name="xml_install" method="POST" target="progressFrame">
<dt><input type="text" name="file" id="xml_file" maxlength="1024" value="" style="width:90%"></dt>
<dd><input type="button" value="_(Install)_" onclick="installXML(this.form.file.value)" style="margin:0"></dd>
</form>
</dl>
</div>

<div markdown="1" class="developer" style="display:none">
<div class="notice">_(Use this to develop language packs)_</div>
<form markdown="1">
<input type="hidden" name="file" value="">

_(Installed languages)_:
: <span class="installed"><?=implode(', ',$installed)?></span>

_(Select language)_:
: <select name="language" class="wide">
  <?=mk_option(1, "", "---")?>
  <?foreach ($keys as $key => $name) echo mk_option(1, $key, $name)?>
  </select>

<div id="selectlang" markdown="1">
_(Select language file)_:
: <span><label for="files" class="button">_(Browse)_</label><input id="files" type="file" style="display:none" accept=".zip" onchange="getZIPfile(event,this.form)"></span>

</div>
&nbsp;
: <input id="uploadbutton" type="button" value="_(Upload)_" onclick="upload(this.form)" disabled><input type="button" value="_(Done)_" class="lock" onclick="done()">
</form>
</div>
