<?php

// Retrieve data
function searchDB($TITLE, $CATEGORY, $KEYWORDS, $LIMIT, $OFFSET, $STRICTSEARCH) {
    $queryPart = "";
    $fillPart = false;
echo $LIMIT;
    if ($TITLE != "") {
        if ($STRICTSEARCH == "true") {
            $queryPart = $queryPart . "title='" . $TITLE . "'"; $fillPart = true;
        } else {
            $queryPart = $queryPart . "title LIKE '%" . $TITLE . "%' "; $fillPart = true;
        }
    }
    if ($CATEGORY != "") {
        if ($STRICTSEARCH == "true") {
            if ($fillPart == true) {
                $queryPart = $queryPart . " AND categories='" . $CATEGORY . "'";
            } else {
                $queryPart = $queryPart . " categories='" . $CATEGORY . "'";
            }
        } else {
            if ($fillPart == true) {
                $queryPart = $queryPart . " AND categories LIKE '%" . $CATEGORY . "%'";
            } else {
                $queryPart = $queryPart . " categories LIKE '%" . $CATEGORY . "%'";
            }
        }
        $fillPart = true;
    }
    if ($KEYWORDS != "") {
        if ($STRICTSEARCH == "true") {
            if ($fillPart == true) {
                $queryPart = $queryPart . " AND keywords='" . $KEYWORDS . "'";
            } else {
                $queryPart = $queryPart . " keywords='" . $KEYWORDS . "'";
            }
        } else {
            if ($fillPart == true) {
                $queryPart = $queryPart . " AND keywords LIKE '%" . $KEYWORDS . "%'";
            } else {
                $queryPart = $queryPart . " keywords LIKE '%" . $KEYWORDS . "%'";
            }
        }
    }

    if ($LIMIT == "") { $LIMIT = 50; }
    if ($OFFSET == "") { $OFFSET = -1; }

    $query = "SELECT * FROM PHub WHERE " . $queryPart . " LIMIT " . $LIMIT . " OFFSET " . $OFFSET . ";";

    if ($TITLE == "" && $CATEGORY == "" && $KEYWORDS == "") {
        $query = "SELECT * FROM PHub LIMIT " . $LIMIT . " OFFSET " . $OFFSET . ";";
    }

    try {
        $serverPDO = new SQLite3('resources/db/phub.db');
        $result = $serverPDO->query($query);
        if ($result->fetchArray() == 0) {
            echo "<div class='message warnning'>Nothing Found...</div>";
        }
        while ($row = $result->fetchArray()) {
            echo "<div class='resultContainer'>" .
                 "<input class='checkbox' type='checkbox' name='faveState' onclick='saveORdel(this.parentElement)'/>" .
                 "<a href='". $row["embedLink"] . "' target='ifrm'>" .
                     "<img src='". $row["thumbnail"] . "' />" .
                     "<p class='title'>" . $row["title"] . "</p>" .
                 "</a></div>";
        }
    } catch (Exception $e) { echo "<h2 class='message error'>Error!</h2><br/>" . $e; }
}

function viewFav() {
    try {
        $serverPDO = new SQLite3('resources/db/phub.db');
        $query = "SELECT * FROM Fave;";
        $result = $serverPDO->query($query);

        if ($result->fetchArray() == 0) {
            echo "<div class='message warnning'>No Favorites Found...</div>";
        }

        while ($row = $result->fetchArray()) {
            echo "<div class='resultContainer'>" .
                 "<input class='checkbox' type='checkbox' name='faveState' checked onclick='saveORdel(this.parentElement)'/>" .
                 "<a href='". $row["embedLink"] . "' target='ifrm'>" .
                     "<img src='". $row["thumbnail"] . "' />" .
                 "</a></div>";
        }
    } catch (Exception $e) { echo "<h2 class='message error'>Error!</h2><br/>" . $e; }
}

function saveFave($link, $thumb) {
    try {
        $serverPDO = new SQLite3('resources/db/phub.db');
        $query = "INSERT INTO Fave VALUES('" . $link . "', '" . $thumb . "');";
        $serverPDO->exec($query);
    } catch (Exception $e) { echo "<h2 class='message error'>Error! Insert Failed. </h2><br/>" . $e; }
}

function delFave($link, $thumb) {
    try {
        $serverPDO = new SQLite3('resources/db/phub.db');
        $query = "DELETE FROM Fave WHERE embedLink='" . $link . "' AND thumbnail='" . $thumb . "';";
        $serverPDO->exec($query);
    } catch (Exception $e) { echo "<h2 class='message error'>Error! Delete Failed. </h2><br/>" . $e; }
}



// Determin action
if (isset($_POST['dbQuery'])) {
    searchDB($_POST['title'], $_POST['category'],
             $_POST['keywords'], $_POST['limit'],
             $_POST['offset'], $_POST['strictSearch']);
} elseif (isset($_POST['saveFave'])) {
    saveFave($_POST['link'], $_POST['thumbnail']);
} elseif (isset($_POST['delFave'])) {
    delFave($_POST['link'], $_POST['thumbnail']);
} elseif (isset($_POST['viewFave'])) {
    viewFav();
} else {
    echo "<h2 class='message error''>Error! Illegal Access Method!</h2>";
}

?>
