view materials/index.php @ 325:b65e0d6efe9d misc

Add copyright blurb.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 31 Dec 2015 02:59:54 +0200
parents b4f34ae41008
children
line wrap: on
line source

<?
//
// BatMUD material information browser
// (C) Copyright 2009 - 2015 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
//
require "mgeneric.inc.php";
require "materials.inc.php";

$pageTitle = "Pupunen BatMUD Material Browser";
$pageIndex = "index.php";


function stParseList($values, $pair)
{
  $res = array();
  if ($values === FALSE)
    return $res;

  // It's an array already
  if (is_array($values))
  {
    if ($pair)
    {
      // The data should already be in our desired format .. maybe
      foreach ($values as $key => $val)
        $res[trim($key)] = trim($val);
    }
    else
    {
      // Place values in hash
      foreach ($values as $key)
        $res[trim($key)] = 1;
    }
    return $res;
  }

  // Assume string, parse it
  if (($list = preg_split("/\s*,\s*/", trim($values), -1, PREG_SPLIT_NO_EMPTY)) === FALSE)
    return $res;

  if ($pair)
  {
    // Parse key-value pairs separated by :
    foreach ($list as $str)
    {
      if (($tmp = preg_split("/\s*:\s*/", trim($str), -1, PREG_SPLIT_NO_EMPTY)) !== FALSE)
        $res[$tmp[0]] = $tmp[1];
    }
  }
  else
  {
    // Should be just values, so place them in hash
    foreach ($list as $str)
      $res[$str] = 1;
  }
  return $res;
}


function stMakeList($name, $url, $array, $pair)
{
  if (count($array) == 0)
    return "";

  $tmp = array();
  if ($pair)
  {
    foreach ($array as $key => $val)
      $tmp[] = $key.":".$val;
  }
  else
  {
    foreach ($array as $key => $val)
      $tmp[] = $key;
  }

  if ($url)
    return "&amp;".$name."=".join(",", $tmp);
  else
    return stGetFormHiddenInput($name, join(",", $tmp))."\n";
}


function stGetTC($a, $column)
{
  global $setSortColumn;
  return "<".$a.($column == $setSortColumn ? " class=\"hilite\"" : "").">";
}


function stGetURLStr($cfilters, $col, $dir, $domfilters, $mfilters)
{
  global $sortDirs, $setHideColumns;
  $str  = "s=".$col."&amp;d=".$sortDirs[$dir]."&amp;mf=".$domfilters;
  $str .= stMakeList("f", TRUE, $cfilters, TRUE);
  $str .= stMakeList("h", TRUE, $setHideColumns, FALSE);
  $str .= stMakeList("m", TRUE, $mfilters, FALSE);
  return $str;
}


function stSortMatFunc($a, $b)
{
  global $setSortDirection, $setSortColumn;
  $index = $setSortColumn - 1;

  if ($a[$index] == $b[$index])
    return 0;
  
  $res = $a[$index] < $b[$index];
  
  if ($setSortDirection)
    return $res ? 1 : -1;
  else
    return $res ? -1 : 1;
}


//
// Actual code starts
//
$sortDirs = array("asc", "desc");

// Horrible kludge
$agent = $_SERVER["HTTP_USER_AGENT"];
if (stristr($agent, "GoogleBot") !== FALSE)
{
  header("HTTP/1.1 403 Forbidden");
  exit;
}

// Sorting column
$setSortColumn = intval(stGetRequestItem("s", 0, TRUE));
$setSortDirection = substr(strtolower(stGetRequestItem("d", "", TRUE)), 0, 1) == "d";

// Material column hiding
$setHideColumns = stParseList(stGetRequestItem("h", FALSE, TRUE), FALSE);

// Column filters
$setAttrFilters = stParseList(stGetRequestItem("f", FALSE, TRUE), TRUE);

// Material filters
$setMatFilters = stParseList(stGetRequestItem("m", FALSE, TRUE), FALSE);
$setEnableMatFilters = intval(stGetRequestItem("mf", 0, TRUE));


