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

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