changeset 93:f36ebd03afd6

User AJAX.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Oct 2013 20:26:03 +0300
parents 8efda89e765a
children 6edd7d623eab
files usrajax.php
diffstat 1 files changed, 113 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usrajax.php	Fri Oct 18 20:26:03 2013 +0300
@@ -0,0 +1,113 @@
+<?
+//
+// AJAX request handler backend module
+//
+$sessionType = "user";
+require "mconfig.inc.php";
+require "msite.inc.php";
+require "msession.inc.php";
+
+
+function stSubmitOneVote($voter_id, $entry_id, $vote)
+{
+  $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
+    $voter_id, $entry_id);
+
+  if (($res = stFetchSQLColumn($sql)) === false)
+  {
+    $sql = stPrepareSQL(
+      "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
+      $voter_id, $entry_id, $vote);
+
+    if (stExecSQL($sql) === false)
+      return FALSE;
+  }
+  else
+  {
+    $sql = stPrepareSQL(
+      "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
+      $vote, $voter_id, $eid);
+
+    if (stExecSQL($sql) === false)
+      return FALSE;
+  }
+
+  return TRUE;
+}
+
+
+
+// Check if we are allowed to execute
+if (!stUserSessionAuth())
+{
+  stSetupCacheControl();
+
+  stSessionEnd(SESS_USER);
+
+  header("Location: news");
+  exit;
+}
+
+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