← All changes
|
backend/src/Repository/Notification/NotificationRepository.php
+234
-0
0.6.1
→
1.5.1
View file →
| @@ -1,0 +1,234 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | + * @licence See LICENCE.md for license details. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace IvyForms\Repository\Notification; | |
| 9 | + | |
| 10 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 11 | +use IvyForms\Entity\Notification\Notification; | |
| 12 | +use IvyForms\Factory\Notification\NotificationFactory; | |
| 13 | +use IvyForms\Repository\AbstractRepository; | |
| 14 | +use IvyForms\Repository\Notification\NotificationRepositoryInterface; | |
| 15 | +use IvyForms\Services\Translations\BackendStrings; | |
| 16 | + | |
| 17 | +class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface | |
| 18 | +{ | |
| 19 | + public const FACTORY = NotificationFactory::class; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Add notification to the database | |
| 23 | + * | |
| 24 | + * @param Notification $entity | |
| 25 | + * | |
| 26 | + * @return int | |
| 27 | + * | |
| 28 | + * @throws QueryExecutionException | |
| 29 | + */ | |
| 30 | + public function add($entity): int | |
| 31 | + { | |
| 32 | + $data = $entity->toArray(); | |
| 33 | + $row = $this->buildPersistRow($data); | |
| 34 | + | |
| 35 | + $result = $this->wpdb->insert( | |
| 36 | + $this->table, | |
| 37 | + $row['values'], | |
| 38 | + $row['formats'] | |
| 39 | + ); | |
| 40 | + | |
| 41 | + if ($result === false) { | |
| 42 | + throw new QueryExecutionException( | |
| 43 | + BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ | |
| 44 | + ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + return $this->wpdb->insert_id; | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Update form in the database | |
| 52 | + * | |
| 53 | + * @param int $id | |
| 54 | + * @param Notification $entity | |
| 55 | + * | |
| 56 | + * @return bool | |
| 57 | + * | |
| 58 | + * @throws QueryExecutionException | |
| 59 | + */ | |
| 60 | + public function update(int $id, $entity): bool | |
| 61 | + { | |
| 62 | + $data = $entity->toArray(); | |
| 63 | + $row = $this->buildPersistRow($data); | |
| 64 | + | |
| 65 | + $result = $this->wpdb->update( | |
| 66 | + $this->table, | |
| 67 | + $row['values'], | |
| 68 | + ['id' => $id], | |
| 69 | + $row['formats'], | |
| 70 | + ['%d'] | |
| 71 | + ); | |
| 72 | + | |
| 73 | + if ($result === false) { | |
| 74 | + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); | |
| 75 | + } | |
| 76 | + | |
| 77 | + return true; | |
| 78 | + } | |
| 79 | + | |
| 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 | + /** | |
| 140 | + * Get all notifications by form ID. | |
| 141 | + * | |
| 142 | + * @param int $id | |
| 143 | + * | |
| 144 | + * @return array<Notification> | |
| 145 | + * @throws QueryExecutionException | |
| 146 | + */ | |
| 147 | + public function getAllById(int $id): array | |
| 148 | + { | |
| 149 | + $rows = $this->wpdb->get_results( | |
| 150 | + $this->wpdb->prepare( | |
| 151 | + $this->selectQuery() . " WHERE $this->table.formId = %d", | |
| 152 | + [$id] | |
| 153 | + ), | |
| 154 | + ARRAY_A | |
| 155 | + ); | |
| 156 | + if ($rows === false) { | |
| 157 | + throw new QueryExecutionException( | |
| 158 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 159 | + ); | |
| 160 | + } | |
| 161 | + if (empty($rows)) { | |
| 162 | + return []; | |
| 163 | + } | |
| 164 | + return array_map(function ($row) { | |
| 165 | + return call_user_func([static::FACTORY, 'create'], $row); | |
| 166 | + }, $rows); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Get all active notifications by form ID. | |
| 171 | + * | |
| 172 | + * @param int $id | |
| 173 | + * | |
| 174 | + * @return array<Notification> | |
| 175 | + * @throws QueryExecutionException | |
| 176 | + */ | |
| 177 | + public function getAllActiveById(int $id): array | |
| 178 | + { | |
| 179 | + $rows = $this->wpdb->get_results( | |
| 180 | + $this->wpdb->prepare( | |
| 181 | + $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1", | |
| 182 | + $id | |
| 183 | + ), | |
| 184 | + ARRAY_A | |
| 185 | + ); | |
| 186 | + if ($rows === false) { | |
| 187 | + throw new QueryExecutionException( | |
| 188 | + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ | |
| 189 | + ); | |
| 190 | + } | |
| 191 | + if (empty($rows)) { | |
| 192 | + return []; | |
| 193 | + } | |
| 194 | + return array_map(function ($row) { | |
| 195 | + return call_user_func([static::FACTORY, 'create'], $row); | |
| 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 | + ]; | |
| 233 | + } | |
| 234 | +} | |