Menu="Flash"
Title="Syslinux Configuration"
Tag="edit"
Cond="file_exists('/boot/syslinux/syslinux.cfg')"
---
<?PHP
/* Copyright 2005-2023, Lime Technology
 * Copyright 2012-2023, 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.
 */
?>
<?
function strip($area) {
  return preg_replace(["/^|(\n)  /","/\n$/"],["$1",""],$area);
}
$file    = '/boot/syslinux/syslinux.cfg';
$current = @file_get_contents($file);
$default = @file_get_contents("$file-") ?: $current;
$current = preg_replace(["/\r\n/","/\r/","/\n$/"],["\n","\n",""],$current);
$default = preg_replace(["/\r\n/","/\r/","/\n$/"],["\n","\n",""],$default);

$title   = _('Global Configuration');
$menu    = 'menu default';
$mark    = 'label ';
?>
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.switchbutton.css')?>">

<script src="<?autov('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script>
const title = '<?=$title?>';
const menu = '<?=$menu?>';
const mark = '<?=$mark?>';

Array.prototype.indent = function(o) {
  if (o) for (var i=0; i < this.length; i++) this[i] = '  '+this[i];
  return this;
};
Array.prototype.spliceArray = function(i,n,a) {
  return Array.prototype.splice.apply(this,[i,n].concat(a));
};
function prepareMenu(form) {
  $('input[name="#arg[1]"]').val(form.boot.checked?1:0);
  if ($('div.basic').is(':visible')) {
    var label = [], area = [];
    $(form).find('label[id^=label]').each(function(){
      var $label = $(this).clone();
      $label.find('input').remove();
      var labelText = $label.text().replace(/\s+/g, ' ').trim();
      label.push(labelText);
    });
    $(form).find('textarea.menu').each(function(i){
      var start = $('#input-'+i).prop('checked') ? menu+'\n' : '';
      area.push(start+$(this).val());
    });
    var text = [];
    for (var i=0; i < label.length; i++) {
      if (i==0) {
        text.push(area[i]);
      } else {
        text.push(mark+label[i]);
        text.push(area[i].replace(/^|(\n)/g,'$1  '));
      }
    }
    // menu view
    form.text.value = text.join('\n')+'\n';
  } else {
    // raw view
    form.text.value = form.raw.value+'\n';
  }
  form.raw.disabled = true;
}
function setDefault(form) {
  var text = <?=json_encode(array_map('strip',explode($mark,$default)))?>;
  $(form).find('textarea.menu').each(function(i){
    if (i < text.length) {
      var area = text[i].split('\n');
      var label = (i) ? area.shift():title;
      var start = (area[0]==menu);
      var checked = start ? ' checked':'';
      if (i) label += "<span style='float:right'><input type='radio' id='input-"+i+"' title='<?=_('Set default boot menu')?>' onchange='changeMenu(this.form,this.id,true)'"+checked+"></span>";
      $('#label-'+i).html(label).prop('class',start ? 'array':'system');
      if (start) area.shift();
      $(this).val(area.join('\n')).prop('rows',area.length).trigger('change');
    } else {
      $('#label-'+i).remove();
      $(this).remove();
    }
  });
  $(form).find('textarea.raw').val(<?=json_encode($default)?>).prop('rows',$(this).val().match(/\n/g).length+1).trigger('change');
}
function changeMenu(form,id,update) {
  $(form).find('input.menu').each(function(){
    // highlight default boot menu
    var i = $(this).prop('id');
    var label = $('#'+i.replace('input','label'));
    if (i == id) {
      label.prop('class','array');
      $(this).prop('checked',true);
    } else {
      label.prop('class','system');
      $(this).prop('checked',false);
    }
  });
  if (update) {
    // menu view -> update raw view
    var n = 0, o = null;
    var x = id.split('-')[1];
    var text = form.raw.value.split('\n');
    for (var i=0; i < text.length; i++) {
      if (text[i].indexOf(mark) >= 0) if (++n == x) o = i + 1;
      if (text[i].indexOf(menu) >= 0) text.splice(i,1);
    }
    if (o) text.splice(o,0,'  '+menu);
    $(form).find('textarea.raw').val(text.join('\n')).prop('rows',text.length);
  }
}
$(function(){
  $('form').find('textarea').each(function(){$(this).on('input change',function(event){
    $(this).prop('rows',($(this).val().match(/\n/g)||[]).length+1);
    if (event.type == 'input') return;
    // propogate changes to 'other' view mode
    var form = $(this).closest('form');
    if ($(this).prop('class')=='menu') {
      // menu view -> update raw view
      var n = 0, o = 0, x = null;
      var id = $(this).prop('id').split('-')[1];
      var area = $(this).val().split('\n');
      var raw = form.find('textarea.raw');
      var text = raw.val().split('\n');
      for (var i=0; i < text.length; i++) {
        if (text[i].indexOf(mark) >= 0) {
          if (n++ == id) x = i; else o = i + 1;
        }
        if (text[i].indexOf(menu) >= 0) o++;
        if (x) break;
      }
      text.spliceArray(o,(x||text.length)-o,area.indent(o));
      raw.val(text.join('\n')).prop('rows',text.length);
    } else {
      // raw view -> update menu view
      var n = 0, id = null, area = [];
      var text = $(this).val().split('\n');
      for (var i=0; i < text.length; i++) {
        if (text[i].indexOf(mark) >= 0) {
          $('#menu-'+(n++)).val(area.join('\n')).prop('rows',area.length);
          var label = $('#label-'+n);
          label.html(label.html().replace(/^.*(<span.*)/,text[i].replace(mark,'')+'$1'));
          area = [];
        } else {
          if (text[i].indexOf(menu) >= 0) id = 'input-'+n; else if (text[i].length) area.push(text[i].replace(/^  /,''));
        }
      }
      $('#menu-'+n).val(area.join('\n')).prop('rows',area.length);
      if (id) changeMenu(form,id,false);
    }
  });});
  if ($.cookie('syslinux_viewmode')=='advanced') {
    $('.advanced').show();
    $('.basic').hide();
  }
  $('.advancedview').switchButton({
    labels_placement: 'left',
    off_label: "_(Menu View)_",
    on_label: "_(Raw View)_",
    checked: $.cookie('syslinux_viewmode')=='advanced'
  });
  $('.advancedview').change(function() {
    $('.advanced').toggle('slow');
    $('.basic').toggle('slow');
    $.cookie('syslinux_viewmode', $('.advancedview').is(':checked') ? 'advanced':'basic', {expires:3650});
  });
});
</script>

