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 / Form / FormRepository.php

FormRepository.php in The Innovative Form Builder – IvyForms 0.1.2, at backend/src/Repository/Form/FormRepository.php

215 lines 5.4 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\Form;
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\Form\Form;
17 use IvyForms\Factory\Form\FormFactory;
18 use IvyForms\Repository\AbstractRepository;
19 use IvyForms\Services\InstallActions\DB\Notification\NotificationsTable;
20
21 class FormRepository extends AbstractRepository implements FormRepositoryInterface
22 {
23 public const FACTORY = FormFactory::class;
24
25 /**
26 * Add form to the database
27 *
28 * @param Form $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 'name' => $data['name'],
42 'author' => wp_get_current_user()->display_name,
43 'starred' => (int) $data['starred'],
44 'published' => (int) $data['published'],
45 'dateCreated' => current_time('mysql'),
46 'dateEdited' => current_time('mysql'),
47 'description' => $data['description'],
48 'settings' => json_encode([
49 'showTitle' => (int) $data['showTitle'],
50 'showDescription' => (int) $data['showDescription'],
51 ])
52 ],
53 ['%s', '%s', '%d', '%d', '%s', '%s', '%s']
54 );
55
56 if ($result === false) {
57 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
58 }
59
60 return $this->wpdb->insert_id;
61 }
62
63 /**
64 * Update form in the database
65 *
66 * @param int $id
67 * @param Form $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 'name' => $data['name'],
81 'starred' => (int) $data['starred'],
82 'published' => (int) $data['published'],
83 'description' => $data['description'],
84 'dateEdited' => current_time('mysql'),
85 'settings' => json_encode([
86 'showTitle' => (int)$data['showTitle'],
87 'showDescription' => (int)$data['showDescription'],
88 ])
89 ],
90 ['id' => $id],
91 ['%s', '%d', '%d', '%s', '%s'],
92 ['%d']
93 );
94
95 if ($result === false) {
96 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
97 }
98
99 return $result;
100 }
101
102 /**
103 * Update the form starred value by ID.
104 *
105 * @param int $id
106 * @param string $value
107 *
108 * @return bool
109 * @throws QueryExecutionException
110 */
111 public function updateFormStarred(int $id, string $value): bool
112 {
113 // Update the starred column in the database
114 $result = $this->wpdb->update(
115 $this->table,
116 ['starred' => $value],
117 ['id' => $id],
118 ['%d'],
119 ['%d']
120 );
121
122 if ($result === false) {
123 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
124 }
125
126 return $result;
127 }
128
129 /**
130 * Update the published status of a form by ID.
131 *
132 * @param int $formId
133 * @param string $value
134 *
135 * @return bool
136 * @throws QueryExecutionException
137 */
138 public function updateFormStatus(int $formId, string $value): bool
139 {
140 // Update the published column in the database
141 $result = $this->wpdb->update(
142 $this->table,
143 ['published' => $value],
144 ['id' => $formId],
145 ['%d'],
146 ['%d']
147 );
148
149 if ($result === false) {
150 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
151 }
152
153 return $result;
154 }
155
156 /**
157 * Check if a form exists by its ID.
158 *
159 * @param int $formId
160 *
161 * @return bool
162 */
163 public function exists(int $formId): bool
164 {
165 $query = $this->wpdb->prepare(
166 "SELECT COUNT(*) FROM " . $this->table . " WHERE id = %d",
167 $formId
168 );
169
170 $count = $this->wpdb->get_var($query);
171
172 return (bool) $count;
173 }
174
175 /**
176 * Get searchable columns
177 *
178 * @return array<string>
179 */
180 protected function getSearchableColumns(): array
181 {
182 return ['name', 'author', 'description'];
183 }
184
185 /**
186 * Get filterable columns
187 *
188 * @return array<string>
189 */
190 protected function getFilterableColumns(): array
191 {
192 return ['starred', 'published'];
193 }
194
195 /**
196 * Get sortable columns
197 *
198 * @return array<string>
199 */
200 protected function getSortableColumns(): array
201 {
202 return ['id', 'name', 'dateCreated'];
203 }
204
205 /**
206 * Get the date column
207 *
208 * @return string
209 */
210 protected function getDateColumn(): string
211 {
212 return 'dateCreated';
213 }
214 }
215