PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Repository / Notification / NotificationRepository.php

NotificationRepository.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Repository/Notification/NotificationRepository.php

199 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\QueryExecutionException;
16 use IvyForms\Entity\Notification\Notification;
17 use IvyForms\Factory\Notification\NotificationFactory;
18 use IvyForms\Repository\AbstractRepository;
19 use IvyForms\Repository\Notification\NotificationRepositoryInterface;
20 use IvyForms\Services\Translations\BackendStrings;
21
22 class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface
23 {
24 public const FACTORY = NotificationFactory::class;
25
26 /**
27 * Add notification to the database
28 *
29 * @param Notification $entity
30 *
31 * @return int
32 *
33 * @throws QueryExecutionException
34 */
35 public function add($entity): int
36 {
37 $data = $entity->toArray();
38
39 $result = $this->wpdb->insert(
40 $this->table,
41 [
42 'formId' => (int) $data['formId'],
43 'name' => $data['name'],
44 'sender' => $data['sender'],
45 'replyTo' => $data['replyTo'],
46 'receiver' => $data['receiver'],
47 'enabled' => (int) $data['enabled'],
48 'subject' => $data['subject'],
49 'message' => $data['message'],
50 'smartLogic' => (int) $data['smartLogic'],
51 ],
52 ['%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d']
53 );
54
55 if ($result === false) {
56 throw new QueryExecutionException(
57 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
58 );
59 }
60
61 return $this->wpdb->insert_id;
62 }
63
64 /**
65 * Update form in the database
66 *
67 * @param int $id
68 * @param Notification $entity
69 *
70 * @return bool
71 *
72 * @throws QueryExecutionException
73 */
74 public function update(int $id, $entity): bool
75 {
76 $data = $entity->toArray();
77
78 $result = $this->wpdb->update(
79 $this->table,
80 [
81 'formId' => (int) $data['formId'],
82 'name' => $data['name'],
83 'sender' => $data['sender'],
84 'replyTo' => $data['replyTo'],
85 'receiver' => $data['receiver'],
86 'enabled' => (int) $data['enabled'],
87 'subject' => $data['subject'],
88 'message' => $data['message'],
89 'smartLogic' => (int) $data['smartLogic'],
90 ],
91 ['id' => $id],
92 ['%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d'],
93 ['%d']
94 );
95
96 if ($result === false) {
97 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
98 }
99
100 return $result;
101 }
102
103 /**
104 * Get all notifications by form ID.
105 *
106 * @param int $id
107 *
108 * @return array<Notification>
109 * @throws QueryExecutionException
110 */
111 public function getAllById(int $id): array
112 {
113 $rows = $this->wpdb->get_results(
114 $this->wpdb->prepare(
115 $this->selectQuery() . " WHERE $this->table.formId = %d",
116 [$id]
117 ),
118 ARRAY_A
119 );
120 if ($rows === false) {
121 throw new QueryExecutionException(
122 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
123 );
124 }
125 if (empty($rows)) {
126 return [];
127 }
128 return array_map(function ($row) {
129 return call_user_func([static::FACTORY, 'create'], $row);
130 }, $rows);
131 }
132
133 /**
134 * Get all active notifications by form ID.
135 *
136 * @param int $id
137 *
138 * @return array<Notification>
139 * @throws QueryExecutionException
140 */
141 public function getAllActiveById(int $id): array
142 {
143 $rows = $this->wpdb->get_results(
144 $this->wpdb->prepare(
145 $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1",
146 $id
147 ),
148 ARRAY_A
149 );
150 if ($rows === false) {
151 throw new QueryExecutionException(
152 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
153 );
154 }
155 if (empty($rows)) {
156 return [];
157 }
158 return array_map(function ($row) {
159 return call_user_func([static::FACTORY, 'create'], $row);
160 }, $rows);
161 }
162
163 /**
164 * Define searchable columns for notifications
165 *
166 * @return string[]
167 */
168 protected function getSearchableColumns(): array
169 {
170 return ['name', 'receiver', 'subject'];
171 }
172
173 /**
174 * Define filterable columns for notifications
175 *
176 * @return string[]
177 */
178 protected function getFilterableColumns(): array
179 {
180 return ['formId'];
181 }
182
183 /**
184 * Define sortable columns for notifications
185 *
186 * @return string[]
187 */
188 protected function getSortableColumns(): array
189 {
190 return [
191 'id',
192 'name',
193 'receiver',
194 'subject',
195 'enabled'
196 ];
197 }
198 }
199