Skip to content

Commit

Permalink
Merge pull request #8 from RRZE-Webteam/bugfix/ids-relation
Browse files Browse the repository at this point in the history
Correct ids relation in the degree programs overview shortcode
  • Loading branch information
zhyian authored May 31, 2024
2 parents 2541cfa + 54d3aae commit 0089bff
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/Infrastructure/Repository/WpQueryArgsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,35 @@ public function build(CollectionCriteria $criteria): WpQueryArgs
$queryArgs = $this->applyFilter($filter, $queryArgs, $criteria->languageCode());
}

foreach ($criteria->hisCodes() as $hisCode) {
$queryArgs = $this->applyHisCode($hisCode, $queryArgs);
if (count($criteria->hisCodes()) > 0) {
$queryArgs = $this->applyHisCodes($criteria->hisCodes(), $queryArgs);
}

return $queryArgs;
}

public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryArgs
/**
* @param array<string> $hisCodes
*/
public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryArgs
{
$taxQueryItem = [
'relation' => 'AND',
];

try {
$taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
CampoKeys::fromHisCode($hisCode)
);
$hisCodesQuery = [];

foreach ($hisCodes as $hisCode) {
$taxQueryItem = [
'relation' => 'AND',
];

try {
$taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
CampoKeys::fromHisCode($hisCode)
);
} catch (RuntimeException) {
continue;
}

if (count($taxonomyToTermMapping) === 0) {
return $queryArgs;
continue;
}

foreach ($taxonomyToTermMapping as $taxonomy => $termId) {
Expand All @@ -119,14 +128,16 @@ public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryAr
];
}

return $queryArgs->withTaxQueryItem($taxQueryItem);
} catch (RuntimeException) {
/*
* Return an empty result if one or more campo keys in HIS code are not matched to any terms.
* Otherwise invalid HIS codes would be matched to false results.
*/
return $queryArgs->withArg('post__in', [0]);
$hisCodesQuery[] = $taxQueryItem;
}

if (count($hisCodesQuery) === 0) {
return $queryArgs;
}

$hisCodesQuery['relation'] = 'OR';

return $queryArgs->withTaxQueryItem($hisCodesQuery);
}

private function applyOrderBy(
Expand Down

0 comments on commit 0089bff

Please sign in to comment.