Skip to content

Commit

Permalink
Merge pull request #107 from ConductionNL/development
Browse files Browse the repository at this point in the history
development to main
  • Loading branch information
rjzondervan authored Dec 19, 2024
2 parents 07e6dd1 + 5f9631f commit eb62f1a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/Db/AuditTrailMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/**
* The AuditTrailMapper class
*
*
* @package OCA\OpenRegister\Db
*/
class AuditTrailMapper extends QBMapper
Expand Down Expand Up @@ -125,7 +125,7 @@ public function createFromArray(array $object): Log

return $this->insert(entity: $log);
}

/**
* Creates an audit trail for object changes
*
Expand All @@ -152,7 +152,7 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new =
if ($action !== 'delete') {
$oldArray = $old ? $old->jsonSerialize() : [];
$newArray = $new->jsonSerialize();

// Compare old and new values to detect changes
foreach ($newArray as $key => $value) {
if (!isset($oldArray[$key]) || $oldArray[$key] !== $value) {
Expand All @@ -162,7 +162,7 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new =
];
}
}

// For updates, check for removed fields
if ($action === 'update') {
foreach ($oldArray as $key => $value) {
Expand All @@ -185,8 +185,8 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new =
$auditTrail->setObject($objectEntity->getId());
$auditTrail->setAction($action);
$auditTrail->setChanged($changed);
$auditTrail->setUser($user->getUID());
$auditTrail->setUserName($user->getDisplayName());
$auditTrail->setUser(($user !== null) ? $user->getUID() : 'System');
$auditTrail->setUserName(($user !== null) ? $user->getDisplayName() : 'System');
$auditTrail->setSession(session_id());
$auditTrail->setRequest(\OC::$server->getRequest()->getId());
$auditTrail->setIpAddress(\OC::$server->getRequest()->getRemoteAddress());
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
use Psr\Log\LoggerInterface;

/**
* Service class for handling file operations in OpenCatalogi
* Service for handling file operations in OpenRegister.
*
* This service provides functionalities for managing files and folders within the NextCloud environment,
* including creation, deletion, sharing, and file updates. It integrates with NextCloud's file and
* sharing APIs to provide seamless file management for the application.
*/
class FileService
{
Expand Down
80 changes: 79 additions & 1 deletion lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ public function findMultiple(array $ids): array
return $result;
}

/**
* Find subobjects for a certain property with given ids
*
* @param array $ids The IDs to fetch the subobjects for
* @param string $property The property in which the objects reside.
*
* @return array The resulting subobjects.
*/
public function findSubObjects(array $ids, string $property): array
{
$schemaObject = $this->schemaMapper->find($this->schema);
$property = $schemaObject->getProperties()[$property];

if (isset($property['items']) === true) {
$ref = explode('/', $property['items']['$ref']);
} else {
$subSchema = explode('/', $property['$ref']);
}
$subSchema = end($ref);

$subSchemaMapper = $this->getMapper(register: $this->getRegister(), schema: $subSchema);

return $subSchemaMapper->findMultiple($ids);
}

/**
* Get aggregations for objects matching filters
*
Expand Down Expand Up @@ -312,6 +337,60 @@ private function getDataFromObject(mixed $object, ?array $extend = []): mixed
return $object->getObject();
}

/**
* Find all objects conforming to the request parameters, surrounded with pagination data.
*
* @param array $requestParams The request parameters to search with.
*
* @return array The result including pagination data.
*/
public function findAllPaginated(array $requestParams): array
{
// Extract specific parameters
$limit = $requestParams['limit'] ?? $requestParams['_limit'] ?? null;
$offset = $requestParams['offset'] ?? $requestParams['_offset'] ?? null;
$order = $requestParams['order'] ?? $requestParams['_order'] ?? [];
$extend = $requestParams['extend'] ?? $requestParams['_extend'] ?? null;
$page = $requestParams['page'] ?? $requestParams['_page'] ?? null;
$search = $requestParams['_search'] ?? null;

if ($page !== null && isset($limit)) {
$page = (int) $page;
$offset = $limit * ($page - 1);
}

// Ensure order and extend are arrays
if (is_string($order) === true) {
$order = array_map('trim', explode(',', $order));
}
if (is_string($extend) === true) {
$extend = array_map('trim', explode(',', $extend));
}

// Remove unnecessary parameters from filters
$filters = $requestParams;
unset($filters['_route']); // TODO: Investigate why this is here and if it's needed
unset($filters['_extend'], $filters['_limit'], $filters['_offset'], $filters['_order'], $filters['_page'], $filters['_search']);
unset($filters['extend'], $filters['limit'], $filters['offset'], $filters['order'], $filters['page']);

$objects = $this->findAll(limit: $limit, offset: $offset, filters: $filters, sort: $order, search: $search, extend: $extend);
$total = $this->count($filters);
$pages = $limit !== null ? ceil($total/$limit) : 1;

$facets = $this->getAggregations(
filters: $filters,
search: $search
);

return [
'results' => $objects,
'facets' => $facets,
'total' => $total,
'page' => $page ?? 1,
'pages' => $pages,
];
}

/**
* Gets all objects of a specific type.
*
Expand Down Expand Up @@ -408,7 +487,6 @@ public function saveObject(int $register, int $schema, array $object): ObjectEnt
// Handle object properties that are either nested objects or files
if ($schemaObject->getProperties() !== null && is_array($schemaObject->getProperties()) === true) {
$objectEntity = $this->handleObjectRelations($objectEntity, $object, $schemaObject->getProperties(), $register, $schema);
$objectEntity->setObject($object);
}

$objectEntity->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('openregister.Objects.show', ['id' => $objectEntity->getUuid()])));
Expand Down

0 comments on commit eb62f1a

Please sign in to comment.