<?php
ob_start();
require_once 'functions.php';

?>
<html>
<head>
<title>WORM!!!  NERD!!!  WORM NERD!!!</title>
</head>
<body>
<h6><a href="/">wanna go HOME now...</a></h6>
<h3>Welcome to WORM NERD <span style="font-size:smaller;">lite++</span></h3>
<?php

$page = request_value('page',1);
$good = request_value('good');
$words_per = request_value('words_per',($good?20:10));

$dbh = dbconnect();

if ($result = $dbh->query("select * from wormwords where created >= date('now') limit 1")) {
  if ($wordrow = $result->fetchObject()) {
    $word = $wordrow->word;
    $result->closeCursor();
  }
}
if (empty($wordrow)) {
  list($word) = $dbh->query('select word from dictionary where did = abs(random()) % ' . MAXDID)->fetchAll(PDO::FETCH_COLUMN);
  if ($isth = $dbh->prepare('insert into wormwords (word, created) values (:word, date(:created))')) {
    if ($isth->execute(array(':word' => $word, ':created' => 'now'))) {
      if ($fsth = $dbh->prepare('select * from wormwords where word = :word')) {
        if ($fsth->execute(array(':word' => $word))) {
          $wordrow = $fsth->fetchObject();
        }
        else {
          print "fsth execute failo\n";
          var_dump($fsth->errorInfo());
          var_dump($db->errorInfo());
        }
      }
      else {
        print "fsth prepare failo\n";
        var_dump($fsth->errorInfo());
        var_dump($db->errorInfo());
      }
    }
    else {
      print "isth execute failo\n";
      var_dump($isth->errorInfo());
      var_dump($db->errorInfo());
    }
  }
  else {
    print "isth prepare failo\n";
    var_dump($isth->errorInfo());
    var_dump($db->errorInfo());
  }
}
// print "<xmp>\n"; var_dump($word, $wordrow); print "</xmp>\n";

$empty = array(
  'word' => '',
  'sentence' => '',
  'signed' => '',
  'word_up' => '',
);
$params = $_POST + $empty;
if (request_value('submit') == 'The Button') {
  if ($word_up = write_word($params)) {
    $params['word_up'] = $word_up;
  }
  else {
    $params = $empty;
  }
  if ($params['word'] != $word) {
    // the clock ticks for all
    $params = $empty;
  }
}

?>
<p>
<b>Today's word is <span style="font-size:larger;"><?=$word?></span></b>
</p>
<p>
<b>
You know the <a href="rules.html">rules</a>.
Enter your submission and push the button.
Then go talk amongst <a href="/looselips">yourselves</a>.
</b>
<br/>
<br/>
<b>PS:</b> OK, no, seriously, this time, I think it's better.
<br/>
If you run into any problems, <a href="/looselips">leave me a note</a>.
<br/>
- The Mgmt
</p>
<p>
<form action="index.php" method="post">
<input type="hidden" name="page" value="<?=$page?>">
<input type="hidden" name="good" value="<?=$good?>">
<input type="hidden" name="word" value="<?=$word?>">
<input type="text" name="sentence" size="70" max="150" value="<?=htmlentities($params['sentence'])?>" />
<br/>
Yours truly, <input type="text" name="signed" size="20" max="150" value="<?=htmlentities($params['signed'])?>" />, Mrs.
<br/>
<input type="submit" name="submit" value="The Button">
</form>
<?=$params['word_up']?>
</p>
<?php

if ($page == 1) {
	print <<<EOT
<h3>And here are today's entries...</h3>
<div>

EOT;

  $count = 0;
  if ($result = $dbh->query('select * from wormlines where wid = ' . $wordrow->wid)) {
    while ($row = $result->fetchObject()) {
      $row->sentence = stripslashes($row->sentence);
      $row->signed = stripslashes($row->signed);
      $count++;
      print $row->sentence;
      if ($row->signed) {
        print ' <b style="font-size:smaller;">(' . $row->signed . ')</b>';
      }
      print "<br/>\n";
    }
  }
  if (!$count) {
	  print "Congratulations! You're our first player today!\n";
  }
  print <<<EOT
</div>

<p>
<b style="color:red;">NEW!</b>
Take revenge on an uncaring society! Encourage beauty, defy darkness!
Make other people feel bad!  Vote on your favorite - or least-favorite -
entries.
If you <b>hate</b> it, it will get darker; enough hate and it will
turn black.
If you <span style="color:red;">love</span> it, it will start turning pretty
colors.  Well, something other than black and white, anyway.
</p>

EOT;
}
?>
<h3>Some Past Entries</h3>
<p>
<form action="vote.php" method="post">
<input type="hidden" name="page" value="<?=$page?>">
<input type="hidden" name="good" value="<?=$good?>">
<input type="submit" name="cast" value="Vote!">
<?php

