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 +121 -7 1.0 → 1.5 View file →
@@ -29,8 +29,9 @@
29 29 $result = $this->wpdb->insert(
30 30 $this->table,
31 31 [
32 32 'formId' => (int) $data['formId'],
33 + 'name' => $data['name'],
33 34 'type' => $data['type'],
34 35 'enabled' => (int) $data['enabled'],
35 36 'showForm' => (int) $data['showForm'],
36 37 'message' => $data['message'],
@@ -35,10 +36,13 @@
35 36 'showForm' => (int) $data['showForm'],
36 37 'message' => $data['message'],
37 38 'url' => $data['url'],
38 39 'page' => $data['page'],
40 + 'position' => (int) $data['position'],
41 + 'isDefault' => (int) $data['isDefault'],
42 + 'smartLogic' => wp_json_encode($data['smartLogic']),
39 43 ],
40 - ['%d', '%s', '%d', '%d', '%s', '%s', '%s']
44 + ['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s']
41 45 );
42 46
43 47 if ($result === false) {
44 48 throw new QueryExecutionException(
@@ -66,8 +70,9 @@
66 70 $result = $this->wpdb->update(
67 71 $this->table,
68 72 [
69 73 'formId' => (int) $data['formId'],
74 + 'name' => $data['name'],
70 75 'type' => $data['type'],
71 76 'enabled' => (int) $data['enabled'],
72 77 'showForm' => (int) $data['showForm'],
73 78 'message' => $data['message'],
@@ -72,11 +77,14 @@
72 77 'showForm' => (int) $data['showForm'],
73 78 'message' => $data['message'],
74 79 'url' => $data['url'],
75 80 'page' => $data['page'],
81 + 'position' => (int) $data['position'],
82 + 'isDefault' => (int) $data['isDefault'],
83 + 'smartLogic' => wp_json_encode($data['smartLogic']),
76 84 ],
77 85 ['id' => $id],
78 - ['%d', '%s', '%d', '%d', '%s', '%s', '%s'],
86 + ['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s'],
79 87 ['%d']
80 88 );
81 89
82 90 if ($result === false) {
@@ -86,9 +94,9 @@
86 94 return $result;
87 95 }
88 96
89 97 /**
90 - * Get all confirmations by form ID.
98 + * Get all confirmations by form ID ordered by position.
91 99 *
92 100 * @param int $id
93 101 *
94 102 * @return array<Confirmation>
@@ -95,13 +103,14 @@
95 103 * @throws QueryExecutionException
96 104 */
97 105 public function getAllById(int $id): array
98 106 {
107 + $sql = $this->selectQuery()
108 + . " WHERE $this->table.formId = %d"
109 + . " ORDER BY $this->table.position ASC, $this->table.id ASC";
110 +
99 111 $rows = $this->wpdb->get_results(
100 - $this->wpdb->prepare(
101 - $this->selectQuery() . " WHERE $this->table.formId = %d",
102 - $id
103 - ),
112 + $this->wpdb->prepare($sql, $id),
104 113 ARRAY_A
105 114 );
106 115 if ($rows === false) {
107 116 throw new QueryExecutionException(
@@ -113,6 +122,111 @@
113 122 }
114 123 return array_map(function ($row) {
115 124 return call_user_func([static::FACTORY, 'create'], $row);
116 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 + ];
117 231 }
118 232 }