← All changes
|
backend/src/Repository/Notification/NotificationRepository.php
+123
-36
0.1.2
→
1.5
View file →
| @@ -1,23 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See LICENCE.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Repository\Notification; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 16 | 11 | use IvyForms\Entity\Notification\Notification; |
| 17 | 12 | use IvyForms\Factory\Notification\NotificationFactory; |
| 18 | 13 | use IvyForms\Repository\AbstractRepository; |
| 19 | 14 | use IvyForms\Repository\Notification\NotificationRepositoryInterface; |
| 15 | +use IvyForms\Services\Translations\BackendStrings; | |
| 20 | 16 | |
| 21 | 17 | class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface |
| 22 | 18 | { |
| 23 | 19 | public const FACTORY = NotificationFactory::class; |
| @@ -33,26 +29,20 @@ | ||
| 33 | 29 | */ |
| 34 | 30 | public function add($entity): int |
| 35 | 31 | { |
| 36 | 32 | $data = $entity->toArray(); |
| 33 | + $row = $this->buildPersistRow($data); | |
| 37 | 34 | |
| 38 | 35 | $result = $this->wpdb->insert( |
| 39 | 36 | $this->table, |
| 40 | - [ | |
| 41 | - 'formId' => (int) $data['formId'], | |
| 42 | - 'name' => $data['name'], | |
| 43 | - 'sender' => $data['sender'], | |
| 44 | - 'receiver' => $data['receiver'], | |
| 45 | - 'status' => (int) $data['status'], | |
| 46 | - 'subject' => $data['subject'], | |
| 47 | - 'message' => $data['message'], | |
| 48 | - 'smartLogic' => (int) $data['smartLogic'], | |
| 49 | - ], | |
| 50 | - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'] | |
| 37 | + $row['values'], | |
| 38 | + $row['formats'] | |
| 51 | 39 | ); |
| 52 | 40 | |
| 53 | 41 | if ($result === false) { |
| 54 | - throw new QueryExecutionException('Unable to add data in ' . __CLASS__); | |
| 42 | + throw new QueryExecutionException( | |
| 43 | + BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ | |
| 44 | + ); | |
| 55 | 45 | } |
| 56 | 46 | |
| 57 | 47 | return $this->wpdb->insert_id; |
| 58 | 48 | } |
| @@ -69,38 +59,91 @@ | ||
| 69 | 59 | */ |
| 70 | 60 | public function update(int $id, $entity): bool |
| 71 | 61 | { |
| 72 | 62 | $data = $entity->toArray(); |
| 63 | + $row = $this->buildPersistRow($data); | |
| 73 | 64 | |
| 74 | 65 | $result = $this->wpdb->update( |
| 75 | 66 | $this->table, |
| 76 | - [ | |
| 77 | - 'formId' => (int) $data['formId'], | |
| 78 | - 'name' => $data['name'], | |
| 79 | - 'sender' => $data['sender'], | |
| 80 | - 'receiver' => $data['receiver'], | |
| 81 | - 'status' => (int) $data['status'], | |
| 82 | - 'subject' => $data['subject'], | |
| 83 | - 'message' => $data['message'], | |
| 84 | - 'smartLogic' => (int) $data['smartLogic'], | |
| 85 | - ], | |
| 67 | + $row['values'], | |
| 86 | 68 | ['id' => $id], |
| 87 | - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'], | |
| 69 | + $row['formats'], | |
| 88 | 70 | ['%d'] |
| 89 | 71 | ); |
| 90 | 72 | |
| 91 | 73 | if ($result === false) { |
| 92 | - throw new QueryExecutionException('Unable to save data in ' . __CLASS__); | |
| 74 | + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); | |
| 93 | 75 | } |
| 94 | 76 | |
| 95 | - return $result; | |
| 77 | + return true; | |
| 96 | 78 | } |
| 79 | + | |
| 97 | 80 | /** |
| 81 | + * Build insert/update row values and formats, allowing Pro to append columns. | |
| 82 | + * | |
| 83 | + * @param array<string, mixed> $data | |
| 84 | + * @return array{values: array<string, mixed>, formats: string[]} | |
| 85 | + */ | |
| 86 | + private function buildPersistRow(array $data): array | |
| 87 | + { | |
| 88 | + $values = [ | |
| 89 | + 'formId' => (int) $data['formId'], | |
| 90 | + 'name' => $data['name'], | |
| 91 | + 'sender' => $data['sender'], | |
| 92 | + 'replyTo' => $data['replyTo'], | |
| 93 | + 'receiver' => $data['receiver'], | |
| 94 | + 'cc' => $data['cc'], | |
| 95 | + 'bcc' => $data['bcc'], | |
| 96 | + 'enabled' => (int) $data['enabled'], | |
| 97 | + 'subject' => $data['subject'], | |
| 98 | + 'message' => $data['message'], | |
| 99 | + 'showEmptyFields' => (int) $data['showEmptyFields'], | |
| 100 | + 'smartLogic' => wp_json_encode($data['smartLogic']), | |
| 101 | + ]; | |
| 102 | + $formats = ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s']; | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Extend notification DB persist row (values + formats) for Pro columns. | |
| 106 | + * | |
| 107 | + * @since 1.2.0 | |
| 108 | + * | |
| 109 | + * @param array<string, mixed> $row | |
| 110 | + * @param array<string, mixed> $data | |
| 111 | + */ | |
| 112 | + $filtered = apply_filters( | |
| 113 | + 'ivyforms/notification/repository_row', | |
| 114 | + [ | |
| 115 | + 'values' => $values, | |
| 116 | + 'formats' => $formats, | |
| 117 | + ], | |
| 118 | + $data | |
| 119 | + ); | |
| 120 | + | |
| 121 | + if ( | |
| 122 | + !is_array($filtered) | |
| 123 | + || !isset($filtered['values'], $filtered['formats']) | |
| 124 | + || !is_array($filtered['values']) | |
| 125 | + || !is_array($filtered['formats']) | |
| 126 | + ) { | |
| 127 | + return [ | |
| 128 | + 'values' => $values, | |
| 129 | + 'formats' => $formats, | |
| 130 | + ]; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return [ | |
| 134 | + 'values' => $filtered['values'], | |
| 135 | + 'formats' => $filtered['formats'], | |
| 136 | + ]; | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 98 | 140 | * Get all notifications by form ID. |
| 99 | 141 | * |
| 100 | 142 | * @param int $id |
| 101 | 143 | * |
| 102 | 144 | * @return array<Notification> |
| 145 | + * @throws QueryExecutionException | |
| 103 | 146 | */ |
| 104 | 147 | public function getAllById(int $id): array |
| 105 | 148 | { |
| 106 | 149 | $rows = $this->wpdb->get_results( |
| @@ -109,17 +152,21 @@ | ||
| 109 | 152 | [$id] |
| 110 | 153 | ), |
| 111 | 154 | ARRAY_A |
| 112 | 155 | ); |
| 113 | - | |
| 156 | + if ($rows === false) { | |
| 157 | + throw new QueryExecutionException( | |
| 158 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 159 | + ); | |
| 160 | + } | |
| 114 | 161 | if (empty($rows)) { |
| 115 | 162 | return []; |
| 116 | 163 | } |
| 117 | - | |
| 118 | 164 | return array_map(function ($row) { |
| 119 | 165 | return call_user_func([static::FACTORY, 'create'], $row); |
| 120 | 166 | }, $rows); |
| 121 | 167 | } |
| 168 | + | |
| 122 | 169 | /** |
| 123 | 170 | * Get all active notifications by form ID. |
| 124 | 171 | * |
| 125 | 172 | * @param int $id |
| @@ -124,24 +171,64 @@ | ||
| 124 | 171 | * |
| 125 | 172 | * @param int $id |
| 126 | 173 | * |
| 127 | 174 | * @return array<Notification> |
| 175 | + * @throws QueryExecutionException | |
| 128 | 176 | */ |
| 129 | 177 | public function getAllActiveById(int $id): array |
| 130 | 178 | { |
| 131 | 179 | $rows = $this->wpdb->get_results( |
| 132 | 180 | $this->wpdb->prepare( |
| 133 | - $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.status = 1", | |
| 181 | + $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1", | |
| 134 | 182 | $id |
| 135 | 183 | ), |
| 136 | 184 | ARRAY_A |
| 137 | 185 | ); |
| 138 | - | |
| 186 | + if ($rows === false) { | |
| 187 | + throw new QueryExecutionException( | |
| 188 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 189 | + ); | |
| 190 | + } | |
| 139 | 191 | if (empty($rows)) { |
| 140 | 192 | return []; |
| 141 | 193 | } |
| 142 | - | |
| 143 | 194 | return array_map(function ($row) { |
| 144 | 195 | return call_user_func([static::FACTORY, 'create'], $row); |
| 145 | 196 | }, $rows); |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Define searchable columns for notifications | |
| 201 | + * | |
| 202 | + * @return string[] | |
| 203 | + */ | |
| 204 | + protected function getSearchableColumns(): array | |
| 205 | + { | |
| 206 | + return ['name', 'receiver', 'subject']; | |
| 207 | + } | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * Define filterable columns for notifications | |
| 211 | + * | |
| 212 | + * @return string[] | |
| 213 | + */ | |
| 214 | + protected function getFilterableColumns(): array | |
| 215 | + { | |
| 216 | + return ['formId']; | |
| 217 | + } | |
| 218 | + | |
| 219 | + /** | |
| 220 | + * Define sortable columns for notifications | |
| 221 | + * | |
| 222 | + * @return string[] | |
| 223 | + */ | |
| 224 | + protected function getSortableColumns(): array | |
| 225 | + { | |
| 226 | + return [ | |
| 227 | + 'id', | |
| 228 | + 'name', | |
| 229 | + 'receiver', | |
| 230 | + 'subject', | |
| 231 | + 'enabled' | |
| 232 | + ]; | |
| 146 | 233 | } |
| 147 | 234 | } |