Skip to content

Commit

Permalink
Merge pull request #125 from ConductionNL/feature/DIMOC-165/sort-func…
Browse files Browse the repository at this point in the history
…tionality

Sort and filter functionality
  • Loading branch information
rubenvdlinde authored Aug 8, 2024
2 parents 2a71806 + 8b1ec2b commit a75ba6c
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 38 deletions.
6 changes: 5 additions & 1 deletion lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ public function index(ObjectService $objectService, SearchService $searchService
) {
$searchParams = $searchService->createMySQLSearchParams(filters: $filters);
$searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch);
$sort = $searchService->createSortForMySQL(filters: $filters);
$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);
$filters = $searchService->unsetSpecialQueryParams(filters: $filters);

// @todo Fix mongodb sort
// $sort = $searchService->createSortForMongoDB(filters: $filters);

$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/CatalogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (!empty($searchConditions)) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (!empty($searchConditions)) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/MetaDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (!empty($searchConditions)) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/OrganisationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (!empty($searchConditions)) {
Expand Down
27 changes: 23 additions & 4 deletions lib/Db/PublicationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('*')
Expand All @@ -38,16 +44,29 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$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);
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Db/ThemeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
->setFirstResult($offset);

foreach($filters as $filter => $value) {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (!empty($searchConditions)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function findObjects(array $filters, array $config): array
$object['dataSource'] = $config['mongodbCluster'];
$object['filter'] = $filters;

// @todo Fix mongodb sort
// if (empty($sort) === false) {
// $object['filter'][] = ['$sort' => $sort];
// }

$returnData = $client->post(
uri: 'action/find',
options: ['json' => $object]
Expand Down
52 changes: 52 additions & 0 deletions lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ public function createMongoDBSearchFilter(array $filters, array $fieldsToSearch)
unset($filters['_search']);
}

foreach ($filters as $field => $value) {
if ($value === 'IS NOT NULL') {
$filters[$field] = ['$ne' => null];
}
if ($value === 'IS NULL') {
$filters[$field] = ['$eq' => null];
}
}

return $filters;

}//end createMongoDBSearchFilter()
Expand Down Expand Up @@ -278,6 +287,49 @@ 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 createSortForMySQL(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()

/**
* This function creates an sort array based on given order param from request.
*
* @todo Not functional yet. Needs to be fixed (see PublicationsController->index).
*
* @param array $filters Query parameters from request.
*
* @return array $sort
*/
public function createSortForMongoDB(array $filters): array
{
$sort = [];
if (isset($filters['_order']) && is_array($filters['_order'])) {
foreach ($filters['_order'] as $field => $direction) {
$sort[$field] = strtoupper($direction) === 'DESC' ? -1 : 1;
}
}

return $sort;

}//end createSortForMongoDB()

/**
* Parses the request query string and returns it as an array of queries.
*
Expand Down
28 changes: 25 additions & 3 deletions src/store/modules/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,34 @@ 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?
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 !== '' && sortDirection !== null && sortDirection !== '') {
if (sortField === 'Titel') {
sortField = 'title'
}
if (sortField === 'Datum gepubliceerd') {
sortField = 'published'
}
if (sortField === 'Datum aangepast') {
sortField = 'modified'
}
params.append('_order[' + sortField + ']', sortDirection)
}
if (params.toString()) {
endpoint += '?' + params.toString()
}

return fetch(
endpoint,
{
Expand Down
Loading

0 comments on commit a75ba6c

Please sign in to comment.