view showajax.php @ 139:75cf14ee99a7

More work on party information system.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Oct 2013 23:58:41 +0300
parents aeebfedb5709
children 20ca8edfb01a
line wrap: on
line source

<?
//
// FAPWeb Simple Demoparty System
// Party information display system AJAX backend module
// (C) Copyright 2012-2013 Tecnic Software productions (TNSP)
//
require "mconfig.inc.php";
require "msite.inc.php";


function stPrintCompoEntry($entry, $class, $compo, $title = false)
{
  echo
  "  <div class=\"".$class."\">\n"; 
  
  if ($title !== false)
  echo
  "    <div class=\"title\">".$title."</div>\n";

  echo
  "    <div class=\"entryIndex\">#".$entry["show_id"]."</div>\n".
  "    <div class=\"entryName\">".chentities($entry["name"])."</div>\n";

  if ($compo["showAuthors"])
    echo "    <div class=\"entryAuthor\">".chentities($entry["author"])."</div>\n";

  echo
  "    <div class=\"entryInfo\">".chentities($entry["info"])."</div>\n".
  "  </div>\n";
}

function stPrintCompoPage($compo, $entry, $prev)
{
  echo
  "  <div class=\"compoHeader\">\n".
  "    <img src=\"img/fapsm.png\" /><br />\n".
  "    <div class=\"title\">".chentities($compo["name"])." competition</div>\n".
  "  </div>\n";
  
  if ($entry !== false)
  stPrintCompoEntry($entry, "compoNext", $compo, false);
  
  if ($prev !== false)
    stPrintCompoEntry($prev, "compoPrev", $compo, "Previous entry");
}


function stPrintRotationPage($page)
{
  echo
  "  <div class=\"showHeader\">\n".
  "    <img src=\"img/fapsm.png\" /><br />\n".
  "  </div>\n".
  "  <div class=\"showText\">\n".
  $page["text"].
  "  </div>\n";
}


// Handle requests
stSetupCacheControl();

if (!stConnectSQLDB())
  die("Could not connect to SQL database.");


// Fetch party display variables and settings
if (($res = stExecSQL("SELECT * FROM displayVars")) !== FALSE)
{
  $displayVars = array();
  $displayVarsChanged = array();

  foreach ($res as $row)
  {
    switch ($row["vtype"])
    {
      case VT_INT:  $val = intval($row["vint"]); break;
      case VT_BOOL: $val = intval($row["vint"]) ? true : false; break;
      case VT_STR:  $val = $row["vstr"]; break;
      case VT_TEXT: $val = $row["vtext"]; break;
    }
    $displayVars[$row["key"]] = $val;
  }
}
else
  exit;


function stGetDisplayVar($name)
{
  global $displayVars;
  if (isset($displayVars[$name]))
    return $displayVars[$name];
  else
    die("No display setting for '".$name."'.\n");
}


function stSetDisplayVar($name, $value)
{
  global $displayVars, $displayVarsChanged;
  if (isset($displayVars[$name]))
  {
    $displayVars[$name] = $value;
    $displayVarsChanged[$name] = true;
  }
  else
    die("No display setting for '".$name."'.\n");
}