// Sort materials by selected column
if ($setSortColumn <= 0 || $setSortColumn >= 18)
{
  $setSortColumn = 0;
  if ($setSortDirection)
    krsort($matDataTable, SORT_STRING);
  else
    ksort($matDataTable, SORT_STRING);
}
else
  uasort($matDataTable, "stSortMatFunc");


// Print page header
cmPrintPageHeader($pageTitle, "
 <meta name=\"robots\" content=\"nofollow\" />
 <style type=\"text/css\">
    td a.matToggle {
            display: inline-block;
            background: black;
            width: 1em;
            font-size: 1.2em;
            padding: 0.1em;
            text-align: center;
            margin-right: 0.3em;
    }

    td a.matToggle:hover {
            text-decoration: none;
            background: white;
    }

    td a.matToggle.active {
            color: green !important;
    }

    td a.matToggle.inactive {
            color: red !important;
    }
 </style>
");
?>
<h1><? echo $pageTitle; ?></h1>
<form action="<? echo $pageIndex; ?>" method="get">
<?
echo
  stGetFormHiddenInput("s", $setSortColumn)."\n".
  stGetFormHiddenInput("d", $sortDirs[$setSortDirection])."\n".
  stGetFormHiddenInput("mf", $setEnableMatFilters)."\n".
  stMakeList("f", FALSE, $setAttrFilters, TRUE).
  stMakeList("m", FALSE, $setMatFilters, FALSE);
?>
<table class="optionsTable">
 <tr>
  <th>Hide columns</th>
  <th>Show only type</th>
  <td rowspan="3" class="infobox">
   [<a href="<? echo $pageIndex; ?>">Reset/Clear</a>] [<a href="info.php">Alloy browser</a>]
   - <b>CHANGE COLOURS/STYLE: <?
   for ($i = 1; $i <= 4; $i++)
     echo "<a href=\"?css=".$i."\">(".$i.")</a> "
   ?></b>
   <br />
   <ul>
    <li>All material data updated as of November 2011 with some updates in 2014 and 2015.</li>
    <li>This information is free to use in any way you wish. It would be nice to be credited, however.</li>
    <li>There may be bugs. If you notice any inaccuracies, contact <b>Ggr</b> @ Bat.</li>
    <li><b>Raw material data is also available in <a href="materials.csv">CSV format</a>, <a href="materials_short.txt">short tabular format</a> and <a href="materials_long.txt">long format</a></b>.</li>
    <li>You can sort by any column by clicking on the column name. Clicking again will change
    sorting direction (ascending/descending).</li>
    <li>Filtering to show only certain type of materials or materials with certain feature(s) is possible,
      for example <a href="index.php?s=0&amp;d=asc&amp;f[1]=5&amp;f[2]=6">show only 'incr. sturdy'
      AND 'highly magical'</a> materials. Click on desired feature (table cell) to add a filter.</li>
    <li>Columns can be disabled from the filtering list (shift or ctrl with mouse click to select several)</li>
    <li>The code for these pages and Perl + shellscript based backend utilities is available in
    <a href="http://pupunen.net/hg/misc/">Pupunen.net "misc" Mercurial repository</a>.</li>
   </ul>
  </td>
 </tr>
 <tr>
  <td>
   <select multiple="multiple" size="5" name="h[]">
<?
reset($matTransNames);
next($matTransNames);
while (list($key, $value) = each($matTransNames))
{
  echo "    <option".
    (isset($setHideColumns[$key]) ? " selected=\"selected\"" : "").
    " value=\"$key\">".chentities($value)."</option>\n";
}
?>
   </select>
  </td>
  <td>
   <select name="f[14]">
<?
$typeFilter = isset($setAttrFilters[14]) ? $setAttrFilters[14] : -1;
if ($typeFilter == -1) unset($setAttrFilters[14]);
$typeTable = $matTransTable[14];
$typeTable[-1] = "EVERYTHING";
ksort($typeTable);
foreach ($typeTable as $key => $value)
{
  echo "    <option value=\"$key\"".
    ($typeFilter == $key ? " selected=\"selected\"" : "").
    ">".chentities($value)."</option>\n";
}
?>
   </select>
  </td>
 </tr>
 <tr>
  <td colspan="2" class="icenter"><input type="submit" value=" Filter " class="isubmit" /></td>
 </tr>
</table>
</form>
<?
//
// List active filters
//
if (count($setAttrFilters) > 0)
{
  $filters = array();
  foreach ($setAttrFilters as $fkey => $fval)
  {
    if ($fkey >= 0)
      $filters[] = "<b>".strtolower($matTransNames[$fkey + 1])."</b>=".strtolower($matTransTable[$fkey][$fval]);
  }

  echo "<p>Filtering: ".implode(", ", $filters)."</p>\n";
}

if (count($setMatFilters) > 0)
{
  echo
    "<p>Material filter: ".implode(", ", array_keys($setMatFilters))." ".
    "<a href=\"?".stGetURLStr($setAttrFilters, $setSortColumn, $setSortDirection, !$setEnableMatFilters, $setMatFilters).
    "\">[".($setEnableMatFilters ? "Deactivate" : "Activate")."]</a></p>\n";
}


//
// Material table headers
//
echo
  "<div class=\"matTable\">\n".
  "<table class=\"matTable\">\n".
  " <tr>\n";

foreach ($matTransNames as $key => $value)
{
  if ($key == 0 || !isset($setHideColumns[$key]))
  {
    echo "  ".stGetTC("th", $key);
    if ($key < 18)
    {
      echo
      "<a href=\"?".
      stGetURLStr($setAttrFilters, $key,
      ($key == $setSortColumn) ? !$setSortDirection : $setSortDirection,
      $setEnableMatFilters, $setMatFilters).
      "\">".chentities($value)."</a>";
    }
    else
      echo chentities($value);

    echo "</th>\n";
  }
}
echo " </tr>\n";


//
// Material data rows
//
foreach ($matDataTable as $mname => $mdata)
{
  //
  // Check what filters apply here
  //
  $doShow = TRUE;
  foreach ($setAttrFilters as $fkey => $fval)
  if ($fval >= 0 && $mdata[$fkey] != $fval)
  {
    $doShow = FALSE;
    break;
  }

  if ($setEnableMatFilters && !isset($setMatFilters[$mname]))
    $doShow = FALSE;

  if ($doShow)
  {
    // Create temporary filter list for material filter link
    $tmpFilters = $setMatFilters;
    if (isset($tmpFilters[$mname]))
      unset($tmpFilters[$mname]);
    else
      $tmpFilters[$mname] = 1;

    // First column is material name
    $col = 0;
    echo
      " <tr>".stGetTC("td", $col++).
      "<a class=\"matToggle ".(isset($setMatFilters[$mname]) ? "active" : "inactive").
      "\" href=\"?".stGetURLStr($setAttrFilters, $setSortColumn, $setSortDirection, $setEnableMatFilters, $tmpFilters).
      "\">".(isset($setMatFilters[$mname]) ? "-" : "+")."</a>".
      "<a href=\"/mat/".urlencode($mname)."\">".chentities($mname)."</a>".
      "</td>";

    foreach ($mdata as $kkey => $kvalue)
    {
      if (!isset($setHideColumns[$kkey + 1]))
      {
        echo stGetTC("td", $col);
        if (is_array($kvalue))
        {
          // Arrays need special handling, and is bit messy
          reset($kvalue);

          if (list($nam, $lode) = each($kvalue))
            echo chentities($matTransTable[$kkey][$lode]);

          while (list($nam, $lode) = each($kvalue))
            echo ", ".chentities($matTransTable[$kkey][$lode]);
        }
        else
        {
          // Create temporary filter list for attribute filter link
          $tmpFilters = $setAttrFilters;
          if (isset($tmpFilters[$kkey]))
            unset($tmpFilters[$kkey]);
          else
            $tmpFilters[$kkey] = $kvalue;

          echo "<a href=\"?".
            stGetURLStr($tmpFilters, $setSortColumn, $setSortDirection, $setEnableMatFilters, $setMatFilters).
            "\">".chentities($matTransTable[$kkey][$kvalue])."</a>";
        }
        echo "</td>";
      }
      $col++;
    }
    echo "</tr>\n";
  }
}
echo
  "</table>\n".
  "</div>\n";

cmPrintPageFooter();
?>