PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.64
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.64
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components / BaseComponent.php

BaseComponent.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.64, at app/Services/FormBuilder/Components/BaseComponent.php

276 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder\Components;
4
5 use FluentForm\App;
6 use FluentForm\App\Modules\Component\Component;
7 use FluentForm\Framework\Helpers\ArrayHelper;
8
9 class BaseComponent
10 {
11
12 public $app;
13
14 public function __construct($key = '', $title = '', $tags = [], $position = 'advanced')
15 {
16 $this->app = wpFluentForm();
17 }
18
19 /**
20 * Build unique ID concating form id and name attribute
21 * @param array $data $form
22 * @return string for id value
23 */
24 protected function makeElementId($data, $form)
25 {
26 if (isset($data['attributes']['name'])) {
27 if (!empty($data['attributes']['id'])) {
28 return $data['attributes']['id'];
29 }
30 $elementName = $data['attributes']['name'];
31 $elementName = str_replace(['[', ']', ' '], '_', $elementName);
32 return "ff_{$form->id}_{$elementName}";
33 }
34 }
35
36 /**
37 * Build attributes for any html element
38 * @param array $attributes
39 * @return string [Compiled key='value' attributes]
40 */
41 protected function buildAttributes($attributes, $form = null)
42 {
43 $atts = '';
44
45 foreach ($attributes as $key => $value) {
46 if ($value || $value === 0 || $value === '0') {
47 $value = htmlspecialchars($value);
48 $atts .= $key . '="' . $value . '" ';
49 }
50 }
51
52 return $atts;
53 }
54
55 /**
56 * Extract value attribute from attribute list
57 * @param array &$element
58 * @return string
59 */
60 protected function extractValueFromAttributes(&$element)
61 {
62 $value = '';
63
64 if (isset($element['attributes']['value'])) {
65 $value = $element['attributes']['value'];
66 unset($element['attributes']['value']);
67 }
68
69 return $value;
70 }
71
72 protected function extractDynamicValues($data, $form)
73 {
74 $defaultValues = [];
75 if ($dynamicDefaultValue = ArrayHelper::get($data, 'settings.dynamic_default_value')) {
76 $parseValue = $this->parseEditorSmartCode($dynamicDefaultValue, $form);
77 if ($parseValue) {
78 $defaultValues = explode(',', $parseValue);
79 $defaultValues = array_map('trim', $defaultValues);
80 }
81 }
82 return $defaultValues;
83 }
84
85 /**
86 * Determine if the given element has conditions bound
87 * @param array $element [Html element being compiled]
88 * @return boolean
89 */
90 protected function hasConditions($element)
91 {
92 $conditionals = ArrayHelper::get($element, 'settings.conditional_logics');
93
94 if (isset($conditionals['status']) && $conditionals['status']) {
95 return array_filter($conditionals['conditions'], function ($item) {
96 return $item['field'] && $item['operator'];
97 });
98 }
99 }
100
101 /**
102 * Generate a unique id for an element
103 * @param string $str [preix]
104 * @return string [Unique id]
105 */
106 protected function getUniqueId($str)
107 {
108 return $str . '_' . md5(uniqid(mt_rand(), true));
109 }
110
111 /**
112 * Get a default class for each form element wrapper
113 * @return string
114 */
115 protected function getDefaultContainerClass()
116 {
117 return 'ff-el-group ';
118 }
119
120 /**
121 * Get required class for form element wrapper
122 * @param array $rules [Validation rules]
123 * @return mixed
124 */
125 protected function getRequiredClass($rules)
126 {
127 if (isset($rules['required'])) {
128 return $rules['required']['value'] ? 'ff-el-is-required ' : '';
129 }
130 }
131
132 /**
133 * Get asterisk placement for the required form elements
134 * @return String
135 */
136 protected function getAsteriskPlacement($form)
137 {
138 // for older version compatibility
139 $asteriskPlacement = 'asterisk-right';
140
141 if (isset($form->settings['layout']['asteriskPlacement'])) {
142 $asteriskPlacement = $form->settings['layout']['asteriskPlacement'];
143 }
144
145 return $asteriskPlacement . ' ';
146 }
147
148 /**
149 * Generate a label for any element
150 * @param array $data
151 * @return string [label Html element]
152 */
153 protected function buildElementLabel($data, $form)
154 {
155 $helpMessage = '';
156 if ($form->settings['layout']['helpMessagePlacement'] == 'with_label') {
157 $helpMessage = $this->getLabelHelpMessage($data);
158 }
159
160 $id = isset($data['attributes']['id']) ? $data['attributes']['id'] : '';
161 $label = isset($data['settings']['label']) ? $data['settings']['label'] : '';
162 $requiredClass = $this->getRequiredClass(ArrayHelper::get($data, 'settings.validation_rules', []));
163 $classes = trim('ff-el-input--label ' . $requiredClass . $this->getAsteriskPlacement($form));
164
165 return "<div class='" . $classes . "'><label for='" . $id . "'>" . $label . "</label>{$helpMessage}</div>";
166 }
167
168 /**
169 * Generate html/markup for any element
170 * @param string $elMarkup [Predifined partial markup]
171 * @param array $data
172 * @param StdClass $form [Form object]
173 * @return string [Compiled markup]
174 */
175 protected function buildElementMarkup($elMarkup, $data, $form)
176 {
177 $hasConditions = $this->hasConditions($data) ? 'has-conditions ' : '';
178
179 $labelPlacement = ArrayHelper::get($data, 'settings.label_placement');
180
181 $labelPlacementClass = $labelPlacement ? 'ff-el-form-' . $labelPlacement . ' ' : '';
182
183 $validationRules = ArrayHelper::get($data, 'settings.validation_rules');
184
185 $labelClass = trim(
186 'ff-el-input--label ' .
187 $this->getRequiredClass($validationRules) .
188 $this->getAsteriskPlacement($form)
189 );
190
191 $formGroupClass = trim(
192 $this->getDefaultContainerClass() .
193 $labelPlacementClass .
194 $hasConditions .
195 ArrayHelper::get($data, 'settings.container_class')
196 );
197
198 $labelHelpText = $inputHelpText = '';
199
200 if ($form->settings['layout']['helpMessagePlacement'] == 'with_label') {
201 $labelHelpText = $this->getLabelHelpMessage($data);
202 } elseif ($form->settings['layout']['helpMessagePlacement'] == 'on_focus') {
203 $inputHelpText = $this->getInputHelpMessage($data, 'ff-hidden');
204 } else {
205 $inputHelpText = $this->getInputHelpMessage($data);
206 }
207
208 $forStr = '';
209 if (isset($data['attributes']['id'])) {
210 $forStr = "for='{$data['attributes']['id']}'";
211 }
212
213 $labelMarkup = '';
214
215 if (!empty($data['settings']['label'])) {
216 $label = ArrayHelper::get($data, 'settings.label');
217
218 $labelMarkup = sprintf(
219 "<div class='%s'><label %s>%s</label> %s</div>",
220 $labelClass,
221 $forStr,
222 $label,
223 $labelHelpText
224 );
225
226 // $labelMarkup = "<div class='".$labelClass."'><label ".$forStr.">".$label."</label> {$labelHelpText}</div>";
227 }
228
229
230 return sprintf(
231 "<div class='%s'>%s<div class='ff-el-input--content'>%s%s</div></div>",
232 $formGroupClass,
233 $labelMarkup,
234 $elMarkup,
235 $inputHelpText
236 );
237
238 // return "<div class='".$formGroupClass."'>{$labelMarkup}<div class='ff-el-input--content'>{$elMarkup}{$inputHelpText}</div></div>";
239 }
240
241 /**
242 * Generate a help message for any element beside label
243 * @param array $data
244 * @return string [Html]
245 */
246 protected function getLabelHelpMessage($data)
247 {
248 if (isset($data['settings']['help_message']) && $data['settings']['help_message'] != '') {
249 $icon = '<svg width="16" height="16" viewBox="0 0 25 25"><path d="m329 393l0-46c0-2-1-4-2-6-2-2-4-3-7-3l-27 0 0-146c0-3-1-5-3-7-2-1-4-2-7-2l-91 0c-3 0-5 1-7 2-1 2-2 4-2 7l0 46c0 2 1 5 2 6 2 2 4 3 7 3l27 0 0 91-27 0c-3 0-5 1-7 3-1 2-2 4-2 6l0 46c0 3 1 5 2 7 2 1 4 2 7 2l128 0c3 0 5-1 7-2 1-2 2-4 2-7z m-36-256l0-46c0-2-1-4-3-6-2-2-4-3-7-3l-54 0c-3 0-5 1-7 3-2 2-3 4-3 6l0 46c0 3 1 5 3 7 2 1 4 2 7 2l54 0c3 0 5-1 7-2 2-2 3-4 3-7z m182 119c0 40-9 77-29 110-20 34-46 60-80 80-33 20-70 29-110 29-40 0-77-9-110-29-34-20-60-46-80-80-20-33-29-70-29-110 0-40 9-77 29-110 20-34 46-60 80-80 33-20 70-29 110-29 40 0 77 9 110 29 34 20 60 46 80 80 20 33 29 70 29 110z" transform="scale(0.046875 0.046875)"></path></svg>';
250 return "<div class='ff-el-tooltip' data-content='{$data['settings']['help_message']}'>
251 {$icon}
252 </div>";
253 }
254 }
255
256 /**
257 * Generate a help message for any element beside form element
258 * @param array $data
259 * @return string [Html]
260 */
261 protected function getInputHelpMessage($data, $hideClass = '')
262 {
263 $class = trim('ff-el-help-message ' . $hideClass);
264
265 if (isset($data['settings']['help_message']) && !empty($data['settings']['help_message'])) {
266 return "<div class='{$class}'>{$data['settings']['help_message']}</div>";
267 }
268 return false;
269 }
270
271 protected function parseEditorSmartCode($text, $form)
272 {
273 return (new Component($this->app))->replaceEditorSmartCodes($text, $form);
274 }
275 }
276