view showajax.php @ 152:6e6fba2da3d1

Add some comments.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 25 Oct 2013 02:10:14 +0300
parents 6d4325451570
children 1386a7a8816c
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 stPrintCompoSlide($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 stPrintRotationSlide($slide)
{
  echo
  "  <div class=\"showHeader\">\n".
  "    <img src=\"img/fapsm.png\" /><br />\n".
  "  </div>\n".
  "  <div class=\"showText\">\n".
  $slide["text"].
  "  </div>\n";
}

//
// Initialize
//
stSetupCacheControl();

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

stReloadDisplayVars();


//
// Check if the slide needs updating?
//
$updated = FALSE;


// Temporary slides are handled globally
if (stGetDisplayVar("tempDuration") > 0 &&
    stGetDisplayVar("tempSlide") > 0 &&
    stGetDisplayVar("tempSlideSet"))
{
  $sql = stPrepareSQL("SELECT * FROM displaySlides WHERE id=%d",
    stGetDisplayVar("tempSlide"));

  if (($res = stFetchSQL($sql)) !== false)
  {
    stSetDisplayVar("tempSlideSet", FALSE);
    stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
    stSetDisplayVar("activeSlide", stGetDisplayVar("tempSlide"));
    stSetDisplayVar("activeSlideExpire", time() + stGetDisplayVar("tempDuration"));
    $updated = TRUE;
  }
}
else
// Otherwise we act according to global show mode
switch (stGetDisplayVar("showMode"))
{
  case SMODE_ROTATE:
  default:
    //
    // Rotation / normal slide show mode
    //
    stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
    
    if (stGetDisplayVar("rotateList") == 0)
    {
      stSetDisplayVar("rotateList", 1);
      stSetDisplayVar("activeSlideExpire", 0);
      $updated = TRUE;
    }

    if (time() >= stGetDisplayVar("activeSlideExpire") &&
        stGetDisplayVar("rotateList") > 0)
    {
      // Get list of slides from active rotation list
      $list = stGetDisplayVar("rotateList");
      $sql = stPrepareSQL(
        "SELECT * FROM displayListSlides WHERE list_id=%d ORDER BY id",
        $list);

      if (($slideList = stExecSQL($sql)) !== false)
      {
        // Get slide at current index
        $slides = array();
        foreach ($slideList as $slide)
          $slides[] = $slide;
        
        $index = stGetDisplayVar("rotateListIndex");
        if (count($slides) > $index)
        {
          $slide_id = $slides[$index]["slide_id"];
          if ($slide_id != stGetDisplayVar("activeSlide"))
          {
            stSetDisplayVar("activeSlide", $slide_id);
            stSetDisplayVar("activeSlideExpire", time() + stGetDisplayVar("rotateDuration"));
            $updated = TRUE;
          }
        }

        // Rotate to next slide
        if (++$index >= count($slides))
          $index = 0;
        
        stSetDisplayVar("rotateListIndex", $index);
      }
    }
    break;
  
  case SMODE_COMPO:
    //
    // Competition mode, is controlled from admin UI, so we donẗ
    // actually do anything here.
    //
    break;
}

// Check if we need to update the "last updated" timestamp
if ($updated)
  stSetDisplayVar("lastUpdate", time());


//
// Serve the request
//
$type = stGetRequestItem("type");
switch (stGetRequestItem("action"))
{
  case "check":
    // Check if there has been any change
    $changed = stGetRequestItem("lastUpdate") != stGetDisplayVar("lastUpdate");
    echo $changed ? "changed" : "nochange";
    stSetStatus(200, "OK");
    break;

  case "get":
    switch ($type)
    {
      case "update":
        echo stGetDisplayVar("lastUpdate");
        break;

      case "slide":
      // Based on the currently active mode ...
      switch (stGetDisplayVar("activeSlideMode"))
      {
        case SMODE_ROTATE:
          // Slide rotation mode, display currently active slide
          if (stGetDisplayVar("activeSlide") > 0)
          {
            $sql = stPrepareSQL("SELECT * FROM displaySlides WHERE id=%d",
              stGetDisplayVar("activeSlide"));

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

          $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)
            stPrintCompoSlide($compo, $entry, $prev);
          break;
      }
      break;
    }
    break;

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


// Save changed variables
stSaveDisplayVars();
?>