$action = stChkRequestItem("action") ? $_REQUEST["action"] : "";
switch ($action)
{
  case "check":
    // Check if there has been any change
    $changed = FALSE;
    if (stGetDisplayVar("tempPageDuration") > 0 && stGetDisplayVar("tempPage") > 0)
    {
      $sql = stPrepareSQL("SELECT * FROM displayPages WHERE id=%d",
        stGetDisplayVar("tempPage"));

      if (($res = stFetchSQL($sql)) !== false)
      {
        stSetDisplayVar("tempPageDuration", 0);
        stSetDisplayVar("activePageMode", SMODE_ROTATE);
        stSetDisplayVar("activePage", stGetDisplayVar("tempPage"));
        stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("tempPageDuration"));
        $changed = TRUE;
      }
    }
    else
    switch (stGetDisplayVar("showMode"))
    {
      case SMODE_ROTATE:
      default:
        stSetDisplayVar("activePageMode", SMODE_ROTATE);
        
        if (stGetDisplayVar("rotateList") == 0)
        {
          stSetDisplayVar("rotateList", 1);
          stSetDisplayVar("activePageExpire", 0);
          $changed = TRUE;
        }

        if (time() >= stGetDisplayVar("activePageExpire") &&
            stGetDisplayVar("rotateList") > 0)
        {
          error_log("page rotated");

          // Get list of pages from active rotation list
          $list = stGetDisplayVar("rotateList");
          $sql = stPrepareSQL(
            "SELECT * FROM displayListPages WHERE list_id=%d ORDER BY id",
            $list);

          if (($pageList = stFetchSQL($sql)) !== false)
          {
            // Get page at current index
            $index = stGetDisplayVar("rotateListIndex");

            if (count($pageList) < $index)
            {
              $page_id = $pageList[$index]["page_id"];
              stSetDisplayVar("activePage", $page_id);
              stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("rotatePageTime"));
            }

            // Rotate to next page
            if (++$index >= count($pageList))
              $index = 0;
            
            stSetDisplayVar("rotateListIndex", $index);
            $changed = TRUE;
          }
        }
        else
        if (stGetDisplayVar("activePage") != -1)
        {
          // Emergency page
          stSetDisplayVar("activePage", -1);
          stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("rotatePageTime"));
          $changed = TRUE;
        }
        break;
      
      case SMODE_COMPO:
        break;
    }

    $changed = FALSE;
    
    
    echo $changed ? "changed" : "nochange";
    stSetStatus(200, "OK");
    break;

  case "get":
    // Based on the currently active mode ...
    switch (stGetDisplayVar("activePageMode"))
    {
      case SMODE_ROTATE:
        // Page rotation mode, display currently active page
        if (stGetDisplayVar("activePage") > 0)
        {
          $sql = stPrepareSQL("SELECT * FROM displayPages WHERE id=%d",
            stGetDisplayVar("activePage"));

          if (($page = stFetchSQL($sql)) !== false)
            stPrintRotationPage($page);
        }
        else
        {
          echo "<div>ERROR!</div>\n";
        }
        break;
      
      case SMODE_COMPO:
        // Competition mode
        $compo = stFetchSQL(stPrepareSQL(
          "SELECT * FROM compos WHERE id=%d",
          stGetDisplayVar("compoCompo")));

        $prev = stFetchSQL(stPrepareSQL(
          "SELECT * FROM entries WHERE id=%d",
          stGetDisplayVar("compoPrevEntry")));

        $entry = stFetchSQL(stPrepareSQL(
          "SELECT * FROM entries WHERE id=%d",
          stGetDisplayVar("compoCurrEntry")));

        if ($compo !== false)
          stPrintCompoPage($compo, $entry, $prev);
        break;
    }
    break;

  default:
    stSetStatus(404, "Not Found");
    break;
}


// Save changed variables
foreach (stExecSQL("SELECT * FROM displayVars") as $item)
if (isset($displayVarsChanged[$item["key"]]))
{
  $val = $displayVars[$item["key"]];
  switch ($item["vtype"])
  {
    case VT_INT:  $vsql = stPrepareSQL("vint=%d", $val); break;
    case VT_BOOL: $vsql = stPrepareSQL("vint=%d", $val ? 1 : 0); break;
    case VT_STR:  $vsql = stPrepareSQL("vstr=%s", $val); break;
    case VT_TEXT: $vsql = stPrepareSQL("vtext=%s", $val); break;
  }
  
  $sql = "UPDATE displayVars SET ".$vsql." WHERE key=".$db->quote($item["key"]);
  stExecSQL($sql);
}
?>