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