<?php

include "static_html_handler.php";
include "staff_api.php";

function staff_entry($line, $rank) {

  $data = "";

  $vfrom = $line['validfrom'];
  $vto   = $line['validto'];
  $login = $line['login'];
  $salut = $line['salutation'];
  $fname = $line['firstname'];
  $lname = $line['lastname'];
  $url   = $line['url'];
  $tel   = $line['telephone'];
  $mail  = $line['email'];
  $rloc  = $line['location'];
  $rnum  = $line['no'];
  $cat   = $line['category'];
  $dept  = $line['department'];

  if (strtotime($vfrom) > time() or strtotime($vto) < time())
    return "";
  if(!empty($rank) and !in_array($cat,$rank)){
    return ""; 
  }

  $data .= "<tr>";

  # Name and link to personal page
  $name = $salut.". ".$fname." ".$lname;
  if ($url == '')
    $url = "http://www.doc.ic.ac.uk/~$login";
  else if (substr($url, 0, 3) == "www")
    $url = "http://".$url;
  $data .= "<td><a href='$url'><strong>$name</strong></a></td>";

  # Telephone number
  $template = "+44 (0)20 759x xxxx";
  if (!empty($tel) and strpos($tel, "+4") !== 0) {
    $index = 0;
    while (strpos($template, "x") !== False and $index <= strlen($tel)) {
      $template[strpos($template, "x")] = $tel[$index];
      $index++;
    }
    if (strpos($template, "x") === False)
      $tel = $template;
  }
  $data .= "<td>$tel</td>";

  # E-mail
  if ($mail !== '') {
    if (strpos($mail, '@') != true)
      $mail = $mail."@imperial.ac.uk";
    else 
      $mail = $login."@imperial.ac.uk";
  }
  $mail = strtolower($mail);
  $data .= "<td><a href='mailto:$mail'>e-mail</a></td>";

  # Room
  $room = $rloc." ".$rnum;
  $data .= "<td>$room</td>";

  $data .= "</tr>\n";
  return $data;
}

function generate_page($title, $rank) {
  $content = gen_header();
  $content .= gen_static_maincontent_div();
  #$content .= "<h1>Academic Staff</h1>";
  $content .= "<h1>$title Staff</h1>";
  $content .= gen_static_table_header_people();
  
  $staff = get_staff();
  
  #$rank = array("TF");
  
  foreach ($staff as $line) {
    $data = staff_entry($line, $rank);
    if ($data)
      $content .= $data;  
  }
  
  $content .= gen_static_table_footer ();
  $content .= gen_timestamp ();
  $content .= gen_footer();
  
  return $content;
}
