PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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
ivyforms / backend / src / Services / Confirmation / ConfirmationService.php

ConfirmationService.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/Services/Confirmation/ConfirmationService.php

214 lines 6.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\Confirmation;
4
5 use IvyForms\Common\Exceptions\NotFoundException;
6 use IvyForms\Common\Exceptions\InvalidArgumentException;
7 use IvyForms\Common\Exceptions\QueryExecutionException;
8 use IvyForms\Entity\Confirmation\Confirmation;
9 use IvyForms\Factory\Confirmation\ConfirmationFactory;
10 use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface;
11 use IvyForms\Services\Placeholder\PlaceholderService;
12 use IvyForms\Services\Media\ImageService;
13 use IvyForms\Services\Translations\BackendStrings;
14
15 class ConfirmationService
16 {
17 private ConfirmationRepositoryInterface $confirmationRepository;
18 private ImageService $imageService;
19
20 // Constructor injection for the ConfirmationRepository via PHP-DI
21 public function __construct(
22 ConfirmationRepositoryInterface $confirmationRepository,
23 ImageService $imageService
24 ) {
25 $this->confirmationRepository = $confirmationRepository;
26 $this->imageService = $imageService;
27 }
28
29 /**
30 * Create a new confirmation.
31 *
32 * @param Confirmation $confirmationData
33 *
34 * @return int Confirmation ID
35 */
36 public function createConfirmation(Confirmation $confirmationData): int
37 {
38 // Process images in message content before saving
39 $message = $confirmationData->getMessage();
40 if (!empty($message)) {
41 $processedMessage = $this->imageService->processImagesInContent($message, 'confirmation');
42 $confirmationData->setMessage($processedMessage);
43 }
44
45 // Create confirmation via repository and return new confirmation ID
46 return $this->confirmationRepository->add($confirmationData);
47 }
48
49 /**
50 * Update an existing confirmation.
51 *
52 * @param int $confirmationId
53 * @param Confirmation $confirmationData
54 *
55 * @return bool
56 */
57 public function updateConfirmation(int $confirmationId, Confirmation $confirmationData): bool
58 {
59 // Process images in message content before saving
60 $message = $confirmationData->getMessage();
61 if (!empty($message)) {
62 $processedMessage = $this->imageService->processImagesInContent($message, 'confirmation');
63 $confirmationData->setMessage($processedMessage);
64 }
65
66 return $this->confirmationRepository->update($confirmationId, $confirmationData);
67 }
68
69 /**
70 * Get confirmations by form ID.
71 *
72 * @param int $id
73 *
74 * @return mixed[]|null
75 *
76 * @throws NotFoundException If no confirmations are found
77 */
78 public function getConfirmationsById(int $id): ?array
79 {
80 $confirmation = $this->confirmationRepository->getAllById($id);
81
82 if (!$confirmation) {
83 throw new NotFoundException(
84 BackendStrings::getExceptionStrings()['confirmation_not_found']
85 );
86 }
87
88 return $confirmation;
89 }
90 /**
91 * Get a specific form by its ID.
92 *
93 * @param int $id
94 *
95 * @return object ConfirmationEntity
96 *
97 * @throws NotFoundException If the confirmation is not found
98 */
99 public function getConfirmationById(int $id): object
100 {
101 $confirmation = $this->confirmationRepository->getById($id);
102
103 if (!$confirmation) {
104 throw new NotFoundException(
105 BackendStrings::getExceptionStrings()['confirmation_not_found']
106 );
107 }
108
109 return $confirmation;
110 }
111
112 /**
113 * Delete confirmations by multiple form IDs.
114 *
115 * @param array<int> $formIds
116 * @return int Number of deleted confirmations
117 * @throws InvalidArgumentException If no form IDs are provided
118 */
119 public function deleteConfirmationsByFormIds(array $formIds): int
120 {
121 if (empty($formIds)) {
122 throw new InvalidArgumentException(
123 BackendStrings::getExceptionStrings()['no_form_ids_provided']
124 );
125 }
126
127 return $this->confirmationRepository->deleteManyByForeignKeyValues($formIds);
128 }
129
130 /**
131 * Duplicate confirmations from one form to another.
132 *
133 * @param int $originalFormId
134 * @param int $newFormId
135 *
136 * @return void
137 * @throws NotFoundException
138 */
139 public function duplicateConfirmations(int $originalFormId, int $newFormId): void
140 {
141 $confirmations = $this->getConfirmationsById($originalFormId);
142 foreach ($confirmations as $confirmation) {
143 $confirmationData = $confirmation->toArray();
144 unset($confirmationData['id']);
145 $confirmationData['formId'] = $newFormId;
146
147 $this->createConfirmation(ConfirmationFactory::create($confirmationData));
148 }
149 }
150
151 /**
152 * Create a default confirmation for a form.
153 *
154 * @param int $formId
155 * @return int Confirmation ID
156 */
157 public function createDefaultConfirmationForForm(int $formId): int
158 {
159 $defaultConfirmation = [
160 'formId' => $formId,
161 'type' => 'successMessage',
162 'enabled' => 1,
163 'showForm' => 0,
164 'message' => BackendStrings::getAllFormsStrings()['thanks_reaching'],
165 'url' => '',
166 'page' => '',
167 ];
168 $confirmation = ConfirmationFactory::create($defaultConfirmation);
169 $confirmationId = $this->createConfirmation($confirmation);
170 if (!$confirmationId) {
171 throw new QueryExecutionException(
172 BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_confirmation']
173 );
174 }
175 $confirmation->setId($confirmationId);
176 return $confirmationId;
177 }
178
179 /**
180 * Process confirmations for the form.
181 *
182 * @param int $formId
183 * @param array<string, mixed> $fieldData
184 * @param array<string, mixed> $generalData
185 * @param array<string, string> $fieldLabels
186 * @return string
187 * @throws NotFoundException
188 */
189 public function processConfirmations(
190 int $formId,
191 array $fieldData,
192 array $generalData,
193 array $fieldLabels = []
194 ): string {
195 $confirmations = $this->getConfirmationsById($formId);
196 foreach ($confirmations as $confirmation) {
197 if (
198 !empty($confirmation) &&
199 $confirmation->isEnabled() &&
200 $confirmation->getType() === 'successMessage'
201 ) {
202 $confirmationMessage = $confirmation->getMessage();
203 return PlaceholderService::replacePlaceholders(
204 $confirmationMessage,
205 $fieldData,
206 $generalData,
207 $fieldLabels
208 );
209 }
210 }
211 return '';
212 }
213 }
214