<?php
// rename_to_aid_sorting.php
// RJM Programming
// April, 2018

$putthisback="";
$dprefix="./";
$candidate=false;
if (isset($_GET['dirone']) && isset($_GET['dirtwo'])) {
  $dprefix=urldecode($_GET['dirone']) . urldecode($_GET['dirtwo']);
  if (substr($dprefix, -1) != "/" && substr($dprefix, -1) != "\\") {
    if (strpos($dprefix, "/") !== false) {
      $putthisback=explode("/",$dprefix)[-1 + sizeof(explode("/",$dprefix))];
    } else if (strpos($dprefix, "\\") !== false) {
      $putthisback=explode("\\",$dprefix)[-1 + sizeof(explode("\\",$dprefix))];
    }
  }
} else if (isset($_POST['dirone']) && isset($_POST['dirtwo'])) {
  $dprefix=urldecode($_POST['dirone']) . urldecode($_POST['dirtwo']);
  if (substr($dprefix, -1) != "/" && substr($dprefix, -1) != "\\") {
    if (strpos($dprefix, "/") !== false) {
      $putthisback=explode("/",$dprefix)[-1 + sizeof(explode("/",$dprefix))];
    } else if (strpos($dprefix, "\\") !== false) {
      $putthisback=explode("\\",$dprefix)[-1 + sizeof(explode("\\",$dprefix))];
    }
  }
}

$cnt=0;
$recnt=0;
$extarr=[];
$extarrlongestnumerical=[];

$changesfrom=[];
$changesto=[];
$fileis=[];
$categoryis=[];
$numcis=[];

$extlist=",";
$ext_nl_list=",";
$ext_nf_list=",";
$ext_dl_list=",";
$ext_fo_list=",";

$asteriskybit="[0123456789]*.*";
//$asteriskybit="[0123456789]*_[0123456789]*.png";
if (isset($_GET['asteriskybit'])) {
  $asteriskybit=urldecode($_GET['asteriskybit']);
} else if (isset($_POST['asteriskybit'])) {
  $asteriskybit=urldecode($_POST['asteriskybit']);
}


$htmlis="<!doctype html><head><title>Rename to Aid Sorting Candidates - RJM Programming - April, 2018</title></head>
<body style=background-color:yellow;>
<h1>Rename to Aid Sorting Candidates</h1>
<h3>RJM Programming - April, 2018</h3>
<table cellpadding=5 cellspacing=5 border=5><tr><td>
<table cellpadding=5 cellspacing=5 id='mytable' border=20>
<tbody id='mytbody'>
<tr style=background-color:#d0d0d0;><th>Existing Filename <br>in " . $dprefix . " <br>satisfying " . $asteriskybit . "</th><th>Could Rename to Aid <br>With Numerical File Sorts</th><th>Make That Happen?</th></tr>
</tbody>
</table></td><td style=background-color:#f0f0f0;vertical-align:top;>
<form method=GET action=./rename_to_aid_sorting.php>
Relative to This Program Directory: <input type=text name=dirone id=dirone value='./'></input><br><br>
Filename Prefix (that File Filter does not apply to): <input type=text name=dirtwo id=dirtwo value=''></input><br><br>
File Filter: <input type=text name=asteriskybit id=asteriskybit value='[0123456789]*.*'></input><br><br>
<input style=background-color:pink; type=submit value=Analyze></input>
</form>
</td></tr></table>
</body>
</html>
";

