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

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