PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Entry / Managers / EntryFieldManager.php

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

166 lines 5.7 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 $value = $this->valueManager->resolveFieldValue(
78 $field,
79 $submissionData,
80 $parentChildrenMap,
81 $parentSubfieldKeys
82 );
83
84 $entryFieldData = [
85 'entryId' => $entryId,
86 'fieldId' => $field->getId(),
87 'fieldValue' => $value,
88 ];
89
90 /**
91 * Filters the entry field data before creating the entry field object.
92 * Allows modification of entry data before persistence.
93 *
94 * @since 0.1.0
95 *
96 * @param mixed[] $entryFieldData The entry field data array.
97 * @param object $field The field object.
98 * @param mixed[] $submissionData The submission data array.
99 * @return mixed[] The modified entry field data array.
100 */
101 $entryFieldData = apply_filters(
102 'ivyforms/entry/field/before_create',
103 $entryFieldData,
104 $field,
105 $submissionData
106 );
107
108 /**
109 * Filters the entry field data for a specific field type before creating the entry field object.
110 * Allows dynamic hook registration per field type
111 *
112 * @since 0.1.0
113 *
114 * @param mixed[] $entryFieldData The entry field data array.
115 * @param object $field The field object.
116 * @param mixed[] $submissionData The submission data array.
117 * @return mixed[] The modified entry field data array.
118 */
119 $entryFieldData = apply_filters(
120 "ivyforms/entry/field/before_create/{$field->getType()}",
121 $entryFieldData,
122 $field,
123 $submissionData
124 );
125
126 $entryField = EntryFieldFactory::create($entryFieldData);
127
128 $this->entryFieldRepository->add($entryField);
129
130 /**
131 * Action fired after an entry field has been saved to the database.
132 * Allows post-processing or logging of entry field creation.
133 *
134 * @since 0.1.0
135 *
136 * @param object $entryField The created entry field object.
137 * @param object $field The field object.
138 * @param mixed[] $submissionData The submission data array.
139 */
140 do_action(
141 'ivyforms/entry/field/after_create',
142 $entryField,
143 $field,
144 $submissionData
145 );
146
147 /**
148 * Action fired after an entry field of a specific type has been saved to the database.
149 * Allows dynamic hook registration per field type
150 *
151 * @since 0.1.0
152 *
153 * @param object $entryField The created entry field object.
154 * @param object $field The field object.
155 * @param mixed[] $submissionData The submission data array.
156 */
157 do_action(
158 "ivyforms/entry/field/after_create/{$field->getType()}",
159 $entryField,
160 $field,
161 $submissionData
162 );
163 }
164 }
165 }
166