foreach (glob($dprefix . $asteriskybit) as $filename) {
  //echo $filename . "\n";
  //$htmlis=str_replace("</tbody>","\n<tr><td id=c1_" . $cnt . ">" . str_replace($dprefix, "", $filename) . "</td><td id=c2_" . $cnt . "></td><td id=c3_" . $cnt . "></td></tr>\n</tbody>",$htmlis);
  $thisext="." . explode(".", str_replace($dprefix, "", $filename))[-1 + sizeof(explode(".", str_replace($dprefix, "", $filename)))];
  $nfields=1;
  $ncnt=0;
  $delimis=".";
  while (substr(str_replace($dprefix, "", $filename),$ncnt,1) >= '0' && substr(str_replace($dprefix, "", $filename),$ncnt,1) <= '9') {
   $ncnt++;
  }
  if (substr(str_replace($dprefix, "", $filename),$ncnt,1) != '.') {
    $delimis=substr(str_replace($dprefix, "", $filename),$ncnt,1);
    $nfs=explode(substr(str_replace($dprefix, "", $filename),$ncnt,1), str_replace($dprefix, "", $filename));
    $nfields=sizeof($nfs);
  }
    $fileis[sizeof($fileis)]=str_replace($dprefix, "", $filename);
    $categoryis[sizeof($categoryis)]=$thisext . "." . $nfields . ";" . $delimis;
    $numcis[sizeof($numcis)]=$ncnt;
  $extarrlongestnumerical[sizeof($extarrlongestnumerical)]=$ncnt;
  $extarr[sizeof($extarr)]=$thisext;
  if (strpos($extlist, "," . $thisext . "." . $nfields . ";" . $delimis . ",") === false) {
    //echo str_replace($dprefix, "", $filename) . " ... " . $thisext . "." . $nfields . ";" . $delimis;
    //exit;
    $extlist.=$thisext . "." . $nfields . ";" . $delimis . ",";
    $ext_nl_list.="" . $ncnt . ",";     // count of numbers before delimiter happens
    $ext_nf_list.="" . $nfields . ",";  // count of fields of delimiter below
    $ext_dl_list.="" . $delimis . ",";  // delimiter
    $ext_fo_list.="" . str_replace($dprefix, "", $filename) . ",";  // first filename example
    //$fileis[sizeof($fileis)]=str_replace($dprefix, "", $filename);
    //$categoryis[sizeof($categoryis)]=$thisext . "." . $nfields . ";" . $delimis;
    //$numcis[sizeof($numcis)]=$ncnt;
  } else {
    $xxsl=sizeof(explode(",", explode("," . $thisext . "." . $nfields . ";" . $delimis . ",", $extlist)[0]));
    $vsx=explode(",", $ext_nl_list);
    if ($vsx[$xxsl] != $ncnt) {
    //$fileis[sizeof($fileis)]=str_replace($dprefix, "", $filename);
    //$categoryis[sizeof($categoryis)]=$thisext . "." . $nfields . ";" . $delimis;
    //$numcis[sizeof($numcis)]=$ncnt;
      if ($ncnt < $vsx[$xxsl]) {
        $changesfrom[sizeof($changesfrom)]=str_replace($dprefix, "", $filename);
        $prefix="";
        while ($ncnt < $vsx[$xxsl]) {
          $prefix.="0";
          $ncnt++;
        }
        $changesto[sizeof($changesto)]=$prefix . str_replace($dprefix, "", $filename);
      //echo "0 " . $prefix . str_replace($dprefix, "", $filename) . $xxsl . "<br>" . explode(",",$ext_fo_list)[$xxsl] . "<br>" . $thisext . "." . $nfields . ";" . $delimis . "<br>" . str_replace($dprefix, "", $filename) . " \$vsx[\$xxsl]=" . $vsx[$xxsl] . " and \$ncnt=" . $ncnt . " ... " . $ext_fo_list;
      // //$ext_nl_list[$xxsl].=";" . $nfields;
      //exit;
      } else {
        $recon=$vsx[0];
        for ($ivi=1; $ivi<sizeof($vsx); $ivi++) {
          if ($ivi == $xxsl) {
            $recon.="," . $ncnt;
          } else {
            $recon.="," . $vsx[$ivi];
          }
        }
            //if ($ncnt > 3) {
            //echo $thisext . "." . $nfields . ";" . $delimis . " changing from " . $vsx[$ivi] . " to " . $ncnt . "<br>" . $extlist . "<br>" . $ext_nl_list . "<br> to <br>" . $recon;
            //exit;
            //}
        $ext_nl_list=$recon;
    //$fileis[sizeof($fileis)]=str_replace($dprefix, "", $filename);
    //$categoryis[sizeof($categoryis)]=$thisext . "." . $nfields . ";" . $delimis;
    //$numcis[sizeof($numcis)]=$ncnt;
        for ($ii=0; $ii<sizeof($categoryis); $ii++) {
          if ($categoryis[$ii] == ($thisext . "." . $nfields . ";" . $delimis)) {
            $vs=$numcis[$ii];
        $changesfrom[sizeof($changesfrom)]=$fileis[$ii];
            $prefix="";
        while ($vs < $ncnt) {
          $prefix.="0";
          $vs++;
        }
        $changesto[sizeof($changesto)]=$prefix . $fileis[$ii];
          }
        }
      //echo "1 " . $xxsl . "<br>" . explode(",",$ext_fo_list)[$xxsl] . "<br>" . $thisext . "." . $nfields . ";" . $delimis . "<br>" . str_replace($dprefix, "", $filename) . " \$vsx[\$xxsl]=" . $vsx[$xxsl] . " and \$ncnt=" . $ncnt . " ... " . $ext_fo_list;
      // //$ext_nl_list[$xxsl].=";" . $nfields;
      //exit;
      }
    }
  }
  $cnt++;
}


foreach (glob($dprefix . $asteriskybit) as $filename) {
  //$htmlis=str_replace("</tbody>","\n<tr><td id=c1_" . $cnt . ">" . str_replace($dprefix, "", $filename) . "</td><td id=c2_" . $cnt . "></td><td id=c3_" . $cnt . "></td></tr>\n</tbody>",$htmlis);
  $found=false;
  $newf="";
  for ($iu=(-1 + sizeof($changesfrom)); $iu>=0; $iu--) {
    if (!$found) {
      if ($fileis[$recnt] == $changesfrom[$iu] && $changesto[$iu] != $fileis[$recnt] && trim($changesto[$iu]) != "" && strpos($changesto[$iu],".") !== false) {
        //$newf=$putthisback . $changesto[$iu];
        $newf="<input type=text id=ic2_" . $recnt . " value='" . $putthisback . $changesto[$iu] . "'></input>";
      }
    }
  }
  if ($fileis[$recnt] != '') {
  $htmlis=str_replace("</tbody>","\n<tr><td id=c1_" . $recnt . ">" . $putthisback . $fileis[$recnt] . "</td><td id=c2_" . $recnt . ">" . $newf . "</td><td id=c3_" . $recnt . "></td></tr>\n</tbody>",$htmlis);
  $recnt++;
  }
}

//echo sizeof($fileis);
echo $htmlis;
?>

