Skip to content

Commit 4974b87

Browse files
authored
Merge pull request #612 from distributive/main
More minor search fixes
2 parents 669a4e3 + 8772d0a commit 4974b87

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/AppBundle/Controller/SearchController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,16 @@ public function displayAction(
425425
$rows = $cardsData->get_search_rows($conditions, $sort, $locale);
426426

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

432-
// If there are still no results, try again but with aliases
432+
// If there are results, rebuild the query from the uppercase conditions
433433
if ($rows) {
434434
$conditions = $capsConditions;
435435
}
436-
else {
437-
$cardsData->unaliasCardNames($conditions);
436+
// If there are still no results, try again but with aliases
437+
else if ($cardsData->unaliasCardNames($conditions)) {
438438
$rows = $cardsData->get_search_rows($conditions, $sort, $locale);
439439
}
440440
}

src/AppBundle/Service/CardsData.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function get_search_rows(array $conditions, string $sortorder, string $lo
267267
$parameters[$i++] = "%$arg%";
268268
}
269269
}
270-
$clauses[] = implode(" or ", $or);
270+
$clauses[] = implode($operator == '!' ? " and " : " or ", $or);
271271
break;
272272
case 'x': // text
273273
$or = [];
@@ -947,19 +947,38 @@ public function validateConditions(array &$conditions)
947947
}
948948
}
949949

950+
public function simplifyConditions(array &$conditions)
951+
{
952+
// Checks if the given conditions are valid
953+
if (array_filter($conditions, function($c) {return $c[0] != "_" || $c[1] != ":";}))
954+
return false;
955+
956+
// Combine all conditions into a single string
957+
$s = preg_replace("/[^A-Za-z0-9 ]/", "", implode(" ", array_map(function($c) {return $c[2];}, $conditions)));
958+
959+
// Update the array if the combined conditions are non-empty
960+
if (!$s)
961+
return false;
962+
$conditions = [["_", ":", strtolower($s)]];
963+
return true;
964+
}
965+
966+
// Assumes given conditions are simplified
950967
public function unaliasCardNames(array &$conditions)
951968
{
952-
// Join all the conditions without criteria into a single string
953-
$title = preg_replace("/[^A-Za-z0-9 ]/", "", implode(" ", array_map(function($c) {return $c[0] == "_" ? strtolower($c[2]) : "";}, $conditions)));
969+
// Get the simplified conditions
970+
$title = $conditions[0][2];
954971

955-
if (!$title) {
956-
return;
957-
}
972+
// Check the conditions are non-empty
973+
if (!$title)
974+
return false;
958975

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

965984
public function buildQueryFromConditions(array $conditions)

web/card_aliases.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ yogurt : Economic Warfare
9898
cell phones : 02069
9999
cell phone man : 02069
100100
mister phones : 02069
101-
# mr phones : 02069
101+
mr phones : 02069
102102

103103
# Cars
104104
nice car : Sports Hopper

0 commit comments

Comments
 (0)