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

197 lines 5.2 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 /**
102 * Get all notifications by form ID.
103 *
104 * @param int $id
105 *
106 * @return array<Notification>
107 * @throws QueryExecutionException
108 */
109 public function getAllById(int $id): array
110 {
111 $rows = $this->wpdb->get_results(
112 $this->wpdb->prepare(
113 $this->selectQuery() . " WHERE $this->table.formId = %d",
114 [$id]
115 ),
116 ARRAY_A
117 );
118 if ($rows === false) {
119 throw new QueryExecutionException(
120 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
121 );
122 }
123 if (empty($rows)) {
124 return [];
125 }
126 return array_map(function ($row) {
127 return call_user_func([static::FACTORY, 'create'], $row);
128 }, $rows);
129 }
130
131 /**
132 * Get all active notifications by form ID.
133 *
134 * @param int $id
135 *
136 * @return array<Notification>
137 * @throws QueryExecutionException
138 */
139 public function getAllActiveById(int $id): array
140 {
141 $rows = $this->wpdb->get_results(
142 $this->wpdb->prepare(
143 $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1",
144 $id
145 ),
146 ARRAY_A
147 );
148 if ($rows === false) {
149 throw new QueryExecutionException(
150 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
151 );
152 }
153 if (empty($rows)) {
154 return [];
155 }
156 return array_map(function ($row) {
157 return call_user_func([static::FACTORY, 'create'], $row);
158 }, $rows);
159 }
160
161 /**
162 * Define searchable columns for notifications
163 *
164 * @return string[]
165 */
166 protected function getSearchableColumns(): array
167 {
168 return ['name', 'receiver', 'subject'];
169 }
170
171 /**
172 * Define filterable columns for notifications
173 *
174 * @return string[]
175 */
176 protected function getFilterableColumns(): array
177 {
178 return ['formId'];
179 }
180
181 /**
182 * Define sortable columns for notifications
183 *
184 * @return string[]
185 */
186 protected function getSortableColumns(): array
187 {
188 return [
189 'id',
190 'name',
191 'receiver',
192 'subject',
193 'enabled'
194 ];
195 }
196 }
197