2121 */
2222class WebformAllFormSubmissions extends ResourceBase {
2323
24- /**
25- * The current request.
26- *
27- * @var \Symfony\Component\HttpFoundation\Request
28- */
29- private $ currentRequest ;
30-
31- /**
32- * The entity type manager object.
33- *
34- * @var \Drupal\Core\Entity\EntityTypeManager
35- */
36- private $ entityTypeManager ;
37-
38- /**
39- * The webform helper.
40- *
41- * @var \Drupal\os2forms_rest_api\WebformHelper
42- */
43- private $ webformHelper ;
44-
45- /**
46- * {@inheritdoc}
47- */
48- public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
49- $ instance = parent ::create ($ container , $ configuration , $ plugin_id , $ plugin_definition );
50-
51- $ instance ->entityTypeManager = $ container ->get ('entity_type.manager ' );
52- $ instance ->setCurrentRequest ($ container ->get ('request_stack ' )->getCurrentRequest ());
53- $ instance ->webformHelper = $ container ->get ('Drupal\os2forms_rest_api\WebformHelper ' );
54-
55- return $ instance ;
24+ /**
25+ * The current request.
26+ *
27+ * @var \Symfony\Component\HttpFoundation\Request
28+ */
29+ private $ currentRequest ;
30+
31+ /**
32+ * The entity type manager object.
33+ *
34+ * @var \Drupal\Core\Entity\EntityTypeManager
35+ */
36+ private $ entityTypeManager ;
37+
38+ /**
39+ * The webform helper.
40+ *
41+ * @var \Drupal\os2forms_rest_api\WebformHelper
42+ */
43+ private $ webformHelper ;
44+
45+ /**
46+ * {@inheritdoc}
47+ */
48+ public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
49+ $ instance = parent ::create ($ container , $ configuration , $ plugin_id , $ plugin_definition );
50+
51+ $ instance ->entityTypeManager = $ container ->get ('entity_type.manager ' );
52+ $ instance ->setCurrentRequest ($ container ->get ('request_stack ' )->getCurrentRequest ());
53+ $ instance ->webformHelper = $ container ->get ('Drupal\os2forms_rest_api\WebformHelper ' );
54+
55+ return $ instance ;
56+ }
57+
58+ /**
59+ * Sets the current request.
60+ *
61+ * @param \Symfony\Component\HttpFoundation\Request $current_request
62+ * The current request.
63+ *
64+ * @return $this
65+ * Class.
66+ */
67+ protected function setCurrentRequest (Request $ current_request ) {
68+ $ this ->currentRequest = $ current_request ;
69+ return $ this ;
70+ }
71+
72+ /**
73+ * Retrieve all submissions for a given webform id.
74+ *
75+ * @param string $webform_id
76+ * Webform ID.
77+ *
78+ * @return \Drupal\rest\ModifiedResourceResponse
79+ * HTTP response object containing webform submissions.
80+ *
81+ * @throws \Symfony\Component\HttpKernel\Exception\HttpException
82+ * Throws HttpException in case of error.
83+ */
84+ public function get (string $ webform_id ): ModifiedResourceResponse {
85+ if (empty ($ webform_id )) {
86+ $ errors = [
87+ 'error ' => [
88+ 'message ' => 'Webform ID is required. ' ,
89+ ],
90+ ];
91+ return new ModifiedResourceResponse ($ errors , 400 );
5692 }
5793
58- /**
59- * Sets the current request.
60- *
61- * @param \Symfony\Component\HttpFoundation\Request $current_request
62- * The current request.
63- *
64- * @return $this
65- * Class.
66- */
67- protected function setCurrentRequest (Request $ current_request ) {
68- $ this ->currentRequest = $ current_request ;
69- return $ this ;
94+ // Webform access check.
95+ $ webform = $ this ->webformHelper ->getWebform ($ webform_id );
96+
97+ if (NULL === $ webform ) {
98+ $ errors = [
99+ 'error ' => [
100+ 'message ' => $ this ->t ('Could not find webform with id :webform_id ' , [':webform_id ' => $ webform_id ]),
101+ ],
102+ ];
103+ return new ModifiedResourceResponse ($ errors , 400 );
104+ }
105+
106+ if (!$ this ->webformHelper ->hasWebformAccess ($ webform , $ this ->webformHelper ->getCurrentUser ())) {
107+ $ errors = [
108+ 'error ' => [
109+ 'message ' => $ this ->t ('Access denied ' ),
110+ ],
111+ ];
112+ return new ModifiedResourceResponse ($ errors , 401 );
113+ }
114+
115+ $ submissionData = [];
116+
117+ $ result = ['webform_id ' => $ webform_id ];
118+
119+ // Query for webform submissions with this webform_id.
120+ $ query = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->getQuery ()
121+ ->condition ('webform_id ' , $ webform_id );
122+
123+ $ startTimestamp = $ this ->currentRequest ->query ->get ('starttime ' );
124+ if (is_numeric ($ startTimestamp )) {
125+ $ query ->condition ('created ' , $ startTimestamp , '>= ' );
126+ $ result ['starttime ' ] = $ startTimestamp ;
127+ }
128+
129+ $ endTimestamp = $ this ->currentRequest ->query ->get ('endtime ' );
130+ if (is_numeric ($ endTimestamp )) {
131+ $ query ->condition ('created ' , $ endTimestamp , '<= ' );
132+ $ result ['endtime ' ] = $ endTimestamp ;
70133 }
71134
72- /**
73- * Retrieve all submissions for a given webform id.
74- *
75- * @param string $webform_id
76- * Webform ID.
77- *
78- * @return \Drupal\rest\ModifiedResourceResponse
79- * HTTP response object containing webform submissions.
80- *
81- * @throws \Symfony\Component\HttpKernel\Exception\HttpException
82- * Throws HttpException in case of error.
83- */
84- public function get (string $ webform_id ): ModifiedResourceResponse {
85- if (empty ($ webform_id )) {
86- $ errors = [
87- 'error ' => [
88- 'message ' => 'Webform ID is required. ' ,
89- ],
90- ];
91- return new ModifiedResourceResponse ($ errors , 400 );
92- }
93-
94- // Webform access check.
95- $ webform = $ this ->webformHelper ->getWebform ($ webform_id );
96-
97- if (NULL === $ webform ) {
98- $ errors = [
99- 'error ' => [
100- 'message ' => $ this ->t ('Could not find webform with id :webform_id ' , [':webform_id ' => $ webform_id ]),
101- ],
102- ];
103- return new ModifiedResourceResponse ($ errors , 400 );
104- }
105-
106- if (!$ this ->webformHelper ->hasWebformAccess ($ webform , $ this ->webformHelper ->getCurrentUser ())) {
107- $ errors = [
108- 'error ' => [
109- 'message ' => $ this ->t ('Access denied ' ),
110- ],
111- ];
112- return new ModifiedResourceResponse ($ errors , 401 );
113- }
114-
115- $ submissionData = [];
116-
117- $ result = ['webform_id ' => $ webform_id ];
118-
119- // Query for webform submissions with this webform_id.
120- $ query = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->getQuery ()
121- ->condition ('webform_id ' , $ webform_id );
122-
123- $ startTimestamp = $ this ->currentRequest ->query ->get ('starttime ' );
124- if (is_numeric ($ startTimestamp )) {
125- $ query ->condition ('created ' , $ startTimestamp , '>= ' );
126- $ result ['starttime ' ] = $ startTimestamp ;
127- }
128-
129- $ endTimestamp = $ this ->currentRequest ->query ->get ('endtime ' );
130- if (is_numeric ($ endTimestamp )) {
131- $ query ->condition ('created ' , $ endTimestamp , '<= ' );
132- $ result ['endtime ' ] = $ endTimestamp ;
133- }
134-
135- $ query ->accessCheck (FALSE );
136- $ sids = $ query ->execute ();
137-
138- foreach ($ sids as $ sid ) {
139- /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
140- $ webform_submission = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->load ($ sid );
141-
142- $ url = Url::fromRoute (
143- 'rest.webform_rest_submission.GET ' ,
144- [
145- 'webform_id ' => $ webform_id ,
146- 'uuid ' => $ webform_submission ->uuid (),
147- ],
148- [
149- 'absolute ' => TRUE ,
150- ]
151- )->toString ();
152-
153- $ submissionData [$ sid ] = $ url ;
154- }
155-
156- $ result ['submissions ' ] = $ submissionData ;
157-
158- return new ModifiedResourceResponse ($ result );
135+ $ query ->accessCheck (FALSE );
136+ $ sids = $ query ->execute ();
137+
138+ foreach ($ sids as $ sid ) {
139+ /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
140+ $ webform_submission = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->load ($ sid );
141+
142+ $ url = Url::fromRoute (
143+ 'rest.webform_rest_submission.GET ' ,
144+ [
145+ 'webform_id ' => $ webform_id ,
146+ 'uuid ' => $ webform_submission ->uuid (),
147+ ],
148+ [
149+ 'absolute ' => TRUE ,
150+ ]
151+ )->toString ();
152+
153+ $ submissionData [$ sid ] = $ url ;
159154 }
160155
161- }
156+ $ result ['submissions ' ] = $ submissionData ;
157+
158+ return new ModifiedResourceResponse ($ result );
159+ }
160+
161+ }
0 commit comments