$sql = 'select * from wormwords where wid in (select wid from wormlines) and wid < %d';
$link = 'index.php?';
if ($good) {
  $sql .= ' and wid in (select wid from wormlines where score > 1)';
  $link .= 'good=be&';
}
$page_string = '<p>Page ';
for ($i = 1; $i < $page; ++$i) {
  $page_string .= '<a href="' . $link . 'page=' . $i . '">' . $i . "</a>\n";
}
$page_string .= '<b>' . $i++ . "</b>\n";
$page_string .= '<a href="' . $link . 'page=' . $i . '">Next page</a>' . "\n";
$page_string .= '&nbsp;&nbsp;&nbsp;';
if ($good) {
  $page_string .= '<a href="index.php">Show me everything</a>';
}
else {
  $page_string .= '<a href="index.php?good=be">Just show me the good ones</a>';
}
$page_string .= "</p>\n";

print $page_string;

$offset = ($page - 1) * $words_per;
$sql .= ' order by wid desc limit %d offset %d';
$query = sprintf($sql
  , $wordrow->wid
  , $words_per
  , $offset
);
// print "<p>{$query}</p>\n";
if ($result = $dbh->query($query)) {
  $sql = 'select * from wormlines where wid = :wid';
  if ($good) {
    $sql .= ' and score > 1';
  }
  $lsth = $dbh->prepare($sql);
  while ($wrow = $result->fetchObject()) {
// print "<xmp>\n"; var_dump($wrow); print "</xmp>\n";

	  print <<<EOT
<p>
<table cellspacing="0" cellpadding="0" border="0">
 <tr>
  <td valign="top" align="left" width="100"><b>{$wrow->word}</b></td>
  <td>
   <table cellspacing="0" cellpadding="2" border="0">

EOT;
    $lsth->execute(array(':wid' => $wrow->wid));
    while ($row = $lsth->fetchObject()) {
// print "<xmp>\n"; var_dump($row); print "</xmp>\n";

      $row->sentence = stripslashes($row->sentence);
      $row->signed = stripslashes($row->signed);
      $signed = ($row->signed) ? '<b style="font-size:smaller;">(' . $row->signed . ')</b>' : '';

  	  $color = '#FFFFFF';
      $score = $row->score;
  	  if ($score > 0) {
  		  $score = $score > 16 ? 16 : $score;
  		  $color = strtoupper(sprintf('#F%x%x%x%x%x'
  			  , (16 - (2*$score))
  			  , (16 - ($score))
  			  , (16 - (2*$score))
  			  , (16 - (2*$score))
  			  , (16 - ($score))
  		  ));
  	  }
  	  elseif ($score < 0) {
  		  $score = $score < -16 ? -16 : $score;
  		  $color = strtoupper(sprintf('#%x%x%x%x%x%x'
  			  , (16 + (2*$score))
    			, (16 + (2*$score))
    			, (16 + (2*$score))
    			, (16 + (2*$score))
    			, (16 + (2*$score))
    			, (16 + (2*$score))
    		));
    	}

      if ($error = check_entry($wrow->word, $row->sentence)) {
        $row->sentence .= "\n<br/>{$error}\n";
      }

  	  print <<<EOT
     <tr>
      <td bgcolor="{$color}" valign="top" align="left" width="400">
       {$row->sentence} 
      </td>
      <td valign="top" align="left">
       <select name="vote[{$row->lid}]">
        <option value="-3"><b>HATE!</b></option>
        <option value="-2">Hate</option>
        <option value="-1">hate</option>
        <option selected value="0"></option>
        <option value="1">love</option>
        <option value="2">Love</option>
        <option value="3"><b>LOVE!</b></option>
       </select>
      </td>
      <td valign="top" align="left">
       {$signed}
      </td>
     </tr>

EOT;
    }

    print <<<EOT
   <table cellspacing="0" cellpadding="0" border="0">
   </table>
  </td>
 </tr>
</table>

EOT;
  }
}
else {
  print "<h1>kaboom.</h1>\n";
  var_dump($dbh->errorInfo());
}

print $page_string;

?>
<input type="submit" name="cast" value="Vote!">
</form>
</body>
</html>
