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

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