Fix query error in getMovieRange function

This commit is contained in:
DariusIII
2024-10-12 21:59:36 +02:00
parent 831fefbde4
commit d05a2ecfc7
2 changed files with 55 additions and 58 deletions
+25 -21
View File
@@ -165,9 +165,10 @@ class Movie
*
* @return array|mixed
*/
public function getMovieRange($page, $cat, $start, $num, $orderBy, int $maxAge = -1, array $excludedCats = [])
public function getMovieRange($page, $cat, $start, $num, $orderBy, int $maxAge = -1, array $excludedCats = []): mixed
{
$categorySearch = $this->buildCategorySearch($cat);
$order = $this->getMovieOrder($orderBy);
$cacheKey = md5(json_encode([$cat, $start, $num, $orderBy, $maxAge, $excludedCats, $page]));
$expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
@@ -179,15 +180,18 @@ class Movie
}
$moviesQuery = MovieInfo::query()
->selectRaw('m.imdbid, GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_id')
->join('releases as r', 'm.imdbid', '=', 'r.imdbid')
->selectRaw('movieinfo.imdbid, GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_id')
->join('releases as r', 'movieinfo.imdbid', '=', 'r.imdbid')
->where('r.nzbstatus', 1)
->where('m.title', '!=', '')
->where('m.imdbid', '!=', '0000000')
->where('movieinfo.title', '!=', '')
->where('movieinfo.imdbid', '!=', '0000000')
->when($maxAge > 0, fn ($query) => $query->whereRaw('r.postdate > NOW() - INTERVAL ? DAY', [$maxAge]))
->when(! empty($excludedCats), fn ($query) => $query->whereNotIn('r.categories_id', $excludedCats))
->when(! empty($categorySearch), fn ($query) => $query->whereRaw($categorySearch))
->groupBy('m.imdbid')
->when(!empty($categorySearch), fn ($query) => $query->whereRaw(
// Check if $categorySearch starts with AND or OR and clean it up
preg_match('/^\s*(AND|OR)\s+/i', $categorySearch) ? preg_replace('/^\s*(AND|OR)\s+/i', '', $categorySearch) : $categorySearch
))
->groupBy('movieinfo.imdbid')
->orderBy($order[0], $order[1])
->offset($start)
->limit($num);
@@ -239,7 +243,7 @@ class Movie
}
// Cache the result
Cache::put($cacheKey, $moviesDetailQuery, $expiresAt);
//Cache::put($cacheKey, $moviesDetailQuery, $expiresAt);
return $moviesDetailQuery;
}
@@ -257,20 +261,20 @@ class Movie
}
/**
* Get the order type the user requested on the movies page.
*/
protected function getMovieOrder($orderBy): array
{
$orderArr = explode('_', (($orderBy === '') ? 'MAX(r.postdate)' : $orderBy));
$orderField = match ($orderArr[0]) {
'title' => 'm.title',
'year' => 'm.year',
'rating' => 'm.rating',
default => 'MAX(r.postdate)',
};
* Get the order type the user requested on the movies page.
*/
protected function getMovieOrder($orderBy): array
{
$orderArr = explode('_', (($orderBy === '') ? 'MAX(r.postdate)' : $orderBy));
$orderField = match ($orderArr[0]) {
'title' => 'm.title',
'year' => 'm.year',
'rating' => 'm.rating',
default => DB::raw('MAX(r.postdate)'),
};
return [$orderField, isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1]) ? $orderArr[1] : 'desc'];
}
return [$orderField, isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1]) ? $orderArr[1] : 'desc'];
}
/**
* Order types for movies page.
+30 -37
View File
@@ -305,47 +305,40 @@ class Category extends Model
}
public static function getCategorySearch(array $cat = []): string
{
$categories = [];
// If multiple categories were sent in a single array position, slice and add them
if (strpos($cat[0], ',') !== false) {
$tmpcats = explode(',', $cat[0]);
// Reset the category to the first comma separated value in the string
$cat[0] = $tmpcats[0];
// Add the remaining categories in the string to the original array
foreach (\array_slice($tmpcats, 1) as $tmpcat) {
$cat[] = $tmpcat;
}
}
foreach ($cat as $category) {
if (is_numeric($category) && $category !== -1 && self::isParent($category)) {
foreach (RootCategory::find($category)->categories as $child) {
$categories[] = $child['id'];
}
} elseif (is_numeric($category) && $category > 0) {
$categories[] = $category;
}
}
$catCount = \count($categories);
switch ($catCount) {
//No category constraint
case 0:
$catsrch = 'AND 1=1';
break;
// One category constraint
case 1:
$catsrch = $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : '';
break;
// Multiple category constraints
default:
{
$categories = [];
$catsrch = ' AND r.categories_id IN ('.implode(', ', $categories).') ';
break;
// If multiple categories were sent in a single array position, slice and add them
if (strpos($cat[0], ',') !== false) {
$tmpcats = explode(',', $cat[0]);
// Reset the category to the first comma separated value in the string
$cat[0] = $tmpcats[0];
// Add the remaining categories in the string to the original array
foreach (array_slice($tmpcats, 1) as $tmpcat) {
$cat[] = $tmpcat;
}
return $catsrch;
}
foreach ($cat as $category) {
if (is_numeric($category) && $category !== -1 && self::isParent($category)) {
$children = RootCategory::find($category)->categories->pluck('id')->toArray();
$categories = array_merge($categories, $children);
} elseif (is_numeric($category) && $category > 0) {
$categories[] = $category;
}
}
$catCount = count($categories);
$catSearch = match ($catCount) {
0 => 'AND 1=1',
1 => $categories[0] !== -1 ? ' AND r.categories_id = ' . $categories[0] : '',
default => ' AND r.categories_id IN (' . implode(', ', $categories) . ') ',
};
return $catSearch;
}
/**
* Returns a concatenated list of other categories.
*/