getDatabase()->userfields()->orderBy('name', 'COLLATE NOCASE')->fetchAll(); } public function GetAllValues($entity) { if (!$this->IsValidEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } return $this->getDatabase()->userfield_values_resolved()->where('entity', $entity)->orderBy('name', 'COLLATE NOCASE')->fetchAll(); } public function GetEntities() { $exposedDefaultEntities = $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum; $userentities = []; foreach ($this->getDatabase()->userentities()->orderBy('name', 'COLLATE NOCASE') as $userentity) { $userentities[] = 'userentity-' . $userentity->name; } return array_merge($exposedDefaultEntities, $userentities); } public function GetField($fieldId) { return $this->getDatabase()->userfields($fieldId); } public function GetFieldTypes() { return GetClassConstants('\Grocy\Services\UserfieldsService'); } public function GetFields($entity) { if (!$this->IsValidEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } return $this->getDatabase()->userfields()->where('entity', $entity)->orderBy('sort_number')->orderBy('name', 'COLLATE NOCASE')->fetchAll(); } public function GetValues($entity, $objectId) { if (!$this->IsValidEntity($entity)) { 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 = []; foreach ($userfields as $userfield) { $userfieldKeyValuePairs[$userfield->name] = $userfield->value; } return $userfieldKeyValuePairs; } public function SetValues($entity, $objectId, $userfields) { if (!$this->IsValidEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } foreach ($userfields as $key => $value) { $fieldRow = $this->getDatabase()->userfields()->where('entity = :1 AND name = :2', $entity, $key)->fetch(); if ($fieldRow === null) { throw new \Exception("Field $key is not a valid userfield of the given entity"); } $fieldId = $fieldRow->id; $alreadyExistingEntry = $this->getDatabase()->userfield_values()->where('field_id = :1 AND object_id = :2', $fieldId, $objectId)->fetch(); if ($alreadyExistingEntry) { // Update $alreadyExistingEntry->update([ 'value' => $value ]); } else { // Insert $newRow = $this->getDatabase()->userfield_values()->createRow([ 'field_id' => $fieldId, 'object_id' => $objectId, 'value' => $value ]); $newRow->save(); } } } public function __construct() { parent::__construct(); } protected function getOpenApispec() { if ($this->OpenApiSpec == null) { $this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json')); } return $this->OpenApiSpec; } private function IsValidEntity($entity) { return in_array($entity, $this->GetEntities()); } }