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

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