<?php
 // wordtree_chart.php
 // Using Google Wordtree Chart as of https://developers.google.com/chart/interactive/docs/gallery/wordtree
 $docb = false;
 $word = "cats";
 if (isset($_GET['word'])) $word = urldecode($_GET['word']);
 if (isset($_POST['word'])) $word = urldecode($_POST['word']);
 $data = "  ['cats are better than dogs'],
            ['cats eat kibble'],
            ['cats are better than hamsters'],
            ['cats are awesome'],
            ['cats are people too'],
            ['cats eat mice'],
            ['cats meowing'],
            ['cats in the cradle'],
            ['cats eat mice'],
            ['cats in the cradle lyrics'],
            ['cats eat kibble'],
            ['cats for adoption'],
            ['cats are family'],
            ['cats eat mice'],
            ['cats are better than kittens'],
            ['cats are evil'],
            ['cats are weird'],
            ['cats eat mice'],
";
 $url = "";
 if ((isset($_GET['url']) || isset($_POST['url'])) && !isset($_GET['data']) && !isset($_POST['data'])) {
  if (isset($_GET['url'])) $url = urldecode($_GET['url']);
  if (isset($_POST['url'])) $url = urldecode($_POST['url']);
  $data = file_get_contents($url);
 }
 if (isset($_GET['data'])) {
   $data = urldecode($_GET['data']);
 } else if (isset($_POST['data'])) {
   $data = urldecode($_POST['data']);
 } else {
 if ((isset($_GET['url']) || isset($_POST['url'])) && !isset($_GET['data']) && !isset($_POST['data'])) {
  try {
  $doc = new DOMDocument();
  $doc->preserveWhiteSpace = FALSE;
  $before = libxml_use_internal_errors(true);
  if (strpos(strtolower($url) . "?", ".xml?") !== false) {
  $doc->loadXML($data);
  $predata = "";
  $data = $doc->textContent;
  $suffix = ".";
  $phrases = explode(".", $data);
  for ($i=0; $i<sizeof($phrases); $i++) {
    if (($i + 1) >= sizeof($phrases)) $suffix = "";
    $predata .= "  ['";
    $predata .= $phrases[$i] . $suffix . "'], \n";
  }
  $data = $predata;
  } else {
  $parts = explode("head>", $data);
  if (sizeof($parts) > 1) {
    $data = substr($parts[0], 0, (strlen($parts[0]) - 1)) . $parts[2];
  }
  $doc->loadHTML($data);
  $data = $doc->textContent;
  $predata = "";
  $suffix = ".";
  $lines = explode("\n", $data);
  for ($l=0; $l<sizeof($lines); $l++) {
  if (str_replace(" ","",str_replace("\r","",$lines[$l])) != "") {
  $phrases = explode(".", str_replace("\r","",$lines[$l]));
  for ($i=0; $i<sizeof($phrases); $i++) {
    if (($i + 1) >= sizeof($phrases)) $suffix = "";
    $semic = explode(";", $phrases[$i]);
    for ($ii=0; $ii<sizeof($semic); $ii++) {
    $predata .= "  ['";
    $predata .= str_replace("  ", " ", str_replace("  ", " ", str_replace("  ", " ", str_replace("  ", " ", str_replace("\n", " ", str_replace("[", " ", str_replace("]", " ", str_replace("'", " ", $semic[$ii])))))))) . "'], \n";
    }
  }
  }
  }
  $data = $predata;
  }
  libxml_use_internal_errors($before);  
  $docb = true;
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }

 }
 }

?>
<html>
  <head>
    <title>Google Wordtree Chart - RJM Programming - December, 2014</title>
    <meta charset="UTF-8" />
    <link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1.1", {packages:["wordtree"]});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable(
          [ ['Phrases'],
            <?php echo $data; ?>
          ]
        );

        var options = {
          wordtree: {
            format: 'implicit',
            word: '<?php echo $word; ?>'
          }
        };

        var chart = new google.visualization.WordTree(document.getElementById('wordtree_basic'));
        chart.draw(data, options);
      }
      
      function ask() {
        var word = "";
        var url=prompt("Please enter a URL that we will search through for your Theme Word ... Cancel exits", "");
        if (url != null) {
          if (url != "") {
            word=prompt("Please Theme Word to look for within " + url + " ... Cancel exits", "");
			if (word != null) {
			  if (word != "") {
			    location.href = "./wordtree_chart.php?word=" + encodeURIComponent(word) + "&url=" + encodeURIComponent(url);
			  }
			}
          }
        }
      }
    </script>
  </head>
  <body style='background-color:fffff0;' <?php if ($url == "") { echo " onload='ask();' ";  } ?>>
    <h1 align='center'>Google <a target=_blank title='Google Wordtree Chart' href='https://developers.google.com/chart/interactive/docs/gallery/wordtree'>Wordtree</a> Chart - RJM Programming - December, 2014</h1>
    <div align='center'>
    <div id="wordtree_basic" style="width: 900px; height: 500px;"></div>
    <?php if ($url != "") { echo "<br><a href='./wordtree_chart.php' title='Another go?'>Another go?</a>";  } ?>
    </div>
  </body>
</html>
