Skip to content

Commit

Permalink
Merge pull request #612 from distributive/main
Browse files Browse the repository at this point in the history
More minor search fixes
  • Loading branch information
plural authored Mar 24, 2022
2 parents 669a4e3 + 8772d0a commit 4974b87
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/AppBundle/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,16 @@ public function displayAction(
$rows = $cardsData->get_search_rows($conditions, $sort, $locale);

// If there are no results, and no specific criteria were searched, try again but force acronyms
if (!$rows && !array_filter($conditions, function($c) {return $c[0] != "_";})) {
$capsConditions = array_map(function($c) {return ["_", $c[1], strtoupper($c[2])];}, $conditions);
if (!$rows && $cardsData->simplifyConditions($conditions)) {
$capsConditions = [["_", ":", strtoupper($conditions[0][2])]];
$rows = $cardsData->get_search_rows($capsConditions, $sort, $locale);

// If there are still no results, try again but with aliases
// If there are results, rebuild the query from the uppercase conditions
if ($rows) {
$conditions = $capsConditions;
}
else {
$cardsData->unaliasCardNames($conditions);
// If there are still no results, try again but with aliases
else if ($cardsData->unaliasCardNames($conditions)) {
$rows = $cardsData->get_search_rows($conditions, $sort, $locale);
}
}
Expand Down
31 changes: 25 additions & 6 deletions src/AppBundle/Service/CardsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function get_search_rows(array $conditions, string $sortorder, string $lo
$parameters[$i++] = "%$arg%";
}
}
$clauses[] = implode(" or ", $or);
$clauses[] = implode($operator == '!' ? " and " : " or ", $or);
break;
case 'x': // text
$or = [];
Expand Down Expand Up @@ -947,19 +947,38 @@ public function validateConditions(array &$conditions)
}
}

public function simplifyConditions(array &$conditions)
{
// Checks if the given conditions are valid
if (array_filter($conditions, function($c) {return $c[0] != "_" || $c[1] != ":";}))
return false;

// Combine all conditions into a single string
$s = preg_replace("/[^A-Za-z0-9 ]/", "", implode(" ", array_map(function($c) {return $c[2];}, $conditions)));

// Update the array if the combined conditions are non-empty
if (!$s)
return false;
$conditions = [["_", ":", strtolower($s)]];
return true;
}

// Assumes given conditions are simplified
public function unaliasCardNames(array &$conditions)
{
// Join all the conditions without criteria into a single string
$title = preg_replace("/[^A-Za-z0-9 ]/", "", implode(" ", array_map(function($c) {return $c[0] == "_" ? strtolower($c[2]) : "";}, $conditions)));
// Get the simplified conditions
$title = $conditions[0][2];

if (!$title) {
return;
}
// Check the conditions are non-empty
if (!$title)
return false;

// If they are the substring of an alias for a card, replace the conditions with that card's name
if ($match = current(preg_grep("/^$title/", array_keys($this->cardAliases)))) {
$conditions = [["_", ":", $this->cardAliases[$match]]];
return true;
}
return false;
}

public function buildQueryFromConditions(array $conditions)
Expand Down
2 changes: 1 addition & 1 deletion web/card_aliases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ yogurt : Economic Warfare
cell phones : 02069
cell phone man : 02069
mister phones : 02069
# mr phones : 02069
mr phones : 02069

# Cars
nice car : Sports Hopper
Expand Down

0 comments on commit 4974b87

Please sign in to comment.