public function ajaxCatalogGetBooks() { $number = $_POST['number']; $selectedGenresId = []; $availability = 0; $start = 0; if (!empty($_POST['genres'])) { $selectedGenresId = $_POST['genres']; } if (!empty($_POST['availability'])) { $availability = $_POST['availability']; } if (!empty($_POST['start'])) { $start = $_POST['start']; } $booksIdsToFind = []; if (count($selectedGenresId) == 1) { $bookGenreManager = new BookGenreManager(); $booksIdsToFind = $bookGenreManager->findBooksIdsByGenresAndAvailability($selectedGenresId, $availability); } else { if (count($selectedGenresId) > 1) { $bookGenreManager = new BookGenreManager(); $unsortedBooksIds = $bookGenreManager->findBooksIdsByGenresAndAvailability($selectedGenresId, $availability); $booksIdsToFind = $this->sortBooksIdsByOccurence($unsortedBooksIds); } } $bookManager = new BookManager(); $last = $start + $number; if (count($booksIdsToFind) == 0) { $books = $bookManager->findBooks($start, $number); $total = $bookManager->count(); } else { $books = []; $total = count($booksIdsToFind); if ($total >= $start + $number) { for ($index = $start; $index < $start + $number; $index++) { $books[] = $bookManager->extendedFind($booksIdsToFind[$index]); } } else { for ($index = $start; $index < $total; $index++) { $books[] = $bookManager->extendedFind($booksIdsToFind[$index]); } $last = $total; } } $first = $start + 1; $precStart = $start - $number; $nextStart = $start + $number; $data = array('precStart' => $precStart, 'first' => $first, 'last' => $last, 'total' => $total, 'nextStart' => $nextStart, 'books' => $books); $this->show('book/ajax_catalog_showBooks', $data); }