PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Repository / Form / FormRepository.php

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

272 lines 7.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 © Melograno Venture Studio. 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 use IvyForms\Services\Translations\BackendStrings;
21 use IvyForms\ValueObjects\Form\IntegrationSettings;
22
23 class FormRepository extends AbstractRepository implements FormRepositoryInterface
24 {
25 public const FACTORY = FormFactory::class;
26
27 /**
28 * Add form to the database
29 *
30 * @param Form $entity
31 *
32 * @return int
33 *
34 * @throws QueryExecutionException
35 */
36 public function add($entity): int
37 {
38 $data = $entity->toArray();
39
40 $result = $this->wpdb->insert(
41 $this->table,
42 [
43 'name' => $data['name'],
44 'author' => wp_get_current_user()->display_name,
45 'starred' => (int) $data['starred'],
46 'published' => (int) $data['published'],
47 'dateCreated' => current_time('mysql'),
48 'dateEdited' => current_time('mysql'),
49 'description' => $data['description'],
50 'settings' => json_encode([
51 'showTitle' => (int) $data['showTitle'],
52 'showDescription' => (int) $data['showDescription'],
53 'storeEntries' => (int) $data['storeEntries'],
54 'formActionButtons' => $data['formActionButtons'] ?? [
55 'submitButtonSettings' => [
56 'label' => BackendStrings::getCommonStrings()['submit'],
57 'position' => 'default'
58 ]
59 ],
60 ]),
61 'integrationSettings' => json_encode($data['integrationSettings']),
62 ],
63 ['%s', '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s']
64 );
65
66 if ($result === false) {
67 throw new QueryExecutionException(
68 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
69 );
70 }
71
72 return $this->wpdb->insert_id;
73 }
74
75 /**
76 * Update form in the database
77 *
78 * @param int $id
79 * @param Form $entity
80 *
81 * @return bool
82 *
83 * @throws QueryExecutionException
84 */
85 public function update(int $id, $entity): bool
86 {
87 $data = $entity->toArray();
88
89 $result = $this->wpdb->update(
90 $this->table,
91 [
92 'name' => $data['name'],
93 'starred' => (int) $data['starred'],
94 'published' => (int) $data['published'],
95 'description' => $data['description'],
96 'dateEdited' => current_time('mysql'),
97 'settings' => json_encode([
98 'showTitle' => (int)$data['showTitle'],
99 'showDescription' => (int)$data['showDescription'],
100 'storeEntries' => (int)$data['storeEntries'],
101 'formActionButtons' => $data['formActionButtons'] ?? [
102 'submitButtonSettings' => [
103 'label' => BackendStrings::getCommonStrings()['submit'],
104 'position' => 'default'
105 ]
106 ],
107 ]),
108 'integrationSettings' => json_encode($data['integrationSettings']),
109 ],
110 ['id' => $id],
111 ['%s', '%d', '%d', '%s', '%s', '%s', '%s'],
112 ['%d']
113 );
114
115 if ($result === false) {
116 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
117 }
118
119 return $result;
120 }
121
122 /**
123 * Update the form starred value by ID.
124 *
125 * @param int $id
126 * @param string $value
127 *
128 * @return bool
129 * @throws QueryExecutionException
130 */
131 public function updateFormStarred(int $id, string $value): bool
132 {
133 // Update the starred column in the database
134 $result = $this->wpdb->update(
135 $this->table,
136 ['starred' => $value],
137 ['id' => $id],
138 ['%d'],
139 ['%d']
140 );
141
142 if ($result === false) {
143 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
144 }
145
146 return $result;
147 }
148
149 /**
150 * Update the published status of a form by ID.
151 *
152 * @param int $formId
153 * @param string $value
154 *
155 * @return bool
156 * @throws QueryExecutionException
157 */
158 public function updateFormStatus(int $formId, string $value): bool
159 {
160 // Update the published column in the database
161 $result = $this->wpdb->update(
162 $this->table,
163 ['published' => $value],
164 ['id' => $formId],
165 ['%d'],
166 ['%d']
167 );
168
169 if ($result === false) {
170 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
171 }
172
173 return $result;
174 }
175
176 /**
177 * Get the count of filters for the filter dropdown.
178 *
179 * @return array<string, int>
180 * @throws QueryExecutionException
181 */
182 public function getFilterCount(): array
183 {
184 $result = $this->wpdb->get_row(
185 $this->wpdb->prepare(
186 "SELECT
187 SUM(published = 1) as publishedTrueCount,
188 SUM(published = 0) as publishedFalseCount,
189 SUM(starred = 1) as starredTrueCount,
190 SUM(starred = 0) as starredFalseCount
191 FROM {$this->table}"
192 ),
193 ARRAY_A
194 );
195 if ($result === false) {
196 throw new QueryExecutionException(
197 BackendStrings::getExceptionStrings()['unable_search_entries'] . __CLASS__
198 );
199 }
200 // Convert all values to integers and provide defaults if query returns null
201 return array_map('intval', $result ?: [
202 'publishedTrueCount' => 0,
203 'publishedFalseCount' => 0,
204 'starredTrueCount' => 0,
205 'starredFalseCount' => 0,
206 ]);
207 }
208
209 /**
210 * Check if a form exists by its ID.
211 *
212 * @param int $formId
213 *
214 * @return bool
215 * @throws QueryExecutionException
216 */
217 public function exists(int $formId): bool
218 {
219 $query = $this->wpdb->prepare(
220 "SELECT COUNT(*) FROM " . $this->table . " WHERE id = %d",
221 $formId
222 );
223 $count = $this->wpdb->get_var($query);
224 if ($count === false) {
225 throw new QueryExecutionException(
226 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
227 );
228 }
229 return (int)$count > 0;
230 }
231
232 /**
233 * Get searchable columns
234 *
235 * @return array<string>
236 */
237 protected function getSearchableColumns(): array
238 {
239 return ['name', 'author', 'description'];
240 }
241
242 /**
243 * Get filterable columns
244 *
245 * @return array<string>
246 */
247 protected function getFilterableColumns(): array
248 {
249 return ['starred', 'published'];
250 }
251
252 /**
253 * Get sortable columns
254 *
255 * @return array<string>
256 */
257 protected function getSortableColumns(): array
258 {
259 return ['id', 'name', 'dateCreated'];
260 }
261
262 /**
263 * Get the date column
264 *
265 * @return string
266 */
267 protected function getDateColumn(): string
268 {
269 return 'dateCreated';
270 }
271 }
272