|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_rest_api\Plugin\rest\resource; |
| 4 | + |
| 5 | +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; |
| 6 | +use Drupal\Component\Plugin\Exception\PluginNotFoundException; |
| 7 | +use Drupal\Core\Url; |
| 8 | +use Drupal\os2forms_rest_api\WebformHelper; |
| 9 | +use Drupal\rest\ModifiedResourceResponse; |
| 10 | +use Drupal\rest\Plugin\ResourceBase; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +/** |
| 15 | + * Creates a rest resource for retrieving webform submissions. |
| 16 | + * |
| 17 | + * @RestResource( |
| 18 | + * id = "webform_rest_form_submissions", |
| 19 | + * label = @Translation("Webform - submissions for a form"), |
| 20 | + * uri_paths = { |
| 21 | + * "canonical" = "/webform_rest/{webform_id}/submissions" |
| 22 | + * } |
| 23 | + * ) |
| 24 | + */ |
| 25 | +class WebformAllFormSubmissions extends ResourceBase { |
| 26 | + /** |
| 27 | + * Allowed DateTime query parameters and their operation. |
| 28 | + */ |
| 29 | + private const ALLOWED_DATETIME_QUERY_PARAMS = [ |
| 30 | + 'starttime' => '>=', |
| 31 | + 'endtime' => '<=', |
| 32 | + ]; |
| 33 | + |
| 34 | + /** |
| 35 | + * The current request. |
| 36 | + * |
| 37 | + * @var \Symfony\Component\HttpFoundation\Request |
| 38 | + */ |
| 39 | + private $currentRequest; |
| 40 | + |
| 41 | + /** |
| 42 | + * The entity type manager object. |
| 43 | + * |
| 44 | + * @var \Drupal\Core\Entity\EntityTypeManager |
| 45 | + */ |
| 46 | + private $entityTypeManager; |
| 47 | + |
| 48 | + /** |
| 49 | + * The webform helper. |
| 50 | + * |
| 51 | + * @var \Drupal\os2forms_rest_api\WebformHelper |
| 52 | + */ |
| 53 | + private $webformHelper; |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + * |
| 58 | + * @phpstan-param array<string, mixed> $configuration |
| 59 | + */ |
| 60 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 61 | + $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
| 62 | + |
| 63 | + $instance->entityTypeManager = $container->get('entity_type.manager'); |
| 64 | + $instance->currentRequest = $container->get('request_stack')->getCurrentRequest(); |
| 65 | + $instance->webformHelper = $container->get(WebformHelper::class); |
| 66 | + |
| 67 | + return $instance; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get submissions for a given webform. |
| 72 | + * |
| 73 | + * @param string $webform_id |
| 74 | + * Webform ID. |
| 75 | + * |
| 76 | + * @return \Drupal\rest\ModifiedResourceResponse |
| 77 | + * Response object. |
| 78 | + */ |
| 79 | + public function get(string $webform_id): ModifiedResourceResponse { |
| 80 | + if (empty($webform_id)) { |
| 81 | + $errors = [ |
| 82 | + 'error' => [ |
| 83 | + 'message' => 'Webform ID is required.', |
| 84 | + ], |
| 85 | + ]; |
| 86 | + return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST); |
| 87 | + } |
| 88 | + |
| 89 | + // Attempt finding webform. |
| 90 | + $webform = $this->webformHelper->getWebform($webform_id); |
| 91 | + |
| 92 | + if (NULL === $webform) { |
| 93 | + $errors = [ |
| 94 | + 'error' => [ |
| 95 | + 'message' => $this->t('Could not find webform with id :webform_id', [':webform_id' => $webform_id]), |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + return new ModifiedResourceResponse($errors, Response::HTTP_NOT_FOUND); |
| 100 | + } |
| 101 | + |
| 102 | + // Webform access check. |
| 103 | + if (!$this->webformHelper->hasWebformAccess($webform, $this->webformHelper->getCurrentUser())) { |
| 104 | + $errors = [ |
| 105 | + 'error' => [ |
| 106 | + 'message' => $this->t('Access denied'), |
| 107 | + ], |
| 108 | + ]; |
| 109 | + |
| 110 | + return new ModifiedResourceResponse($errors, Response::HTTP_UNAUTHORIZED); |
| 111 | + } |
| 112 | + |
| 113 | + $result = ['webform_id' => $webform_id]; |
| 114 | + |
| 115 | + try { |
| 116 | + $submissionEntityStorage = $this->entityTypeManager->getStorage('webform_submission'); |
| 117 | + } |
| 118 | + catch (InvalidPluginDefinitionException | PluginNotFoundException $e) { |
| 119 | + $errors = [ |
| 120 | + 'error' => [ |
| 121 | + 'message' => $this->t('Could not load webform submission storage'), |
| 122 | + ], |
| 123 | + ]; |
| 124 | + |
| 125 | + return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR); |
| 126 | + } |
| 127 | + |
| 128 | + // Query for webform submissions with this webform_id. |
| 129 | + $submissionQuery = $submissionEntityStorage->getQuery() |
| 130 | + ->condition('webform_id', $webform_id); |
| 131 | + |
| 132 | + $requestQuery = $this->currentRequest->query; |
| 133 | + |
| 134 | + foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) { |
| 135 | + $value = $requestQuery->get($param); |
| 136 | + |
| 137 | + if (!empty($value)) { |
| 138 | + try { |
| 139 | + $dateTime = new \DateTimeImmutable($value); |
| 140 | + $submissionQuery->condition('created', $dateTime->getTimestamp(), $operator); |
| 141 | + $result[$param] = $value; |
| 142 | + } |
| 143 | + catch (\Exception $e) { |
| 144 | + $errors = [ |
| 145 | + 'error' => [ |
| 146 | + 'message' => $this->t('Invalid :param: :value', [':param' => $param, ':value' => $value]), |
| 147 | + ], |
| 148 | + ]; |
| 149 | + |
| 150 | + return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Complete query. |
| 156 | + $submissionQuery->accessCheck(FALSE); |
| 157 | + $sids = $submissionQuery->execute(); |
| 158 | + |
| 159 | + // Generate submission URLs. |
| 160 | + try { |
| 161 | + $result['submissions'] = array_map( |
| 162 | + static fn($submission) => Url::fromRoute( |
| 163 | + 'rest.webform_rest_submission.GET', |
| 164 | + [ |
| 165 | + 'webform_id' => $webform_id, |
| 166 | + 'uuid' => $submission->uuid(), |
| 167 | + ] |
| 168 | + ) |
| 169 | + ->setAbsolute() |
| 170 | + ->toString(TRUE)->getGeneratedUrl(), |
| 171 | + $submissionEntityStorage->loadMultiple($sids ?: []) |
| 172 | + ); |
| 173 | + } |
| 174 | + catch (\Exception $e) { |
| 175 | + $errors = [ |
| 176 | + 'error' => [ |
| 177 | + 'message' => $this->t('Could not generate submission URLs'), |
| 178 | + ], |
| 179 | + ]; |
| 180 | + |
| 181 | + return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR); |
| 182 | + } |
| 183 | + |
| 184 | + return new ModifiedResourceResponse($result); |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments