<?php
// json_array_post.php - RJM Programming - August, 2014
if (isset($_GET['look'])) {   // analyze posted data
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $done = "";
  $htmlis = "<html><head>
<style>

table { border-collapse: collapse; }

tr.even { background-color: #C2C8D4; }

tr.over { background-color: #8797B7; }

td {
  border-bottom: 1px solid #C2C8D4;
  padding: 5px;
}

th {
  border-right: 2px solid #FFF;
  color: #FFF;
  padding-right: 40px;
  padding-left: 20px;
  background-color: #626975;

}

th.sortUp {
  background: #626975 url(http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/images/asc.gif) no-repeat right center;
}

th.sortDown {
  background: #626975 url(http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/images/desc.gif) no-repeat right center;
}

</style>

<script type='text/javascript' src='http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/jquery-latest.js'></script>
<script type='text/javascript' src='http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/jquery.tablesorter.js'></script>
<script type='text/javascript'>

\$(document).ready(function() {
  \$('tr').mouseover(function() {
    \$(this).addClass('over');
  });
  \$('tr').mouseout(function() {
    \$(this).removeClass('over');
  });
  \$('tr:even').addClass('even');
  \$('#myt').tablesorter({
    sortList:[[1,0]],
    cssAcc: 'sortUp',
    cssDesc: 'sortDown',
    widgets: ['zebra']
  });
});
</script>
</head><body></body></html>";
  $numcols = 0;
  $thiscols = 0;
  $rowdat = "";
  if ($_GET['look'] == "POST") {
  $idbasis = "[id]";
  if (isset($_GET['tableid'])) {
    $idbasis = $_GET['tableid'];
  }
  $thbasis = "[label]";
  if (isset($_GET['tableth'])) {
    $thbasis = $_GET['tableth'];
  }
  $tdbasis = "[v]";
  if (isset($_GET['tabletd'])) {
    $tdbasis = $_GET['tabletd'];
  }
  foreach($_POST as $key=>$value) {
    //echo str_replace("-", "[", str_replace("_", "]", $key)).'='.$value."\n";
    //if (str_replace("-", "[", str_replace("_", "]", $key)) == "table[cols][0][id]") {
    if ($done == "" && strpos(str_replace("-", "[", str_replace("_", "]", $key)), $idbasis) !== false) {
       $done = "y";
       $htmlis = str_replace("</body>", "<table title='" . $value . "' id='myt' class='tablesorter' cellpadding=5 cellspacing=5 align='center' border=7></table></body>", $htmlis); 
    }
    if ($done == "y" && strpos(str_replace("-", "[", str_replace("_", "]", $key)), $thbasis) !== false) {
       if (str_replace("<thead>", "", $htmlis) == $htmlis) $htmlis = str_replace("</table>", "<thead></thead></table>", $htmlis);
       $htmlis = str_replace("</thead>", "<th>" . $value . "</th></thead>", $htmlis);
       $numcols++;
    } else if ($done == "y" && strpos(str_replace("-", "[", str_replace("_", "]", $key)), $tdbasis) !== false) {
       if ($thiscols == 0) $rowdat = "<tr>";
       $rowdat .= "<td>" . $value . "</td>";
       $thiscols++;
       if ($thiscols == $numcols) {
         $htmlis = str_replace("</table>", $rowdat . "</table>", $htmlis);
         $thiscols = 0;
         $rowdat = "";
       } else if (strpos(str_replace("-", "[", str_replace("_", "]", $key)), $idbasis) !== false) {
         $done = "Yes";
       }
    }
  }
  if ($done == "") {
    $done = "n";
    $htmlis = "<html><body><p></p></body></html>";
    foreach($_POST as $key=>$value) {
      if ($key != "look") $htmlis = str_replace("</p></body>", str_replace("-", "[", str_replace("_", "]", $key)).'='.$value."<br></p></body>", $htmlis);
    }
  }
  $htmlis = str_replace("</body>", "<br><div align='center'><a title='Back' href='" . $sru . "?ask=y'>Back</a></div></body>", $htmlis); 
  } else { // analyze get data
    $htmlis = "<html><body><p></p></body></html>";
    foreach($_GET as $key=>$value) {
      if ($key != "look") $htmlis = str_replace("</p></body>", str_replace("-", "[", str_replace("_", "]", $key)).'='.$value."<br></p></body>", $htmlis);
    }
    $htmlis = str_replace("</body>", "<br><div align='center'><a title='Back' href='" . $sru . "?ask=y'>Back</a></div></body>", $htmlis); 
  }
  echo $htmlis;

} else {  // analyze input json data
 $prejd = "?look=GET&";
 $jd = "";
 $fu = "";
 if (isset($_GET['feedurl'])) {
   $fu = $_GET['feedurl'];
 } else if (isset($_POST['feedurl'])) {
   $fu = $_POST['feedurl'];
 } 
 if (isset($_GET['jsondatais'])) {
   $jd = $_GET['jsondatais'];
   if ($jd == "" && isset($_GET['feedurl'])) {
     if ($_GET['feedurl'] != "") $jd = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($_GET['feedurl'])));
   }
 } else if (isset($_POST['jsondatais'])) {
   $prejd = "";
   $jd = $_POST['jsondatais'];
   if ($jd == "" && isset($_POST['feedurl'])) {
     if ($_POST['feedurl'] != "") $jd = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($_POST['feedurl'])));
   }
 }
 if (isset($_GET['ask'])) {
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $htmlis = "<html><body style='background-color: yellow;' align='center'><h1 align='center'>JSON Data Criteria</h1><form align='center' id='xmyf' method='POST' action='" . $sru . "'><br><div align='center'><input type='submit' name='submit' id='submit' value='Submit'></input></div></form></body></html>";
  $htmlis = str_replace("<br>", "\n<div id='fs' align='center'>JSON Array data <input type='text' title='{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}' onchange='document.getElementById(\"ss\").style.display = \"none\"; document.getElementById(\"feedurl\").value = \"\"; document.getElementById(\"jsondatais\").value = this.value;' size=120 name='prejsondatais' id='prejsondatais' value=''></input><hr><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<input type='hidden' name='jsondatais' id='jsondatais' value='" . str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents("http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV"))) . "'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />or JSON Feed url <input type='text' onchange='document.getElementById(\"ss\").style.display = \"block\"; document.getElementById(\"prejsondatais\").value = \"\"; document.getElementById(\"jsondatais\").value = \"\"; ' size=120 name='feedurl' id='feedurl' value='http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV'></input></div><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<div id='ss' align='center'><br /><br />Table ID searchspec <input type='text' name='tableid' id='tableid' value='[id]'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />Table Heading searchspec <input type='text' name='tableth' id='tableth' value='[label]'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />Table Cell searchspec <input type='text' name='tabletd' id='tabletd' value='[v]'></input></div><br>", $htmlis);
  echo $htmlis;
 } else if ($jd != "" && $prejd != "") {
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $json = urldecode($jd); // eg. '{"a":1,"b":2,"c":3,"d":4,"e":5}';
  //echo $json;
  try {
  if (strpos($jd, "\\") !== false) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  } else {
  $objarr = json_decode($json);
  $server_query = http_build_query($objarr);
  }
  } catch (Exception $e) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  }
  //echo $server_query;
  if ($server_query != "") {
    $url = $sru . $prejd . $server_query;
    header("Location: " . $url);
  } else {
    $hj = substr($jd, 1, (strlen($jd) - 2));
    $json = urldecode($hj); // eg. '{"a":1,"b":2,"c":3,"d":4,"e":5}';
   try {
  if (strpos($hj, "\\") !== false) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  } else {
    $objarr = json_decode($json);
    $server_query = http_build_query($objarr);
    }
  } catch (Exception $e) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  }
    if ($server_query != "") {
      $url = $sru . "?look=GET&" . $server_query;
      //echo $hj . " to " . $json . " and then " . $url;
      header("Location: " . $url);
    } else {
      echo "Your JSON data is malformed";
    }
  }
 } else if (1 == 1) { // else ... post our default scenario
  $urlis = "http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV";
  if (isset($_GET['feedurl'])) {
    $urlis = $_GET['feedurl'];
  } else if (isset($_POST['feedurl'])) {
    $urlis = $_POST['feedurl'];
  }
  $idbasis = "[id]";
  if (isset($_GET['tableid'])) {
    $idbasis = $_GET['tableid'];
  } else if (isset($_POST['tableid'])) {
    $idbasis = $_POST['tableid'];
  } 
  $thbasis = "[label]";
  if (isset($_GET['tableth'])) {
    $thbasis = $_GET['tableth'];
  } else if (isset($_POST['tableth'])) {
    $thbasis = $_POST['tableth'];
  }
  $tdbasis = "[v]";
  if (isset($_GET['tabletd'])) {
    $tdbasis = $_GET['tabletd'];
  } else if (isset($_POST['tabletd'])) {
    $tdbasis = $_POST['tabletd'];
  }
  if ($prejd == "") {
    //$tdbasis .= "&jsondatais=" . $jd;
    $json = $jd;
    if ($fu == "") header("Location: " . $_SERVER['REQUEST_URI'] . "?jsondatais=" . $jd);
  } else {
    $json = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($urlis)));   //http://www.datatables.net/examples/ajax/data/arrays.txt");
  }
  $htmlis = "<html><body onload=\"document.getElementById('submit').click();\"><form id='myf' method='POST' action='" . $_SERVER['REQUEST_URI'] . "?look=POST&tableid=" . $idbasis . "&tableth=" . $thbasis . "&tdbasis=" . $tdbasis . "'><br><input type='submit' name='submit' id='submit' value='Submit'></input></form></body></html>";

  //echo $json;
 // try {
 // if (strpos($json, "\\") !== false && 1 == 2) {
 // $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
 //   $htmlis = str_replace("<br>", "\n<input type='hidden' name='sq' id='sq' value='" . $server_query . "'></input><br>", $htmlis);
 // } else {
  //echo $json;
  $objarr = json_decode(str_replace("\\", "", $json));
  $server_query = http_build_query($objarr);
 // }
 // } catch (Exception $e) {
 // $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
 // }

// version=0.6&status=ok&sig=&table=cols=[id=A&label=Country&type=string&pattern=,id=B&label=Population&type=number&pattern=#0.###############,id=C&label=Crude Birth Rate&type=number&pattern=#0.###############]&rows=[c=[v=China,v=1347000000&f=1347000000,v=12&f=12],c=[v=India,v=1241000000&f=1241000000,v=22&f=22],c=[v=United States,v=312000000&f=312000000,v=14&f=14],c=[v=Indonesia,v=238000000&f=238000000,v=18&f=18],c=[v=Brazil,v=197000000&f=197000000,v=15&f=15],c=[v=Pakistan,v=177000000&f=177000000,v=27&f=27],c=[v=Nigeria,v=162000000&f=162000000,v=40&f=40],c=[v=Bangladesh,v=151000000&f=151000000,v=20&f=20],c=[v=Russia,v=143000000&f=143000000,v=13&f=13],c=[v=Japan,v=128000000&f=128000000,v=8&f=8],c=[v=Mexico,v=115000000&f=115000000,v=19&f=19],c=[v=Philippines,v=96000000&f=96000000,v=25&f=25],c=[v=Vietnam,v=88000000&f=88000000,v=16&f=16],c=[v=Ethiopia,v=87000000&f=87000000,v=31&f=31],c=[v=Germany,v=81800000&f=81800000,v=8&f=8],c=[v=Egypt,v=82600000&f=82600000,v=23&f=23],c=[v=Iran,v=78000000&f=78000000,v=17&f=17],c=[v=Turkey,v=74000000&f=74000000,v=18&f=18],c=[v=Thailand,v=69500000&f=69500000,v=12&f=12],c=[v=Congo,v=67800000&f=67800000,v=43&f=43],c=[v=France,v=63300000&f=63300000,v=13&f=13],c=[v=United Kingdom,v=62700000&f=62700000,v=13&f=13],c=[v=Italy,v=60800000&f=60800000,v=9&f=9]]

  //$objarr = json_decode($json);
  //$server_query = http_build_query($objarr);
  $ds = urldecode($server_query);
  $ads = explode("&", $ds);
  for ($i=0; $i<sizeof($ads); $i++) {
    $aads = explode("=", $ads[$i]);
    $htmlis = str_replace("<br>", "\n<input type='hidden' name='" . str_replace("[", "-", str_replace("]", "_", $aads[0])) . "' id='" . str_replace("[", "-", str_replace("]", "_", $aads[0])) . "' value='" . $aads[1] . "'></input><br>", $htmlis);
  }
  echo $htmlis;
 } 
}
?>
