Files
newznab-tmux-NNTmux/app/Http/Controllers/BooksController.php
T
2026-03-17 12:48:23 +01:00

111 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\Category;
use App\Services\BookService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class BooksController extends BasePageController
{
protected BookService $bookService;
public function __construct(BookService $bookService)
{
parent::__construct();
$this->bookService = $bookService;
}
/**
* @throws \Exception
*/
public function index(Request $request, string $id = ''): mixed
{
$boocats = Category::getChildren(Category::BOOKS_ROOT);
$btmp = [];
foreach ($boocats as $bcat) {
$btmp[] =
[
'id' => $bcat->id,
'title' => $bcat->title,
];
}
$category = $request->has('t') ? $request->input('t') : Category::BOOKS_ROOT;
if ($id && \in_array($id, Arr::pluck($btmp, 'title'), false)) {
$cat = Category::query()
->where('title', $id)
->where('root_categories_id', '=', Category::BOOKS_ROOT)
->first(['id']);
$category = $cat !== null ? $cat['id'] : Category::BOOKS_ROOT;
}
$catarray = [];
$catarray[] = $category;
$ordering = $this->bookService->getBookOrdering();
$orderby = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : '';
$books = [];
$pageInput = $request->input('page');
$page = is_scalar($pageInput) && preg_match('/^\d+$/', (string) $pageInput) === 1 ? max(1, (int) $pageInput) : 1;
$offset = ($page - 1) * (int) config('nntmux.items_per_cover_page');
$rslt = $this->bookService->getBookRange($page, $catarray, $offset, (int) config('nntmux.items_per_cover_page'), $orderby, (array) $this->userdata->categoryexclusions);
$results = $this->paginate($rslt, $rslt[0]->_totalcount ?? 0, (int) config('nntmux.items_per_cover_page'), $page, $request->url(), $request->query());
$maxwords = 50;
foreach ($results as $result) {
if (! empty($result->overview)) {
$words = explode(' ', $result->overview);
if (\count($words) > $maxwords) {
$newwords = \array_slice($words, 0, $maxwords);
$result->overview = implode(' ', $newwords).'...';
}
}
$books[] = $result;
}
$author = ($request->has('author') && ! empty($request->input('author'))) ? stripslashes($request->input('author')) : '';
$title = ($request->has('title') && ! empty($request->input('title'))) ? stripslashes($request->input('title')) : '';
$browseby_link = '&title='.$title.'&author='.$author;
if ((int) $category === -1) {
$catname = 'All';
} else {
$cdata = Category::find($category);
if ($cdata !== null) {
$catname = $cdata->title;
} else {
$catname = 'All';
}
}
// Build order by URLs
$orderByUrls = [];
foreach ($ordering as $ordertype) {
$orderByUrls['orderby'.$ordertype] = url('/Books/'.($id ?: 'All').'?t='.$category.$browseby_link.'&ob='.$ordertype.'&offset=0');
}
$this->viewData = array_merge($this->viewData, [
'catlist' => $btmp,
'category' => $category,
'categorytitle' => $id,
'catname' => $catname,
'author' => $author,
'title' => $title,
'resultsadd' => $books,
'results' => $results,
'covgroup' => 'books',
'meta_title' => 'Browse Books',
'meta_keywords' => 'browse,nzb,books,description,details',
'meta_description' => 'Browse for Books',
], $orderByUrls);
return view('books.index', $this->viewData);
}
}