<?php

// Displays the data using the TikiWiki odd/even table style
//
// Parameters:
//   head -- the column header row
//
// Usage:
//   The data (and the head paramter) is given one row per line, with columns
//   separated by ~|~.
//
// Example:
// {FANCYTABLE( head => header column 1 ~|~ header column 2 ~|~ header column 3 )}
// row 1 column 1 ~|~ row 1 column 2 ~|~ row 1 column 3
// row 2 column 1 ~|~ row 2 column 2 ~|~ row 2 column 3
// {FANCYTABLE}

function wikiplugin_fancytable_help() {
  return tra( "Displays the data using the TikiWiki odd/even table style" );
}

function wikiplugin_fancytable($data,$params)
{
  global $tikilib;

  // Start the table
  $wret = "<table class=\"normal\">";

  $tdhdr = "<td class=\"heading\">";
  $tdend = "</td>";
  $trbeg = "<tr>";
  $trend = "</tr>";
  
  // Parse the parameters
  extract( $params ); 
  if (isset( $head )) {
    $parts = explode( "~|~", $head );
    $row   = "";
    foreach ($parts as $column) {
      $row .= $tdhdr.$column.$tdend;
    }
    $wret .= $trbeg.$row.$trend;
  }
  
  // Each line of the data is a row, the first line is the header
  $row_is_odd  = true;
  $lines = split("\n",$data);
  foreach ($lines as $line) {
    $line = trim($line);
    if (strlen($line) > 0) {
      if ($row_is_odd) {
        $tdbeg = "<td class=\"odd\">";
        $row_is_odd = false;
      } else {
        $tdbeg = "<td class=\"even\">";
        $row_is_odd = true;
      }
      $parts = explode( "~|~", $line );
      $row = "";
      foreach ($parts as $column) {
        if (strcmp( trim($column), "~blank~" ) == 0) {
          $row .= $tdhdr."&nbsp;".$tdend;
        } else {
          $row .= $tdbeg.$column.$tdend;
        }
      }
      $wret .= $trbeg.$row.$trend;
    }
  }

  // End the table
  $wret .= "</table>";
  
  return $wret;
}

?>
