PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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 / Services / Template / TemplateDefaults.php

TemplateDefaults.php in The Innovative Form Builder – IvyForms 1.0, at backend/src/Services/Template/TemplateDefaults.php

137 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Template;
4
5 use IvyForms\Common\Exceptions\InvalidArgumentException;
6 use IvyForms\Services\Translations\BackendStrings;
7
8 /**
9 * Class TemplateDefaults
10 * Holds default configuration values for form templates
11 *
12 * @package IvyForms\Services\Template
13 */
14 class TemplateDefaults
15 {
16 /**
17 * Cached default form styles loaded from JSON.
18 *
19 * @var array<string, mixed>|null
20 */
21 private static ?array $cachedFormStyles = null;
22
23 /**
24 * Default integration settings for new forms
25 *
26 * @var array<string, array<string, mixed>>
27 */
28 public const DEFAULT_INTEGRATION_SETTINGS = [
29 'wpdatatables' => [
30 'enabled' => true
31 ]
32 ];
33
34 /**
35 * Get default integration settings
36 *
37 * @return array<string, array<string, mixed>>
38 */
39 public static function getDefaultIntegrationSettings(): array
40 {
41 return self::DEFAULT_INTEGRATION_SETTINGS;
42 }
43
44 /**
45 * Get default form style settings from bundled JSON (`backend/resources/defaultFormStyles.json`).
46 *
47 * @return array<string, mixed>
48 * @throws InvalidArgumentException
49 */
50 public static function getDefaultFormStyles(): array
51 {
52 $exceptionStrings = BackendStrings::getExceptionStrings();
53
54 if (self::$cachedFormStyles !== null) {
55 return self::$cachedFormStyles;
56 }
57
58 $jsonPath = self::getDefaultFormStylesJsonPath();
59
60 if (!is_readable($jsonPath)) {
61 throw new InvalidArgumentException(
62 sprintf(
63 $exceptionStrings['default_styles_json_not_found'],
64 $jsonPath
65 )
66 );
67 }
68
69 $jsonContent = file_get_contents($jsonPath);
70 if ($jsonContent === false) {
71 throw new InvalidArgumentException(
72 sprintf($exceptionStrings['default_styles_json_read_failed'], $jsonPath)
73 );
74 }
75
76 try {
77 $decoded = json_decode($jsonContent, true, 512, JSON_THROW_ON_ERROR);
78 } catch (\JsonException $jsonException) {
79 throw new InvalidArgumentException(
80 sprintf($exceptionStrings['default_styles_json_parse_failed'], $jsonException->getMessage())
81 );
82 }
83
84 if (!is_array($decoded)) {
85 throw new InvalidArgumentException($exceptionStrings['default_styles_json_invalid_structure']);
86 }
87
88 self::$cachedFormStyles = $decoded;
89
90 return self::$cachedFormStyles;
91 }
92
93 /**
94 * Default form styles from JSON, or a minimal safe structure if the file is missing or invalid.
95 *
96 * @return array<string, mixed>
97 */
98 public static function getDefaultFormStylesSafe(): array
99 {
100 try {
101 return self::getDefaultFormStyles();
102 } catch (InvalidArgumentException $e) {
103 return self::getMinimalFormStyleSettingsFallback();
104 }
105 }
106
107 /**
108 * Minimal style settings when JSON defaults cannot be loaded (matches sanitizer-valid shape).
109 *
110 * @return array<string, mixed>
111 */
112 private static function getMinimalFormStyleSettingsFallback(): array
113 {
114 return [
115 'stylesEnabled' => false,
116 'selectedTheme' => 'ivy-default',
117 'styleMode' => 'quick',
118 'customCssClass' => '',
119 'additionalCssClasses' => '',
120 'quickMode' => [],
121 'advancedMode' => [],
122 ];
123 }
124
125 /**
126 * Absolute path to the bundled default form styles JSON.
127 *
128 * @return string
129 */
130 private static function getDefaultFormStylesJsonPath(): string
131 {
132 $root = defined('IVYFORMS_PATH') ? IVYFORMS_PATH : dirname(__DIR__, 4);
133
134 return $root . '/backend/resources/defaultFormStyles.json';
135 }
136 }
137