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 / Services / Entry / Managers / EntryFieldManager.php

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

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