web/www/ebooks/index.php

155 lines
4.8 KiB
PHP

<?
require_once('Core.php');
use function Safe\preg_replace;
try{
$page = HttpInput::GetInt('page', 1);
$perPage = HttpInput::GetInt('per-page', EBOOKS_PER_PAGE);
$query = HttpInput::GetString('query', false);
$tags = HttpInput::GetArray('tags', []);
$collection = HttpInput::GetString('collection', false);
$view = HttpInput::GetString('view', false);
$sort = HttpInput::GetString('sort', false);
$pages = 0;
$totalEbooks = 0;
if($page <= 0){
$page = 1;
}
if($perPage != EBOOKS_PER_PAGE && $perPage != 24 && $perPage != 48){
$perPage = EBOOKS_PER_PAGE;
}
// If we're passed string values that are the same as the defaults,
// set them to null so that we can have cleaner query strings in the navigation footer
if($view !== null){
$view = mb_strtolower($view);
}
if($sort !== null){
$sort = mb_strtolower($sort);
}
if($view === 'grid'){
$view = null;
}
if($sort === 'newest'){
$sort = null;
}
if($query === ''){
$query = null;
}
if(sizeof($tags) == 1 && mb_strtolower($tags[0]) == 'all'){
$tags = [];
}
// Are we looking at a collection?
if($collection !== null){
$ebooks = Library::GetEbooksByCollection($collection);
$collectionObject = null;
// Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin")
if(sizeof($ebooks) > 0){
foreach($ebooks[0]->Collections as $c){
if($collection == Formatter::MakeUrlSafe($c->Name)){
$collectionObject = $c;
}
}
}
if($collectionObject !== null){
$collectionName = preg_replace('/^The /ius', '', $collectionObject->Name) ?? '';
$collectionType = $collectionObject->Type ?? 'collection';
# This is a kind of .endswith() test
if(substr_compare(mb_strtolower($collectionObject->Name), mb_strtolower($collectionObject->Type), -strlen(mb_strtolower($collectionObject->Type))) !== 0){
$collectionType = ' ' . $collectionType;
}
else{
$collectionType = '';
}
$pageTitle = 'Browse ebooks in the ' . Formatter::ToPlainText($collectionName) . $collectionType;
$pageDescription = 'A list of ebooks in the ' . Formatter::ToPlainText($collectionName) . ' ' . $collectionType;
$pageHeader = 'Ebooks in the ' . Formatter::ToPlainText($collectionName) . ' ' . $collectionType;
}
else{
throw new InvalidCollectionException();
}
}
else{
$ebooks = Library::FilterEbooks($query, $tags, $sort);
$pageTitle = 'Browse Standard Ebooks';
$pageHeader = 'Browse Ebooks';
$pages = ceil(sizeof($ebooks) / $perPage);
$totalEbooks = sizeof($ebooks);
$ebooks = array_slice($ebooks, ($page - 1) * $perPage, $perPage);
}
if($page > 1){
$pageTitle .= ', page ' . $page;
}
$pageDescription = 'Page ' . $page . ' of the Standard Ebooks ebook library';
$queryString = '';
if($collection === null){
if($query != ''){
$queryString .= '&amp;query=' . urlencode($query);
}
foreach($tags as $tag){
$queryString .= '&amp;tags[]=' . urlencode($tag);
}
if($view !== null){
$queryString .= '&amp;view=' . urlencode($view);
}
if($sort !== null){
$queryString .= '&amp;sort=' . urlencode($sort);
}
if($perPage !== EBOOKS_PER_PAGE){
$queryString .= '&amp;per-page=' . urlencode((string)$perPage);
}
}
$queryString = preg_replace('/^&amp;/ius', '', $queryString);
}
catch(\Exception $ex){
http_response_code(404);
include(WEB_ROOT . '/404.php');
exit();
}
?><?= Template::Header(['title' => $pageTitle, 'highlight' => 'ebooks', 'description' => $pageDescription]) ?>
<main class="ebooks">
<h1><?= $pageHeader ?></h1>
<? if($collection === null){ ?>
<?= Template::SearchForm(['query' => $query, 'tags' => $tags, 'sort' => $sort, 'view' => $view, 'perPage' => $perPage]) ?>
<? } ?>
<? if(sizeof($ebooks) == 0){ ?>
<p class="no-results">No ebooks matched your filters. You can try different filters, or <a href="/ebooks">browse all of our ebooks</a>.</p>
<? }else{ ?>
<?= Template::EbookGrid(['ebooks' => $ebooks, 'view' => $view]) ?>
<? } ?>
<? if(sizeof($ebooks) > 0 && $collection === null){ ?>
<nav>
<a<? if($page > 1){ ?> href="/ebooks/?page=<?= $page - 1 ?><? if($queryString != ''){ ?>&amp;<?= $queryString ?><? } ?>" rel="previous"<? }else{ ?> aria-disabled="true"<? } ?>>Back</a>
<ol>
<? for($i = 1; $i < $pages + 1; $i++){ ?>
<li<? if($page == $i){ ?> class="highlighted"<? } ?>><a href="/ebooks/?page=<?= $i ?><? if($queryString != ''){ ?>&amp;<?= $queryString ?><? } ?>"><?= $i ?></a></li>
<? } ?>
</ol>
<a<? if($page < ceil($totalEbooks / $perPage)){ ?> href="/ebooks/?page=<?= $page + 1 ?><? if($queryString != ''){ ?>&amp;<?= $queryString ?><? } ?>" rel="next"<? }else{ ?> aria-disabled="true"<? } ?>>Next</a>
</nav>
<? } ?>
<? if(sizeof($ebooks) > 0 && $query === null && sizeof($tags) == 0 && $collection === null && $page == 1){ ?>
<?= Template::ContributeAlert() ?>
<? } ?>
</main>
<?= Template::Footer() ?>