← All changes
|
backend/src/Repository/Field/FieldRepository.php
+178
-112
0.6.1-backup
→
1.5
View file →
| @@ -6,13 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Repository\Field; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 16 | 11 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 17 | 12 | use IvyForms\Entity\Field\Field; |
| 18 | 13 | use IvyForms\Factory\Field\FieldFactory; |
| @@ -52,64 +47,17 @@ | ||
| 52 | 47 | 'required' => (int)$data['required'], |
| 53 | 48 | 'defaultValue' => $data['defaultValue'], |
| 54 | 49 | 'placeholder' => $data['placeholder'], |
| 55 | 50 | 'position' => $data['position'], |
| 51 | + 'rowIndex' => $data['rowIndex'] ?? 0, | |
| 52 | + 'columnIndex' => $data['columnIndex'] ?? 0, | |
| 53 | + 'width' => $data['width'] ?? 100, | |
| 56 | 54 | 'parentId' => $data['parentId'] ?? null, |
| 57 | - 'settings' => json_encode( | |
| 58 | - /** | |
| 59 | - * Allows modification of the settings data before saving to database. | |
| 60 | - * @since 0.1.0 | |
| 61 | - * | |
| 62 | - * @param mixed[] $settings The current settings array. | |
| 63 | - * @param mixed[] $data The full field data array. | |
| 64 | - * @param Field $entity The Field entity instance. | |
| 65 | - * @return mixed[] The modified settings array. | |
| 66 | - */ | |
| 67 | - apply_filters( | |
| 68 | - 'ivyforms/repository/field/settings', | |
| 69 | - [ | |
| 70 | - 'hideLabel' => (int)$data['hideLabel'], | |
| 71 | - 'readOnly' => (int)$data['readOnly'], | |
| 72 | - 'description' => $data['description'], | |
| 73 | - 'requiredMessage' => $data['requiredMessage'], | |
| 74 | - 'cssClasses' => $data['cssClasses'], | |
| 75 | - 'limitMaxLength' => (int)$data['limitMaxLength'], | |
| 76 | - 'maxLength' => $data['maxLength'], | |
| 77 | - 'labelPosition' => $data['labelPosition'], | |
| 78 | - 'noDuplicates' => (int)$data['noDuplicates'], | |
| 79 | - 'shuffleOptions' => (int)$data['shuffleOptions'], | |
| 80 | - 'showValues' => (int)$data['showValues'], | |
| 81 | - 'enableSearch' => (int)$data['enableSearch'], | |
| 82 | - 'rows' => (int)$data['rows'], | |
| 83 | - 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0), | |
| 84 | - 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '', | |
| 85 | - 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '', | |
| 86 | - 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0), | |
| 87 | - 'phoneFormat' => $data['phoneFormat'], | |
| 88 | - 'phoneAutoDetect' => $data['phoneAutoDetect'], | |
| 89 | - 'minValue' => isset($data['minValue']) | |
| 90 | - ? (float)$data['minValue'] : null, | |
| 91 | - 'maxValue' => isset($data['maxValue']) | |
| 92 | - ? (float)$data['maxValue'] : null, | |
| 93 | - 'step' => (float)$data['step'], | |
| 94 | - 'numberFormat' => $data['numberFormat'] ?? '', | |
| 95 | - 'visible' => (int)$data['visible'], | |
| 96 | - 'inputPrefix' => $data['inputPrefix'] ?? '', | |
| 97 | - 'inputSuffix' => $data['inputSuffix'] ?? '', | |
| 98 | - 'timeFieldType' => $data['timeFieldType'] ?? '', | |
| 99 | - 'timeFormat' => $data['timeFormat'] ?? '', | |
| 100 | - 'dateFieldType' => $data['dateFieldType'] ?? '', | |
| 101 | - 'dateFormat' => $data['dateFormat'] ?? '', | |
| 102 | - 'minDateValue' => $data['minDateValue'] ?? '', | |
| 103 | - 'maxDateValue' => $data['maxDateValue'] ?? '', | |
| 104 | - ], | |
| 105 | - $data, | |
| 106 | - $entity | |
| 107 | - ) | |
| 108 | - ), | |
| 55 | + 'pageId' => $data['pageId'] ?? null, | |
| 56 | + 'settings' => $this->encodeFieldSettings($data, $entity, false), | |
| 109 | 57 | 'dateCreated' => current_time('mysql') |
| 110 | 58 | ], |
| 111 | - ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%s', '%s'] | |
| 59 | + ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s'] | |
| 112 | 60 | ); |
| 113 | 61 | |
| 114 | 62 | if ($result === false) { |
| 115 | 63 | throw new QueryExecutionException( |
| @@ -131,8 +79,11 @@ | ||
| 131 | 79 | * @throws QueryExecutionException |
| 132 | 80 | */ |
| 133 | 81 | public function update(int $id, $entity): bool |
| 134 | 82 | { |
| 83 | + if ($id < 1) { | |
| 84 | + throw new InvalidArgumentException('Field id must be a positive integer.'); | |
| 85 | + } | |
| 135 | 86 | |
| 136 | 87 | $data = $entity->toArray(); |
| 137 | 88 | |
| 138 | 89 | $result = $this->wpdb->update( |
| @@ -145,64 +96,17 @@ | ||
| 145 | 96 | 'required' => (int)$data['required'], |
| 146 | 97 | 'defaultValue' => $data['defaultValue'], |
| 147 | 98 | 'placeholder' => $data['placeholder'], |
| 148 | 99 | 'position' => $data['position'], |
| 100 | + 'rowIndex' => $data['rowIndex'] ?? 0, | |
| 101 | + 'columnIndex' => $data['columnIndex'] ?? 0, | |
| 102 | + 'width' => $data['width'] ?? 100, | |
| 149 | 103 | 'parentId' => $data['parentId'] ?? null, |
| 150 | - 'settings' => json_encode( | |
| 151 | - /** | |
| 152 | - * Allows modification of the settings data before updating database. | |
| 153 | - * @since 0.1.0 | |
| 154 | - * | |
| 155 | - * @param mixed[] $settings The current settings array. | |
| 156 | - * @param mixed[] $data The full field data array. | |
| 157 | - * @param Field $entity The Field entity instance. | |
| 158 | - * @return mixed[] The modified settings array. | |
| 159 | - */ | |
| 160 | - apply_filters( | |
| 161 | - 'ivyforms/repository/field/settings', | |
| 162 | - [ | |
| 163 | - 'hideLabel' => (int)$data['hideLabel'], | |
| 164 | - 'readOnly' => (int)$data['readOnly'], | |
| 165 | - 'description' => $data['description'], | |
| 166 | - 'requiredMessage' => $data['requiredMessage'], | |
| 167 | - 'cssClasses' => $data['cssClasses'], | |
| 168 | - 'limitMaxLength' => (int)$data['limitMaxLength'], | |
| 169 | - 'maxLength' => $data['maxLength'], | |
| 170 | - 'labelPosition' => $data['labelPosition'], | |
| 171 | - 'noDuplicates' => (int)$data['noDuplicates'], | |
| 172 | - 'shuffleOptions' => (int)$data['shuffleOptions'], | |
| 173 | - 'showValues' => (int)$data['showValues'], | |
| 174 | - 'enableSearch' => (int)$data['enableSearch'], | |
| 175 | - 'rows' => (int)$data['rows'], | |
| 176 | - 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0), | |
| 177 | - 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '', | |
| 178 | - 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '', | |
| 179 | - 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0), | |
| 180 | - 'phoneFormat' => $data['phoneFormat'], | |
| 181 | - 'phoneAutoDetect' => $data['phoneAutoDetect'], | |
| 182 | - 'minValue' => isset($data['minValue']) | |
| 183 | - ? (float)$data['minValue'] : null, | |
| 184 | - 'maxValue' => isset($data['maxValue']) | |
| 185 | - ? (float)$data['maxValue'] : null, | |
| 186 | - 'step' => (float)$data['step'], | |
| 187 | - 'numberFormat' => $data['numberFormat'] ?? '', | |
| 188 | - 'visible' => array_key_exists('visible', $data) ? (int)$data['visible'] : 1, | |
| 189 | - 'inputPrefix' => $data['inputPrefix'] ?? '', | |
| 190 | - 'inputSuffix' => $data['inputSuffix'] ?? '', | |
| 191 | - 'timeFieldType' => $data['timeFieldType'] ?? '', | |
| 192 | - 'timeFormat' => $data['timeFormat'] ?? '', | |
| 193 | - 'dateFieldType' => $data['dateFieldType'] ?? '', | |
| 194 | - 'dateFormat' => $data['dateFormat'] ?? '', | |
| 195 | - 'minDateValue' => $data['minDateValue'] ?? '', | |
| 196 | - 'maxDateValue' => $data['maxDateValue'] ?? '', | |
| 197 | - ], | |
| 198 | - $data, | |
| 199 | - $entity | |
| 200 | - ) | |
| 201 | - ), | |
| 104 | + 'pageId' => $data['pageId'] ?? null, | |
| 105 | + 'settings' => $this->encodeFieldSettings($data, $entity, true), | |
| 202 | 106 | ], |
| 203 | - ['id' => $data['id']], | |
| 204 | - ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%s'], | |
| 107 | + ['id' => $id], | |
| 108 | + ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s'], | |
| 205 | 109 | ['%d'] |
| 206 | 110 | ); |
| 207 | 111 | |
| 208 | 112 | if ($result === false) { |
| @@ -209,8 +113,170 @@ | ||
| 209 | 113 | throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); |
| 210 | 114 | } |
| 211 | 115 | |
| 212 | 116 | return $result; |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * @param mixed[] $data | |
| 121 | + */ | |
| 122 | + private function encodeFieldSettings(array $data, Field $entity, bool $forUpdate): string | |
| 123 | + { | |
| 124 | + $settings = $this->buildFieldSettingsPayload($data, $forUpdate); | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Allows modification of the settings data before saving to database. | |
| 128 | + * | |
| 129 | + * @param mixed[] $settings The current settings array. | |
| 130 | + * @param mixed[] $data The full field data array. | |
| 131 | + * @param Field $entity The Field entity instance. | |
| 132 | + * @return mixed[] The modified settings array. | |
| 133 | + */ | |
| 134 | + $settings = apply_filters('ivyforms/repository/field/settings', $settings, $data, $entity); | |
| 135 | + | |
| 136 | + $encoded = wp_json_encode($settings); | |
| 137 | + if ($encoded === false) { | |
| 138 | + throw new QueryExecutionException( | |
| 139 | + 'Failed to encode field settings: ' . json_last_error_msg() | |
| 140 | + ); | |
| 141 | + } | |
| 142 | + | |
| 143 | + return $encoded; | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * @param mixed[] $data | |
| 148 | + * | |
| 149 | + * @return mixed[] | |
| 150 | + */ | |
| 151 | + private function buildFieldSettingsPayload(array $data, bool $forUpdate): array | |
| 152 | + { | |
| 153 | + return array_merge( | |
| 154 | + $this->buildCoreFieldSettings($data, $forUpdate), | |
| 155 | + $this->buildFileUploadFieldSettings($data) | |
| 156 | + ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * @param mixed[] $data | |
| 161 | + * | |
| 162 | + * @return mixed[] | |
| 163 | + */ | |
| 164 | + private function buildCoreFieldSettings(array $data, bool $forUpdate): array | |
| 165 | + { | |
| 166 | + return array_merge( | |
| 167 | + $this->buildBaseFieldSettings($data), | |
| 168 | + [ | |
| 169 | + 'limitRange' => $this->resolveLimitRangeFlag($data, $forUpdate), | |
| 170 | + 'visible' => $this->resolveVisibleFlag($data, $forUpdate), | |
| 171 | + ], | |
| 172 | + $this->buildExtendedFieldSettings($data) | |
| 173 | + ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * @param mixed[] $data | |
| 178 | + * | |
| 179 | + * @return mixed[] | |
| 180 | + */ | |
| 181 | + private function buildBaseFieldSettings(array $data): array | |
| 182 | + { | |
| 183 | + return [ | |
| 184 | + 'hideLabel' => (int) $data['hideLabel'], | |
| 185 | + 'readOnly' => (int) $data['readOnly'], | |
| 186 | + 'description' => $data['description'], | |
| 187 | + 'requiredMessage' => $data['requiredMessage'], | |
| 188 | + 'cssClasses' => $data['cssClasses'], | |
| 189 | + 'limitMaxLength' => (int) $data['limitMaxLength'], | |
| 190 | + 'maxLength' => $data['maxLength'], | |
| 191 | + 'labelPosition' => $data['labelPosition'], | |
| 192 | + 'noDuplicates' => (int) $data['noDuplicates'], | |
| 193 | + 'shuffleOptions' => (int) $data['shuffleOptions'], | |
| 194 | + 'showValues' => (int) $data['showValues'], | |
| 195 | + 'enableSearch' => (int) $data['enableSearch'], | |
| 196 | + 'rows' => (int) $data['rows'], | |
| 197 | + ]; | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * @param mixed[] $data | |
| 202 | + * | |
| 203 | + * @return mixed[] | |
| 204 | + */ | |
| 205 | + private function buildExtendedFieldSettings(array $data): array | |
| 206 | + { | |
| 207 | + return [ | |
| 208 | + 'confirmFieldEnabled' => (int) ($data['confirmFieldEnabled'] ?? 0), | |
| 209 | + 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '', | |
| 210 | + 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '', | |
| 211 | + 'confirmFieldHideLabel' => (int) ($data['confirmFieldHideLabel'] ?? 0), | |
| 212 | + 'phoneFormat' => $data['phoneFormat'], | |
| 213 | + 'phoneAutoDetect' => $data['phoneAutoDetect'], | |
| 214 | + 'minValue' => isset($data['minValue']) ? (float) $data['minValue'] : null, | |
| 215 | + 'maxValue' => isset($data['maxValue']) ? (float) $data['maxValue'] : null, | |
| 216 | + 'step' => (float) $data['step'], | |
| 217 | + 'showGrid' => (int) ($data['showGrid'] ?? 0), | |
| 218 | + 'numberFormat' => $data['numberFormat'] ?? '', | |
| 219 | + 'inputPrefix' => $data['inputPrefix'] ?? '', | |
| 220 | + 'inputSuffix' => $data['inputSuffix'] ?? '', | |
| 221 | + 'timeFieldType' => $data['timeFieldType'] ?? '', | |
| 222 | + 'timeFormat' => $data['timeFormat'] ?? '', | |
| 223 | + 'dateFieldType' => $data['dateFieldType'] ?? '', | |
| 224 | + 'dateFormat' => $data['dateFormat'] ?? '', | |
| 225 | + 'minDateValue' => $data['minDateValue'] ?? '', | |
| 226 | + 'maxDateValue' => $data['maxDateValue'] ?? '', | |
| 227 | + 'showRatingText' => (int) ($data['showRatingText'] ?? 0), | |
| 228 | + 'ratingIcon' => $data['ratingIcon'] ?? 'star', | |
| 229 | + 'htmlContent' => $data['htmlContent'] ?? '', | |
| 230 | + 'agreementText' => $data['agreementText'] ?? '', | |
| 231 | + 'limitDateRange' => (int) $data['limitDateRange'], | |
| 232 | + 'minTimeValue' => $data['minTimeValue'] ?? '', | |
| 233 | + 'maxTimeValue' => $data['maxTimeValue'] ?? '', | |
| 234 | + 'limitTimeRange' => (int) $data['limitTimeRange'], | |
| 235 | + 'nameFieldType' => $data['nameFieldType'] ?? '', | |
| 236 | + 'subFieldIndex' => isset($data['subFieldIndex']) ? (int) $data['subFieldIndex'] : null, | |
| 237 | + ]; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * @param mixed[] $data | |
| 242 | + */ | |
| 243 | + private function resolveLimitRangeFlag(array $data, bool $forUpdate): int | |
| 244 | + { | |
| 245 | + if (!$forUpdate) { | |
| 246 | + return (int) $data['limitRange']; | |
| 247 | + } | |
| 248 | + | |
| 249 | + return filter_var($data['limitRange'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0; | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * @param mixed[] $data | |
| 254 | + */ | |
| 255 | + private function resolveVisibleFlag(array $data, bool $forUpdate): int | |
| 256 | + { | |
| 257 | + if (!$forUpdate) { | |
| 258 | + return (int) $data['visible']; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return array_key_exists('visible', $data) ? (int) $data['visible'] : 1; | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * @param mixed[] $data | |
| 266 | + * | |
| 267 | + * @return mixed[] | |
| 268 | + */ | |
| 269 | + private function buildFileUploadFieldSettings(array $data): array | |
| 270 | + { | |
| 271 | + return [ | |
| 272 | + 'maxFileSizeMb' => isset($data['maxFileSizeMb']) ? (int) $data['maxFileSizeMb'] : 0, | |
| 273 | + 'saveUploadsTo' => (string) ($data['saveUploadsTo'] ?? 'ivyforms'), | |
| 274 | + 'allowedFileExtensions' => isset($data['allowedFileExtensions']) | |
| 275 | + && is_array($data['allowedFileExtensions']) | |
| 276 | + ? array_values($data['allowedFileExtensions']) | |
| 277 | + : [], | |
| 278 | + ]; | |
| 213 | 279 | } |
| 214 | 280 | |
| 215 | 281 | /** |
| 216 | 282 | * Get fields by form ID |