PluginProbe
The Innovative Form Builder – IvyForms / 0.9
The Innovative Form Builder – IvyForms v0.9
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.9, at backend/src/Repository/Form/FormRepository.php

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