changeset 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
files msite.inc.php showajax.php
diffstat 2 files changed, 136 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/msite.inc.php	Wed Oct 23 00:47:33 2013 +0300
+++ b/msite.inc.php	Wed Oct 23 01:21:12 2013 +0300
@@ -105,6 +105,75 @@
 }
 
 
+function stReloadDisplayVars()
+{
+  global $displayVars, $displayVarsChanged;
+
+  $displayVars = array();
+  $displayVarsChanged = array();
+
+  if (($res = stExecSQL("SELECT * FROM displayVars")) !== FALSE)
+  {
+    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;
+    }
+  }
+}
+
+
+function stSaveDisplayVars()
+{
+  global $displayVars, $displayVarsChanged;
+
+  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);
+  }
+}
+
+
+function stGetDisplayVar($name)
+{
+  global $displayVars;
+  if (isset($displayVars[$name]))
+    return $displayVars[$name];
+  else
+    die("No display var for '".$name."'.\n");
+}
+
+
+function stSetDisplayVar($name, $value)
+{
+  global $displayVars, $displayVarsChanged;
+  if (isset($displayVars[$name]))
+  {
+    $displayVars[$name] = $value;
+    $displayVarsChanged[$name] = true;
+  }
+  else
+    die("No display var for '".$name."'.\n");
+}
+
+
 function dhentities($str)
 {
   return str_replace(array("&lt;","&gt;"), array("<", ">"), htmlentities($str, ENT_NOQUOTES, "UTF-8"));
--- a/showajax.php	Wed Oct 23 00:47:33 2013 +0300
+++ b/showajax.php	Wed Oct 23 01:21:12 2013 +0300
@@ -56,56 +56,84 @@
   "  </div>\n";
 }
 
-
-// Handle requests
+//
+// Initialize
+//
 stSetupCacheControl();
 
 if (!stConnectSQLDB())
   die("Could not connect to SQL database.");
 
+stReloadDisplayVars();
 
-// Fetch party display variables and settings
-if (($res = stExecSQL("SELECT * FROM displayVars")) !== FALSE)
+
+//
+// Check if the slide needs updating?
+//
+if (stGetDisplayVar("tempDuration") > 0 && stGetDisplayVar("tempSlide") > 0)
 {
-  $displayVars = array();
-  $displayVarsChanged = array();
+  $sql = stPrepareSQL("SELECT * FROM displaySlides WHERE id=%d",
+    stGetDisplayVar("tempSlide"));
 
-  foreach ($res as $row)
+  if (($res = stFetchSQL($sql)) !== false)
   {
-    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;
+    stSetDisplayVar("tempDuration", 0);
+    stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
+    stSetDisplayVar("activeSlide", stGetDisplayVar("tempSlide"));
+    stSetDisplayVar("activeSlideExpire", time() + stGetDisplayVar("tempDuration"));
   }
 }
 else
-  exit;
-
-
-function stGetDisplayVar($name)
+switch (stGetDisplayVar("showMode"))
 {
-  global $displayVars;
-  if (isset($displayVars[$name]))
-    return $displayVars[$name];
-  else
-    die("No display setting for '".$name."'.\n");
-}
+  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"));
+          }
+        }
 
-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");
+        // Rotate to next slide
+        if (++$index >= count($slides))
+          $index = 0;
+        
+        stSetDisplayVar("rotateListIndex", $index);
+      }
+    }
+    break;
+  
+  case SMODE_COMPO:
+    break;
 }
 
 
@@ -114,85 +142,16 @@
 {
   case "check":
     // Check if there has been any change
-    $changed = FALSE;
-    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"));
-        $changed = TRUE;
-      }
-    }
-    else
-    switch (stGetDisplayVar("showMode"))
-    {
-      case SMODE_ROTATE:
-      default:
-        stSetDisplayVar("activeSlideMode", SMODE_ROTATE);
-        
-        if (stGetDisplayVar("rotateList") == 0)
-        {
-          stSetDisplayVar("rotateList", 1);
-          stSetDisplayVar("activeSlideExpire", 0);
-          $changed = TRUE;
-        }
-
-        if (time() >= stGetDisplayVar("activeSlideExpire") &&
-            stGetDisplayVar("rotateList") > 0)
-        {
-              error_log("Y");
-          // 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"));
-                $changed = TRUE;
-                error_log("rotated: ".$slide_id);
-              }
-            }
-
-            // Rotate to next slide
-            if (++$index >= count($slides))
-              $index = 0;
-            
-            stSetDisplayVar("rotateListIndex", $index);
-          }
-        }
-        break;
+    $changed =
+      stGetRequestItem("activeSlide") != stGetDisplayVar("activeSlide") ||
+      stGetRequestItem("activeSlide") != stGetDisplayVar("activeSlide");
       
-      case SMODE_COMPO:
-        break;
-    }
-
     echo $changed ? "changed" : "nochange";
     stSetStatus(200, "OK");
     break;
 
   case "get":
     // Based on the currently active mode ...
-    error_log("GET");
     switch (stGetDisplayVar("activeSlideMode"))
     {
       case SMODE_ROTATE:
@@ -238,19 +197,5 @@
 
 
 // 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);
-}
+stSaveDisplayVars();
 ?>
\ No newline at end of file