Return empty Userfields empty (closes #1412)

This commit is contained in:
Bernd Bestel 2021-07-08 20:12:58 +02:00
parent 14cd6ca3bf
commit 7595d640f5
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 44 additions and 29 deletions

View File

@ -83,6 +83,7 @@
- Added a new API endpoint `/system/localization-strings` to get the localization strings (gettext JSON representation; in the by the user desired language)
- The `GET /chores` endpoint now also returns the `next_execution_assigned_user` per chore (like the endpoint `GET /chores/{choreId}` already did for a single chore)
- The `GET /tasks` endpoint now also returns the assigned user and category object per task
- Empty Userfields are now also returned (were previously omitted, endpoint `GET /objects/{entity}` and `GET /objects/{entity}/{objectId}`)
- Fixed that due soon products with `due_type` = "Expiration date" were missing in `due_products` of the `/stock/volatile` endpoint
- Fixed that `PUT/DELETE /objects/{entity}/{objectId}` produced an internal server error when the given object id was invalid (now returns `400 Bad Request`)
- Fixed that hyphens in filter values did not work

View File

@ -121,14 +121,12 @@ class GenericEntityApiController extends BaseApiController
if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoListing($args['entity']))
{
$userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']);
if (count($userfields) === 0)
{
$userfields = null;
}
$object = $this->getDatabase()->{$args['entity']}($args['objectId']);
if ($object == null)
{
return $this->GenericErrorResponse($response, 'Object not found', 404);
@ -146,34 +144,40 @@ class GenericEntityApiController extends BaseApiController
public function GetObjects(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
$objects = $this->queryData($this->getDatabase()->{$args['entity']}(), $request->getQueryParams());
$allUserfields = $this->getUserfieldsService()->GetAllValues($args['entity']);
foreach ($objects as $object)
if (!$this->IsValidExposedEntity($args['entity']) || $this->IsEntityWithNoListing($args['entity']))
{
$userfields = FindAllObjectsInArrayByPropertyValue($allUserfields, 'object_id', $object->id);
$userfieldKeyValuePairs = null;
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
}
$objects = $this->queryData($this->getDatabase()->{$args['entity']}(), $request->getQueryParams());
$userfields = $this->getUserfieldsService()->GetFields($args['entity']);
if (count($userfields) > 0)
{
$allUserfieldValues = $this->getUserfieldsService()->GetAllValues($args['entity']);
foreach ($objects as $object)
{
$userfieldKeyValuePairs = null;
foreach ($userfields as $userfield)
{
$userfieldKeyValuePairs[$userfield->name] = $userfield->value;
$value = FindObjectInArrayByPropertyValue($allUserfieldValues, 'object_id', $object->id);
if ($value)
{
$userfieldKeyValuePairs[$userfield->name] = $value->value;
}
else
{
$userfieldKeyValuePairs[$userfield->name] = null;
}
}
$object->userfields = $userfieldKeyValuePairs;
}
}
if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoListing($args['entity']))
{
return $this->ApiResponse($response, $objects);
}
else
{
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
}
}
public function GetUserfields(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{

View File

@ -44,6 +44,7 @@ class UserfieldsService extends BaseService
throw new \Exception('Entity does not exist or is not exposed');
}
$userfields = $this->GetFields($entity);
return $this->getDatabase()->userfield_values_resolved()->where('entity', $entity)->orderBy('name', 'COLLATE NOCASE')->fetchAll();
}
@ -88,12 +89,21 @@ class UserfieldsService extends BaseService
throw new \Exception('Entity does not exist or is not exposed');
}
$userfields = $this->getDatabase()->userfield_values_resolved()->where('entity = :1 AND object_id = :2', $entity, $objectId)->orderBy('name', 'COLLATE NOCASE')->fetchAll();
$userfieldKeyValuePairs = [];
$userfields = $this->GetFields($entity);
$userfieldValues = $this->getDatabase()->userfield_values_resolved()->where('entity = :1 AND object_id = :2', $entity, $objectId)->orderBy('name', 'COLLATE NOCASE')->fetchAll();
$userfieldKeyValuePairs = [];
foreach ($userfields as $userfield)
{
$userfieldKeyValuePairs[$userfield->name] = $userfield->value;
$value = FindObjectInArrayByPropertyValue($userfieldValues, 'name', $userfield->name);
if ($value)
{
$userfieldKeyValuePairs[$userfield->name] = $value->value;
}
else
{
$userfieldKeyValuePairs[$userfield->name] = null;
}
}
return $userfieldKeyValuePairs;