/**
 * This function populates the main content of the classes.php: the classes. Each class is displayed as a well HTML
 * element. Additionally, this function calls populatePagination.
 * @param $conn object the opened database connection
 * @param $prefix string the currently selected prefix
 * @param $search string the search query
 * @param $sort string the currently selected sort parameter
 * @param $page int the page to display
 * @param $isAdmin boolean whether or not the logged-in user is an administrator
 */
function populateData($conn, $prefix, $search, $sort, $page, $isAdmin)
{
    try {
        //includes possibilitied for sort and search depending on what was selected
        if ($prefix != "") {
            $p = "'" . $prefix . "'";
            $var = $conn->prepare("SELECT * FROM `categories` WHERE `prefix` = {$p} AND `title` LIKE '%{$search}%' ORDER BY {$sort}");
            $count = $conn->query("SELECT COUNT(`title`) FROM `categories` WHERE `prefix` = {$p} AND `title` LIKE '%{$search}%'")->fetchColumn();
        } else {
            $var = $conn->prepare("SELECT * FROM `categories` WHERE `title` LIKE '%{$search}%' ORDER BY {$sort}");
            $count = $conn->query("SELECT COUNT(`title`) FROM `categories` WHERE `title` LIKE '%{$search}%'")->fetchColumn();
        }
        //count will be used for pagination
        if ($count == NULL) {
            $count = 0;
        }
        //execute prepared statements
        $var->execute();
        //c will also be used for pagination
        $c = 0;
        $p = $page != "" ? intval($page) : 1;
        //print them in columns
        echo "<div class='row'>";
        while ($result = $var->fetch(PDO::FETCH_ASSOC)) {
            if ($c >= ($p - 1) * 18 && $c < $p * 18) {
                //to get the number of links
                $l = $conn->query("SELECT COUNT(link_id) FROM `links` WHERE `category_id` = '" . $result["category_id"] . "'")->fetchColumn();
                if ($l == NULL) {
                    $l = 0;
                }
                //print out the boxes
                echo "<div class='col-md-6'><a href='links.php?class=" . $result["category_id"] . "''>";
                echo "<div class='well well-sm well-hover'>";
                // display the prefix for the class
                echo "<h6 class='text-muted'>" . $result["prefix"] . "</h6>";
                // class title
                echo "<h4 class='truncate'>" . $result["title"] . "</h4>";
                // number of links
                echo "<p>Contains " . $l . ($l != 1 ? " links" : " link") . ".</p>";
                // metadata
                echo "<p class='text-muted small info-text'>";
                // submitted by
                echo "<span class='pull-left'>submitted by " . $result["rcs_id"] . "</span>";
                // creation date
                echo "<span class='pull-right'>" . $result["creation_date"] . "</span>";
                // fix the floats and close
                echo "<span class='clearfix'></span>";
                echo "</p></div></a>";
                // Determine whether or not to show the edit and delete buttons
                // Only show for admins or if the user created the class
                if ($isAdmin || $result["rcs_id"] == phpCAS::getUser()) {
                    echo "<form method=\"post\" action='classes.php";
                    if ($prefix != "") {
                        echo "?prefix=" . $prefix;
                    }
                    echo "' class=\"admin-panel delete-form\">";
                    echo "<button type=\"submit\" class=\"btn btn-default pull-right\" name=\"delete\" value=" . $result["category_id"] . ">Delete</button></form>";
                    echo "<form method=\"post\" action='editclass.php' class=\"admin-panel\">";
                    echo "<button type=\"submit\" class=\"btn btn-default\" name=\"edit\" value=" . $result["category_id"] . ">Edit</button></form>";
                }
                echo "</div>";
            } else {
                if ($c >= $p * 18) {
                    break;
                }
            }
            ++$c;
        }
        //if there were no classes to display
        if ($c == 0) {
            echo "<div class='col-xs-12'>" . infoAlert("No classes. You should add one!") . "</div>";
        }
    } catch (PDOException $e) {
        echo $e;
    }
    //add the pagination
    populatePagination($count, $prefix, $p);
}
Exemple #2
0
function populateData($conn, $dataReq, $isAdmin)
{
    try {
        $count = 0;
        if (isset($_GET["page"])) {
            $p = $_GET["page"];
        } else {
            $p = 1;
        }
        echo "<div class='row'>";
        $data = $dataReq->fetchAll(PDO::FETCH_ASSOC);
        foreach ($data as $result) {
            if ($count >= ($p - 1) * 16 && $count < $p * 16) {
                $categoryHTML = "<h6 class='text-muted truncate'>";
                if (!isset($_GET["class"])) {
                    $var = $conn->prepare("SELECT `title`,`prefix` FROM `categories` WHERE `category_id` = :id");
                    $var->bindParam(':id', $result["category_id"]);
                    $var->execute();
                    $r2 = $var->fetch(PDO::FETCH_ASSOC);
                    $categoryHTML .= $r2['title'] . " (" . $r2['prefix'] . ")";
                } else {
                    $categoryHTML .= "Link";
                }
                $categoryHTML .= "</h6>";
                echo "<div class='col-md-3'>\n                        <a href='" . $result["link"] . "' target=\"_blank\">\n                            <div class='well well-sm well-hover'>" . $categoryHTML . "<h4 class='truncate'>" . $result["title"] . "</h4>" . "<p class='text-muted small'>\n                                <span class='pull-left'>\n                                    submitted by " . $result["rcs_id"] . "</span>\n                                <span class='pull-right'>" . $result["creation_date"] . "</span>\n                                <span class='clearfix'></span>\n                               </p>\n                           </div>\n                         </a>";
                //for getting back to the same page you started on
                $class = "?";
                if (isset($_GET["class"])) {
                    $class .= "class=" . $_GET["class"];
                }
                $page = $class;
                if (isset($_GET["page"])) {
                    if ($class == "?") {
                        $page = "?page=" . $_GET["page"];
                    } else {
                        $page = $class . "&page=" . $_GET["page"];
                    }
                }
                //for upvote and downvote
                echo "<div class='row'><div class=\"col-xs-12 text-center\">";
                echo "<form class='admin-panel' method=\"post\" action='links.php" . $page . "' class=\"form-inline\">";
                echo "<button type=\"submit\" class=\"btn btn-default pull-left\" name=\"downvote\" value=" . $result["link_id"] . ">\n                <span class='fa fa-thumbs-down'</span></button>";
                echo "<button type=\"submit\" class=\"btn btn-default pull-right\" name=\"upvote\" value=" . $result["link_id"] . ">\n                <span class='fa fa-thumbs-up'</span></button></form>";
                echo "<span class=\"text-center\"><a class='btn btn-default disabled'>" . $result["score"] . "</a></span>";
                echo "</div></div>";
                //if the user is an administrator, they can delete and edit
                if ($isAdmin || $result["rcs_id"] == phpCAS::getUser()) {
                    //delete button
                    echo "<form class='admin-panel delete-form' method=\"post\" action='links.php{$class}' class=\"form-horizontal\">";
                    echo "<button type=\"submit\" class=\"btn btn-default pull-right\" name=\"delete\" value=" . $result["link_id"] . ">Delete</button></form>";
                    //edit button
                    echo "<form class='admin-panel' method=\"post\" action='editlink.php" . $page . "' class=\"form-horizontal\">";
                    echo "<button type=\"submit\" class=\"btn btn-default pull-left\" name=\"edit\" value=" . $result["link_id"] . ">Edit</button></form>";
                }
                echo "</div>";
            }
            $count++;
        }
        if ($count == 0) {
            echo "<div class='row'><div class='col-xs-12'>" . infoAlert("No links. You should add one.") . "</div></div>";
        }
    } catch (PDOException $e) {
        echo $e;
    }
    if ($count / 16 + 1 >= 2) {
        echo "<div class=\"clearfix\"></div><div class=\"col-xs-12 centered\"><hr/><div class=\"btn-group\">";
        for ($button = 1; $button < $count / 16 + 1; $button++) {
            $link = "?";
            if (isset($_GET["class"])) {
                $link = $link . "class=" . $_GET["class"] . "&";
            }
            $link .= "page={$button}";
            echo "<a href=\"{$link}\" class=\"btn btn-primary\">{$button}</a>";
        }
        echo "</div></div>";
    }
    echo "</div>";
}