From 6f4a6af8d4330625fa1a0b9590aae5679c1c00f2 Mon Sep 17 00:00:00 2001 From: "jack.conner" Date: Tue, 21 May 2024 13:59:51 +1200 Subject: [PATCH 1/5] Added parameter to allow filtering of ORM objects on fetchResults method --- src/Service/AlgoliaQuerier.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Service/AlgoliaQuerier.php b/src/Service/AlgoliaQuerier.php index 54bc3c4..a954e4e 100644 --- a/src/Service/AlgoliaQuerier.php +++ b/src/Service/AlgoliaQuerier.php @@ -23,10 +23,11 @@ class AlgoliaQuerier * @param string $selectedIndex * @param string $query * @param array $searchParameters + * @param array $ORMFilters This argument is used to filter ORM objects prior to returning the results as a PaginatedList * * @return PaginatedList */ - public function fetchResults($selectedIndex = null, $query = '', $searchParameters = []) + public function fetchResults($selectedIndex = null, $query = '', $searchParameters = [], $ORMFilters = []) { $service = Injector::inst()->get(AlgoliaService::class); $results = false; @@ -78,6 +79,10 @@ function array_key_first(array $arr) $this->lastResult = $results; + if (!empty($ORMFilters)) { + $records = $records->filter($ORMFilters); + } + $output = PaginatedList::create($records); if ($results) { From 4b0435c52f2d658ba05d90e31295d71dc97b3dd4 Mon Sep 17 00:00:00 2001 From: "jack.conner" Date: Tue, 21 May 2024 14:04:53 +1200 Subject: [PATCH 2/5] Added null check on AbsoluteLink --- src/Service/AlgoliaIndexer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index 641a778..733e130 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -162,7 +162,7 @@ public function exportAttributesFromObject($item) 'objectCreated' => $item->dbObject('Created')->getTimestamp() ]; - if ($item->hasMethod('AbsoluteLink')) { + if ($item->hasMethod('AbsoluteLink') && !empty($item->AbsoluteLink())) { $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->AbsoluteLink()); } From b82883226d6a4253bcb8b964e06348cf4b3a9982 Mon Sep 17 00:00:00 2001 From: "jack.conner" Date: Tue, 21 May 2024 14:48:57 +1200 Subject: [PATCH 3/5] Using Silverstripe orm for consistency --- src/Service/AlgoliaIndexer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index 733e130..fb29703 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -282,7 +282,7 @@ public function exportAttributesFromRelationship($item, $relationship, $attribut try { $data = []; - $related = $item->{$relationship}(); + $related = $item->relObject($relationship); if (!$related || !$related->exists()) { return; From 1b938bfbecc7e12b6ccf796dc6a22251c53e3772 Mon Sep 17 00:00:00 2001 From: "jack.conner" Date: Tue, 21 May 2024 14:52:16 +1200 Subject: [PATCH 4/5] Added check for Link if AbsoluteLink is not available --- src/Service/AlgoliaIndexer.php | 2 ++ src/Service/AlgoliaQuerier.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index fb29703..e505799 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -164,6 +164,8 @@ public function exportAttributesFromObject($item) if ($item->hasMethod('AbsoluteLink') && !empty($item->AbsoluteLink())) { $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->AbsoluteLink()); + } elseif (method_exists($item, 'Link') && !empty($item->Link())) { + $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->Link()); } if ($item && $item->hasMethod('exportObjectToAlgolia')) { diff --git a/src/Service/AlgoliaQuerier.php b/src/Service/AlgoliaQuerier.php index a954e4e..a473ae0 100644 --- a/src/Service/AlgoliaQuerier.php +++ b/src/Service/AlgoliaQuerier.php @@ -23,7 +23,7 @@ class AlgoliaQuerier * @param string $selectedIndex * @param string $query * @param array $searchParameters - * @param array $ORMFilters This argument is used to filter ORM objects prior to returning the results as a PaginatedList + * @param array $ORMFilters filter ORM objects prior to returning the results as a PaginatedList * * @return PaginatedList */ From d5dcd18735d9b8c57177f6e178902cb5c4642a1f Mon Sep 17 00:00:00 2001 From: "jack.conner" Date: Tue, 21 May 2024 15:27:54 +1200 Subject: [PATCH 5/5] AbsoluteLink and Link will now only be called a single time per record --- src/Service/AlgoliaIndexer.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index e505799..d14d3cb 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -162,10 +162,18 @@ public function exportAttributesFromObject($item) 'objectCreated' => $item->dbObject('Created')->getTimestamp() ]; - if ($item->hasMethod('AbsoluteLink') && !empty($item->AbsoluteLink())) { - $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->AbsoluteLink()); - } elseif (method_exists($item, 'Link') && !empty($item->Link())) { - $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->Link()); + if ($item->hasMethod('AbsoluteLink')) { + $link = $item->AbsoluteLink(); + + if (!empty($link)) { + $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $link); + } + } elseif ($item->hasMethod('Link')) { + $link = $item->Link(); + + if (!empty($link)) { + $toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $link); + } } if ($item && $item->hasMethod('exportObjectToAlgolia')) {