PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
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.5, at backend/src/Services/Entry/Managers/EntryFieldManager.php

164 lines 5.6 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 * @return array<mixed>
41 */
42 public function getEntryFields(array $entries): array
43 {
44 $entryFields = $this->entryRepository->getEntryFields($entries);
45 return $this->valueManager->formatForFrontend($entryFields);
46 }
47
48 /**
49 * Check if a value already exists in entry fields for a specific field.
50 *
51 * @param int $formId
52 * @param int $fieldId
53 * @param string $value
54 * @return bool
55 */
56 public function checkDuplicateValue(int $formId, int $fieldId, string $value): bool
57 {
58 return $this->entryFieldRepository->checkDuplicateValue($formId, $fieldId, $value);
59 }
60
61 /**
62 * Add entry fields to the repository.
63 *
64 * @param array<int, object> $formFields
65 * @param int $entryId
66 * @param array<mixed> $submissionData
67 * @throws ValidationException
68 */
69 public function addEntryFields(array $formFields, int $entryId, array $submissionData): void
70 {
71 $parentChildrenMap = EntryHelper::buildParentChildrenMap($formFields);
72 $parentSubfieldKeys = EntryHelper::buildParentSubfieldKeys($parentChildrenMap, $submissionData);
73
74 foreach ($formFields as $field) {
75 $value = $this->valueManager->resolveFieldValue(
76 $field,
77 $submissionData,
78 $parentChildrenMap,
79 $parentSubfieldKeys
80 );
81
82 $entryFieldData = [
83 'entryId' => $entryId,
84 'fieldId' => $field->getId(),
85 'fieldValue' => $value,
86 ];
87
88 /**
89 * Filters the entry field data before creating the entry field object.
90 * Allows modification of entry data before persistence.
91 *
92 * @since 0.1.0
93 *
94 * @param mixed[] $entryFieldData The entry field data array.
95 * @param object $field The field object.
96 * @param mixed[] $submissionData The submission data array.
97 * @return mixed[] The modified entry field data array.
98 */
99 $entryFieldData = apply_filters(
100 'ivyforms/entry/field/before_create',
101 $entryFieldData,
102 $field,
103 $submissionData
104 );
105
106 /**
107 * Filters the entry field data for a specific field type before creating the entry field object.
108 * Allows dynamic hook registration per field type
109 *
110 * @since 0.1.0
111 *
112 * @param mixed[] $entryFieldData The entry field data array.
113 * @param object $field The field object.
114 * @param mixed[] $submissionData The submission data array.
115 * @return mixed[] The modified entry field data array.
116 */
117 $entryFieldData = apply_filters(
118 "ivyforms/entry/field/before_create/{$field->getType()}",
119 $entryFieldData,
120 $field,
121 $submissionData
122 );
123
124 $entryField = EntryFieldFactory::create($entryFieldData);
125
126 $this->entryFieldRepository->add($entryField);
127
128 /**
129 * Action fired after an entry field has been saved to the database.
130 * Allows post-processing or logging of entry field creation.
131 *
132 * @since 0.1.0
133 *
134 * @param object $entryField The created entry field object.
135 * @param object $field The field object.
136 * @param mixed[] $submissionData The submission data array.
137 */
138 do_action(
139 'ivyforms/entry/field/after_create',
140 $entryField,
141 $field,
142 $submissionData
143 );
144
145 /**
146 * Action fired after an entry field of a specific type has been saved to the database.
147 * Allows dynamic hook registration per field type
148 *
149 * @since 0.1.0
150 *
151 * @param object $entryField The created entry field object.
152 * @param object $field The field object.
153 * @param mixed[] $submissionData The submission data array.
154 */
155 do_action(
156 "ivyforms/entry/field/after_create/{$field->getType()}",
157 $entryField,
158 $field,
159 $submissionData
160 );
161 }
162 }
163 }
164