<?php
// xml_on_the_fly.php
// RJM Programming
// June, 2018
// Thanks to The PHP Anthology by Harry Fuecks

  $formnav=false;
  $toptype="";
  $fromthenon="";
  $attributeideas="";
  $attribo=null;

  foreach ($_POST as $name=>$val) {
    $formnav=true;
    if (htmlspecialchars($name) == "element1") {
      $dom=new DOMDocument('1.0'); //domxml_new_doc('1.0');
      $toptype=htmlspecialchars($val);
      $example=$dom->createElement($toptype); //create_element($toptype);
    } else if (htmlspecialchars($name) == "element2") {
      $fromthenon=htmlspecialchars($val);
    } else if (strpos(htmlspecialchars($name), "span") !== false) {
      $attributeideas=str_replace("&gt;","",str_replace(">","",htmlspecialchars($val)));
      if (strpos($attributeideas,"=") !== false) {
        $eqsare=explode("=", $attributeideas);
        for ($ie=1; $ie<sizeof($eqsare); $ie++) {
          if ($fromthenon == "") {
            $attribo = $dom->createAttribute(explode(" ",$eqsare[-1 + $ie])[-1 + sizeof(explode(" ",$eqsare[-1 + $ie]))]); //create_attribute(explode(" ",$eqsare[-1 + $ie])[-1 + sizeof(explode(" ",$eqsare[-1 + $ie]))]);
            if (substr(($eqsare[$ie] . " "),0,1) == "'") {
              $attribo->value = explode("'",substr(($eqsare[$ie] . " "),1))[0];
            } else if (substr(($eqsare[$ie] . " "),0,1) == '"') {
              $attribo->value = explode('"',substr(($eqsare[$ie] . " "),1))[0];
            } else {
              $attribo->value = explode(' ',substr(($eqsare[$ie] . " "),0))[0];
            }
            $example->appendChild($attribo); //append_child($attribo);
          }
        }
        if ($fromthenon == "") $attributeideas="";
      } 
    } else {
      $date=$dom->createElement($fromthenon); //create_element($fromthenon);
      if (strpos($attributeideas,"=") !== false) {
        $eqsare=explode("=", $attributeideas);
        for ($ie=1; $ie<sizeof($eqsare); $ie++) {
          if ($fromthenon != "") {
            $attribo = $dom->createAttribute(explode(" ",$eqsare[-1 + $ie])[-1 + sizeof(explode(" ",$eqsare[-1 + $ie]))]); //create_attribute(explode(" ",$eqsare[-1 + $ie])[-1 + sizeof(explode(" ",$eqsare[-1 + $ie]))]);
            if (substr(($eqsare[$ie] . " "),0,1) == "'") {
              $attribo->value = explode("'",substr(($eqsare[$ie] . " "),1))[0];
            } else if (substr(($eqsare[$ie] . " "),0,1) == '"') {
              $attribo->value = explode('"',substr(($eqsare[$ie] . " "),1))[0];
            } else {
              $attribo->value = explode(' ',substr(($eqsare[$ie] . " "),0))[0];
            }
            $date->appendChild($attribo); //append_child($attribo);
          }
        }
        $attributeideas="";
      } 
      $dateText=$dom->createTextNode(htmlspecialchars($val)); //create_text_node(htmlspecialchars($val));
      $date->appendChild($dateText); //append_child($dateText);
      $example->appendChild($date); //append_child($date);
    }
  }
  
  if ($formnav) {
    $dom->appendChild($example); //append_child($example);
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    header('Content-Type:text/xml');
    //echo $dom->saveXML(); //dump_mem();
    $xml_string = $dom->saveXML(); //dump_mem();
    echo $xml_string;
  } else {
    echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
var nextl=2;
var tr2='';
var xtdo=null;
var ftt=false;
var replacefrom='youwill' + 'neverfindt' + 'his';
var replaceto='';
function atdit(iw) {
  var aid=prompt('Optionally enter space delimited attribute name=value pairings to apply here.  Please use single quotations for (value) string data.','');
  if (aid != null) {
    if (aid.trim() != '') {
      document.getElementById('element' + iw + 'option').value=' ' + aid + ' &gt;';
      document.getElementById('element' + iw + 'option').innerHTML=' ' + aid + ' &gt;';
      document.getElementById('element' + iw + 'span').value=' ' + aid + ' &gt;';
    }
  }
  if (iw > 1 && !ftt) {
  ftt=true;
  //alert('huhhuh');
  //trtwo=document.getElementById('tr2').innerHTML;
  var tdo=document.getElementById('element2');
  document.getElementById('elem2').value=tdo.value;
  replacefrom=document.getElementById('element2td').innerHTML;
  replaceto=tdo.value;
  var divs=document.getElementsByTagName('div');
  for (idivs=0; idivs<divs.length; idivs++) {
   if (divs[idivs].className == 'divn') {
    divs[idivs].innerHTML=tdo.value;
   }
  }
  document.getElementById('element2td').innerHTML=tdo.value;
  }
}
function span_attribute_stuff() {
  var was='', nw=0, mb='_top';
  var spans=document.getElementsByTagName('span');
  for (ispans=0; ispans<spans.length; ispans++) {
   if (spans[ispans].innerHTML.indexOf(' ') == -1) {
    was=spans[ispans].innerHTML;
    nw=eval(spans[ispans].id.replace('element','').replace('span',''));
    spans[ispans].innerHTML='<select id=' + spans[ispans].id + ' name=' + spans[ispans].id + ' onchange=atdit(' + nw + ');><option value=\"&gt;\">&gt;</option><option id=element' + nw + 'option value=>Your' + mb + '_attribute=idea &gt;</option></select>'; 
   }
   mb='';
  }
}
function fixtopthis(tdo) {
  document.getElementById('elementn').innerHTML=tdo.value;
}
function fixthis(tdo) {
  ftt=true;
  document.getElementById('elem2').value=tdo.value;
  var divs=document.getElementsByTagName('div');
  for (idivs=0; idivs<divs.length; idivs++) {
   if (divs[idivs].className == 'divn') {
    divs[idivs].innerHTML=tdo.value;
   }
  }
  document.getElementById('element2td').innerHTML=tdo.value;
  trtwo=document.getElementById('tr2').innerHTML;
}
function postaddthis() {
  addnew(xtdo);
}
function addnew(tdo) {
  var thisl=eval(tdo.id.replace('element','').replace('val',''));
  if (eval(1 + thisl) > nextl) {
  nextl=eval(1 + thisl);
  thisl=2;
  document.getElementById('tr' + nextl).innerHTML=trtwo.replace(replacefrom,replaceto).replace('(' + thisl + ')','(' + nextl + ')').replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl);
  document.getElementById('tr' + nextl).style.display='table-row';
  }
  span_attribute_stuff();
}
function lotsoftr() {
  trtwo=document.getElementById('tr2').innerHTML;
  for (var i=3; i<100; i++) {
    document.getElementById('mytbody').innerHTML+='<tr style=display:none; id=tr' + i + '><td colspan=7></td></tr>';
  }
  span_attribute_stuff();
  trtwo=document.getElementById('tr2').innerHTML;
}
function okd(event) { // thanks to https://stackoverflow.com/questions/15355948/preventing-tab-to-cycle-through-address-bar
  if (event.keyCode == 9 && event.shiftKey == false) {
    document.getElementById('element1').focus();
  }
}
function okdzero(event) { // thanks to https://stackoverflow.com/questions/15355948/preventing-tab-to-cycle-through-address-bar
  if (event.keyCode == 9 && event.shiftKey == true) {
    if (document.getElementById('element' + eval(1 + nextl) + 'val')) {
    document.getElementById('element' + eval(1 + nextl) + 'val').focus();
    } else {
    document.getElementById('element' + nextl + 'val').focus();
    }
  }
}
</script>
</head>
<body style='background-color: yellow;' onload='lotsoftr();'>
<h1>XML on the Fly</h1>
<h3>RJM Programming - June, 2018</h3>
<h3>Thanks to The PHP Anthology (Volume II: Applications) by Harry Fuecks</h3>
<form method=POST action=./xml_on_the_fly.php>
<table id=mytable style='width:100%;background-color:lightblue;'>
<thead id=myhead>
<tr><th colspan=3 align=right>&lt;</th><th><input type=text onkeydown='okdzero(event);' onblur=fixtopthis(this); style='width:100%;' name=element1 id=element1 value=example></input></th><th colspan=3 align=left><span id=element1span>&gt;</span><input type=hidden name=element2 id=elem2 value=date></input></th></tr>
</thead>
<tbody id=mytbody>
<tr id=tr2><td align=right>&lt;</td><td id=element2td><input type=text onblur=fixthis(this); style='width:100%;' id=element2 value=date></input></td><td align=left><span id=element2span>&gt;</span></td><td><input type=text onkeydown='okd(event);' onblur='xtdo=this; setTimeout(postaddthis,2000);' style='width:100%;' name=element2val id=element2val value='" . date('h:ia l jS M Y') . "'></input></td><td align=right>&lt;/</td><td><div class=divn>date</div></td><td align=left>&gt;</td></tr>
</tbody>
<tfoot id=myfoot>
<tr><th colspan=3 align=right>&lt;/</th><th><div id=elementn align=left>example</div></th><th colspan=3 align=left>&gt;</th></tr>
</tfoot>
</table>
<input style='background-color:pink;width:100%;text-align:center;' type=submit value='Create XML'></input>
</form>
</body>
</html>    
";
  }
?>