&nbsp;
: <span><input type="checkbox" class="advancedview"></span>

:syslinux_cfg_help:

<form markdown="1" method="POST" action="/update.php" target="progressFrame" onsubmit="prepareMenu(this)">
<input type="hidden" name="#include" value="/webGui/include/update.file.php">
<input type="hidden" name="#file" value="<?=$file;?>">
<input type="hidden" name="#command" value="/webGui/scripts/bootmode">
<input type="hidden" name="#arg[1]" value="">
<input type="hidden" name="text" value="">

<div markdown="1" class="basic">
_(Syslinux configuration)_:
: <?$i=0;
  foreach (array_map('strip',explode($mark,$current)) as $area):
  $area = explode("\n", $area);
  $label = ($i) ? array_shift($area):$title;
  $start = in_array($menu,$area);
  if ($start) unset($area[array_search($menu,$area)]);
?><span class="config-item flex flex-col">
    <span class="<?= $start ? 'array' : 'system' ?> config-item-label-wrapper flex flex-row justify-between">
      <label id="label-<?=$i?>">
        <?if ($i):?><input type="radio" id="input-<?=$i?>" class="menu" <?=$start?'checked':''?> title="_(Set default boot menu)_" onchange="changeMenu(this.form,this.id,true)"><?endif;?>
        <?=htmlspecialchars($label)?>
      </label>
    </span>
    <textarea class="menu" id="menu-<?=$i++?>" spellcheck="false" cols="80" rows="<?=count($area)?>" maxlength="2048"><?=htmlspecialchars(implode("\n",$area))?></textarea>
  </span>
  <?endforeach;?>

</div>
<div markdown="1" class="advanced">
_(Syslinux configuration)_:
: <textarea class="raw config-raw" name="raw" spellcheck="false" cols="80" rows="<?=substr_count($current,"\n")+1?>" maxlength="2048"><?=htmlspecialchars($current)?></textarea>

</div>
_(Server boot mode)_:
: <?=is_dir('/sys/firmware/efi') ? 'UEFI' : 'Legacy'?>

_(Boot system in UEFI mode)_:
: <label>
    <input type="checkbox" name="boot" <?=is_dir('/boot/EFI')?'checked':''?>>
    _(Permit UEFI boot mode)_
  </label>
  *_(Please check your system settings to support UEFI boot mode)_.*

<input type="button" value="_(Default)_" onclick="setDefault(this.form)">
: <span class="inline-block">
    <input type="submit" value="_(Apply)_">
    <input type="button" value="_(Done)_" onclick="done()">
  </span>

:syslinux_button_help:

</form>
