From d30c39104618558f20c1a8bfb3e4c33de52c0878 Mon Sep 17 00:00:00 2001 From: Barry Brands Date: Wed, 7 Aug 2024 18:09:10 +0200 Subject: [PATCH 01/10] Sort PublicationsController added --- lib/Controller/PublicationsController.php | 4 +++- lib/Db/PublicationMapper.php | 19 ++++++++++++++++--- lib/Service/SearchService.php | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/Controller/PublicationsController.php b/lib/Controller/PublicationsController.php index d0fe3c44..e6b75ea7 100644 --- a/lib/Controller/PublicationsController.php +++ b/lib/Controller/PublicationsController.php @@ -104,6 +104,8 @@ public function index(ObjectService $objectService, SearchService $searchService $filters = $this->request->getParams(); $fieldsToSearch = ['title', 'description', 'summary']; + $sort = $searchService->createSortArrayFromParams($filters); + if($this->config->hasKey($this->appName, 'mongoStorage') === false || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' ) { @@ -111,7 +113,7 @@ public function index(ObjectService $objectService, SearchService $searchService $searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch); $filters = $searchService->unsetSpecialQueryParams(filters: $filters); - return new JSONResponse(['results' => $this->publicationMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams)]); + return new JSONResponse(['results' => $this->publicationMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams, sort: $sort)]); } $filters = $searchService->createMongoDBSearchFilter(filters: $filters, fieldsToSearch: $fieldsToSearch); diff --git a/lib/Db/PublicationMapper.php b/lib/Db/PublicationMapper.php index c834dfbf..b395572f 100644 --- a/lib/Db/PublicationMapper.php +++ b/lib/Db/PublicationMapper.php @@ -28,8 +28,14 @@ public function find(int $id): Publication return $this->findEntity(query: $qb); } - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array - { + public function findAll( + ?int $limit = null, + ?int $offset = null, + ?array $filters = [], + ?array $searchConditions = [], + ?array $searchParams = [], + ?array $sort = [] + ): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -41,13 +47,20 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters $qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value))); } - if (!empty($searchConditions)) { + if (empty($searchConditions) === false) { $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); foreach ($searchParams as $param => $value) { $qb->setParameter($param, $value); } } + if (empty($sort) === false) { + foreach ($sort as $field => $direction) { + $direction = strtoupper($direction) === 'DESC' ? 'DESC' : 'ASC'; + $qb->addOrderBy($field, $direction); + } + } + return $this->findEntities(query: $qb); } diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index a24ee48c..2dcafeae 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -270,6 +270,27 @@ public function createMySQLSearchParams(array $filters): array }//end createMongoDBSearchFilter() + /** + * This function creates an sort array based on given order param from request. + * + * @param array $filters Query parameters from request. + * + * @return array $sort + */ + public function createSortArrayFromParams(array $filters): array + { + $sort = []; + if (isset($filters['_order']) && is_array($filters['_order'])) { + foreach ($filters['_order'] as $field => $direction) { + $direction = strtoupper($direction) === 'DESC' ? 'DESC' : 'ASC'; + $sort[$field] = $direction; + } + } + + return $sort; + + }//end createSortArrayFromParams() + /** * Parses the request query string and returns it as an array of queries. * From 2bc227d0d1abd8761bcd58ebe6629a3d4310ea74 Mon Sep 17 00:00:00 2001 From: Barry Brands Date: Wed, 7 Aug 2024 18:09:18 +0200 Subject: [PATCH 02/10] Sort publications frontend added --- src/store/modules/publication.js | 20 ++++- src/views/publications/PublicationList.vue | 91 +++++++++++++++------- 2 files changed, 82 insertions(+), 29 deletions(-) diff --git a/src/store/modules/publication.js b/src/store/modules/publication.js index 7fdbc013..a2012099 100644 --- a/src/store/modules/publication.js +++ b/src/store/modules/publication.js @@ -23,12 +23,26 @@ export const usePublicationStore = defineStore( this.publicationList = publicationList.map((publicationItem) => new Publication(publicationItem)) console.log('Active publication item set to ' + publicationList.length) }, - async refreshPublicationList(search = null) { + async refreshPublicationList(normalSearch = [], advancedSearch = null, sortField = null, sortDirection = null) { // @todo this might belong in a service? + console.log(normalSearch) let endpoint = '/index.php/apps/opencatalogi/api/publications' - if (search !== null && search !== '') { - endpoint = endpoint + '?_search=' + search + const params = new URLSearchParams() + for (const item of normalSearch) { + if (item.key && item.value !== undefined) { + params.append(item.key, item.value) + } } + if (advancedSearch !== null && advancedSearch !== '') { + params.append('_search', advancedSearch) + } + if (sortField !== null && sortField.value !== null && sortDirection !== null && sortDirection !== '') { + params.append('_order[' + sortField.value + ']', sortDirection) + } + if (params.toString()) { + endpoint += '?' + params.toString() + } + return fetch( endpoint, { diff --git a/src/views/publications/PublicationList.vue b/src/views/publications/PublicationList.vue index 9394a73a..68e795e3 100644 --- a/src/views/publications/PublicationList.vue +++ b/src/views/publications/PublicationList.vue @@ -7,36 +7,55 @@ import { navigationStore, publicationStore } from '../../store/store.js'