#include <infodrom.style>
#include "calendar.inc"

<page func="Infodrom Oldenburg" title="Events">

<?

  $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 $event_cols FROM events WHERE ";

  $where = '';

  if (strlen ($_POST['city'])) {
    if (strlen ($where)) $where .= "AND ";
    $where .= sprintf ("city = '%s' ", $_POST['city']);
  }

  if (strlen ($_POST['country'])) {
    if (strlen ($where)) $where .= "AND ";
    $where .= sprintf ("country = '%s' ", $_POST['country']);
  }

  if (strlen ($_POST['year'])) {
    if (strlen ($where)) $where .= "AND ";
    $where .= sprintf ("( start >= '%d-01-01' AND start < '%d-01-01' ) ", $_POST['year'], (integer)$_POST['year'] + 1);
  }

  if (strlen ($_POST['key'])) {
    if (strlen ($where)) $where .= "AND ";
    if (strlen ($_POST['city']) || strlen ($_POST['country'])) {
      $where .= sprintf ("( name ~* '%s' OR url ~* '%s' OR comment ~* '%s' ) ",
		       addslashes ($_POST['key']), addslashes ($_POST['key']), addslashes ($_POST['key']));
    } else {
      $where .= sprintf ("( name ~* '%s' OR city ~* '%s' OR url ~* '%s' OR comment ~* '%s' ) ",
		       addslashes ($_POST['key']), addslashes ($_POST['key']), addslashes ($_POST['key']), addslashes ($_POST['key']));
    }
  }
  if (!strlen ($where)) {
    echo "<h3>Empty search?  Euh?</h3>";
  }
  $query .= $where . " ORDER BY start,name";

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

    $year = 0;
    for ($i=0; $i < pg_NumRows ($sth); $i++) {
      $row = pg_fetch_array ($sth, $i);

      $date = explode (" ", $row['start']);
      $date = explode ("-", $date[0]);
      $end = explode (" ", $row[3]);
      $end = explode ("-", $end[0]);
      $thisyear = $date[0];

      $date = format_date ($date, $end);

      if ($thisyear != $year) {
	if ($year > 0) {
	  echo ($event_table_end);
	}
	$color = 0;
	printf ("<h3 class=\"bar\">Events in %s</h3>", $thisyear);
	echo ($event_table_start);
	$year = $thisyear;
      }

      $row['name'] = str_replace (" ", "&nbsp;", $row['name']);
      $row['city'] = str_replace (" ", "&nbsp;", $row['city']);
      $row['country'] = str_replace (" ", "&nbsp;", $row['country']);

      printf ($event_row, $color, $row['oid'], $row['name'], $row['cancelled'] == 1?" *cancelled*":"",
	      $row['city'], $row['country'], $date);
      $color = !$color;
    }
  }
  echo ($event_table_end);
?>

<div style="margin-top: 5px;"></div>
</page>

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