PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.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
← All changes | backend/src/Repository/Confirmation/ConfirmationRepository.php +134 -18 0.1.2 → 1.5 View file →
@@ -1,18 +1,14 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Repository\Confirmation;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
10 5 use IvyForms\Common\Exceptions\QueryExecutionException;
11 6 use IvyForms\Entity\Confirmation\Confirmation;
12 7 use IvyForms\Factory\Confirmation\ConfirmationFactory;
13 8 use IvyForms\Repository\AbstractRepository;
14 9 use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface;
10 +use IvyForms\Services\Translations\BackendStrings;
15 11
16 12 class ConfirmationRepository extends AbstractRepository implements ConfirmationRepositoryInterface
17 13 {
18 14 public const FACTORY = ConfirmationFactory::class;
@@ -33,20 +29,26 @@
33 29 $result = $this->wpdb->insert(
34 30 $this->table,
35 31 [
36 32 'formId' => (int) $data['formId'],
33 + 'name' => $data['name'],
37 34 'type' => $data['type'],
38 - 'status' => (int) $data['status'],
35 + 'enabled' => (int) $data['enabled'],
39 36 'showForm' => (int) $data['showForm'],
40 37 'message' => $data['message'],
41 38 'url' => $data['url'],
42 39 'page' => $data['page'],
40 + 'position' => (int) $data['position'],
41 + 'isDefault' => (int) $data['isDefault'],
42 + 'smartLogic' => wp_json_encode($data['smartLogic']),
43 43 ],
44 - ['%d', '%s', '%d', '%d', '%s', '%s', '%s']
44 + ['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s']
45 45 );
46 46
47 47 if ($result === false) {
48 - throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
48 + throw new QueryExecutionException(
49 + BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
50 + );
49 51 }
50 52
51 53 return $this->wpdb->insert_id;
52 54 }
@@ -68,22 +70,26 @@
68 70 $result = $this->wpdb->update(
69 71 $this->table,
70 72 [
71 73 'formId' => (int) $data['formId'],
74 + 'name' => $data['name'],
72 75 'type' => $data['type'],
73 - 'status' => (int) $data['status'],
76 + 'enabled' => (int) $data['enabled'],
74 77 'showForm' => (int) $data['showForm'],
75 78 'message' => $data['message'],
76 79 'url' => $data['url'],
77 80 'page' => $data['page'],
81 + 'position' => (int) $data['position'],
82 + 'isDefault' => (int) $data['isDefault'],
83 + 'smartLogic' => wp_json_encode($data['smartLogic']),
78 84 ],
79 85 ['id' => $id],
80 - ['%d', '%s', '%d', '%d', '%s', '%s', '%s'],
86 + ['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s'],
81 87 ['%d']
82 88 );
83 89
84 90 if ($result === false) {
85 - throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
91 + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
86 92 }
87 93
88 94 return $result;
89 95 }
@@ -88,29 +94,139 @@
88 94 return $result;
89 95 }
90 96
91 97 /**
92 - * Get all confirmations by form ID.
98 + * Get all confirmations by form ID ordered by position.
93 99 *
94 100 * @param int $id
95 101 *
96 102 * @return array<Confirmation>
103 + * @throws QueryExecutionException
97 104 */
98 105 public function getAllById(int $id): array
99 106 {
107 + $sql = $this->selectQuery()
108 + . " WHERE $this->table.formId = %d"
109 + . " ORDER BY $this->table.position ASC, $this->table.id ASC";
110 +
100 111 $rows = $this->wpdb->get_results(
101 - $this->wpdb->prepare(
102 - $this->selectQuery() . " WHERE $this->table.formId = %d",
103 - $id
104 - ),
112 + $this->wpdb->prepare($sql, $id),
105 113 ARRAY_A
106 114 );
107 -
115 + if ($rows === false) {
116 + throw new QueryExecutionException(
117 + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
118 + );
119 + }
108 120 if (empty($rows)) {
109 121 return [];
110 122 }
111 -
112 123 return array_map(function ($row) {
113 124 return call_user_func([static::FACTORY, 'create'], $row);
114 125 }, $rows);
126 + }
127 +
128 + /**
129 + * Count confirmations for a form.
130 + *
131 + * @param int $formId
132 + * @return int
133 + */
134 + public function countByFormId(int $formId): int
135 + {
136 + return (int) $this->wpdb->get_var(
137 + $this->wpdb->prepare(
138 + "SELECT COUNT(*) FROM $this->table WHERE formId = %d",
139 + $formId
140 + )
141 + );
142 + }
143 +
144 + /**
145 + * Get the next position value for a new confirmation on a form.
146 + *
147 + * @param int $formId
148 + * @return int
149 + */
150 + public function getNextPosition(int $formId): int
151 + {
152 + $max = $this->wpdb->get_var(
153 + $this->wpdb->prepare(
154 + "SELECT MAX(position) FROM $this->table WHERE formId = %d",
155 + $formId
156 + )
157 + );
158 +
159 + return $max !== null ? ((int) $max) + 1 : 0;
160 + }
161 +
162 + /**
163 + * Update positions for confirmations on a form.
164 + *
165 + * @param int $formId
166 + * @param array<int, int> $orderedIds Confirmation IDs in desired order
167 + * @return void
168 + */
169 + public function updatePositions(int $formId, array $orderedIds): void
170 + {
171 + foreach ($orderedIds as $position => $confirmationId) {
172 + $this->wpdb->update(
173 + $this->table,
174 + ['position' => (int) $position],
175 + [
176 + 'id' => (int) $confirmationId,
177 + 'formId' => $formId,
178 + ],
179 + ['%d'],
180 + ['%d', '%d']
181 + );
182 + }
183 + }
184 +
185 + /**
186 + * Set which confirmation is the default for a form.
187 + *
188 + * @param int $formId
189 + * @param int $defaultConfirmationId
190 + * @return void
191 + */
192 + public function syncDefaultFlag(int $formId, int $defaultConfirmationId): void
193 + {
194 + $this->wpdb->query(
195 + $this->wpdb->prepare(
196 + "UPDATE {$this->table} SET isDefault = CASE WHEN id = %d THEN 1 ELSE 0 END WHERE formId = %d",
197 + $defaultConfirmationId,
198 + $formId
199 + )
200 + );
201 + }
202 +
203 + /**
204 + * @return string[]
205 + */
206 + protected function getSearchableColumns(): array
207 + {
208 + return ['name', 'type'];
209 + }
210 +
211 + /**
212 + * @return string[]
213 + */
214 + protected function getFilterableColumns(): array
215 + {
216 + return ['formId'];
217 + }
218 +
219 + /**
220 + * @return string[]
221 + */
222 + protected function getSortableColumns(): array
223 + {
224 + return [
225 + 'id',
226 + 'name',
227 + 'type',
228 + 'enabled',
229 + 'position',
230 + ];
115 231 }
116 232 }