Mercurial > hg > fapweb
diff showajax.php @ 139:75cf14ee99a7
More work on party information system.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Oct 2013 23:58:41 +0300 |
parents | aeebfedb5709 |
children | 20ca8edfb01a |
line wrap: on
line diff
--- a/showajax.php Tue Oct 22 22:05:00 2013 +0300 +++ b/showajax.php Tue Oct 22 23:58:41 2013 +0300 @@ -7,61 +7,232 @@ 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 stPrintCompoPage($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 stPrintRotationPage($page) +{ + echo + " <div class=\"showHeader\">\n". + " <img src=\"img/fapsm.png\" /><br />\n". + " </div>\n". + " <div class=\"showText\">\n". + $page["text"]. + " </div>\n"; +} + + +// Handle requests stSetupCacheControl(); -// Initiate SQL database connection if (!stConnectSQLDB()) die("Could not connect to SQL database."); -// Fetch non-"hardcoded" settings from SQL database -stReloadSettings(); + +// Fetch party display variables and settings +if (($res = stExecSQL("SELECT * FROM displayVars")) !== FALSE) +{ + $displayVars = array(); + $displayVarsChanged = array(); + + 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; + } +} +else + exit; + + +function stGetDisplayVar($name) +{ + global $displayVars; + if (isset($displayVars[$name])) + return $displayVars[$name]; + else + die("No display setting for '".$name."'.\n"); +} + + +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"); +} -// XMLHttp responses -$action = "ERROR"; -if (stChkRequestItem("action") && stChkRequestItem("type")) -{ - $action = $_REQUEST["action"]; - $type = $_REQUEST["type"]; -} - +$action = stChkRequestItem("action") ? $_REQUEST["action"] : ""; switch ($action) { - case "get": - // - // Get specific data - // - switch ($type) + case "check": + // Check if there has been any change + $changed = FALSE; + if (stGetDisplayVar("tempPageDuration") > 0 && stGetDisplayVar("tempPage") > 0) + { + $sql = stPrepareSQL("SELECT * FROM displayPages WHERE id=%d", + stGetDisplayVar("tempPage")); + + if (($res = stFetchSQL($sql)) !== false) + { + stSetDisplayVar("tempPageDuration", 0); + stSetDisplayVar("activePageMode", SMODE_ROTATE); + stSetDisplayVar("activePage", stGetDisplayVar("tempPage")); + stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("tempPageDuration")); + $changed = TRUE; + } + } + else + switch (stGetDisplayVar("showMode")) { - case "votes": - $sql = "SELECT * FROM votes ORDER BY utime DESC"; + case SMODE_ROTATE: + default: + stSetDisplayVar("activePageMode", SMODE_ROTATE); + + if (stGetDisplayVar("rotateList") == 0) + { + stSetDisplayVar("rotateList", 1); + stSetDisplayVar("activePageExpire", 0); + $changed = TRUE; + } + + if (time() >= stGetDisplayVar("activePageExpire") && + stGetDisplayVar("rotateList") > 0) + { + error_log("page rotated"); + + // Get list of pages from active rotation list + $list = stGetDisplayVar("rotateList"); + $sql = stPrepareSQL( + "SELECT * FROM displayListPages WHERE list_id=%d ORDER BY id", + $list); + + if (($pageList = stFetchSQL($sql)) !== false) + { + // Get page at current index + $index = stGetDisplayVar("rotateListIndex"); + + if (count($pageList) < $index) + { + $page_id = $pageList[$index]["page_id"]; + stSetDisplayVar("activePage", $page_id); + stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("rotatePageTime")); + } + + // Rotate to next page + if (++$index >= count($pageList)) + $index = 0; + + stSetDisplayVar("rotateListIndex", $index); + $changed = TRUE; + } + } + else + if (stGetDisplayVar("activePage") != -1) + { + // Emergency page + stSetDisplayVar("activePage", -1); + stSetDisplayVar("activePageExpire", time() + stGetDisplayVar("rotatePageTime")); + $changed = TRUE; + } + break; + + case SMODE_COMPO: break; } + + $changed = FALSE; - // - // Perform query if we need to, output results - // - if (isset($sql) && ($res = stExecSQLCond($sql, "")) !== FALSE) - { - if ($type == "votes") - { - } - } + + echo $changed ? "changed" : "nochange"; + stSetStatus(200, "OK"); break; - case "set": - // - // Set vote - // - if ($type == "votes" && stChkRequestItem("voter_id") && - stChkRequestItem("entry_id") && stChkRequestItem("vote")) + case "get": + // Based on the currently active mode ... + switch (stGetDisplayVar("activePageMode")) { - stGetCompoList(FALSE); + case SMODE_ROTATE: + // Page rotation mode, display currently active page + if (stGetDisplayVar("activePage") > 0) + { + $sql = stPrepareSQL("SELECT * FROM displayPages WHERE id=%d", + stGetDisplayVar("activePage")); + + if (($page = stFetchSQL($sql)) !== false) + stPrintRotationPage($page); + } + else + { + echo "<div>ERROR!</div>\n"; + } + break; - stSubmitOneVote(stGetRequestItem("voter_id"), stGetRequestItem("entry_id"), stGetRequestItem("vote")); + 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) + stPrintCompoPage($compo, $entry, $prev); + break; } - else - stSetStatus(902, "No data."); break; default: @@ -69,4 +240,21 @@ break; } + +// 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); +} ?> \ No newline at end of file