Also return userfields in generic object(s) GET API routes (closes #601)

This commit is contained in:
Bernd Bestel 2020-04-13 16:52:34 +02:00
parent 87e68523e5
commit 25be604b31
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 30 additions and 2 deletions

View File

@ -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

View File

@ -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
{