PluginProbe
The Innovative Form Builder – IvyForms / 0.9.1
The Innovative Form Builder – IvyForms v0.9.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 0.9.1, at backend/src/Services/Confirmation/ConfirmationService.php

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