PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
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.21, at app/Services/FormBuilder/Components/BaseComponent.php

261 lines 7.9 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 /**
73 * Determine if the given element has conditions bound
74 * @param array $element [Html element being compiled]
75 * @return boolean
76 */
77 protected function hasConditions($element)
78 {
79 $conditionals = ArrayHelper::get($element, 'settings.conditional_logics');
80
81 if (isset($conditionals['status']) && $conditionals['status']) {
82 return array_filter($conditionals['conditions'], function($item) {
83 return $item['field'] && $item['operator'];
84 });
85 }
86 }
87
88 /**
89 * Generate a unique id for an element
90 * @param string $str [preix]
91 * @return string [Unique id]
92 */
93 protected function getUniqueId($str)
94 {
95 return $str.'_'.md5(uniqid(mt_rand(), true));
96 }
97
98 /**
99 * Get a default class for each form element wrapper
100 * @return string
101 */
102 protected function getDefaultContainerClass()
103 {
104 return 'ff-el-group ';
105 }
106
107 /**
108 * Get required class for form element wrapper
109 * @param array $rules [Validation rules]
110 * @return mixed
111 */
112 protected function getRequiredClass($rules)
113 {
114 if (isset($rules['required'])) {
115 return $rules['required']['value'] ? 'ff-el-is-required ' : '';
116 }
117 }
118
119 /**
120 * Get asterisk placement for the required form elements
121 * @return String
122 */
123 protected function getAsteriskPlacement($form)
124 {
125 // for older version compatibility
126 $asteriskPlacement = 'asterisk-right';
127
128 if (isset($form->settings['layout']['asteriskPlacement'])) {
129 $asteriskPlacement = $form->settings['layout']['asteriskPlacement'];
130 }
131
132 return $asteriskPlacement.' ';
133 }
134
135 /**
136 * Generate a label for any element
137 * @param array $data
138 * @return string [label Html element]
139 */
140 protected function buildElementLabel($data, $form)
141 {
142 $helpMessage = '';
143 if ($form->settings['layout']['helpMessagePlacement'] == 'with_label') {
144 $helpMessage = $this->getLabelHelpMessage($data);
145 }
146
147 $id = isset($data['attributes']['id']) ? $data['attributes']['id'] : '';
148 $label = isset($data['settings']['label']) ? $data['settings']['label'] : '';
149 $requiredClass = $this->getRequiredClass(ArrayHelper::get($data, 'settings.validation_rules', []));
150 $classes = trim('ff-el-input--label ' . $requiredClass . $this->getAsteriskPlacement($form));
151
152 return "<div class='".$classes."'><label for='".$id."'>".$label."</label>{$helpMessage}</div>";
153 }
154
155 /**
156 * Generate html/markup for any element
157 * @param string $elMarkup [Predifined partial markup]
158 * @param array $data
159 * @param StdClass $form [Form object]
160 * @return string [Compiled markup]
161 */
162 protected function buildElementMarkup($elMarkup, $data, $form)
163 {
164 $hasConditions = $this->hasConditions($data) ? 'has-conditions ' : '';
165
166 $labelPlacement = ArrayHelper::get($data, 'settings.label_placement');
167
168 $labelPlacementClass = $labelPlacement ? 'ff-el-form-'.$labelPlacement.' ' : '';
169
170 $validationRules = ArrayHelper::get($data, 'settings.validation_rules');
171
172 $labelClass = trim(
173 'ff-el-input--label '.
174 $this->getRequiredClass($validationRules).
175 $this->getAsteriskPlacement($form)
176 );
177
178 $formGroupClass = trim(
179 $this->getDefaultContainerClass().
180 $labelPlacementClass.
181 $hasConditions.
182 ArrayHelper::get($data, 'settings.container_class')
183 );
184
185 $labelHelpText = $inputHelpText = '';
186
187 if ($form->settings['layout']['helpMessagePlacement'] == 'with_label') {
188 $labelHelpText = $this->getLabelHelpMessage($data);
189 } elseif ($form->settings['layout']['helpMessagePlacement'] == 'on_focus') {
190 $inputHelpText = $this->getInputHelpMessage($data, 'ff-hidden');
191 } else {
192 $inputHelpText = $this->getInputHelpMessage($data);
193 }
194
195 $forStr = '';
196 if (isset($data['attributes']['id'])) {
197 $forStr = "for='{$data['attributes']['id']}'";
198 }
199
200 $labelMarkup = '';
201
202 if (!empty($data['settings']['label'])) {
203 $label = ArrayHelper::get($data, 'settings.label');
204
205 $labelMarkup = sprintf(
206 "<div class='%s'><label %s>%s</label> %s</div>",
207 $labelClass,
208 $forStr,
209 $label,
210 $labelHelpText
211 );
212
213 // $labelMarkup = "<div class='".$labelClass."'><label ".$forStr.">".$label."</label> {$labelHelpText}</div>";
214 }
215
216 return sprintf(
217 "<div class='%s'>%s<div class='ff-el-input--content'>%s%s</div></div>",
218 $formGroupClass,
219 $labelMarkup,
220 $elMarkup,
221 $inputHelpText
222 );
223
224 // return "<div class='".$formGroupClass."'>{$labelMarkup}<div class='ff-el-input--content'>{$elMarkup}{$inputHelpText}</div></div>";
225 }
226
227 /**
228 * Generate a help message for any element beside label
229 * @param array $data
230 * @return string [Html]
231 */
232 protected function getLabelHelpMessage($data)
233 {
234 if (isset($data['settings']['help_message']) && $data['settings']['help_message'] != '') {
235 return "<div class='ff-el-tooltip' data-content='{$data['settings']['help_message']}'>
236 <i class='ff-icon icon-info-circle'></i>
237 </div>";
238 }
239 }
240
241 /**
242 * Generate a help message for any element beside form element
243 * @param array $data
244 * @return string [Html]
245 */
246 protected function getInputHelpMessage($data, $hideClass = '')
247 {
248 $class = trim('ff-el-help-message '.$hideClass);
249
250 if (isset($data['settings']['help_message']) && !empty($data['settings']['help_message'])) {
251 return "<div class='{$class}'>{$data['settings']['help_message']}</div>";
252 }
253 return false;
254 }
255
256 protected function parseEditorSmartCode($text, $form)
257 {
258 return (new Component($this->app))->replaceEditorSmartCodes($text, $form);
259 }
260 }
261