180 lines
5.5 KiB
PHP
180 lines
5.5 KiB
PHP
|
<?php
|
||
|
// Search posts by filter
|
||
|
|
||
|
|
||
|
|
||
|
// Includes
|
||
|
// API
|
||
|
require_once("api/_input_checks.php");
|
||
|
require_once("api/post/find.php");
|
||
|
// Front pieces
|
||
|
require_once("front/pages/search_posts/gen_post_entry.php");
|
||
|
// Markup includes
|
||
|
require_once("front/pages/main_nav.php");
|
||
|
require_once("front/notifications.php");
|
||
|
|
||
|
|
||
|
|
||
|
// Needed config values
|
||
|
$cfgMaxPostsPerRequest = $Config["max_posts_per_request"];
|
||
|
|
||
|
|
||
|
|
||
|
// Checking request fields
|
||
|
// Checking amount of posts per page
|
||
|
$postsPerPage = null;
|
||
|
if (isset($_REQUEST["amount"])) {
|
||
|
$postsPerPage = $_REQUEST["amount"];
|
||
|
if (
|
||
|
(!InpChk_IsValidInt32($postsPerPage))
|
||
|
|| ($postsPerPage > $cfgMaxPostsPerRequest)
|
||
|
|| ($postsPerPage < 1)
|
||
|
) {
|
||
|
$postsPerPage = $_REQUEST["amount"] = $cfgMaxPostsPerRequest; // TODO: user defaults
|
||
|
NTFY_AddNotice("Wrong posts \"amount\" value in query, was normalized to $postsPerPage.", "warning");
|
||
|
} else {
|
||
|
$_REQUEST["amount"] = intval($_REQUEST["amount"]);
|
||
|
}
|
||
|
} else {
|
||
|
$postsPerPage = $cfgMaxPostsPerRequest; // TODO: user defaults
|
||
|
}
|
||
|
// Checking posts offset
|
||
|
$currentOffset = null;
|
||
|
if (!empty($_REQUEST["offset"])) {
|
||
|
$currentOffset = $_REQUEST["offset"];
|
||
|
if (
|
||
|
(!InpChk_IsValidInt32($currentOffset))
|
||
|
|| $currentOffset < 0
|
||
|
) {
|
||
|
$currentOffset = $_REQUEST["offset"] = 0;
|
||
|
NTFY_AddNotice("Wrong \"offset\" value in query, was defaulted to $currentOffset.", "warning");
|
||
|
} else {
|
||
|
$_REQUEST["offset"] = intval($_REQUEST["offset"]);
|
||
|
}
|
||
|
} else {
|
||
|
$currentOffset = 0;
|
||
|
}
|
||
|
$currentOffsetIsCorrect = ($currentOffset === 0) || (!($currentOffset % $postsPerPage));
|
||
|
|
||
|
|
||
|
|
||
|
// Processing request
|
||
|
$result = Post_GetMatchingPosts_Method($_REQUEST);
|
||
|
$requestedPostsResult = null;
|
||
|
if ($result->IsError()) // Something happened
|
||
|
NTFY_AddNotice("Failed to fetch posts! Reason:<br>" . $result->GetError(), "fail");
|
||
|
else
|
||
|
$requestedPostsResult = $result->GetData();
|
||
|
|
||
|
|
||
|
|
||
|
// Checking offset again NOTICE: doshirak-code
|
||
|
if ($requestedPostsResult && $currentOffset > $requestedPostsResult["total_amount"]) {
|
||
|
$currentOffset = $_REQUEST["offset"] = 0;
|
||
|
NTFY_AddNotice("Wrong \"offset\" value in query!", "fail");
|
||
|
$requestedPostsResult = null;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
NTFY_EchoAllNotices();
|
||
|
|
||
|
|
||
|
|
||
|
?>
|
||
|
<div class="visualbox">
|
||
|
<menu class="paginator">
|
||
|
<?php
|
||
|
// TODO: move to function, remove trashy dependencies and dublicate at bottom of posts list
|
||
|
|
||
|
if ($requestedPostsResult) {
|
||
|
// Calculating current page based on offset NOTICE: counting starts from zero, so user will see $currentPage + 1
|
||
|
$currentPage = intdiv($currentOffset, $postsPerPage);
|
||
|
if (!$currentOffsetIsCorrect)
|
||
|
++$currentPage;
|
||
|
// Calculating maximum page based on total posts amount
|
||
|
$totalPagesAmount = intdiv($requestedPostsResult["total_amount"], $postsPerPage);
|
||
|
if ($requestedPostsResult["total_amount"] % $postsPerPage)
|
||
|
++$totalPagesAmount;
|
||
|
} else {
|
||
|
$currentPage = 0;
|
||
|
$totalPagesAmount = 0;
|
||
|
}
|
||
|
|
||
|
$tempGETArr = $_GET;
|
||
|
$tempGETArr["offset"] = $currentOffset; // Starting offset
|
||
|
$startingPage = $currentPage; // And page, yeah
|
||
|
|
||
|
// Calculating starting offset
|
||
|
for ($i = 0; $i < 2 && $tempGETArr["offset"] > 0; ++$i) {
|
||
|
$tempGETArr["offset"] -= $postsPerPage;
|
||
|
--$startingPage;
|
||
|
}
|
||
|
|
||
|
// If list of posts does not begin from first
|
||
|
if ($currentOffset > 0) { // Then adding link to first and previous page
|
||
|
$secondTempGETArr = $_GET;
|
||
|
$secondTempGETArr["offset"] = 0;
|
||
|
// First page
|
||
|
echo "<li><a title=\"First page\" href=\"./?" . http_build_query($secondTempGETArr) . "\">First</a></li>\n";
|
||
|
// Previous page
|
||
|
if ($currentOffsetIsCorrect) {
|
||
|
$secondTempGETArr["offset"] = $currentOffset - $postsPerPage;
|
||
|
echo "<li><a title=\"Previous page\" href=\"./?" . http_build_query($secondTempGETArr) . "\"><</a></li>\n";
|
||
|
}
|
||
|
unset($secondTempGETArr);
|
||
|
|
||
|
echo "<li><p>...</p></li>";
|
||
|
}
|
||
|
|
||
|
// Showing pages
|
||
|
$i = $startingPage;
|
||
|
$endingPage = $startingPage + 5; // Unaccurate naming, tbh, but who cares
|
||
|
for (; $i < $endingPage && $i < $totalPagesAmount; ++$i) {
|
||
|
echo "<li>";
|
||
|
if ($tempGETArr["offset"] === $currentOffset)
|
||
|
echo "<b>";
|
||
|
echo "<a href=\"./?" . http_build_query($tempGETArr) . "\">" . strval($i + 1) ."</a>";
|
||
|
if ($tempGETArr["offset"] === $currentOffset)
|
||
|
echo "</b>";
|
||
|
echo "</li>\n";
|
||
|
|
||
|
$tempGETArr["offset"] += $postsPerPage;
|
||
|
}
|
||
|
|
||
|
// If we have even MORE posts!
|
||
|
if ($currentPage < ($totalPagesAmount - 1)) { // Then adding link to last and next page
|
||
|
echo "<li><p>...</p></li>";
|
||
|
|
||
|
$secondTempGETArr = $_GET;
|
||
|
// Next page
|
||
|
if ($currentOffsetIsCorrect) {
|
||
|
$secondTempGETArr["offset"] = $currentOffset + $postsPerPage;
|
||
|
echo "<li><a title=\"Next page\" href=\"./?" . http_build_query($secondTempGETArr) . "\">></a></li>\n";
|
||
|
}
|
||
|
// Last page
|
||
|
$secondTempGETArr["offset"] = ($totalPagesAmount - 1) * $postsPerPage;
|
||
|
echo "<li><a title=\"Last page\" href=\"./?" . http_build_query($secondTempGETArr) . "\">Last</a></li>\n";
|
||
|
}
|
||
|
?>
|
||
|
</menu>
|
||
|
<div class="postsearchcolumn">
|
||
|
<h3 style="margin-top: 4px; margin-bottom: 4px;">Search</h3>
|
||
|
<form class="basicform" action="." accept-charset="UTF-8" method="get">
|
||
|
<input type="hidden" name="do" value="search_posts">
|
||
|
<input type="text" name="query" autocomplete="on" <?php if (isset($_REQUEST["query"])) { echo "value=\"" . $_REQUEST["query"] . "\""; } ?>>
|
||
|
<input type="submit" value="Search">
|
||
|
</form>
|
||
|
</div>
|
||
|
<div class="postlist">
|
||
|
<?php
|
||
|
if ($requestedPostsResult && $requestedPostsResult["total_amount"]) {
|
||
|
foreach ($requestedPostsResult["data"] as $postData)
|
||
|
echo GenPostEntry($postData);
|
||
|
} else {
|
||
|
echo "<h2 style=\"color: gray; font-style: italic;\">Nothing found!</h2>";
|
||
|
}
|
||
|
?>
|
||
|
</div>
|
||
|
</div>
|