#include <infocon.style>
#include <phptools.inc>

<page func="Information & Consulting" title="Logbook">

<teaserlink>

<?
  $parms = array ("keyword","category","public","teaser","text");
  $urlbase = "";

  foreach ($parms as $arg) {
    if (isset($_REQUEST[$arg])) {
      if (strlen($urlbase) == 0)
        $urlbase = "find.php?";
      else
	$urlbase .= "&";
      $urlbase .= $arg . '=' . urlencode($_REQUEST[$arg]);
    }
  }

  if (strlen($urlbase) == 0) {
    echo "<p><b>No search restriction provided.</b></p>";
    exit;
  }

  if (isset($_REQUEST["offset"]))
    $linkbase = sprintf ("%s&offset=%d", $urlbase, $_REQUEST["offset"]);
  else
    $linkbase = $urlbase;

  $link_date = sprintf ('<a href="%s&sort=date">Date</a>', $linkbase);
  $link_category = sprintf ('<a href="%s&sort=category">Category</a>', $linkbase);
  $link_title = sprintf ('<a href="%s&sort=title">Title</a>', $linkbase);
?>

<table class="smallfont border" width=100% border=0 cellpadding=0 cellspacing=1>
<tr class="head">
<th width=15%><? echo $link_date; ?></th>
<th width=15%><? echo $link_category; ?></th>
<th width=70%><? echo $link_title; ?></th>
</tr>

<?
  $limit = 50;
  $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
	       or die("Unable to connect to SQL server");

  pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");

  $query = "SELECT id,category,teaser,created,public FROM logbook WHERE ";
  $where = array();
  if (strlen($_REQUEST[keyword])) {
    if (isset($_REQUEST['text'])) {
      $where[] = sprintf ("( teaser ~* '%s' OR body ~* '%s')",
			 addslashes ($_REQUEST[keyword]),
			 addslashes ($_REQUEST[keyword]));
    } else {
      $where[] = sprintf ("teaser ~* '%s'",
			 addslashes ($_REQUEST[keyword]));
    }
  }

  if (strlen($_REQUEST[category])) {
    $where[] = sprintf ("category = '%s'", addslashes ($_REQUEST[category]));
  }

  if (strlen($_REQUEST['public'])) {
    $where[] = "public = 1";
  }

  $query .= implode (" AND ", $where);

  if (isset($_REQUEST[sort]) && $_REQUEST[sort] == "title") {
    $query .= "ORDER BY teaser,created";
  } elseif (isset($_REQUEST[sort]) && $_REQUEST[sort] == "category") {
    $query .= "ORDER BY category,created";
  } else {
    $query .= "ORDER BY created";
  }
  $query .= " LIMIT $limit";
  if (isset ($_GET["offset"]))
    $query .= sprintf (" OFFSET %d\n", $_GET["offset"]);

  $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

  $color = 0;
  for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
    $row = pg_fetch_array ($sth, $nr);
    $date = explode (" ", $row['created']);
    $time = explode (":", $date[1]);
    $date = explode ("-", $date[0]);
    $date = sprintf ("%d.%d.%d %02d:%02d", $date[2], $date[1], $date[0], $time[0], $time[1]);

    if ($row['public'] == 0) {
      printf ("<tr class=\"t%s\"><td align=\"right\">%s&nbsp;</td><td align=\"center\">%s</td>"
	     ."<td><a href=\"show.php?id=%s\">%s</a></td></tr>",
	     $color,
	     $date, $row['category'], $row['id'], $row['teaser']);
    } else {
      $pubbase = 'http://infocon.infodrom.org//logbook/';
      printf ("<tr class=\"t%s\"><td align=\"right\">%s&nbsp;</td><td align=\"center\">%s</td><td><a href=\"show.php?id=%s\">%s</a>"
	     ." (<a href=\"%s%s-%d.html\">pub</a>)</td></tr>",
	     $color,
	     $date, $row['category'], $row['id'], $row['teaser'], $pubbase, teaserlink($row['teaser']), $row['id']);
    }
    $color = !$color;
  }

  echo ("</table>\n");

  if ($nr >= $limit) {
    if (isset($_REQUEST["sort"]))
      $url = sprintf ("%s&sort=%s&offset=%d", $urlbase, urlencode($_REQUEST["sort"]), $_GET["offset"]+$limit);
    else
      $url = sprintf ("%s&offset=%d", $urlbase, $_GET["offset"]+$limit);

    printf ("<p><a href=\"%s\">next</a></p>\n", $url);
  }

?>

</page>

# Local variables:
# mode: text
# mode: auto-fill
# end:
