PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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.0, at backend/src/Repository/Notification/NotificationRepository.php

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