From 25be604b3184a3e5ea4d13f68292c83239e66155 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 13 Apr 2020 16:52:34 +0200 Subject: [PATCH] Also return userfields in generic object(s) GET API routes (closes #601) --- changelog/58_UNRELEASED_2020-xx-xx.md | 1 + controllers/GenericEntityApiController.php | 31 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/changelog/58_UNRELEASED_2020-xx-xx.md b/changelog/58_UNRELEASED_2020-xx-xx.md index 405df3c6..9b7fe375 100644 --- a/changelog/58_UNRELEASED_2020-xx-xx.md +++ b/changelog/58_UNRELEASED_2020-xx-xx.md @@ -36,6 +36,7 @@ - New endpoint `/user/settings` to get all user settings of the currently logged in user (key/value pairs) - New endpoint `/system/config` to get all config settings (`config.php`) (key/value pairs) - The endpoint `/stock/products/{productId}/locations` now also returns the current stock amount of the product in that loctation (new field/property `amount`) (thanks @Forceu) +- The endpoints `/objects/{entity}` and `/objects/{entity}/{objectId}` now also include/return userfields of the object(s) (new field/property `userfields` per object, is an array of key/value pairs and `null`, when the object has no userfields) - Fixed that CORS was broken (there was no response to preflight OPTIONS requests) ### General & other improvements/fixes diff --git a/controllers/GenericEntityApiController.php b/controllers/GenericEntityApiController.php index efbee609..3a2f1754 100644 --- a/controllers/GenericEntityApiController.php +++ b/controllers/GenericEntityApiController.php @@ -11,9 +11,27 @@ class GenericEntityApiController extends BaseApiController public function GetObjects(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { + $objects = $this->getDatabase()->{$args['entity']}(); + $allUserfields = $this->getUserfieldsService()->GetAllValues($args['entity']); + + foreach ($objects as $object) + { + $userfields = FindAllObjectsInArrayByPropertyValue($allUserfields, 'object_id', $object->id); + $userfieldKeyValuePairs = null; + if (count($userfields) > 0) + { + foreach ($userfields as $userfield) + { + $userfieldKeyValuePairs[$userfield->name] = $userfield->value; + } + } + + $object->userfields = $userfieldKeyValuePairs; + } + if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) { - return $this->ApiResponse($response, $this->getDatabase()->{$args['entity']}()); + return $this->ApiResponse($response, $objects); } else { @@ -25,7 +43,16 @@ class GenericEntityApiController extends BaseApiController { if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) { - return $this->ApiResponse($response, $this->getDatabase()->{$args['entity']}($args['objectId'])); + $userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']); + if (count($userfields) === 0) + { + $userfields = null; + } + + $object = $this->getDatabase()->{$args['entity']}($args['objectId']); + $object['userfields'] = $userfields; + + return $this->ApiResponse($response, $object); } else {