<?php
// hierarchy_organization.php
// RJM Programming
// September, 2024
// Trying out Python GraphViz package ... thanks to https://graphviz.readthedocs.io/en/stable/

  $abl=['unix','','',''];
  $newabl=['organization-hierarchy','','',''];
  $prefix='';
  $suffix='';
  $yes='n';
  $fmt='pdf';
  
  $pyprefix='';
  $pymidbit='';
  $pysuffix='';
  
function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    return str_replace('.','_',$rma);
}

  if (!isset($_POST['one']) && !isset($_POST['two']) && !isset($_POST['three']) && !isset($_POST['original'])) {
    $yes='y';
    $_POST['one']=$newabl[1];
    $_POST['two']=$newabl[2];
    $_POST['three']=$newabl[3];
  }

  if (isset($_POST['title'])) {
  $newabl[0]=trim(str_replace('+',' ',urldecode($_POST['title'])));
  } else if (!isset($_POST['original'])) {
  $_POST['title']=$newabl[0];
  }

  if (isset($_POST['fmt'])) {
  $fmt=trim(str_replace('+',' ',urldecode($_POST['fmt'])));
  } else if (!isset($_POST['original'])) {
  $_POST['fmt']='pdf';
  }
   
  if (isset($_POST['one']) && isset($_POST['two']) && isset($_POST['three'])) {
  $prefix=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR; //'/tmp/';
  $newabl[1]=str_replace('+',' ',urldecode(str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['one']))));
  $newabl[2]=str_replace('+',' ',urldecode(str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['two']))));
  $newabl[3]=str_replace('+',' ',urldecode(str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['three']))));
  $pycont=file_get_contents('./hierarchy_organization.py');

  if (strpos($pycont, 'u=u') !== false) {
  $pyprefix=explode('u.edge(', $pycont)[0];
  $pysuffix='' . explode('u=u', $pycont)[1];
  //$pysuffix='' . explode('u.view()', $pycont)[1];
  $pycont=$pyprefix . "\n\n" . $pysuffix;
  } else if (strpos($pycont, 'u.view()') !== false) {
  $pyprefix=explode('u.edge(', $pycont)[0];
  $pysuffix='u.view()' . explode('u.view()', $pycont)[1];
  //$pysuffix='' . explode('u.view()', $pycont)[1];
  $pycont=$pyprefix . "\n\n" . $pysuffix;
  }
  
  
  //$xone=str_replace("\t","+", str_replace('~','',   str_replace('+',' ',str_replace('+|','`|',str_replace('+;','`;',str_replace('+;','`;',str_replace('+,','`,',urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['one'])) . urlencode('~'))) ))) )));
  //$xtwo=str_replace("\t","+", str_replace('~','',   str_replace('+',' ',str_replace('+|','`|',str_replace('+;','`;',str_replace('+;','`;',str_replace('+,','`,',urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['two'])) . urlencode('~'))) ))) )));
  //$xthree=str_replace("\t","+", str_replace('~','',   str_replace('+',' ',str_replace('+|','`|',str_replace('+;','`;',str_replace('+;','`;',str_replace('+,','`,',urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['three'])) . urlencode('~'))) ))) )));
  //$xone=str_replace("\t","+", str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['one']))))));
  //$xtwo=str_replace("\t","+", str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['two']))))));
  //$xthree=str_replace("\t","+", str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['three']))))));
  $xone=str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['one'])))));
  $xtwo=str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['two'])))));
  $xthree=str_replace('+',' ',(urldecode(  str_replace("%2b","%0b",str_replace("%2B","%0b",$_POST['three'])))));
  
  $cols=explode('|', $xone . '|' . $xtwo . '|' . $xthree);
  
  // 5th Edition;6th Edition,PWB 1.0|6th Edition;LSX,1 BSD,Mini Unix,Wollongong,Interdata
  for ($ic=0; $ic<sizeof($cols); $ic++) {
    if (trim($cols[$ic]) != '') {
      $withincols=explode(';', $cols[$ic]);
      for ($jc=1; $jc<sizeof($withincols); $jc++) {
       $rightcols=explode(',', $withincols[$jc]);
       for ($kc=0; $kc<sizeof($rightcols); $kc++) {
         $pymidbit.="u.edge('" . str_replace("\t","+", urldecode($withincols[0])) . "','" . str_replace("\t","+", urldecode($rightcols[$kc])) . "') " . "\n"; 
       }
      }
    }
  }
  
  if ($pymidbit != '' && $pysuffix != "") {
     $pycont=str_replace($pysuffix, $pymidbit . $pysuffix, $pycont);
  }
  
  $pycont=str_replace($abl[0], $newabl[0], $pycont);
  if ($fmt != 'pdf') {
     $pycont=str_replace(", filename=", ", format='" . $fmt . "', filename=", $pycont);
  }
  $pycont=str_replace('doctest-output', $prefix . 'doctest-output', $pycont);

  if ($yes != 'y') { // use crontab 
  $suffix='_' . server_remote_addr();
  file_put_contents($prefix . 'hierarchy_organization_' . $suffix . '.py', $pycont);
  //file_put_contents($prefix . 'hierarchy_organization0_' . $suffix . '.py', $pycont);
  //file_put_contents($prefix . 'hierarchy_organization_' . $suffix . '.ksh', "#!/bin/ksh\n/usr/bin/python3 " . $prefix . "hierarchy_organization_" . $suffix . ".py >> " . $prefix . "hierarchy_organization.good 2>> " . $prefix . "hierarchy_organization.bad \nln -s " . $prefix . "doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf " . $_SERVER['DOCUMENT_ROOT'] .  DIRECTORY_SEPARATOR . "doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf \nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".py \nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".ksh \nexit");
  file_put_contents($prefix . 'hierarchy_organization_' . $suffix . '.ksh', "#!/bin/ksh\n/usr/bin/python3 " . $prefix . "hierarchy_organization_" . $suffix . ".py >> " . $prefix . "hierarchy_organization.good 2>> " . $prefix . "hierarchy_organization.bad \nsleep 5\n\n/usr/bin/python3 " . $prefix . "hierarchy_organization_" . $suffix . ".py >> " . $prefix . "hierarchy_organization.good 2>> " . $prefix . "hierarchy_organization.bad \n\nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".py \nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".ksh \nexit");
  //file_put_contents($prefix . 'hierarchy_organization0_' . $suffix . '.ksh', "#!/bin/ksh\n/usr/bin/python3 " . $prefix . "hierarchy_organization_" . $suffix . ".py >> " . $prefix . "hierarchy_organization.good 2>> " . $prefix . "hierarchy_organization.bad \nsleep(5)\n\n/usr/bin/python3 " . $prefix . "hierarchy_organization_" . $suffix . ".py >> " . $prefix . "hierarchy_organization.good 2>> " . $prefix . "hierarchy_organization.bad \n\n\nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".py \nrm -f " . $prefix . "hierarchy_organization_" . $suffix . ".ksh \nexit");
  exec('chmod 777 ' . $prefix . 'hierarchy_organization_' . $suffix . '.ksh');
  exec('chown root ' . $prefix . 'hierarchy_organization_' . $suffix . '.ksh');
  exec('chgrp root ' . $prefix . 'hierarchy_organization_' . $suffix . '.ksh');
  while (file_exists($prefix . 'hierarchy_organization_' . $suffix . '.ksh')) {
    sleep(10);
  }
  } else {
  file_put_contents('/tmp/hierarchy_organization.py', $pycont);
  exec('chmod 777 ' . '/tmp/hierarchy_organization.py');
  exec('chown rjmprogr ' .'/tmp/hierarchy_organization.py');
  exec('chgrp rjmprogr ' . '/tmp/hierarchy_organization.py');
  //if (file_exists('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf')) {
    //exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
    //exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
    //unlink('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
    //unlink('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
  //}
  exec('/usr/bin/python3 /tmp/hierarchy_organization.py > hierarchy_organization.good 2> hierarchy_organization.bad');
  sleep(8);
  //if (file_exists($prefix . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf')) {
  //exec('ln -s ' . $prefix . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf ' . $_SERVER['DOCUMENT_ROOT'] .  DIRECTORY_SEPARATOR . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
  //}
  }
  } else {
  exec('/usr/bin/python3 hierarchy_organization.py');
  sleep(8);
  }
    
  if (file_exists($prefix . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.' . $fmt)) {
     exec('chmod 777 ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.' . $fmt);
     exec('chmod 777 ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     exec('chown rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.' . $fmt);
     exec('chown rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     exec('chgrp rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.' . $fmt);
     exec('chgrp rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     echo "<html>
<head>
<title>Trying out Python GraphViz hosted in PHP - RJM Programming - September, 2024 - Organization Hierarchy</title>
<scr" . "ipt type=text/javascript>
 function ph() {
   var aplace='';
   // one
   aplace='5th Edition;6th Edition,PWB 1.0|6th Edition;LSX,1 BSD,Mini Unix,Wollongong,Interdata|Interdata;Unix/TS 3.0,PWB 2.0,7th Edition';
   document.getElementById('one').placeholder=aplace;
   // two
   aplace='7th Edition;8th Edition,32V,V7M,Ultrix-11,Xenix,UniPlus+|V7M;Ultrix-11';
   document.getElementById('two').placeholder=aplace;
   aplace='';
   // three
   aplace='8th Edition;9th Edition|1 BSD;2 BSD|2 BSD;2.8 BSD|2.8 BSD;Ultrix-11,2.9 BSD|32V;3 BSD|3 BSD;4 BSD|4 BSD;4.1 BSD|4.1 BSD;4.2 BSD|4.2 BSD;4.3 BSD,Ultrix-32|PWB 1.0;PWB 1.2,USG 1.0|PWB 1.2;PWB 2.0|USG 1.0;CB Unix 1,USG 2.0|CB Unix 1;CB Unix 2|CB Unix 2;CB Unix 3|CB Unix 3;Unix/TS++,PDP-11 Sys V|USG 2.0;USG 3.0|USG 3.0;Unix/TS 3.0|PWB 2.0;Unix/TS 3.0|Unix/TS 1.0;Unix/TS 3.0|Unix/TS 3.0;TS 4.0|Unix/TS++;TS 4.0|CB Unix 3;TS 4.0|TS 4.0;System V.0|System V.0;System V.2|System V.2;System V.3';
   document.getElementById('three').placeholder=aplace;
   
   var atitle=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('title=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('title=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (atitle != '') { document.getElementById('ititle').value=atitle;   }
   var aone=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('one=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('one=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (aone != '') { while (aone.indexOf(String.fromCharCode(9)) != -1) { aone=aone.replace(String.fromCharCode(9),'+');  } document.getElementById('one').value=aone;   document.getElementById('none').value=aone;   }
   var atwo=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('two=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('two=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (atwo != '') {  while (atwo.indexOf(String.fromCharCode(9)) != -1) { atwo=atwo.replace(String.fromCharCode(9),'+');  } document.getElementById('two').value=atwo;   document.getElementById('ntwo').value=atwo;   }
   var athree=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('three=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('three=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (athree != '') { while (athree.indexOf(String.fromCharCode(9)) != -1) { athree=athree.replace(String.fromCharCode(9),'+');  }  document.getElementById('three').value=athree;   document.getElementById('nthree').value=athree;   }
   var afmt=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('fmt=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('fmt=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : 'svg';
   if (afmt != '' && document.getElementById('one').value != '') { document.getElementById('fmt').value=afmt;   }
   if (('' + location.hash).indexOf('title=') != -1 && atitle.trim() != '' && aone.trim() != '') {
     document.getElementById('mysub').click();
   }
  }
  
  function phv(tao) {
    if (tao.value == '') {
      tao.value=tao.placeholder;
    }
  }
  
  function various(indi) {
    var outdi=indi;
    outdi=outdi.replace(/\|\;/g, encodeURIComponent('|') + ';');
    outdi=outdi.replace(/\,\;/g, encodeURIComponent(',') + ';');
    outdi=outdi.replace(/\;\;/g, encodeURIComponent(';') + ';');

    outdi=outdi.replace(/\|\,/g, encodeURIComponent('|') + ',');
    outdi=outdi.replace(/\,\,/g, encodeURIComponent(',') + ',');
    outdi=outdi.replace(/\;\,/g, encodeURIComponent(';') + ',');

    outdi=outdi.replace(/\|\|/g, encodeURIComponent('|') + '|');
    outdi=outdi.replace(/\,\|/g, encodeURIComponent(',') + '|');
    outdi=outdi.replace(/\;\|/g, encodeURIComponent(';') + '|');

    return outdi;
  }
  
  function contentfix(inid) {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    //document.getElementById('one').value=document.getElementById('one').value.replace(/\+/g,'`');
    //document.getElementById('two').value=document.getElementById('two').value.replace(/\+/g,'`');
    //document.getElementById('three').value=document.getElementById('three').value.replace(/\+/g,'`');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
    return inid;
  }


function emailit(inais) {
   event.stopPropagation();
   var em=null;
   em=prompt('Please enter email address to send output ' + document.getElementById('fmt').value + ' URL link to.', '');
   if (em == null) { em=''; }
   if (em.indexOf('@') != -1) {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
     em+='?subject=Hierarchy%20Organization&body=' + encodeURIComponent( (document.URL.split('?')[0].split('#')[0] + '?rand=' + Math.floor(Math.random() * 19897865) + '#title=' + encodeURIComponent(document.getElementById('ititle').value) + '#fmt=' + encodeURIComponent(document.getElementById('fmt').value) + '#one=' + encodeURIComponent(document.getElementById('none').value) + '#two=' + encodeURIComponent(document.getElementById('ntwo').value) + '#three=' + encodeURIComponent(document.getElementById('nthree').value)).replace('#fmt=svg#one=#','#fmt=pdf#one=#')   );
     document.getElementById('dimap').innerHTML=\"<a style=display:none; id=theaemail target=_blank href='mailto:\" + em + \"'>Email</a>\";
     document.getElementById('theaemail').click();
   } 
   return false;
}

function smsit(inais) {
   event.stopPropagation();
   var em=null;
   em=prompt('Please enter SMS number to send output ' + document.getElementById('fmt').value + ' URL link to.', '');
   if (em == null) { em=''; }
   if (em.trim() != '') {
     if (em.trim() != '') {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
     em+='&body=' + encodeURIComponent( (document.URL.split('?')[0].split('#')[0] + '?rand=' + Math.floor(Math.random() * 19897865) + '#title=' + encodeURIComponent(document.getElementById('ititle').value) + '#fmt=' + encodeURIComponent(document.getElementById('fmt').value) + '#one=' + encodeURIComponent(document.getElementById('none').value) + '#two=' + encodeURIComponent(document.getElementById('ntwo').value) + '#three=' + encodeURIComponent(document.getElementById('nthree').value)).replace('#fmt=svg#one=#','#fmt=pdf#one=#')    );
     document.getElementById('dimap').innerHTML=\"<a style=display:none; id=theasms target=_blank href='sms:\" + em + \"'>SMS</a>\";
     if (!document.getElementById('theasms') && document.getElementById('psms')) {
     document.getElementById('psms').click();
     } else {
     document.getElementById('theasms').click();
     }
     }
   }
   return false;
}


</scr" . "ipt>
<style>
  textarea { 
     width: 99%;
     height: 120px;
  }
</style>
</head>
<body onload=ph();>
<h1>Trying out Python GraphViz hosted in PHP - Organization Hierarchy  <a id=pemail title=Email class=share  onclick=emailit(this); style=text-decoration:none;cursor:pointer;>&#128231;</a>&nbsp;<a id=psms title=SMS class=share  onclick=smsit(this); style=text-decoration:none;cursor:pointer;>&#128223;</a></h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank title='Python GraphViz package information' href='//graphviz.readthedocs.io/en/stable/examples.html'>https://graphviz.readthedocs.io/en/stable/examples.html</a></h4>
<table style=width:98%;><tr><td>
<iframe style='width:82%;height:500px;' type=application/" . $fmt . " src='/doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv." . $fmt . "?rand=" . rand(13456,6789056) . "'></iframe>
</td><td style=background-color:#f0f0f0;width:50%;>
<form onsubmit=\"if (document.getElementById('ititle').value.replace('organization-hierarchy','').trim() == '') { alert('Please enter a different title, then click/tap Draw again.');  return false; } document.body.style.cursor=contentfix('progress'); return true;\" method=POST action=./hierarchy_organization.php>
<p>Title ...</p><br>
<input type=text id=ititle name=title placeholder='" . $newabl[0] . "' value=''></input>&nbsp;<select id=fmt name=fmt><option value=svg>SVG</option><option value=pdf>PDF</option></select><br><br>
<p>Hierarchical Relationship Names ...</p><br>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=none name=one value=''>" . str_replace("\t","+",urldecode($newabl[1])) . "</textarea>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=ntwo name=two value=''>" . str_replace("\t","+",urldecode($newabl[2])) . "</textarea>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=nthree name=three value=''>" . str_replace("\t","+",urldecode($newabl[3])) . "</textarea>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-name=one id=one data-contenteditable=true value=''>" . str_replace("\t","+",urldecode($newabl[1])) . "</textarea><br>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-name=two id=two data-contenteditable=true value=''>" . str_replace("\t","+",urldecode($newabl[2])) . "</textarea><br>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-name=three id=three data-contenteditable=true value=''>" . str_replace("\t","+",urldecode($newabl[3])) . "</textarea><br><br>
<input id=mysub type=submit value=Draw style=background-color:pink;></input>
</form>
</td></tr></table>
<div id=dimap></div>
</body>
</html>";
  } else {
     echo "<html>
<head>
<title>Trying out Python GraphViz hosted in PHP - RJM Programming - September, 2024 - Organization Hierarchy</title>
<scr" . "ipt type=text/javascript>
 function ph() {
   var aplace='';
   // one
   aplace='5th Edition;6th Edition,PWB 1.0|6th Edition;LSX,1 BSD,Mini Unix,Wollongong,Interdata|Interdata;Unix/TS 3.0,PWB 2.0,7th Edition';
   document.getElementById('one').placeholder=aplace;
   // two
   aplace='7th Edition;8th Edition,32V,V7M,Ultrix-11,Xenix,UniPlus+|V7M;Ultrix-11';
   document.getElementById('two').placeholder=aplace;
   aplace='';
   // three
   aplace='8th Edition;9th Edition|1 BSD;2 BSD|2 BSD;2.8 BSD|2.8 BSD;Ultrix-11,2.9 BSD|32V;3 BSD|3 BSD;4 BSD|4 BSD;4.1 BSD|4.1 BSD;4.2 BSD|4.2 BSD;4.3 BSD,Ultrix-32|PWB 1.0;PWB 1.2,USG 1.0|PWB 1.2;PWB 2.0|USG 1.0;CB Unix 1,USG 2.0|CB Unix 1;CB Unix 2|CB Unix 2;CB Unix 3|CB Unix 3;Unix/TS++,PDP-11 Sys V|USG 2.0;USG 3.0|USG 3.0;Unix/TS 3.0|PWB 2.0;Unix/TS 3.0|Unix/TS 1.0;Unix/TS 3.0|Unix/TS 3.0;TS 4.0|Unix/TS++;TS 4.0|CB Unix 3;TS 4.0|TS 4.0;System V.0|System V.0;System V.2|System V.2;System V.3';
   document.getElementById('three').placeholder=aplace;
   
   var atitle=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('title=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('title=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (atitle != '') { document.getElementById('ititle').value=atitle;   }
   var aone=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('one=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('one=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (aone != '') { while (aone.indexOf(String.fromCharCode(9)) != -1) { aone=aone.replace(String.fromCharCode(9),'+');  } document.getElementById('one').value=aone;   document.getElementById('none').value=aone;   }
   var atwo=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('two=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('two=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (atwo != '') {  while (atwo.indexOf(String.fromCharCode(9)) != -1) { atwo=atwo.replace(String.fromCharCode(9),'+');  } document.getElementById('two').value=atwo;   document.getElementById('ntwo').value=atwo;   }
   var athree=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('three=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('three=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : '';
   if (athree != '') { while (athree.indexOf(String.fromCharCode(9)) != -1) { athree=athree.replace(String.fromCharCode(9),'+');  }  document.getElementById('three').value=athree;   document.getElementById('nthree').value=athree;   }
   var afmt=(location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('fmt=')[1] ? (decodeURIComponent((location.search + ('' + location.hash).replace(/^null/g,'').replace(/^undefined/g,'').replace(/^\#/g,'&')).split('fmt=')[1].split('&')[0].split('#')[0].split(encodeURIComponent('#'))[0])) : 'svg';
   if (afmt != '' && document.getElementById('one').value != '') { document.getElementById('fmt').value=afmt;   }
   if (('' + location.hash).indexOf('title=') != -1 && atitle.trim() != '' && aone.trim() != '') {
     document.getElementById('mysub').click();
   }
  }
  
  function phv(tao) {
    if (tao.value == '') {
      tao.value=tao.placeholder;
    }
  }
  
  function contentfix(inid) {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
    return inid;
  }
  
  function various(indi) {
    var outdi=indi;
    outdi=outdi.replace(/\|\;/g, encodeURIComponent('|') + ';');
    outdi=outdi.replace(/\,\;/g, encodeURIComponent(',') + ';');
    outdi=outdi.replace(/\;\;/g, encodeURIComponent(';') + ';');

    outdi=outdi.replace(/\|\,/g, encodeURIComponent('|') + ',');
    outdi=outdi.replace(/\,\,/g, encodeURIComponent(',') + ',');
    outdi=outdi.replace(/\;\,/g, encodeURIComponent(';') + ',');

    outdi=outdi.replace(/\|\|/g, encodeURIComponent('|') + '|');
    outdi=outdi.replace(/\,\|/g, encodeURIComponent(',') + '|');
    outdi=outdi.replace(/\;\|/g, encodeURIComponent(';') + '|');

    return outdi;
  }

function emailit(inais) {
   event.stopPropagation();
   var em=null;
   em=prompt('Please enter email address to send output ' + document.getElementById('fmt').value + ' URL link to.', '');
   if (em == null) { em=''; }
   if (em.indexOf('@') != -1) {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
     em+='?subject=Hierarchy%20Organization&body=' + encodeURIComponent( (document.URL.split('?')[0].split('#')[0] + '?rand=' + Math.floor(Math.random() * 19897865) + '#title=' + encodeURIComponent(document.getElementById('ititle').value) + '#fmt=' + encodeURIComponent(document.getElementById('fmt').value) + '#one=' + encodeURIComponent(document.getElementById('none').value) + '#two=' + encodeURIComponent(document.getElementById('ntwo').value) + '#three=' + encodeURIComponent(document.getElementById('nthree').value)).replace('#fmt=svg#one=#','#fmt=pdf#one=#')    );
     document.getElementById('dimap').innerHTML=\"<a style=display:none; id=theaemail target=_blank href='mailto:\" + em + \"'>Email</a>\";
     document.getElementById('theaemail').click();
   } 
   return false;
}

function smsit(inais) {
   event.stopPropagation();
   var em=null;
   em=prompt('Please enter SMS number to send output ' + document.getElementById('fmt').value + ' URL link to.', '');
   if (em == null) { em=''; }
   if (em.trim() != '') {
     if (em.trim() != '') {
    document.getElementById('ititle').value=document.getElementById('ititle').value.replace(/\+/g,'-').replace(/\=/g,'-').replace(/\ /g,'-');
    document.getElementById('none').value=various(document.getElementById('one').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('ntwo').value=various(document.getElementById('two').value.replace(/\+/g,String.fromCharCode(9)));
    document.getElementById('nthree').value=various(document.getElementById('three').value.replace(/\+/g,String.fromCharCode(9)));
     em+='&body=' + encodeURIComponent( (document.URL.split('?')[0].split('#')[0] + '?rand=' + Math.floor(Math.random() * 19897865) + '#title=' + encodeURIComponent(document.getElementById('ititle').value) + '#fmt=' + encodeURIComponent(document.getElementById('fmt').value) + '#one=' + encodeURIComponent(document.getElementById('none').value) + '#two=' + encodeURIComponent(document.getElementById('ntwo').value) + '#three=' + encodeURIComponent(document.getElementById('nthree').value)).replace('#fmt=svg#one=#','#fmt=pdf#one=#')    );
     document.getElementById('dimap').innerHTML=\"<a style=display:none; id=theasms target=_blank href='sms:\" + em + \"'>SMS</a>\";
     if (!document.getElementById('theasms') && document.getElementById('psms')) {
     document.getElementById('psms').click();
     } else {
     document.getElementById('theasms').click();
     }
     }
   }
   return false;
}

</scr" . "ipt>
<style>
  textarea { 
     width: 99%;
     height: 120px;
  }
</style>
</head>
<body onload=ph();>
<h1>Trying out Python GraphViz hosted in PHP - Organization Hierarchy <a id=pemail title=Email class=share  onclick=emailit(this); style=text-decoration:none;cursor:pointer;>&#128231;</a>&nbsp;<a id=psms title=SMS class=share  onclick=smsit(this); style=text-decoration:none;cursor:pointer;>&#128223;</a></h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank title='Python GraphViz package information' href='//graphviz.readthedocs.io/en/stable/examples.html'>https://graphviz.readthedocs.io/en/stable/examples.html</a></h4>
<table style=width:98%;><tr><td>
<iframe style='width:82%;height:500px;' type=application/" . $fmt . " src='/doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv." . $fmt . "?rand=" . rand(13456,6789056) . "'></iframe>
</td><td style=background-color:#f0f0f0;width:50%;>
<form onsubmit=\"if (document.getElementById('ititle').value.replace('organization-hierarchy','').trim() == '') { alert('Please enter a different title, then click/tap Draw again.');  return false; } document.body.style.cursor=contentfix('progress');  return true;\" method=POST action=./hierarchy_organization.php>
<p>Title ...</p><br>
<input type=text id=ititle name=title placeholder='" . $newabl[0] . " value=''></input>&nbsp;<select id=fmt name=fmt><option value=svg>SVG</option><option value=pdf>PDF</option></select><br><br>

<p>Hierarchical Relationship Names ...</p><br>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=none name=one value=''>" . str_replace("\t","+",urldecode($newabl[1])) . "</textarea>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=ntwo name=two value=''>" . str_replace("\t","+",urldecode($newabl[2])) . "</textarea>
<textarea style=display:none; ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=nthree name=three value=''>" . str_replace("\t","+",urldecode($newabl[3])) . "</textarea>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=one data-name=one value=''>" . str_replace("\t","+",urldecode($newabl[1])) . "</textarea><br>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=two data-name=two value=''>" . str_replace("\t","+",urldecode($newabl[2])) . "</textarea><br>
<textarea ondblclick=phv(this); title='Double click makes suggestion the real value' placeholder='' data-contenteditable=true id=three data-name=three value=''>" . str_replace("\t","+",urldecode($newabl[3])) . "</textarea><br><br>
<input id=mysub type=submit value=Draw style=background-color:pink;></input>
</form>
</td></tr></table>
<div id=dimap></div>
</body>
</html>";
  }

?>