← All changes
|
backend/src/Repository/Notification/NotificationRepository.php
+81
-33
0.3
→
1.5.1
View file →
| @@ -1,18 +1,13 @@ | ||
| 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; |
| @@ -34,22 +29,14 @@ | ||
| 34 | 29 | */ |
| 35 | 30 | public function add($entity): int |
| 36 | 31 | { |
| 37 | 32 | $data = $entity->toArray(); |
| 33 | + $row = $this->buildPersistRow($data); | |
| 38 | 34 | |
| 39 | 35 | $result = $this->wpdb->insert( |
| 40 | 36 | $this->table, |
| 41 | - [ | |
| 42 | - 'formId' => (int) $data['formId'], | |
| 43 | - 'name' => $data['name'], | |
| 44 | - 'sender' => $data['sender'], | |
| 45 | - 'receiver' => $data['receiver'], | |
| 46 | - 'enabled' => (int) $data['enabled'], | |
| 47 | - 'subject' => $data['subject'], | |
| 48 | - 'message' => $data['message'], | |
| 49 | - 'smartLogic' => (int) $data['smartLogic'], | |
| 50 | - ], | |
| 51 | - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'] | |
| 37 | + $row['values'], | |
| 38 | + $row['formats'] | |
| 52 | 39 | ); |
| 53 | 40 | |
| 54 | 41 | if ($result === false) { |
| 55 | 42 | throw new QueryExecutionException( |
| @@ -72,23 +59,15 @@ | ||
| 72 | 59 | */ |
| 73 | 60 | public function update(int $id, $entity): bool |
| 74 | 61 | { |
| 75 | 62 | $data = $entity->toArray(); |
| 63 | + $row = $this->buildPersistRow($data); | |
| 76 | 64 | |
| 77 | 65 | $result = $this->wpdb->update( |
| 78 | 66 | $this->table, |
| 79 | - [ | |
| 80 | - 'formId' => (int) $data['formId'], | |
| 81 | - 'name' => $data['name'], | |
| 82 | - 'sender' => $data['sender'], | |
| 83 | - 'receiver' => $data['receiver'], | |
| 84 | - 'enabled' => (int) $data['enabled'], | |
| 85 | - 'subject' => $data['subject'], | |
| 86 | - 'message' => $data['message'], | |
| 87 | - 'smartLogic' => (int) $data['smartLogic'], | |
| 88 | - ], | |
| 67 | + $row['values'], | |
| 89 | 68 | ['id' => $id], |
| 90 | - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'], | |
| 69 | + $row['formats'], | |
| 91 | 70 | ['%d'] |
| 92 | 71 | ); |
| 93 | 72 | |
| 94 | 73 | if ($result === false) { |
| @@ -94,16 +73,77 @@ | ||
| 94 | 73 | if ($result === false) { |
| 95 | 74 | throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); |
| 96 | 75 | } |
| 97 | 76 | |
| 98 | - return $result; | |
| 77 | + return true; | |
| 99 | 78 | } |
| 79 | + | |
| 100 | 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 | + /** | |
| 101 | 140 | * Get all notifications by form ID. |
| 102 | 141 | * |
| 103 | 142 | * @param int $id |
| 104 | 143 | * |
| 105 | 144 | * @return array<Notification> |
| 145 | + * @throws QueryExecutionException | |
| 106 | 146 | */ |
| 107 | 147 | public function getAllById(int $id): array |
| 108 | 148 | { |
| 109 | 149 | $rows = $this->wpdb->get_results( |
| @@ -112,17 +152,21 @@ | ||
| 112 | 152 | [$id] |
| 113 | 153 | ), |
| 114 | 154 | ARRAY_A |
| 115 | 155 | ); |
| 116 | - | |
| 156 | + if ($rows === false) { | |
| 157 | + throw new QueryExecutionException( | |
| 158 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 159 | + ); | |
| 160 | + } | |
| 117 | 161 | if (empty($rows)) { |
| 118 | 162 | return []; |
| 119 | 163 | } |
| 120 | - | |
| 121 | 164 | return array_map(function ($row) { |
| 122 | 165 | return call_user_func([static::FACTORY, 'create'], $row); |
| 123 | 166 | }, $rows); |
| 124 | 167 | } |
| 168 | + | |
| 125 | 169 | /** |
| 126 | 170 | * Get all active notifications by form ID. |
| 127 | 171 | * |
| 128 | 172 | * @param int $id |
| @@ -127,8 +171,9 @@ | ||
| 127 | 171 | * |
| 128 | 172 | * @param int $id |
| 129 | 173 | * |
| 130 | 174 | * @return array<Notification> |
| 175 | + * @throws QueryExecutionException | |
| 131 | 176 | */ |
| 132 | 177 | public function getAllActiveById(int $id): array |
| 133 | 178 | { |
| 134 | 179 | $rows = $this->wpdb->get_results( |
| @@ -137,13 +182,16 @@ | ||
| 137 | 182 | $id |
| 138 | 183 | ), |
| 139 | 184 | ARRAY_A |
| 140 | 185 | ); |
| 141 | - | |
| 186 | + if ($rows === false) { | |
| 187 | + throw new QueryExecutionException( | |
| 188 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 189 | + ); | |
| 190 | + } | |
| 142 | 191 | if (empty($rows)) { |
| 143 | 192 | return []; |
| 144 | 193 | } |
| 145 | - | |
| 146 | 194 | return array_map(function ($row) { |
| 147 | 195 | return call_user_func([static::FACTORY, 'create'], $row); |
| 148 | 196 | }, $rows); |
| 149 | 197 | } |