diff --git a/Blacklight/Releases.php b/Blacklight/Releases.php index 74214bd96..8529aa8d6 100755 --- a/Blacklight/Releases.php +++ b/Blacklight/Releases.php @@ -7,6 +7,7 @@ use Blacklight\db\DB; use App\Models\Release; use App\Models\Category; use App\Models\Settings; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Support\Carbon; use Blacklight\utility\Utility; use Illuminate\Support\Facades\Cache; @@ -104,99 +105,97 @@ class Releases } /** - * Used for browse results. - * - * @param array $cat - * @param $start - * @param $num - * @param string|array $orderBy - * @param int $maxAge - * @param array $excludedCats - * @param string|int $groupName - * @param int $minSize - * - * @return array + * @param $cat + * @param $start + * @param $num + * @param $orderBy + * @param int $maxAge + * @param array $excludedCats + * @param int $groupName + * @param int $minSize + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + * @throws \Exception + * @throws \InvalidArgumentException */ - public function getBrowseRange($cat, $start, $num, $orderBy, $maxAge = -1, array $excludedCats = [], $groupName = -1, $minSize = 0): array + public function getBrowseRange($cat, $orderBy, $maxAge = -1, array $excludedCats = [], $groupName = -1, $minSize = 0): LengthAwarePaginator { $orderBy = $this->getBrowseOrder($orderBy); - - $qry = sprintf( - "SELECT r.*, - CONCAT(cp.title, ' > ', c.title) AS category_name, - CONCAT(cp.id, ',', c.id) AS category_ids, - df.failed AS failed, - rn.releases_id AS nfoid, - re.releases_id AS reid, - v.tvdb, v.trakt, v.tvrage, v.tvmaze, v.imdb, v.tmdb, - tve.title, tve.firstaired - FROM - ( - SELECT r.*, g.name AS group_name - FROM releases r - LEFT JOIN groups g ON g.id = r.groups_id - WHERE r.nzbstatus = %d - AND r.passwordstatus %s - %s %s %s %s %s - ORDER BY %s %s %s - ) r - LEFT JOIN categories c ON c.id = r.categories_id - LEFT JOIN categories cp ON cp.id = c.parentid - LEFT OUTER JOIN videos v ON r.videos_id = v.id - LEFT OUTER JOIN tv_episodes tve ON r.tv_episodes_id = tve.id - LEFT OUTER JOIN video_data re ON re.releases_id = r.id - LEFT OUTER JOIN release_nfos rn ON rn.releases_id = r.id - LEFT OUTER JOIN dnzb_failures df ON df.release_id = r.id - GROUP BY r.id - ORDER BY %8\$s %9\$s", - NZB::NZB_ADDED, - $this->showPasswords, - Category::getCategorySearch($cat), - ($maxAge > 0 ? (' AND postdate > NOW() - INTERVAL '.$maxAge.' DAY ') : ''), - (\count($excludedCats) ? (' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')') : ''), - ((int) $groupName !== -1 ? sprintf(' AND g.name = %s ', $this->pdo->escapeString($groupName)) : ''), - ($minSize > 0 ? sprintf('AND r.size >= %d', $minSize) : ''), - $orderBy[0], - $orderBy[1], - ($start === false ? '' : ' LIMIT '.$num.' OFFSET '.$start) - ); - - $releases = Cache::get(md5($qry)); + $qry = Release::query() + ->fromSub(function ($query) use ($cat, $maxAge, $excludedCats, $groupName, $minSize, $orderBy) { + $query->select(['releases.*', 'g.name as group_name']) + ->from('releases') + ->where('releases.nzbstatus', NZB::NZB_ADDED) + ->where('releases.categories_id', $cat); + self::showPasswords($query, true); + $query->leftJoin('groups as g', 'g.id', '=', 'releases.groups_id'); + if ($maxAge > 0) { + $query->where('releases.postdate', '>', Carbon::now()->subDays($maxAge)); + } + if (\count($excludedCats) > 0) { + $query->whereNotIn('releases.categories_id', $excludedCats); + } + if ($groupName !== -1) { + $query->where('g.name', $groupName); + } + if ($minSize > 0) { + $query->where('releases.size', '>=', $minSize); + } + $query->orderBy($orderBy[0], $orderBy[1]); + }, 'r') + ->select(['r.*', 'df.failed as failed', 'rn.releases_id as nfoid', 're.releases_id as reid', 'v.tvdb', 'v.trakt', 'v.tvrage', 'v.tvmaze', 'v.imdb', 'v.tmdb', 'tve.title', 'tve.firstaired']) + ->selectRaw("CONCAT(cp.title, ' > ', c.title) AS category_name") + ->selectRaw("CONCAT(cp.id, ',', c.id) AS category_ids") + ->leftJoin('categories as c', 'c.id', '=', 'r.categories_id') + ->leftJoin('categories as cp', 'cp.id', '=', 'c.parentid') + ->leftJoin('videos as v', 'v.id', '=', 'r.videos_id') + ->leftJoin('tv_episodes as tve', 'tve.id', '=', 'r.tv_episodes_id') + ->leftJoin('video_data as re', 're.releases_id', '=', 'r.id') + ->leftJoin('release_nfos as rn', 're.releases_id', '=', 'r.id') + ->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'r.id') + ->groupBy('r.id') + ->orderBy($orderBy[0], $orderBy[1]); + $releases = Cache::get(md5(implode('.', $cat).implode('.', $orderBy).$maxAge.implode('.', $excludedCats).$minSize)); if ($releases !== null) { return $releases; } - $sql = $this->pdo->query($qry); - if (\count($sql) > 0) { - $possibleRows = $this->getBrowseCount($cat, $maxAge, $excludedCats, $groupName); - $sql[0]['_totalcount'] = $sql[0]['_totalrows'] = $possibleRows; - } + $sql = $qry->paginate(config('nntmux.items_per_page')); $expiresAt = Carbon::now()->addSeconds(config('nntmux.cache_expiry_medium')); - Cache::put(md5($qry), $sql, $expiresAt); - + Cache::put(md5(implode('.', $cat).implode('.', $orderBy).$maxAge.implode('.', $excludedCats).$minSize), $sql, $expiresAt); return $sql; } - /** - * Return site setting for hiding/showing passworded releases. + * @param null $query + * @param bool $builder * * @return string * @throws \Exception */ - public static function showPasswords(): ?string + public static function showPasswords($query = null, $builder = false) { $setting = Settings::settingValue('..showpasswordedrelease', true); $setting = ($setting !== null && is_numeric($setting)) ? $setting : 10; - switch ($setting) { case 0: // Hide releases with a password or a potential password (Hide unprocessed releases). - return '='.self::PASSWD_NONE; + if ($builder === false) { + return '='.self::PASSWD_NONE; + } + return $query->where('releases.passwordstatus', self::PASSWD_NONE); case 1: // Show releases with no password or a potential password (Show unprocessed releases). - return '<= '.self::PASSWD_POTENTIAL; + if ($builder === false) { + return '<= '.self::PASSWD_POTENTIAL; + } + return $query->where('releases.passwordstatus', '=<', self::PASSWD_POTENTIAL); case 2: // Hide releases with a password or a potential password (Show unprocessed releases). - return '<= '.self::PASSWD_NONE; + if ($builder === false) { + return '<= '.self::PASSWD_NONE; + } + return $query->where('releases.passwordstatus', '=<', self::PASSWD_NONE); case 10: // Shows everything. default: - return '<= '.self::PASSWD_RAR; + if ($builder === false) { + return '<= '.self::PASSWD_RAR; + } + return $query->where('releases.passwordstatus', '=<', self::PASSWD_RAR); } } diff --git a/Changelog b/Changelog index ad2ef4199..a3abdc42a 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2018-04-02 DariusIII + * Chg: Update routes and browse controller 2018-04-01 DariusIII * Chg: Update Base and Browse controllers * Chg: Allow disabling of captcha on login page diff --git a/app/Http/Controllers/BrowseController.php b/app/Http/Controllers/BrowseController.php index 21bdb394c..476e74844 100644 --- a/app/Http/Controllers/BrowseController.php +++ b/app/Http/Controllers/BrowseController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Category; use Blacklight\Releases; use Illuminate\Http\Request; @@ -35,26 +36,9 @@ class BrowseController extends BasePageController $results = $releases->getBrowseRange($catarray, $offset, config('nntmux.items_per_page'), $orderby, -1, $this->userdata['categoryexclusions'], $grp); - $browsecount = $results[0]['_totalcount'] ?? 0; - - $this->smarty->assign( - [ - 'pagertotalitems' => $browsecount, - 'pageroffset'=> $offset, - 'pageritemsperpage'=> config('nntmux.items_per_page'), - 'pagerquerybase' => WWW_TOP.'/browse?t='.$category.'&g='.$grp.'&ob='.$orderby.'&offset=', - 'pagerquerysuffix' => '#results', - ] - ); - - $pager = $this->smarty->fetch('pager.tpl'); - $this->smarty->assign('pager', $pager); - $covgroup = ''; $this->smarty->assign('catname', 'All'); - $this->smarty->assign('covgroup', $covgroup); - $this->smarty->assign('lastvisit', $this->userdata['lastlogin']); $this->smarty->assign('results', $results); @@ -67,4 +51,43 @@ class BrowseController extends BasePageController $this->smarty->assign('content', $this->content); $this->pagerender(); } + + /** + * @param $id + * @throws \Exception + */ + public function showMovies($id) + { + $this->setPrefs(); + $releases = new Releases(['Settings' => $this->settings]); + + $category = Category::query()->where('title', $id)->where('parentid', '2000')->first(['id']); + + $grp = -1; + + $catarray = []; + $catarray[] = $category['id']; + + $this->smarty->assign('category', $category); + + $orderby = ''; + + $results = $releases->getBrowseRange($catarray, $orderby, -1, $this->userdata['categoryexclusions'], $grp); + + + $covgroup = ''; + $this->smarty->assign('catname', $id); + + $this->smarty->assign('lastvisit', $this->userdata['lastlogin']); + + $this->smarty->assign('results', $results); + + $this->meta_title = 'Browse Movies > ' . $id; + $this->meta_keywords = 'browse,nzb,description,details'; + $this->meta_description = 'Browse for Nzbs'; + + $this->content = $this->smarty->fetch('browse.tpl'); + $this->smarty->assign('content', $this->content); + $this->pagerender(); + } } diff --git a/app/Models/Category.php b/app/Models/Category.php index 02c487630..d2de49856 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -191,16 +191,15 @@ class Category extends Model } /** - * Parse category search constraints. - * * @param array $cat + * @param $query + * @param bool $builder * - * @return string $catsrch + * @return string */ - public static function getCategorySearch(array $cat = []): string + public static function getCategorySearch(array $cat = [], $query, $builder = false) { $categories = []; - // If multiple categories were sent in a single array position, slice and add them if (strpos($cat[0], ',') !== false) { $tmpcats = explode(',', $cat[0]); @@ -211,7 +210,6 @@ class Category extends Model $cat[] = $tmpcat; } } - foreach ($cat as $category) { if ($category !== -1 && self::isParent($category)) { foreach (self::getChildren($category) as $child) { @@ -221,27 +219,37 @@ class Category extends Model $categories[] = $category; } } - $catCount = \count($categories); - switch ($catCount) { //No category constraint case 0: - $catsrch = ' AND 1=1 '; + if ($builder === false) { + $catsrch = 'AND 1=1'; + } else { + $catsrch = ''; + } break; // One category constraint case 1: - $catsrch = $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : ''; + if ($builder === false) { + $catsrch = $categories[0] !== -1 ? ' AND releases.categories_id = '.$categories[0] : ''; + } else { + $catsrch = $query->where('releases.categories_id', '=', $categories[0]); + } break; // Multiple category constraints default: - $catsrch = ' AND r.categories_id IN ('.implode(', ', $categories).') '; + if ($builder === false) { + $catsrch = ' AND releases.categories_id IN ('.implode(', ', $categories).') '; + } else { + $catsrch = $query->whereIn('releases.categories_id', $categories); + } break; } - return $catsrch; } + /** * Returns a concatenated list of other categories. * diff --git a/resources/views/themes/Gentele/browse.tpl b/resources/views/themes/Gentele/browse.tpl index 0d43ecda1..4dd2a0b04 100755 --- a/resources/views/themes/Gentele/browse.tpl +++ b/resources/views/themes/Gentele/browse.tpl @@ -70,7 +70,7 @@