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

257 lines 7.0 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 * @throws QueryExecutionException
166 */
167 public function getFilterCount(): array
168 {
169 $result = $this->wpdb->get_row(
170 $this->wpdb->prepare(
171 "SELECT
172 SUM(published = 1) as publishedTrueCount,
173 SUM(published = 0) as publishedFalseCount,
174 SUM(starred = 1) as starredTrueCount,
175 SUM(starred = 0) as starredFalseCount
176 FROM {$this->table}"
177 ),
178 ARRAY_A
179 );
180 if ($result === false) {
181 throw new QueryExecutionException(
182 BackendStrings::getExceptionStrings()['unable_search_entries'] . __CLASS__
183 );
184 }
185 // Convert all values to integers and provide defaults if query returns null
186 return array_map('intval', $result ?: [
187 'publishedTrueCount' => 0,
188 'publishedFalseCount' => 0,
189 'starredTrueCount' => 0,
190 'starredFalseCount' => 0,
191 ]);
192 }
193
194 /**
195 * Check if a form exists by its ID.
196 *
197 * @param int $formId
198 *
199 * @return bool
200 * @throws QueryExecutionException
201 */
202 public function exists(int $formId): bool
203 {
204 $query = $this->wpdb->prepare(
205 "SELECT COUNT(*) FROM " . $this->table . " WHERE id = %d",
206 $formId
207 );
208 $count = $this->wpdb->get_var($query);
209 if ($count === false) {
210 throw new QueryExecutionException(
211 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
212 );
213 }
214 return (int)$count > 0;
215 }
216
217 /**
218 * Get searchable columns
219 *
220 * @return array<string>
221 */
222 protected function getSearchableColumns(): array
223 {
224 return ['name', 'author', 'description'];
225 }
226
227 /**
228 * Get filterable columns
229 *
230 * @return array<string>
231 */
232 protected function getFilterableColumns(): array
233 {
234 return ['starred', 'published'];
235 }
236
237 /**
238 * Get sortable columns
239 *
240 * @return array<string>
241 */
242 protected function getSortableColumns(): array
243 {
244 return ['id', 'name', 'dateCreated'];
245 }
246
247 /**
248 * Get the date column
249 *
250 * @return string
251 */
252 protected function getDateColumn(): string
253 {
254 return 'dateCreated';
255 }
256 }
257