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 / Entry / SearchEntriesController.php

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

74 lines 2.4 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\Entry;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\ValidationException;
13 use IvyForms\Common\Sanitizer\Sanitizer;
14 use IvyForms\Controllers\Controller;
15 use IvyForms\Factory\Entry\EntryFactory;
16 use IvyForms\Services\Entry\EntryService;
17 use IvyForms\Services\Translations\BackendStrings;
18 use WP_REST_Request;
19 use WP_REST_Response;
20
21 class SearchEntriesController extends Controller
22 {
23 private EntryService $entryService;
24
25 public function __construct(
26 EntryService $entryService
27 ) {
28 $this->entryService = $entryService;
29 }
30
31 /**
32 * @param WP_REST_Request $data
33 *
34 * @return WP_REST_Response
35 * @throws ForbiddenException
36 * @throws InvalidArgumentException
37 * @throws ValidationException
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 searchEntries method
48 $entriesArr = $this->entryService->getEntryManager()->searchEntries($params);
49
50 // Map the data to Entry entities
51 $entries = array_map(function ($entryData) {
52 $entryEntity = EntryFactory::create($entryData);
53 return $entryEntity->toArray();
54 }, $entriesArr['data']);
55
56 $filterCount = $this->entryService->getEntryManager()->getFilterCount($params);
57 // Pass `true` to retrieve raw field values for the result table.
58 $entryFields = $this->entryService->getEntryFieldManager()->getEntryFields($entriesArr['data'], true);
59 $formIdToName = $entriesArr['formIdToName'] ?? [];
60
61 // Return the response with data and meta
62 return new WP_REST_Response([
63 'message' => BackendStrings::getCommonStrings()['ok'],
64 'data' => [
65 'data' => $entries,
66 'meta' => $entriesArr['meta'] ?? [],
67 'filterCount' => $filterCount,
68 'entryFields' => $entryFields,
69 'formIdToName' => $formIdToName,
70 ],
71 ], 200);
72 }
73 }
74