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 / Confirmation / ConfirmationService.php

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

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