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

148 lines 3.9 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
21 class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface
22 {
23 public const FACTORY = NotificationFactory::class;
24
25 /**
26 * Add notification to the database
27 *
28 * @param Notification $entity
29 *
30 * @return int
31 *
32 * @throws QueryExecutionException
33 */
34 public function add($entity): int
35 {
36 $data = $entity->toArray();
37
38 $result = $this->wpdb->insert(
39 $this->table,
40 [
41 'formId' => (int) $data['formId'],
42 'name' => $data['name'],
43 'sender' => $data['sender'],
44 'receiver' => $data['receiver'],
45 'status' => (int) $data['status'],
46 'subject' => $data['subject'],
47 'message' => $data['message'],
48 'smartLogic' => (int) $data['smartLogic'],
49 ],
50 ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d']
51 );
52
53 if ($result === false) {
54 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
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 'receiver' => $data['receiver'],
81 'status' => (int) $data['status'],
82 'subject' => $data['subject'],
83 'message' => $data['message'],
84 'smartLogic' => (int) $data['smartLogic'],
85 ],
86 ['id' => $id],
87 ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'],
88 ['%d']
89 );
90
91 if ($result === false) {
92 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
93 }
94
95 return $result;
96 }
97 /**
98 * Get all notifications by form ID.
99 *
100 * @param int $id
101 *
102 * @return array<Notification>
103 */
104 public function getAllById(int $id): array
105 {
106 $rows = $this->wpdb->get_results(
107 $this->wpdb->prepare(
108 $this->selectQuery() . " WHERE $this->table.formId = %d",
109 [$id]
110 ),
111 ARRAY_A
112 );
113
114 if (empty($rows)) {
115 return [];
116 }
117
118 return array_map(function ($row) {
119 return call_user_func([static::FACTORY, 'create'], $row);
120 }, $rows);
121 }
122 /**
123 * Get all active notifications by form ID.
124 *
125 * @param int $id
126 *
127 * @return array<Notification>
128 */
129 public function getAllActiveById(int $id): array
130 {
131 $rows = $this->wpdb->get_results(
132 $this->wpdb->prepare(
133 $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.status = 1",
134 $id
135 ),
136 ARRAY_A
137 );
138
139 if (empty($rows)) {
140 return [];
141 }
142
143 return array_map(function ($row) {
144 return call_user_func([static::FACTORY, 'create'], $row);
145 }, $rows);
146 }
147 }
148