changeset 126:71c35d5302c2

More work on cleanups, and AJAX modularization.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Oct 2013 17:00:49 +0300
parents f364b50e07f7
children 6b71472cda77
files admin.inc.php ajax.js majax.php show.php showajax.php vote.inc.php
diffstat 6 files changed, 245 insertions(+), 167 deletions(-) [+]
line wrap: on
line diff
--- a/admin.inc.php	Tue Oct 22 16:20:47 2013 +0300
+++ b/admin.inc.php	Tue Oct 22 17:00:49 2013 +0300
@@ -1,5 +1,6 @@
 <?
 $sessionType = "admin";
+require "majax.php";
 
 function stCreateSettingsData()
 {
@@ -53,7 +54,7 @@
 {
 ?>
 <script type="text/javascript">
-// <? stCreateSettingsData(); echo "\n"; include "ajax.js"; ?>
+// <? stCreateSettingsData(); stCommonAJAX("admajax.php", "admlogout.php", FALSE); ?>
 
 
 function refreshItems(id,name,msgname)
--- a/ajax.js	Tue Oct 22 16:20:47 2013 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-function statusMsg(msg)
-{
-  document.getElementById("nstatus").innerHTML = msg;
-}
-
-
-function strtrim(str)
-{
-  if (!str || str == null)
-    return "";
-  return str.replace(/^\s+|\s+$/g,'')
-}
-
-
-function strencode(str)
-{
-  return encodeURIComponent(str);
-}
-
-
-function createXMLRequest()
-{
-  var req;
-  if (window.XMLHttpRequest)
-  {
-    // Modern browsers
-    req = new XMLHttpRequest();
-  }
-  else
-  {
-    // Old IE versions
-    req = new ActiveXObject("Microsoft.XMLHTTP");
-  }
-  return req;
-}
-
-
-function sendPOSTRequest(params, success, failure)
-{
-  var req = createXMLRequest();
-  req.open("POST", "admajax.php", true);
-  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-  req.setRequestHeader("Content-length", params.length);
-  req.setRequestHeader("Connection", "close");
-
-  req.onreadystatechange = function()
-  {
-    if (req.readyState == 4)
-    {
-      if (req.status == 404)
-      {
-        window.location = "admlogout.php";
-      }
-      else
-      if (req.status == 200)
-      {
-        if (success)
-        {
-          success(req.responseText);
-        }
-        statusMsg(req.statusText);
-      }
-      else
-      {
-        if (failure)
-        {
-          failure(req.status, req.statusText, req.responseText);
-        }
-        else
-        {
-          statusMsg("["+req.status+" - "+req.statusText+"] "+ req.responseText);
-        }
-      }
-    }
-  }
-  req.send(params);
-}
-
-
-//
-// Function for creating AJAX POST request arguments list based
-// on fields and giving them specified types. Also basic check
-// for validity can be performed (e.g. field empty or not)
-//
-function makePostArgs(fields, fprefix, fsuffix)
-{
-  var res = [];
-  for (var id in fields)
-  {
-    var elem = document.getElementById(fprefix + id + fsuffix);
-    if (!elem)
-    {
-      alert("No such DOM element '"+ fprefix + id + fsuffix +"'.");
-      return "";
-    }
-
-    switch (fields[id])
-    {
-      case 0:
-      case 1:
-      case 4:
-        {
-          var str = strtrim(elem.value);
-          if ((fields[id] == 1 || fields[id] == 4) && str == "")
-          {
-            alert("One or more of the required fields are empty.");
-            return "";
-          }
-          if (fields[id] == 4)
-            res.push(id+"="+parseInt(elem.value));
-          else
-            res.push(id+"="+strencode(str));
-        }
-        break;
-
-      case 2:
-        res.push(id+"="+parseInt(elem.value));
-        break;
-
-      case 3:
-        res.push(id+"="+(elem.checked ? "1" : "0"));
-        break;
-
-    }
-  }
-  return res.join("&");
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/majax.php	Tue Oct 22 17:00:49 2013 +0300
@@ -0,0 +1,139 @@
+<?
+function stCommonAJAX($backend, $failover, $script = FALSE)
+{
+if ($script)
+  echo "<script type=\"text/javascript\">\n";
+
+?>
+function statusMsg(msg)
+{
+  document.getElementById("nstatus").innerHTML = msg;
+}
+
+
+function strtrim(str)
+{
+  if (!str || str == null)
+    return "";
+  return str.replace(/^\s+|\s+$/g,'')
+}
+
+
+function strencode(str)
+{
+  return encodeURIComponent(str);
+}
+
+
+function createXMLRequest()
+{
+  var req;
+  if (window.XMLHttpRequest)
+  {
+    // Modern browsers
+    req = new XMLHttpRequest();
+  }
+  else
+  {
+    // Old IE versions
+    req = new ActiveXObject("Microsoft.XMLHTTP");
+  }
+  return req;
+}
+
+
+function sendPOSTRequest(params, success, failure)
+{
+  var req = createXMLRequest();
+  req.open("POST", "<? echo $backend ?>", true);
+  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+  req.setRequestHeader("Content-length", params.length);
+  req.setRequestHeader("Connection", "close");
+
+  req.onreadystatechange = function()
+  {
+    if (req.readyState == 4)
+    {
+      if (req.status == 404)
+      {
+        window.location = "<? echo $failover ?>";
+      }
+      else
+      if (req.status == 200)
+      {
+        if (success)
+        {
+          success(req.responseText);
+        }
+        statusMsg(req.statusText);
+      }
+      else
+      {
+        if (failure)
+        {
+          failure(req.status, req.statusText, req.responseText);
+        }
+        else
+        {
+          statusMsg("["+req.status+" - "+req.statusText+"] "+ req.responseText);
+        }
+      }
+    }
+  }
+  req.send(params);
+}
+
+
+//
+// Function for creating AJAX POST request arguments list based
+// on fields and giving them specified types. Also basic check
+// for validity can be performed (e.g. field empty or not)
+//
+function makePostArgs(fields, fprefix, fsuffix)
+{
+  var res = [];
+  for (var id in fields)
+  {
+    var elem = document.getElementById(fprefix + id + fsuffix);
+    if (!elem)
+    {
+      alert("No such DOM element '"+ fprefix + id + fsuffix +"'.");
+      return "";
+    }
+
+    switch (fields[id])
+    {
+      case 0:
+      case 1:
+      case 4:
+        {
+          var str = strtrim(elem.value);
+          if ((fields[id] == 1 || fields[id] == 4) && str == "")
+          {
+            alert("One or more of the required fields are empty.");
+            return "";
+          }
+          if (fields[id] == 4)
+            res.push(id+"="+parseInt(elem.value));
+          else
+            res.push(id+"="+strencode(str));
+        }
+        break;
+
+      case 2:
+        res.push(id+"="+parseInt(elem.value));
+        break;
+
+      case 3:
+        res.push(id+"="+(elem.checked ? "1" : "0"));
+        break;
+
+    }
+  }
+  return res.join("&");
+}
+<?
+if ($script)
+  echo "</script>\n";
+}
+?>
\ No newline at end of file
--- a/show.php	Tue Oct 22 16:20:47 2013 +0300
+++ b/show.php	Tue Oct 22 17:00:49 2013 +0300
@@ -2,30 +2,10 @@
 //
 // Entry display / show helper module
 //
-$sessionType = "admin";
 require "mconfig.inc.php";
 require "msite.inc.php";
 require "mcommon.inc.php";
-require "msession.inc.php";
-
-// Check if we are allowed to execute
-if (!stCheckHTTPS() || !stAdmSessionAuth(TRUE))
-{
-  stSetupCacheControl();
-
-  stSessionEnd(SESS_ADMIN);
-
-  stSetStatus(404, "Not Found");
-  
-  cmPrintPageHeader("Error");
-  echo "<div class=\"notice\">".
-  "<h1>Permission denied</h1>\n".
-  "<p>You need to be logged in as administrator to access this data.</p>\n".
-  "<p><a href=\"admin\">Click here for login form</a></p>\n".
-  "</div>\n";
-  cmPrintPageFooter();
-  exit;
-}
+require "majax.php";
 
 stSetupCacheControl();
 
@@ -36,25 +16,43 @@
 // Fetch non-"hardcoded" settings from SQL database
 stReloadSettings();
 
-cmPrintPageHeader("SHOW DISPLAY", "", FALSE);
+cmPrintPageHeader("PARTY DISPLAY", "", FALSE);
+stCommonAJAX("showajax.php", "show.php", TRUE);
 ?>
-<script type="text/javascript">
-// <? echo "\n"; include "ajax.js"; ?>
+
+<!-- ========================== -->
 
-</script>
 <div id="contents">
+
 <noscript>
-<div class="notice">
-<h1>Javascript required</h1>
-<p>
-The compo system page requires Javascript to be enabled for the AJAX functionality.
-<br />
-<a href="#">Please enable Javascript and reload this page</a>.
-</p>
+  <div class="notice">
+    <h1>Javascript required</h1>
+    <p>
+    The compo system page requires Javascript to be enabled for the AJAX functionality.
+    <br />
+    <a href="#">Please enable Javascript and reload this page</a>.
+    </p>
+  </div>
+</noscript>
+
+<div class="showView" id="view1"></div>
+<div class="showView" id="view2"></div>
+<div class="showView" id="view3"></div>
+
 </div>
-</noscript>
-</div>
+
+<!-- ========================== -->
+
 <script type="text/javascript">
+
+function tick()
+{
+  var nitem = document.getElementById("view1");
+  nitem.innerHTML = "<div class=\"notice\">Hello!</div>";
+}
+
+//setTimeout("tick();", 100);
+
 </script>
 <?
 cmPrintPageFooter(FALSE);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/showajax.php	Tue Oct 22 17:00:49 2013 +0300
@@ -0,0 +1,70 @@
+<?
+//
+// Party system / compoviewer AJAX request handler backend module
+//
+require "mconfig.inc.php";
+require "msite.inc.php";
+
+stSetupCacheControl();
+
+// Initiate SQL database connection
+if (!stConnectSQLDB())
+  die("Could not connect to SQL database.");
+
+// Fetch non-"hardcoded" settings from SQL database
+stReloadSettings();
+
+
+// XMLHttp responses
+$action = "ERROR";
+if (stChkRequestItem("action") && stChkRequestItem("type"))
+{
+  $action = $_REQUEST["action"];
+  $type = $_REQUEST["type"];
+}
+
+switch ($action)
+{
+  case "get":
+    //
+    // Get specific data
+    //
+    switch ($type)
+    {
+      case "votes":
+        $sql = "SELECT * FROM votes ORDER BY utime DESC";
+        break;
+    }
+    
+    //
+    // Perform query if we need to, output results
+    //
+    if (isset($sql) && ($res = stExecSQLCond($sql, "")) !== FALSE)
+    {
+      if ($type == "votes")
+      {
+      }
+    }
+    break;
+
+  case "set":
+    //
+    // Set vote
+    //
+    if ($type == "votes" && stChkRequestItem("voter_id") &&
+      stChkRequestItem("entry_id") && stChkRequestItem("vote"))
+    {
+      stGetCompoList(FALSE);
+      
+      stSubmitOneVote(stGetRequestItem("voter_id"), stGetRequestItem("entry_id"), stGetRequestItem("vote"));
+    }
+    else
+      stSetStatus(902, "No data.");
+    break;
+
+  default:
+    stSetStatus(404, "Not Found");
+    break;
+}
+
+?>
\ No newline at end of file
--- a/vote.inc.php	Tue Oct 22 16:20:47 2013 +0300
+++ b/vote.inc.php	Tue Oct 22 17:00:49 2013 +0300
@@ -1,5 +1,6 @@
 <?
 $sessionType = "user";
+require "majax.php";
 $userKeyLen = stGetSetting("userKeyLength");
 
 
@@ -38,11 +39,7 @@
 else
 if (($mode = stGetSessionItem("mode")) == "vote")
 {
-?>
-<script type="text/javascript">
-// <? include "ajax.js"; ?>
-</script>
-<?
+  stCommonAJAX("usrajax.php", "usrlogout.php", TRUE);
   stGetCompoList(TRUE, TRUE);
 
   // Try fetching previously stored votes