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

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