diff --git a/README.md b/README.md index 8b0088e..25113a1 100644 --- a/README.md +++ b/README.md @@ -136,11 +136,11 @@ adding query parameters to the URL: | Name | Value | Example | |-------------|----------------------|--------------| | `starttime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `yesterday` | -| `endtime` | PHP DateTime formats | `2023-10-23` | +| `endtime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `2023-10-23` | If left out, filtering upon the left out parameter will not be done. -This example requests all submissions after on or after October 1st, 2023: +This example requests all submissions on or after October 1st, 2023: Request: @@ -158,7 +158,7 @@ Response: "123": "https://127.0.0.1:8000/da/webform_rest/some_webform_id/submission/44b1fe1b-ee96-481e-b941-d1219d1dcb55", "124": "https://127.0.0.1:8000/da/webform_rest/some_webform_id/submission/3652836d-3dab-4919-b880-e82cbbf3c24c" } -}``` +} ``` ## Custom access control diff --git a/src/Plugin/rest/resource/WebformAllFormSubmissions.php b/src/Plugin/rest/resource/WebformAllFormSubmissions.php index 581b3cd..38acd2e 100644 --- a/src/Plugin/rest/resource/WebformAllFormSubmissions.php +++ b/src/Plugin/rest/resource/WebformAllFormSubmissions.php @@ -4,13 +4,11 @@ use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; -use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Url; use Drupal\os2forms_rest_api\WebformHelper; use Drupal\rest\ModifiedResourceResponse; use Drupal\rest\Plugin\ResourceBase; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** @@ -131,11 +129,26 @@ public function get(string $webform_id): ModifiedResourceResponse { $submissionQuery = $submissionEntityStorage->getQuery() ->condition('webform_id', $webform_id); - foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) { - $errors = $this->updateSubmissionQuery($this->currentRequest, $submissionQuery, $param, $operator, $result); + $requestQuery = $this->currentRequest->query; - if (isset($errors['error'])) { - return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST); + foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) { + $value = $requestQuery->get($param); + + if (!empty($value)) { + try { + $dateTime = new \DateTimeImmutable($value); + $submissionQuery->condition('created', $dateTime->getTimestamp(), $operator); + $result[$param] = $value; + } + catch (\Exception $e) { + $errors = [ + 'error' => [ + 'message' => $this->t('Could not generate DateTime from :time', [':time' => $value]), + ], + ]; + + return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST); + } } } @@ -143,13 +156,10 @@ public function get(string $webform_id): ModifiedResourceResponse { $submissionQuery->accessCheck(FALSE); $sids = $submissionQuery->execute(); - $submissionData = []; - - if (!empty($sids)) { - $submissions = $submissionEntityStorage->loadMultiple($sids); - - foreach ($submissions as $submission) { - $url = Url::fromRoute( + // Generate submission URLs. + try { + $result['submissions'] = array_map( + static fn($submission) => Url::fromRoute( 'rest.webform_rest_submission.GET', [ 'webform_id' => $webform_id, @@ -157,42 +167,21 @@ public function get(string $webform_id): ModifiedResourceResponse { ] ) ->setAbsolute() - ->toString(TRUE)->getGeneratedUrl(); + ->toString(TRUE)->getGeneratedUrl(), + $submissionEntityStorage->loadMultiple($sids ?: []) + ); + } catch (\Exception $e) { + $errors = [ + 'error' => [ + 'message' => $this->t('Could not generate submission URLs'), + ], + ]; - $submissionData[$submission->id()] = $url; - } + return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR); } - $result['submissions'] = $submissionData; return new ModifiedResourceResponse($result); } - /** - * Updates submission query with request query parameters. - * - * @phpstan-param array $result - * @phpstan-return array - */ - private function updateSubmissionQuery(Request $request, QueryInterface $submissionQuery, string $parameter, string $operator, array &$result): array { - $value = $request->query->get($parameter); - - if (!empty($timeQuery)) { - try { - $startTime = new \DateTimeImmutable($timeQuery); - $submissionQuery->condition('created', $startTime->getTimestamp(), $operator); - $result[$parameter] = $timeQuery->format(\DateTimeImmutable::ATOM); - } - catch (\Exception $e) { - $errors = [ - 'error' => [ - 'message' => $this->t('Could not generate DateTime from :time', [':time' => $timeQuery]), - ], - ]; - } - } - - return $errors ?? []; - } - }