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 / Field / FieldRepository.php

FieldRepository.php in The Innovative Form Builder – IvyForms 0.4, at backend/src/Repository/Field/FieldRepository.php

267 lines 10.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\Field;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 use IvyForms\Common\Exceptions\QueryExecutionException;
17 use IvyForms\Entity\Field\Field;
18 use IvyForms\Factory\Field\FieldFactory;
19 use IvyForms\Repository\AbstractRepository;
20 use IvyForms\Services\Translations\BackendStrings;
21
22 /**
23 * Class FieldRepository
24 *
25 * @package IvyForms\Repository\Field
26 */
27 class FieldRepository extends AbstractRepository implements FieldRepositoryInterface
28 {
29 public const FACTORY = FieldFactory::class;
30
31 /**
32 * Add field to the database
33 *
34 * @param Field $entity
35 *
36 * @return int
37 *
38 * @throws QueryExecutionException
39 */
40 public function add($entity): int
41 {
42
43 $data = $entity->toArray();
44
45 $result = $this->wpdb->insert(
46 $this->table,
47 [
48 'fieldIndex' => $data['fieldIndex'],
49 'formId' => $data['formId'],
50 'type' => $data['type'],
51 'label' => $data['label'],
52 'required' => (int)$data['required'],
53 'defaultValue' => $data['defaultValue'],
54 'placeholder' => $data['placeholder'],
55 'position' => $data['position'],
56 'parentId' => $data['parentId'] ?? null,
57 'settings' => json_encode([
58 'hideLabel' => (int)$data['hideLabel'],
59 'readOnly' => (int)$data['readOnly'],
60 'description' => $data['description'],
61 'requiredMessage' => $data['requiredMessage'],
62 'cssClasses' => $data['cssClasses'],
63 'limitMaxLength' => (int)$data['limitMaxLength'],
64 'maxLength' => $data['maxLength'],
65 'labelPosition' => $data['labelPosition'],
66 'noDuplicates' => (int)$data['noDuplicates'],
67 'shuffleOptions' => (int)$data['shuffleOptions'],
68 'showValues' => (int)$data['showValues'],
69 'enableSearch' => (int)$data['enableSearch'],
70 'rows' => (int)$data['rows'],
71 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0),
72 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '',
73 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '',
74 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0),
75 'phoneFormat' => $data['phoneFormat'],
76 'phoneAutoDetect' => $data['phoneAutoDetect'],
77 'minValue' => (float)$data['minValue'],
78 'maxValue' => (float)$data['maxValue'],
79 'step' => (float)$data['step'],
80 'numberFormat' => $data['numberFormat'] ?? '',
81 'visible' => (int)$data['visible'],
82 ]),
83 'dateCreated' => current_time('mysql')
84 ],
85 ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%s', '%s']
86 );
87
88 if ($result === false) {
89 throw new QueryExecutionException(
90 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
91 );
92 }
93
94 return $this->wpdb->insert_id;
95 }
96
97 /**
98 * Update field in the database
99 *
100 * @param int $id
101 * @param Field $entity
102 *
103 * @return bool
104 *
105 * @throws QueryExecutionException
106 */
107 public function update(int $id, $entity): bool
108 {
109
110 $data = $entity->toArray();
111
112 $result = $this->wpdb->update(
113 $this->table,
114 [
115 'fieldIndex' => $data['fieldIndex'],
116 'formId' => $data['formId'],
117 'type' => $data['type'],
118 'label' => $data['label'],
119 'required' => (int)$data['required'],
120 'defaultValue' => $data['defaultValue'],
121 'placeholder' => $data['placeholder'],
122 'position' => $data['position'],
123 'parentId' => $data['parentId'] ?? null,
124 'settings' => json_encode([
125 'hideLabel' => (int)$data['hideLabel'],
126 'readOnly' => (int)$data['readOnly'],
127 'description' => $data['description'],
128 'requiredMessage' => $data['requiredMessage'],
129 'cssClasses' => $data['cssClasses'],
130 'limitMaxLength' => (int)$data['limitMaxLength'],
131 'maxLength' => $data['maxLength'],
132 'labelPosition' => $data['labelPosition'],
133 'noDuplicates' => (int)$data['noDuplicates'],
134 'shuffleOptions' => (int)$data['shuffleOptions'],
135 'showValues' => (int)$data['showValues'],
136 'enableSearch' => (int)$data['enableSearch'],
137 'rows' => (int)$data['rows'],
138 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0),
139 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '',
140 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '',
141 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0),
142 'phoneFormat' => $data['phoneFormat'],
143 'phoneAutoDetect' => $data['phoneAutoDetect'],
144 'minValue' => (float)$data['minValue'],
145 'maxValue' => (float)$data['maxValue'],
146 'step' => (float)$data['step'],
147 'numberFormat' => $data['numberFormat'] ?? '',
148 'visible' => array_key_exists('visible', $data) ? (int)$data['visible'] : 1,
149 ]),
150 ],
151 ['id' => $data['id']],
152 ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%s'],
153 ['%d']
154 );
155
156 if ($result === false) {
157 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
158 }
159
160 return $result;
161 }
162
163 /**
164 * Get fields by form ID
165 *
166 * @param int $id
167 *
168 * @return mixed[]
169 * @throws QueryExecutionException
170 */
171 public function getFieldsById(int $id): array
172 {
173 $rows = $this->wpdb->get_results(
174 $this->wpdb->prepare(
175 $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC",
176 $id
177 ),
178 ARRAY_A
179 );
180 if ($rows === false) {
181 throw new QueryExecutionException(
182 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
183 );
184 }
185 $result = [];
186 foreach ($rows as $row) {
187 if (array_key_exists('parentId', $row)) {
188 $row['parentId'] = $row['parentId'] !== null ? (int)$row['parentId'] : null;
189 }
190 $result[] = call_user_func([static::FACTORY, 'create'], $row);
191 }
192 return $result;
193 }
194
195 /**
196 * Delete multiple fields by their IDs in bulk, and their field options.
197 *
198 * @param array<int> $ids
199 * @return int Number of deleted fields
200 * @throws QueryExecutionException
201 */
202 public function deleteMany(array $ids): int
203 {
204 if (empty($ids)) {
205 return 0;
206 }
207 $placeholders = implode(',', array_fill(0, count($ids), '%d'));
208 $deleteFieldsQuery = "DELETE FROM {$this->table} WHERE id IN ($placeholders)";
209 $result = $this->wpdb->query($this->wpdb->prepare($deleteFieldsQuery, ...$ids));
210 if ($result === false) {
211 throw new QueryExecutionException(
212 sprintf(
213 BackendStrings::getExceptionStrings()['failed_delete_by_ids'],
214 implode(', ', $ids),
215 $this->table
216 )
217 );
218 }
219 return $result;
220 }
221
222 /**
223 * Delete multiple fields by foreign key values (formId), and their field options.
224 *
225 * @param array<int> $formIds
226 * @param string $foreignColumn
227 * @return int Number of deleted fields
228 * @throws QueryExecutionException
229 */
230 public function deleteManyByForeignKeyValues(array $formIds, string $foreignColumn = 'formId'): int
231 {
232 if (empty($formIds)) {
233 return 0;
234 }
235 $formIds = array_map('intval', $formIds);
236 $placeholders = implode(',', array_fill(0, count($formIds), '%d'));
237 $deleteFieldsQuery = "DELETE FROM {$this->table} WHERE {$foreignColumn} IN ($placeholders)";
238 $result = $this->wpdb->query($this->wpdb->prepare($deleteFieldsQuery, ...$formIds));
239 if ($result === false) {
240 throw new QueryExecutionException(
241 BackendStrings::getExceptionStrings()['failed_delete_by_foreign_keys']
242 );
243 }
244 return $result;
245 }
246
247 /**
248 * Delete fields by a single foreign key value (formId), and their field options.
249 *
250 * @param int $formId
251 * @param string $foreignColumn
252 * @return int Number of deleted fields
253 * @throws QueryExecutionException
254 */
255 public function deleteOneByForeignKeyValue(int $formId, string $foreignColumn = 'formId'): int
256 {
257 $deleteFieldsQuery = "DELETE FROM {$this->table} WHERE {$foreignColumn} = %d";
258 $result = $this->wpdb->query($this->wpdb->prepare($deleteFieldsQuery, $formId));
259 if ($result === false) {
260 throw new QueryExecutionException(
261 BackendStrings::getExceptionStrings()['failed_delete_by_foreign_key']
262 );
263 }
264 return $result;
265 }
266 }
267