#include <infodrom.style>

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

<?
  define(OVERVIEW,0);
  define(REFERER,1);
  define(DETAILS,2);

  if (isset($_GET["site"]) && isset($_GET["details"]))
    $mode = DETAILS;
  elseif (isset($_GET["referer"]))
    $mode = REFERER;
  else
    $mode = OVERVIEW;

  $tablehead = array();
  $tablerow = array();
  $tablefoot = array();
  $refcut = array();

  # overview
  $tabhead[] =
'<table class="font height border" width="100%" cellspacing="0">
<tr bgcolor="#<colh>"><th width="10%">Count</th><th width="90%">URLs</th></tr>';
  $tabfoot[] = '</table>';
  $tabrow[] = '<tr bgcolor="#%s"><td width="10%%" align="center">%d</td><td width="90%%"><a href="<get-var WML_SRC_BASENAME>.php?site=%d&details=1&url=%d">http://%s%s</a></td></tr>';
  $refcut[] = 80;

  # referer
  $tabhead[] =
'<table class="font height border" width="100%" cellspacing="0">
<tr bgcolor="#<colh>"><th width="10%">Count</th><th width="90%">Referer</th></tr>';
  $tabfoot[] = '</table>';
  $tabrow[] = '<tr bgcolor="#%s"><td width="10%%" align="center">%d</td><td width="90%%"><a href="<get-var WML_SRC_BASENAME>.php?site=%d&details=1&origin=%d" title="%s">%s</a></td></tr>';
  $refcut[] = 80;

  # details
  $tabhead[] =
'<table class="font height border" width="100%" cellspacing="0">
<tr bgcolor="#<colh>"><th width="16%">Date</th><th width="54%">Referer</th><th width="30%">URL</th></tr>';
  $tabfoot[] = '</table>';
  $tabrow[] = '<tr bgcolor="#%s"><td width="16%%">%s</td><td width="54%%"><a href="%s" title="%s">%s</a></td><td width="30%%"><a href="%s">%s</a></td></tr>';
  $refcut[] = 55;

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


  $query = "SELECT id,name FROM ref_site ";
  if (isset($_GET["site"]))
    $query .= sprintf("WHERE id = %d ", $_GET["site"]);
  $query .= "ORDER BY name";
  $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

  for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
    $site = pg_fetch_array ($sth, $nr);
    if (isset($_GET["details"]) && $_GET["details"] == 1)
      printf ('<h3 class="bar">%s</h3>', $site["name"]);
    elseif (isset($_GET["referer"]) && !isset($_GET["site"]))
      printf ('<h3 class="bar"><a href="<get-var WML_SRC_BASENAME>.php?referer=1&site=%d">%s</a></h3>',
	    $site["id"], $site["name"]);
    elseif (isset($_GET["site"]))
      printf ('<h3 class="bar"><a href="<get-var WML_SRC_BASENAME>.php?site=%d&details=1">%s</a></h3>',
	    $site["id"], $site["name"]);
    else
      printf ('<h3 class="bar"><a href="<get-var WML_SRC_BASENAME>.php?site=%d">%s</a></h3>',
	    $site["id"], $site["name"]);

    $color = "<coln>";
    echo $tabhead[$mode];
    if ($mode == OVERVIEW)
      $query = sprintf (
"SELECT count(*) AS count,ref_log.url,ref_url.path
  FROM ref_log,ref_url
  WHERE ref_log.url = ref_url.id
    AND ref_url.site = %d
  GROUP BY ref_log.url,path
  ORDER BY count DESC,path", $site["id"]);
    elseif ($mode == REFERER)
      $query = sprintf (
"SELECT count(*) AS count,ref_origin.url,ref_origin.title,origin
  FROM ref_origin,ref_url,ref_log
  WHERE ref_log.url = ref_url.id
    AND ref_log.origin = ref_origin.id
    AND ref_url.site = %d
  GROUP BY ref_origin.url,title,origin
  ORDER BY count DESC,title", $site["id"]);
    else {
      $query = sprintf (
"SELECT occurrence,ref_origin.url,title,path,ref_log.url AS urlid
  FROM ref_origin,ref_url,ref_log
  WHERE ref_log.url = ref_url.id
    AND ref_log.origin = ref_origin.id
    AND ref_url.site = %d", $site["id"]);

      if (isset($_GET[origin]))
        $query .= sprintf(" AND origin = %d", $_GET[origin]);
      if (isset($_GET[url]))
        $query .= sprintf(" AND ref_log.url = %d", $_GET[url]);
      $query .= " ORDER BY occurrence DESC";
    }

    if (!isset($_GET["site"]))
      $query .= " LIMIT 10";

    $sth2 = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
    for ($i=0; $i < pg_NumRows ($sth2); $i++) {
      $row = pg_fetch_array ($sth2, $i);
      if (strlen($row["title"])) {
        $title = substr($row["title"], 0, $refcut[$mode]);
      } else {
        $title = htmlentities(substr($row["url"], 0, $refcut[$mode]));
      }

      $color = $color == "<cold>"?"<coln>" : "<cold>";
      if ($mode == OVERVIEW)
        printf ($tabrow[$mode], $color, $row["count"], $site["id"], $row["url"],
	        $site["name"], htmlentities($row["path"]));
      elseif ($mode ==	REFERER)
        printf ($tabrow[$mode], $color, $row["count"], $site["id"], $row["origin"],
	        htmlentities($row["url"]), $title);
      else {
        $date = substr($row["occurrence"], 0, 16);
	if (isset($_GET["url"]))
	  $url = sprintf("http://%s%s", $site["name"], $row["path"]);
	else
	  $url = sprintf("<get-var WML_SRC_BASENAME>.php?site=%d&details=1&url=%d", $site["id"], $row["urlid"]);
        printf ($tabrow[$mode], $color, $date, $row["url"], htmlentities($row["url"]), $title, $url, $row["path"]);
      }
    }
    echo $tabfoot[$mode];
  }
?>

<div style="padding-top: 1em"></div>

</page>

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