PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Form / SearchFormsController.php

SearchFormsController.php in The Innovative Form Builder – IvyForms 1.0, at backend/src/Controllers/Form/SearchFormsController.php

71 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers\Form;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Sanitizer\Sanitizer;
13 use IvyForms\Controllers\Controller;
14 use IvyForms\Services\Entry\EntryService;
15 use IvyForms\Services\Form\FormService;
16 use IvyForms\Services\Translations\BackendStrings;
17 use WP_REST_Request;
18 use WP_REST_Response;
19
20 class SearchFormsController extends Controller
21 {
22 private FormService $formService;
23 private EntryService $entryService;
24 public function __construct(
25 FormService $formService,
26 EntryService $entryService
27 ) {
28 $this->formService = $formService;
29 $this->entryService = $entryService;
30 }
31
32 /**
33 * @param WP_REST_Request $data
34 *
35 * @return WP_REST_Response
36 * @throws ForbiddenException
37 * @throws InvalidArgumentException
38 */
39 public function handle(WP_REST_Request $data): WP_REST_Response
40 {
41 // Verify the nonce
42 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
43
44 // Sanitize the filtering data
45 $params = Sanitizer::sanitizeSearchParams($data->get_query_params());
46
47 // Call the searchForms method
48 $formsArr = $this->formService->searchForms($params);
49
50 // Map the data to an array format
51 $forms = array_map(fn($form) => (array) $form, $formsArr['data']);
52
53 $filterCount = $this->formService->getFilterCount($params['shouldGetCount']);
54
55 // Collect form IDs for entry count
56 $formIds = array_column($forms, 'id');
57 $entryCounts = $this->entryService->getEntryManager()->getEntryCountByFormIds($formIds);
58
59 // Return the response with data, meta, filterCount, and entryCounts
60 return new WP_REST_Response([
61 'message' => BackendStrings::getCommonStrings()['ok'],
62 'data' => [
63 'data' => $forms,
64 'meta' => $formsArr['meta'],
65 'filterCount' => $filterCount,
66 'entryCounts' => $entryCounts,
67 ],
68 ], 200);
69 }
70 }
71