PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
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
← All changes | backend/src/Controllers/Entry/SearchEntriesController.php +92 -0 0.6.1 → 1.5.1 View file →
@@ -1,0 +1,92 @@
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\Form\FormService;
18 +use IvyForms\Services\Permissions\AdminFormListPermissionService;
19 +use IvyForms\Services\Permissions\IvyFormsAccess;
20 +use IvyForms\Services\Translations\BackendStrings;
21 +use WP_REST_Request;
22 +use WP_REST_Response;
23 +
24 +class SearchEntriesController extends Controller
25 +{
26 + private EntryService $entryService;
27 +
28 + private FormService $formService;
29 +
30 + private AdminFormListPermissionService $adminFormListPermissionService;
31 +
32 + public function __construct(
33 + EntryService $entryService,
34 + FormService $formService,
35 + AdminFormListPermissionService $adminFormListPermissionService
36 + ) {
37 + $this->entryService = $entryService;
38 + $this->formService = $formService;
39 + $this->adminFormListPermissionService = $adminFormListPermissionService;
40 + }
41 +
42 + /**
43 + * @param WP_REST_Request $data
44 + *
45 + * @return WP_REST_Response
46 + * @throws ForbiddenException
47 + * @throws InvalidArgumentException
48 + * @throws ValidationException
49 + */
50 + public function handle(WP_REST_Request $data): WP_REST_Response
51 + {
52 + // Verify the nonce
53 + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
54 +
55 + // Sanitize the filtering data
56 + $params = Sanitizer::sanitizeSearchParams($data->get_query_params());
57 +
58 + if (!IvyFormsAccess::hasImplicitElevatedAccess()) {
59 + $allIdsUnique = $this->formService->getAllFormIds();
60 + $visibleIds = $this->adminFormListPermissionService->filterFormIdsVisibleInEntryLists($allIdsUnique);
61 + if ($visibleIds !== $allIdsUnique) {
62 + $params['visibleFormIds'] = $visibleIds;
63 + }
64 + }
65 +
66 + // Call the searchEntries method
67 + $entriesArr = $this->entryService->getEntryManager()->searchEntries($params);
68 +
69 + // Map the data to Entry entities
70 + $entries = array_map(function ($entryData) {
71 + $entryEntity = EntryFactory::create($entryData);
72 + return $entryEntity->toArray();
73 + }, $entriesArr['data']);
74 +
75 + $filterCount = $this->entryService->getEntryManager()->getFilterCount($params);
76 + // Pass `true` to retrieve raw field values for the result table.
77 + $entryFields = $this->entryService->getEntryFieldManager()->getEntryFields($entriesArr['data'], true);
78 + $formIdToName = $entriesArr['formIdToName'] ?? [];
79 +
80 + // Return the response with data and meta
81 + return new WP_REST_Response([
82 + 'message' => BackendStrings::getCommonStrings()['ok'],
83 + 'data' => [
84 + 'data' => $entries,
85 + 'meta' => $entriesArr['meta'] ?? [],
86 + 'filterCount' => $filterCount,
87 + 'entryFields' => $entryFields,
88 + 'formIdToName' => $formIdToName,
89 + ],
90 + ], 200);
91 + }
92 +}