PluginProbe
The Innovative Form Builder – IvyForms / 0.8.2
The Innovative Form Builder – IvyForms v0.8.2
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 / Services / Entry / Managers / EntryFieldManager.php

EntryFieldManager.php in The Innovative Form Builder – IvyForms 0.8.2, at backend/src/Services/Entry/Managers/EntryFieldManager.php

172 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Entry\Managers;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\ValidationException;
11 use IvyForms\Common\Helpers\EntryHelper;
12 use IvyForms\Factory\EntryField\EntryFieldFactory;
13 use IvyForms\Repository\Entry\EntryRepositoryInterface;
14 use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface;
15
16 /**
17 * Manages EntryField repository operations.
18 */
19 class EntryFieldManager
20 {
21 private EntryFieldRepositoryInterface $entryFieldRepository;
22 private EntryRepositoryInterface $entryRepository;
23 private EntryFieldValueManager $valueManager;
24
25 public function __construct(
26 EntryFieldRepositoryInterface $entryFieldRepository,
27 EntryRepositoryInterface $entryRepository,
28 EntryFieldValueManager $valueManager
29 ) {
30 $this->entryFieldRepository = $entryFieldRepository;
31 $this->entryRepository = $entryRepository;
32 $this->valueManager = $valueManager;
33 }
34
35 /**
36 * Fetch entries along with their associated fields.
37 * Automatically formats field values for display.
38 *
39 * @param array<int, array<string, mixed>> $entries
40 * @param bool $skipFormatting Whether to skip formatting and return raw values (used for result tables).
41 * @return array<mixed>
42 * @SuppressWarnings(PHPMD)
43 */
44 public function getEntryFields(array $entries, bool $skipFormatting = false): array
45 {
46 $entryFields = $this->entryRepository->getEntryFields($entries);
47 return $this->valueManager->formatForFrontend($entryFields, $skipFormatting);
48 }
49
50 /**
51 * Check if a value already exists in entry fields for a specific field.
52 *
53 * @param int $formId
54 * @param int $fieldId
55 * @param string $value
56 * @return bool
57 */
58 public function checkDuplicateValue(int $formId, int $fieldId, string $value): bool
59 {
60 return $this->entryFieldRepository->checkDuplicateValue($formId, $fieldId, $value);
61 }
62
63 /**
64 * Add entry fields to the repository.
65 *
66 * @param array<int, object> $formFields
67 * @param int $entryId
68 * @param array<mixed> $submissionData
69 * @throws ValidationException
70 */
71 public function addEntryFields(array $formFields, int $entryId, array $submissionData): void
72 {
73 $parentChildrenMap = EntryHelper::buildParentChildrenMap($formFields);
74 $parentSubfieldKeys = EntryHelper::buildParentSubfieldKeys($parentChildrenMap, $submissionData);
75
76 foreach ($formFields as $field) {
77 // Skip security/CAPTCHA fields - their tokens are only needed for verification, not storage
78 $fieldType = $field->getType();
79 if (in_array($fieldType, ['recaptcha', 'turnstile', 'hcaptcha'], true)) {
80 continue;
81 }
82
83 $value = $this->valueManager->resolveFieldValue(
84 $field,
85 $submissionData,
86 $parentChildrenMap,
87 $parentSubfieldKeys
88 );
89
90 $entryFieldData = [
91 'entryId' => $entryId,
92 'fieldId' => $field->getId(),
93 'fieldValue' => $value,
94 ];
95
96 /**
97 * Filters the entry field data before creating the entry field object.
98 * Allows modification of entry data before persistence.
99 *
100 * @since 0.1.0
101 *
102 * @param mixed[] $entryFieldData The entry field data array.
103 * @param object $field The field object.
104 * @param mixed[] $submissionData The submission data array.
105 * @return mixed[] The modified entry field data array.
106 */
107 $entryFieldData = apply_filters(
108 'ivyforms/entry/field/before_create',
109 $entryFieldData,
110 $field,
111 $submissionData
112 );
113
114 /**
115 * Filters the entry field data for a specific field type before creating the entry field object.
116 * Allows dynamic hook registration per field type
117 *
118 * @since 0.1.0
119 *
120 * @param mixed[] $entryFieldData The entry field data array.
121 * @param object $field The field object.
122 * @param mixed[] $submissionData The submission data array.
123 * @return mixed[] The modified entry field data array.
124 */
125 $entryFieldData = apply_filters(
126 "ivyforms/entry/field/before_create/{$field->getType()}",
127 $entryFieldData,
128 $field,
129 $submissionData
130 );
131
132 $entryField = EntryFieldFactory::create($entryFieldData);
133
134 $this->entryFieldRepository->add($entryField);
135
136 /**
137 * Action fired after an entry field has been saved to the database.
138 * Allows post-processing or logging of entry field creation.
139 *
140 * @since 0.1.0
141 *
142 * @param object $entryField The created entry field object.
143 * @param object $field The field object.
144 * @param mixed[] $submissionData The submission data array.
145 */
146 do_action(
147 'ivyforms/entry/field/after_create',
148 $entryField,
149 $field,
150 $submissionData
151 );
152
153 /**
154 * Action fired after an entry field of a specific type has been saved to the database.
155 * Allows dynamic hook registration per field type
156 *
157 * @since 0.1.0
158 *
159 * @param object $entryField The created entry field object.
160 * @param object $field The field object.
161 * @param mixed[] $submissionData The submission data array.
162 */
163 do_action(
164 "ivyforms/entry/field/after_create/{$field->getType()}",
165 $entryField,
166 $field,
167 $submissionData
168 );
169 }
170 }
171 }
172