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

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