PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 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 All 27 releases
ivyforms / backend / src / Repository / Notification / NotificationRepository.php

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

200 lines 5.7 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 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
34 $result = $this->wpdb->insert(
35 $this->table,
36 [
37 'formId' => (int) $data['formId'],
38 'name' => $data['name'],
39 'sender' => $data['sender'],
40 'replyTo' => $data['replyTo'],
41 'receiver' => $data['receiver'],
42 'cc' => $data['cc'],
43 'bcc' => $data['bcc'],
44 'enabled' => (int) $data['enabled'],
45 'subject' => $data['subject'],
46 'message' => $data['message'],
47 'showEmptyFields' => (int) $data['showEmptyFields'],
48 'smartLogic' => wp_json_encode($data['smartLogic']),
49 ],
50 ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s']
51 );
52
53 if ($result === false) {
54 throw new QueryExecutionException(
55 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
56 );
57 }
58
59 return $this->wpdb->insert_id;
60 }
61
62 /**
63 * Update form in the database
64 *
65 * @param int $id
66 * @param Notification $entity
67 *
68 * @return bool
69 *
70 * @throws QueryExecutionException
71 */
72 public function update(int $id, $entity): bool
73 {
74 $data = $entity->toArray();
75
76 $result = $this->wpdb->update(
77 $this->table,
78 [
79 'formId' => (int) $data['formId'],
80 'name' => $data['name'],
81 'sender' => $data['sender'],
82 'replyTo' => $data['replyTo'],
83 'receiver' => $data['receiver'],
84 'cc' => $data['cc'],
85 'bcc' => $data['bcc'],
86 'enabled' => (int) $data['enabled'],
87 'subject' => $data['subject'],
88 'message' => $data['message'],
89 'showEmptyFields' => (int) $data['showEmptyFields'],
90 'smartLogic' => wp_json_encode($data['smartLogic']),
91 ],
92 ['id' => $id],
93 ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s'],
94 ['%d']
95 );
96
97 if ($result === false) {
98 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
99 }
100
101 return true;
102 }
103
104 /**
105 * Get all notifications by form ID.
106 *
107 * @param int $id
108 *
109 * @return array<Notification>
110 * @throws QueryExecutionException
111 */
112 public function getAllById(int $id): array
113 {
114 $rows = $this->wpdb->get_results(
115 $this->wpdb->prepare(
116 $this->selectQuery() . " WHERE $this->table.formId = %d",
117 [$id]
118 ),
119 ARRAY_A
120 );
121 if ($rows === false) {
122 throw new QueryExecutionException(
123 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
124 );
125 }
126 if (empty($rows)) {
127 return [];
128 }
129 return array_map(function ($row) {
130 return call_user_func([static::FACTORY, 'create'], $row);
131 }, $rows);
132 }
133
134 /**
135 * Get all active notifications by form ID.
136 *
137 * @param int $id
138 *
139 * @return array<Notification>
140 * @throws QueryExecutionException
141 */
142 public function getAllActiveById(int $id): array
143 {
144 $rows = $this->wpdb->get_results(
145 $this->wpdb->prepare(
146 $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1",
147 $id
148 ),
149 ARRAY_A
150 );
151 if ($rows === false) {
152 throw new QueryExecutionException(
153 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
154 );
155 }
156 if (empty($rows)) {
157 return [];
158 }
159 return array_map(function ($row) {
160 return call_user_func([static::FACTORY, 'create'], $row);
161 }, $rows);
162 }
163
164 /**
165 * Define searchable columns for notifications
166 *
167 * @return string[]
168 */
169 protected function getSearchableColumns(): array
170 {
171 return ['name', 'receiver', 'subject'];
172 }
173
174 /**
175 * Define filterable columns for notifications
176 *
177 * @return string[]
178 */
179 protected function getFilterableColumns(): array
180 {
181 return ['formId'];
182 }
183
184 /**
185 * Define sortable columns for notifications
186 *
187 * @return string[]
188 */
189 protected function getSortableColumns(): array
190 {
191 return [
192 'id',
193 'name',
194 'receiver',
195 'subject',
196 'enabled'
197 ];
198 }
199 }
200