diff --git a/Blacklight/Releases.php b/Blacklight/Releases.php index 1f532da64..470b4e7c7 100755 --- a/Blacklight/Releases.php +++ b/Blacklight/Releases.php @@ -370,120 +370,71 @@ class Releases /** * Get TV for my shows page. * - * @param $userShows - * @param int|bool $offset - * @param int $limit - * @param string|array $orderBy - * @param int $maxAge - * @param array $excludedCats * - * @return array + * @param $userShows + * @param $orderBy + * @param int $maxAge + * @param array $excludedCats + * + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed + * @throws \Exception */ - public function getShowsRange($userShows, $offset, $limit, $orderBy, $maxAge = -1, array $excludedCats = []): array + public function getShowsRange($userShows, $orderBy, $maxAge = -1, array $excludedCats = []) { $orderBy = $this->getBrowseOrder($orderBy); - $sql = sprintf( - "SELECT r.*, - CONCAT(cp.title, '-', c.title) AS category_name, - %s AS category_ids, - g.name AS group_name, - rn.releases_id AS nfoid, re.releases_id AS reid, - tve.firstaired, - (SELECT df.failed) AS failed - FROM releases r - LEFT OUTER JOIN video_data re ON re.releases_id = r.id - LEFT JOIN groups g ON g.id = r.groups_id - LEFT OUTER JOIN release_nfos rn ON rn.releases_id = r.id - LEFT OUTER JOIN tv_episodes tve ON tve.videos_id = r.videos_id - LEFT JOIN categories c ON c.id = r.categories_id - LEFT JOIN categories cp ON cp.id = c.parentid - LEFT OUTER JOIN dnzb_failures df ON df.release_id = r.id - WHERE r.categories_id BETWEEN 5000 AND 5999 %s %s - AND r.nzbstatus = %d - AND r.passwordstatus %s - %s - GROUP BY r.id - ORDER BY %s %s %s", - $this->getConcatenatedCategoryIDs(), - $this->uSQL($userShows, 'videos_id'), - (\count($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''), - NZB::NZB_ADDED, - $this->showPasswords, - ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : ''), - $orderBy[0], - $orderBy[1], - ($offset === false ? '' : (' LIMIT '.$limit.' OFFSET '.$offset)) - ); - $releases = Cache::get(md5($sql)); + $sql = Release::query() + ->with('group as g', 'nfo as rn', 'category as c', 'failed as df', 'episode as tve') + ->select( + [ + 'r.*', + \Illuminate\Support\Facades\DB::raw("CONCAT(cp.title, '-', c.title) AS category_name"), + 'g.name as group_name', + 'rn.releases_id as nfoid', + 're.releases_id as reid', + 'tve.firstaired', + 'df.failed as failed', + ] + ) + ->leftJoin('video_date as re', 're.releases_id', '=', 'releases.id') + ->leftJoin('categories as cp', 'cp.id', '=', 'c.parentid') + ->whereBetween('releases.categories_id', [5000, 5999]) + ->where('releases.nzbstatus', NZB::NZB_ADDED); + self::showPasswords($sql, true); + foreach ($userShows as $query) { + $sql->orWhere('releases.videos_id', '=', $query['videos_id']); + if ($query['categories'] !== '') { + $catsArr = explode('|', $query['categories']); + if (\count($catsArr) > 1) { + $sql->whereIn('releases.categories_id', $catsArr); + } else { + $sql->where('releases.categories_id', $catsArr[0]); + } + } + } + + if (\count($excludedCats) > 0) { + $sql->whereNotIn('releases.categories_id', $excludedCats); + } + + if ($maxAge > 0) { + $sql->where('releases.postdate', '>', Carbon::now()->subDays($maxAge)); + } + + $sql->orderBy($orderBy[0], $orderBy[1]); + $releases = Cache::get(md5(implode('.', $userShows).implode('.', $orderBy).$maxAge.implode('.', $excludedCats))); if ($releases !== null) { return $releases; } - $releases = $this->pdo->query($sql); + $releases = $sql->paginate(config('nntmux.items_per_page')); $expiresAt = Carbon::now()->addSeconds(config('nntmux.cache_expiry_medium')); - Cache::put(md5($sql), $releases, $expiresAt); + Cache::put(md5(implode('.', $userShows).implode('.', $orderBy).$maxAge.implode('.', $excludedCats)), $releases, $expiresAt); return $releases; } - /** - * Get count for my shows page pagination. - * - * @param $userShows - * @param int $maxAge - * @param array $excludedCats - * - * @return int - */ - public function getShowsCount($userShows, $maxAge = -1, array $excludedCats = []): int - { - return $this->getPagerCount( - sprintf( - 'SELECT r.id - FROM releases r - WHERE r.categories_id BETWEEN 5000 AND 5999 %s %s - AND r.nzbstatus = %d - AND r.passwordstatus %s - %s', - $this->uSQL($userShows, 'videos_id'), - (\count($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''), - NZB::NZB_ADDED, - $this->showPasswords, - ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : '') - ) - ); - } - - /** - * Get count for my shows page pagination. - * - * @param $userMovies - * @param int $maxAge - * @param array $excludedCats - * - * @return int - */ - public function getMovieCount($userMovies, $maxAge = -1, array $excludedCats = []): int - { - return $this->getPagerCount( - sprintf( - 'SELECT r.id - FROM releases r - WHERE r.categories_id BETWEEN 3000 AND 3999 %s %s - AND r.nzbstatus = %d - AND r.passwordstatus %s - %s', - $this->uSQL($userMovies, 'imdbid'), - (\count($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''), - NZB::NZB_ADDED, - $this->showPasswords, - ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : '') - ) - ); - } - /** * Delete multiple releases, or a single by ID. * diff --git a/Changelog b/Changelog index 529e3c856..611048d64 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-04-13 DariusIII + * Chg: Add MyShowsController, adjust rest of the controllers and classes * Chg: Add MyMoviesController * Fix: Fix search 2018-04-12 DariusIII diff --git a/app/Http/Controllers/BasePageController.php b/app/Http/Controllers/BasePageController.php index a3e3acd4c..5ffb69a8a 100644 --- a/app/Http/Controllers/BasePageController.php +++ b/app/Http/Controllers/BasePageController.php @@ -153,10 +153,18 @@ class BasePageController extends Controller /** * Show 404 page. + * + * @param $message + * + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function show404(): void + public function show404($message = null) { - abort(404); + if ($message !== null) { + return view('errors.404')->with('Message', $message); + } + + return view('errors.404'); } /** diff --git a/app/Http/Controllers/DetailsController.php b/app/Http/Controllers/DetailsController.php index 7af8c7ce4..537b9d45a 100644 --- a/app/Http/Controllers/DetailsController.php +++ b/app/Http/Controllers/DetailsController.php @@ -56,7 +56,7 @@ class DetailsController extends BasePageController $reAudio = $re->getAudio($data['id']); $reSubs = $re->getSubs($data['id']); $comments = ReleaseComment::getComments($data['id']); - $similars = $releases->searchSimilar($data['id'], $data['searchname'], 6, $this->userdata['categoryexclusions']); + $similars = $releases->searchSimilar($data['id'], $data['searchname'], $this->userdata['categoryexclusions']); $failed = DnzbFailure::getFailedCount($data['id']); $showInfo = ''; diff --git a/app/Http/Controllers/MyShowsController.php b/app/Http/Controllers/MyShowsController.php new file mode 100644 index 000000000..346b1c2fc --- /dev/null +++ b/app/Http/Controllers/MyShowsController.php @@ -0,0 +1,209 @@ +setPrefs(); + $action = $request->input('id') ?? ''; + $videoId = $request->input('subpage') ?? ''; + + if ($request->has('from')) { + $this->smarty->assign('from', WWW_TOP.$request->input('from')); + } else { + $this->smarty->assign('from', WWW_TOP.'/myshows'); + } + + switch ($action) { + case 'delete': + $show = UserSerie::getShow(Auth::id(), $videoId); + if ($request->has('from')) { + header('Location:'.WWW_TOP.$request->input('from')); + } else { + return redirect('myshows'); + } + if (! $show) { + $this->show404(); + } else { + UserSerie::delShow(Auth::id(), $videoId); + } + + break; + case 'add': + case 'doadd': + $show = UserSerie::getShow(Auth::id(), $videoId); + if ($show) { + $this->show404('Already subscribed'); + } else { + $show = Video::getByVideoID($videoId); + if (! $show) { + $this->show404('No matching show.'); + } + } + + if ($action === 'doadd') { + $category = ($request->has('category') && is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : []; + UserSerie::addShow(Auth::id(), $videoId, $category); + if ($request->has('from')) { + header('Location:'.WWW_TOP.$request->input('from')); + } else { + return redirect('myshows'); + } + } else { + $tmpcats = Category::getChildren(Category::TV_ROOT); + $categories = []; + foreach ($tmpcats as $c) { + // If TV WEB-DL categorization is disabled, don't include it as an option + if ((int) $c['id'] === Category::TV_WEBDL && (int) Settings::settingValue('indexer.categorise.catwebdl') === 0) { + continue; + } + $categories[$c['id']] = $c['title']; + } + $this->smarty->assign('type', 'add'); + $this->smarty->assign('cat_ids', array_keys($categories)); + $this->smarty->assign('cat_names', $categories); + $this->smarty->assign('cat_selected', []); + $this->smarty->assign('video', $videoId); + $this->smarty->assign('show', $show); + $content = $this->smarty->fetch('myshows-add.tpl'); + $this->smarty->assign([ + 'content' => $content, + ]); + $this->pagerender(); + } + break; + case 'edit': + case 'doedit': + $show = UserSerie::getShow(Auth::id(), $videoId); + + if (! $show) { + $this->show404(); + } + + if ($action === 'doedit') { + $category = ($request->has('category') && \is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : []; + UserSerie::updateShow(Auth::id(), $videoId, $category); + if ($request->has('from')) { + return redirect($request->input('from')); + } + + return redirect('myshows'); + } + + $tmpcats = Category::getChildren(Category::TV_ROOT); + $categories = []; + foreach ($tmpcats as $c) { + $categories[$c['id']] = $c['title']; + } + + $this->smarty->assign('type', 'edit'); + $this->smarty->assign('cat_ids', array_keys($categories)); + $this->smarty->assign('cat_names', $categories); + $this->smarty->assign('cat_selected', explode('|', $show['categories'])); + $this->smarty->assign('video', $videoId); + $this->smarty->assign('show', $show); + $content = $this->smarty->fetch('myshows-add.tpl'); + $this->smarty->assign([ + 'content' => $content, + ]); + $this->pagerender(); + break; + case 'browse': + + $title = 'Browse My Shows'; + $meta_title = 'My Shows'; + $meta_keywords = 'search,add,to,cart,nzb,description,details'; + $meta_description = 'Browse Your Shows'; + + $shows = UserSerie::getShows(Auth::id()); + + $releases = new Releases(['Settings' => $this->settings]); + + $ordering = $releases->getBrowseOrdering(); + $orderby = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : ''; + + $results = $releases->getShowsRange($shows, $orderby, -1, $this->userdata['categoryexclusions']); + + $this->smarty->assign('covgroup', ''); + + foreach ($ordering as $ordertype) { + $this->smarty->assign('orderby'.$ordertype, WWW_TOP.'/myshows/browse?ob='.$ordertype.'&offset=0'); + } + + $this->smarty->assign('lastvisit', $this->userdata['lastlogin']); + + $this->smarty->assign('results', $results); + + $this->smarty->assign('shows', true); + + $content = $this->smarty->fetch('browse.tpl'); + $this->smarty->assign([ + 'content' => $content, + 'title' => $title, + 'meta_title' => $meta_title, + 'meta_keywords' => $meta_keywords, + 'meta_description' => $meta_description, + ]); + $this->pagerender(); + break; + default: + + $title = 'My Shows'; + $meta_title = 'My Shows'; + $meta_keywords = 'search,add,to,cart,nzb,description,details'; + $meta_description = 'Manage Your Shows'; + + $tmpcats = Category::getChildren(Category::TV_ROOT); + $categories = []; + foreach ($tmpcats as $c) { + $categories[$c['id']] = $c['title']; + } + + $shows = UserSerie::getShows(Auth::id()); + $results = []; + foreach ($shows as $showk => $show) { + $showcats = explode('|', $show['categories']); + if (\is_array($showcats) && \count($showcats) > 0) { + $catarr = []; + foreach ($showcats as $scat) { + if (! empty($scat)) { + $catarr[] = $categories[$scat]; + } + } + $show['categoryNames'] = implode(', ', $catarr); + } else { + $show['categoryNames'] = ''; + } + + $results[$showk] = $show; + } + $this->smarty->assign('shows', $results); + + $content = $this->smarty->fetch('myshows.tpl'); + $this->smarty->assign([ + 'content' => $content, + 'title' => $title, + 'meta_title' => $meta_title, + 'meta_keywords' => $meta_keywords, + 'meta_description' => $meta_description, + ]); + $this->pagerender(); + break; + } + } +} diff --git a/routes/web.php b/routes/web.php index 8f4b0667d..6e5cb3d07 100644 --- a/routes/web.php +++ b/routes/web.php @@ -139,7 +139,11 @@ Route::post('search', 'SearchController@search'); Route::get('mymovies', 'MyMoviesController@show'); -Route::post('mymovies', 'MymoviesController@show'); +Route::post('mymovies', 'MyMoviesController@show'); + +Route::get('myshows', 'MyShowsController@show'); + +Route::post('myshows', 'MyShowsController@show'); Route::get('filelist/{guid}', 'FileListController@show');