view showajax.php @ 143:20893a5442b7

Move some functions to site module, and use them.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Oct 2013 01:21:12 +0300
parents d2e9285b69ad
children c030c3cf0d80
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?
//
if (stGetDisplayVar("tempDuration") > 0 && stGetDisplayVar("tempSlide") > 0)
{
  $sql = stPrepareSQL("SELECT * FROM displaySlides WHERE id=%d",
    stGetDisplayVar("tempSlide"));

  if (($res = stFetchSQL($sql)) !== false)
  {
    stSetDisplayVar("tempDuration", 0);
    stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
    stSetDisplayVar("activeSlide", stGetDisplayVar("tempSlide"));
    stSetDisplayVar("activeSlideExpire", time() + stGetDisplayVar("tempDuration"));
  }
}
else
switch (stGetDisplayVar("showMode"))
{
  case SMODE_ROTATE:
  default:
    stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
    
    if (stGetDisplayVar("rotateList") == 0)
    {
      stSetDisplayVar("rotateList", 1);
      stSetDisplayVar("activeSlideExpire", 0);
    }

    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"));
          }
        }

        // Rotate to next slide
        if (++$index >= count($slides))
          $index = 0;
        
        stSetDisplayVar("rotateListIndex", $index);
      }
    }
    break;
  
  case SMODE_COMPO:
    break;
}


$action = stChkRequestItem("action") ? $_REQUEST["action"] : "";
switch ($action)
{
  case "check":
    // Check if there has been any change
    $changed =
      stGetRequestItem("activeSlide") != stGetDisplayVar("activeSlide") ||
      stGetRequestItem("activeSlide") != stGetDisplayVar("activeSlide");
      
    echo $changed ? "changed" : "nochange";
    stSetStatus(200, "OK");
    break;

  case "get":
    // 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
        $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)
          stPrintCompoSlide($compo, $entry, $prev);
        break;
    }
    break;

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


// Save changed variables
stSaveDisplayVars();
?>