Skip to content

Commit

Permalink
feat(browse): add no subtype, more specific hybrid subtype search; up…
Browse files Browse the repository at this point in the history
…date sublist search

- minor refactoring, particularly of hybrid/any query
  • Loading branch information
itinerare committed Sep 3, 2024
1 parent 043dd1f commit 3d92eb4
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,17 @@ public function getCharacters(Request $request) {
$imageQuery->where('species_id', $request->get('species_id'));
}
if ($request->get('subtype_ids')) {
// if subtype ids contains "any" search for all subtypes
if (in_array('any', $request->get('subtype_ids')) || in_array('hybrid', $request->get('subtype_ids'))) {
$imageQuery->whereHas('subtypes', function ($query) use ($request) {
$query->havingRaw('COUNT(*) > '.(in_array('hybrid', $request->get('subtype_ids')) ? 1 : 0));
if (in_array('none', $request->get('subtype_ids'))) {
$imageQuery->doesntHave('subtypes');
} elseif (in_array('hybrid', $request->get('subtype_ids')) && !in_array('any', $request->get('subtype_ids')) && count($request->get('subtype_ids')) > 1) {
// If hybrid + any number of subtype IDs, return characters with the subtype(s) and any additional subtypes
// This is functionally somewhat redundant, but allows some specialized searches (e.g. hybrids including a specific subtype)
$imageQuery->has('subtypes', '>', 1)->whereHas('subtypes', function ($query) use ($request) {
$query->whereIn('character_image_subtypes.subtype_id', $request->get('subtype_ids'));
});
} elseif (in_array('any', $request->get('subtype_ids')) || in_array('hybrid', $request->get('subtype_ids'))) {
// If subtype ids contains "any" search for all subtypes
$imageQuery->has('subtypes', '>', in_array('hybrid', $request->get('subtype_ids')) ? 1 : 0);
} else {
if (config('lorekeeper.extensions.exclusionary_search')) {
$imageQuery->whereHas('subtypes', function ($query) use ($request) {
Expand All @@ -232,10 +238,8 @@ public function getCharacters(Request $request) {
->groupBy('character_image_subtypes.character_image_id')
->havingRaw('COUNT(character_image_subtypes.subtype_id) = ?', [count($subtypeIds)]);
})->whereDoesntHave('subtypes', function ($query) use ($request) {
$subtypeIds = $request->get('subtype_ids');

// Ensure that no additional subtypes are present
$query->whereNotIn('character_image_subtypes.subtype_id', $subtypeIds);
$query->whereNotIn('character_image_subtypes.subtype_id', $request->get('subtype_ids'));
});
} else {
$imageQuery->whereHas('subtypes', function ($query) use ($request) {
Expand Down Expand Up @@ -339,7 +343,7 @@ public function getCharacters(Request $request) {
'characters' => $query->paginate(24)->appends($request->query()),
'categories' => [0 => 'Any Category'] + CharacterCategory::whereNotIn('id', $subCategories)->visible(Auth::user() ?? null)->orderBy('character_categories.sort', 'DESC')->pluck('name', 'id')->toArray(),
'specieses' => [0 => 'Any Species'] + Species::whereNotIn('id', $subSpecies)->visible(Auth::user() ?? null)->orderBy('specieses.sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['any' => 'Any Subtype', 'hybrid' => 'Multiple / Hybrid Subtypes'] + Subtype::visible(Auth::user() ?? null)->orderBy('subtypes.sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No Subtypes', 'any' => 'Any Subtype', 'hybrid' => 'Multiple / Hybrid Subtypes'] + Subtype::visible(Auth::user() ?? null)->orderBy('subtypes.sort', 'DESC')->pluck('name', 'id')->toArray(),
'rarities' => [0 => 'Any Rarity'] + Rarity::orderBy('rarities.sort', 'DESC')->pluck('name', 'id')->toArray(),
'features' => Feature::getDropdownItems(),
'sublists' => Sublist::orderBy('sort', 'DESC')->get(),
Expand Down Expand Up @@ -581,8 +585,37 @@ public function getSublist(Request $request, $key) {
if ($request->get('species_id')) {
$imageQuery->where('species_id', $request->get('species_id'));
}
if ($request->get('subtype_id')) {
$imageQuery->where('subtype_id', $request->get('subtype_id'));
if ($request->get('subtype_ids')) {
if (in_array('none', $request->get('subtype_ids'))) {
$imageQuery->doesntHave('subtypes');
} elseif (in_array('hybrid', $request->get('subtype_ids')) && !in_array('any', $request->get('subtype_ids')) && count($request->get('subtype_ids')) > 1) {
// If hybrid + any number of subtype IDs, return characters with the subtype(s) and any additional subtypes
// This is functionally somewhat redundant, but allows some specialized searches (e.g. hybrids including a specific subtype)
$imageQuery->has('subtypes', '>', 1)->whereHas('subtypes', function ($query) use ($request) {
$query->whereIn('character_image_subtypes.subtype_id', $request->get('subtype_ids'));
});
} elseif (in_array('any', $request->get('subtype_ids')) || in_array('hybrid', $request->get('subtype_ids'))) {
// If subtype ids contains "any" search for all subtypes
$imageQuery->has('subtypes', '>', in_array('hybrid', $request->get('subtype_ids')) ? 1 : 0);
} else {
if (config('lorekeeper.extensions.exclusionary_search')) {
$imageQuery->whereHas('subtypes', function ($query) use ($request) {
$subtypeIds = $request->get('subtype_ids');

// Filter to ensure the character has all the specified subtypes
$query->whereIn('character_image_subtypes.subtype_id', $subtypeIds)
->groupBy('character_image_subtypes.character_image_id')
->havingRaw('COUNT(character_image_subtypes.subtype_id) = ?', [count($subtypeIds)]);
})->whereDoesntHave('subtypes', function ($query) use ($request) {
// Ensure that no additional subtypes are present
$query->whereNotIn('character_image_subtypes.subtype_id', $request->get('subtype_ids'));
});
} else {
$imageQuery->whereHas('subtypes', function ($query) use ($request) {
$query->whereIn('character_image_subtypes.subtype_id', $request->get('subtype_ids'));
});
}
}
}
if ($request->get('feature_ids')) {
$featureIds = $request->get('feature_ids');
Expand Down Expand Up @@ -661,7 +694,7 @@ public function getSublist(Request $request, $key) {
'characters' => $query->paginate(24)->appends($request->query()),
'categories' => [0 => 'Any Category'] + $subCategory,
'specieses' => [0 => 'Any Species'] + $subSpecies,
'subtypes' => [0 => 'Any Subtype'] + Subtype::visible(Auth::user() ?? null)->orderBy('subtypes.sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No Subtypes', 'any' => 'Any Subtype', 'hybrid' => 'Multiple / Hybrid Subtypes'] + Subtype::visible(Auth::user() ?? null)->orderBy('subtypes.sort', 'DESC')->pluck('name', 'id')->toArray(),
'rarities' => [0 => 'Any Rarity'] + Rarity::orderBy('rarities.sort', 'DESC')->pluck('name', 'id')->toArray(),
'features' => Feature::getDropdownItems(),
'sublist' => $sublist,
Expand Down

0 comments on commit 3d92eb4

Please sign in to comment.