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

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