From 32c03348839317474db2bed3517c5644620417e1 Mon Sep 17 00:00:00 2001 From: Mike Cantelon Date: Thu, 12 Oct 2023 13:53:25 -0700 Subject: [PATCH] Add phys. storage creation REST endpoint. (#1628) (#1629) Added a REST API endpoint for creating physical objects. --- .../arRestApiPluginConfiguration.class.php | 5 + .../physicalobjectsCreateAction.class.php | 123 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 plugins/arRestApiPlugin/modules/api/actions/physicalobjectsCreateAction.class.php diff --git a/plugins/arRestApiPlugin/config/arRestApiPluginConfiguration.class.php b/plugins/arRestApiPlugin/config/arRestApiPluginConfiguration.class.php index b9dd64b011..1ab090ff69 100644 --- a/plugins/arRestApiPlugin/config/arRestApiPluginConfiguration.class.php +++ b/plugins/arRestApiPlugin/config/arRestApiPluginConfiguration.class.php @@ -101,6 +101,11 @@ public function routingLoadConfiguration(sfEvent $event) 'action' => 'digitalobjectsCreate', ]); + $this->addRoute('POST', '/api/physicalobjects', [ + 'module' => 'api', + 'action' => 'physicalobjectsCreate', + ]); + $this->addRoute('*', '/api/*', [ 'module' => 'api', 'action' => 'endpointNotFound', diff --git a/plugins/arRestApiPlugin/modules/api/actions/physicalobjectsCreateAction.class.php b/plugins/arRestApiPlugin/modules/api/actions/physicalobjectsCreateAction.class.php new file mode 100644 index 0000000000..865c3dd161 --- /dev/null +++ b/plugins/arRestApiPlugin/modules/api/actions/physicalobjectsCreateAction.class.php @@ -0,0 +1,123 @@ +. + */ + +class ApiPhysicalObjectsCreateAction extends QubitApiAction +{ + protected function post($request, $payload) + { + // Optionally set culture + $this->culture = 'en'; + if (!empty($payload->culture)) { + $this->culture = trim(strtolower($payload->culture)); + } + + // Create empty physical object + $this->po = new QubitPhysicalObject(); + $this->sourceCulture = $this->culture; + + // Populate physical object fields and save + foreach ($payload as $field => $value) { + $this->processField($field, $value); + } + + $this->po->save(); + + // Add physical object to other AtoM objects + $this->createRelations($payload); + + // Return response + $this->response->setStatusCode(201); + + return ['slug' => $this->po->slug]; + } + + protected function processField($field, $value) + { + $value = trim($value); + + switch ($field) { + case 'type': + $typeNormalized = strtolower($value); + + // Load physical object type data + $taxonomy = QubitTaxonomy::getById(QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID); + $termsByCulture = $taxonomy->getTermNameToIdLookupTable(); + + // Check if physical object type term already exists + if (!empty($termsByCulture['en'][$typeNormalized])) { + $this->po->typeId = $termsByCulture[$this->culture][$typeNormalized]; + } else { + // Create new physical object type term + $term = new QubitTerm(); + $term->taxonomyId = QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID; + $term->setName($value); + $term->setSourceCulture($this->culture); + $term->save(); + + $this->po->typeId = $term->id; + } + + break; + + case 'name': + $this->po->setName($value); + + break; + + case 'location': + $this->po->setLocation($value); + + break; + } + } + + protected function createRelations($payload) + { + if (!empty($payload->add_to)) { + if (!is_array($payload->add_to)) { + throw new QubitApiBadRequestException('related_to must be an array'); + } + + foreach ($payload->add_to as $slug) { + // Attempt to load target resource to add the physical object to + $relatedObject = QubitObject::getBySlug($slug); + + // Return error if the target resource is invalid + if (null == $relatedObject) { + throw new QubitApiBadRequestException('Invalid parent_slug'); + } + + // Make sure the target resource is an appropriate type + if ('QubitInformationObject' != $relatedObject->className) { + throw new QubitApiBadRequestException('Invalid parent resource'); + } + + // Make sure user has permission to modify the target resource + if (!QubitAcl::check($relatedObject, 'update')) { + throw new QubitApiNotAuthorizedException(); + } + + // Add physical object to target resource + $relatedResource = $relatedObject->className::getById($relatedObject->id); + $relatedResource->addPhysicalObject($this->po); + $relatedResource->save(); + } + } + } +}