Example #1
0
function Init()
{
    LoadConfig();
    LoadUsers();
    LoadEntries();
    LoadThemes();
    LoadAssets();
    InitStream();
    GetNextJamDateAndTime();
}
function LoadEntries()
{
    global $dictionary, $jams, $authors, $entries, $users, $dbConn;
    //Clear public lists which get updated by this function
    $dictionary["jams"] = array();
    $dictionary["authors"] = array();
    $jams = array();
    $authors = array();
    $entries = array();
    //Create lists of jams and jam entries
    $authorList = array();
    $firstJam = true;
    $jamFromStart = 1;
    $totalEntries = 0;
    $largest_jam_number = -1;
    $sql = "SELECT * FROM jam WHERE jam_deleted = 0 ORDER BY jam_jam_number DESC";
    $data = mysqli_query($dbConn, $sql);
    $sql = "";
    while ($info = mysqli_fetch_array($data)) {
        //Read data about the jam
        $newData = array();
        $newData["jam_number"] = intval($info["jam_jam_number"]);
        $newData["start_time"] = $info["jam_start_datetime"];
        $newData["jam_id"] = intval($info["jam_id"]);
        $newData["jam_number_ordinal"] = ordinal(intval($info["jam_jam_number"]));
        $newData["theme"] = $info["jam_theme"];
        $newData["theme_visible"] = $info["jam_theme"];
        //Used for administration
        $newData["date"] = date("d M Y", strtotime($info["jam_start_datetime"]));
        $newData["time"] = date("G:i", strtotime($info["jam_start_datetime"]));
        $newData["minutes_to_jam"] = floor((strtotime($info["jam_start_datetime"] . " UTC") - time()) / 60);
        $newData["entries"] = array();
        $newData["first_jam"] = $firstJam;
        $newData["entries_visible"] = $jamFromStart <= 2;
        if ($firstJam) {
            $firstJam = false;
        }
        $sql = "SELECT * FROM entry WHERE entry_deleted = 0 AND entry_jam_id = " . $newData["jam_id"] . " ORDER BY entry_id ASC";
        $data2 = mysqli_query($dbConn, $sql);
        $sql = "";
        $i = 0;
        while ($info2 = mysqli_fetch_array($data2)) {
            $entry = array();
            $entry["title"] = $info2["entry_title"];
            $entry["title_url_encoded"] = urlencode($info2["entry_title"]);
            $entry["description"] = $info2["entry_description"];
            $author_username = $info2["entry_author"];
            $author = $author_username;
            $author_display = $author_username;
            if (isset($users[$author_username]["display_name"])) {
                $author_display = $users[$author_username]["display_name"];
            }
            $entry["author_display"] = $author_display;
            $entry["author"] = $author;
            $entry["author_url_encoded"] = urlencode($author);
            $entry["url"] = str_replace("'", "\\'", $info2["entry_url"]);
            $entry["url_web"] = str_replace("'", "\\'", $info2["entry_url_web"]);
            $entry["url_windows"] = str_replace("'", "\\'", $info2["entry_url_windows"]);
            $entry["url_mac"] = str_replace("'", "\\'", $info2["entry_url_mac"]);
            $entry["url_linux"] = str_replace("'", "\\'", $info2["entry_url_linux"]);
            $entry["url_ios"] = str_replace("'", "\\'", $info2["entry_url_ios"]);
            $entry["url_android"] = str_replace("'", "\\'", $info2["entry_url_android"]);
            $entry["screenshot_url"] = str_replace("'", "\\'", $info2["entry_screenshot_url"]);
            if ($entry["url"] != "") {
                $entry["has_url"] = 1;
            }
            if ($entry["url_web"] != "") {
                $entry["has_url_web"] = 1;
            }
            if ($entry["url_windows"] != "") {
                $entry["has_url_windows"] = 1;
            }
            if ($entry["url_mac"] != "") {
                $entry["has_url_mac"] = 1;
            }
            if ($entry["url_linux"] != "") {
                $entry["has_url_linux"] = 1;
            }
            if ($entry["url_ios"] != "") {
                $entry["has_url_ios"] = 1;
            }
            if ($entry["url_android"] != "") {
                $entry["has_url_android"] = 1;
            }
            $entry["jam_number"] = $newData["jam_number"];
            $entry["jam_theme"] = $newData["theme"];
            $hasTitle = false;
            $hasDesc = false;
            $hasSS = false;
            if ($entry["screenshot_url"] != "logo.png" && $entry["screenshot_url"] != "") {
                $entry["has_screenshot"] = 1;
                $hasSS = true;
            }
            if (trim($entry["title"]) != "") {
                $entry["has_title"] = 1;
                $hasTitle = true;
            }
            if (trim($entry["description"]) != "") {
                $entry["has_description"] = 1;
                $hasDesc = true;
            }
            if (isset($authorList[$author])) {
                $authorList[$author]["entry_count"] += 1;
                if (intval($newData["jam_number"]) < intval($authorList[$author]["first_jam_number"])) {
                    $authorList[$author]["first_jam_number"] = $newData["jam_number"];
                }
                if (intval($newData["jam_number"]) > intval($authorList[$author]["last_jam_number"])) {
                    $authorList[$author]["last_jam_number"] = $newData["jam_number"];
                }
                $authorList[$author]["entries"][] = $entry;
            } else {
                if (isset($users[$author])) {
                    $authorList[$author] = $users[$author];
                } else {
                    //Author does not have matching account (very old entry)
                    $authorList[$author] = array("username" => $author, "display_name" => $author_display);
                }
                $authorList[$author]["entry_count"] = 1;
                $authorList[$author]["first_jam_number"] = $newData["jam_number"];
                $authorList[$author]["last_jam_number"] = $newData["jam_number"];
                $authorList[$author]["entries"][] = $entry;
            }
            $newData["entries"][$i] = $entry;
            $entries[] = $entry;
            $i++;
        }
        $totalEntries += count($newData["entries"]);
        $newData["entries_count"] = count($newData["entries"]);
        //Hide theme of not-yet-started jams
        $now = new DateTime();
        $datetime = new DateTime($newData["start_time"] . " UTC");
        $timeUntilJam = date_diff($datetime, $now);
        if ($datetime > $now) {
            $newData["theme"] = "Not yet announced";
            $newData["jam_started"] = false;
            if ($timeUntilJam->days > 0) {
                $newData["time_left"] = $timeUntilJam->format("%a days %H:%I:%S");
            } else {
                if ($timeUntilJam->h > 0) {
                    $newData["time_left"] = $timeUntilJam->format("%H:%I:%S");
                } else {
                    if ($timeUntilJam->i > 0) {
                        $newData["time_left"] = $timeUntilJam->format("%I:%S");
                    } else {
                        if ($timeUntilJam->s > 0) {
                            $newData["time_left"] = $timeUntilJam->format("%S seconds");
                        } else {
                            $newData["time_left"] = "Now!";
                        }
                    }
                }
            }
        } else {
            $newData["jam_started"] = true;
        }
        //Insert into dictionary array
        $dictionary["jams"][] = $newData;
        if ($largest_jam_number < intval($newData["jam_number"])) {
            $largest_jam_number = intval($newData["jam_number"]);
            $dictionary["current_jam"] = $newData;
        }
        $jams[] = $newData;
        $jamFromStart++;
    }
    $dictionary["all_entries_count"] = $totalEntries;
    //Insert authors into dictionary
    foreach ($authorList as $k => $authorData) {
        $dictionary["authors"][] = $authorData;
        //Update users list with entry count for each
        foreach ($dictionary["users"] as $i => $dictUserInfo) {
            if ($dictUserInfo["username"] == $k) {
                $dictionary["users"][$i]["entry_count"] = $authorData["entry_count"];
                $dictionary["users"][$i]["first_jam_number"] = $authorData["first_jam_number"];
                $dictionary["users"][$i]["last_jam_number"] = $authorData["last_jam_number"];
            }
        }
        //Update admins list with entry count for each
        foreach ($dictionary["admins"] as $i => $dictUserInfo) {
            if ($dictUserInfo["username"] == $k) {
                $dictionary["admins"][$i]["entry_count"] = $authorData["entry_count"];
                $dictionary["admins"][$i]["first_jam_number"] = $authorData["first_jam_number"];
                $dictionary["admins"][$i]["last_jam_number"] = $authorData["last_jam_number"];
            }
        }
        //Update registered users list with entry count for each
        foreach ($dictionary["registered_users"] as $i => $dictUserInfo) {
            if ($dictUserInfo["username"] == $k) {
                $dictionary["registered_users"][$i]["entry_count"] = $authorData["entry_count"];
                $dictionary["registered_users"][$i]["first_jam_number"] = $authorData["first_jam_number"];
                $dictionary["registered_users"][$i]["last_jam_number"] = $authorData["last_jam_number"];
            }
        }
        $authors[$authorData["username"]] = $authorData;
    }
    $dictionary["all_authors_count"] = count($authors);
    $dictionary["all_jams_count"] = count($jams);
    GetNextJamDateAndTime();
}