PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.12
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.12
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 / TabularGrid.php

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

159 lines 6.6 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\Framework\Helpers\ArrayHelper;
6
7 class TabularGrid extends BaseComponent
8 {
9 /**
10 * Compile and echo the html element
11 *
12 * @param array $data [element data]
13 * @param \stdClass $form [Form Object]
14 *
15 * @return void
16 */
17 public function compile($data, $form)
18 {
19 $elementName = $data['element'];
20 $data = apply_filters_deprecated(
21 'fluentform_rendering_field_data_' . $elementName,
22 [
23 $data,
24 $form
25 ],
26 FLUENTFORM_FRAMEWORK_UPGRADE,
27 'fluentform/rendering_field_data_' . $elementName,
28 'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName
29 );
30 $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form);
31
32 $checked = $data['settings']['selected_grids'];
33 $columnLabels = $data['settings']['grid_columns'];
34
35 $fieldType = $data['settings']['tabular_field_type'];
36 $columnHeaders = implode('</th><th>', array_values($columnLabels));
37 $elementHelpMessage = $this->getElementHelpMessage($data, $form);
38 $elementLabel = $this->setClasses($data)->buildElementLabel($data, $form);
39
40 $elMarkup = "<table class='ff-table ff-checkable-grids ff_flexible_table' role='table'><thead><tr><th></th><th>" . fluentform_sanitize_html($columnHeaders) . '</th></tr></thead><tbody>';
41
42 $tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex();
43 foreach ($this->makeTabularData($data) as $index => $row) {
44 $elMarkup .= '<tr role="row"">';
45 $elMarkup .= "<td class='ff_grid_header' role='cell'>" . fluentform_sanitize_html($row['label']) . '</td>';
46 $isRowChecked = in_array($row['name'], $checked) ? 'checked' : '';
47 foreach ($row['columns'] as $column) {
48 $name = $data['attributes']['name'] . '[' . $row['name'] . ']';
49 $name = 'checkbox' == $fieldType ? ($name . '[]') : $name;
50 $isColChecked = in_array($column['name'], $checked) ? 'checked' : '';
51 $isChecked = $isRowChecked ? $isRowChecked : $isColChecked;
52
53 $atts = [
54 'name' => $name,
55 'type' => $fieldType,
56 'value' => $column['name'],
57 ];
58 if ($tabIndex) {
59 $atts['tabindex'] = $tabIndex;
60 }
61 $attributes = $this->buildAttributes($atts, $form);
62
63 $ariaRequired = 'false';
64 if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) {
65 $ariaRequired = 'true';
66 }
67
68 // SECURITY (FINDING-12): esc_attr the row/column labels before interpolating them
69 // into the double-quoted aria-label; save-time sanitizers do not encode quotes.
70 $input = '<input aria-label="'. esc_attr($row['name']) .'-'. esc_attr($column['label']) . '" ' . $attributes . " {$isChecked} aria-invalid='false' aria-required={$ariaRequired}>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $attributes is escaped before being passed in.
71 $elMarkup .= "<td data-label='" . fluentform_sanitize_html($column['label']) . "'>{$input}</td>";
72 }
73 $elMarkup .= '</tr>';
74 }
75
76 $elMarkup .= '</tbody></table>';
77
78 $elMarkup = "<div class='ff-el-input--content'>{$elMarkup}" . fluentform_sanitize_html($elementHelpMessage) . '</div>';
79
80 // SECURITY (FINDING-12): esc_attr the attribute values interpolated into the single-quoted
81 // data-type / data-name / class attributes; sanitize_text_field at save does not encode quotes.
82 $html = sprintf(
83 "<div data-type='%s' data-name='%s' class='%s'>%s",
84 esc_attr($data['attributes']['data-type']),
85 esc_attr($data['attributes']['name']),
86 esc_attr($data['attributes']['class']),
87 $elementLabel
88 ) . $elMarkup . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $elementLabel is escaped before being passed in.
89
90 $html = apply_filters_deprecated(
91 'fluentform_rendering_field_html_' . $elementName,
92 [
93 $html,
94 $data,
95 $form
96 ],
97 FLUENTFORM_FRAMEWORK_UPGRADE,
98 'fluentform/rendering_field_html_' . $elementName,
99 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName
100 );
101 $this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
102 }
103
104 public function makeTabularData($data)
105 {
106 $table = [];
107 $rows = $data['settings']['grid_rows'];
108 $columns = $data['settings']['grid_columns'];
109
110 foreach ($rows as $rowKey => $rowValue) {
111 $rowKey = trim(sanitize_text_field($rowKey));
112 $table[$rowKey] = [
113 'name' => $rowKey,
114 'label' => $rowValue,
115 'columns' => [],
116 ];
117
118 foreach ($columns as $columnKey => $columnValue) {
119 $columnKey = trim(sanitize_text_field($columnKey));
120 $table[$rowKey]['columns'][] = [
121 'name' => $columnKey,
122 'label' => $columnValue,
123 ];
124 }
125 }
126
127 return $table;
128 }
129
130 protected function getElementHelpMessage($data, $form)
131 {
132 $elementHelpMessage = '';
133 $helpMessagePlacement = ArrayHelper::get($form->settings, 'layout.helpMessagePlacement', 'with_label');
134 if ('under_input' == $helpMessagePlacement) {
135 $elementHelpMessage = $this->getInputHelpMessage($data);
136 }
137
138 return $elementHelpMessage;
139 }
140
141 protected function setClasses(&$data)
142 {
143 if (! isset($data['attributes']['class'])) {
144 $data['attributes']['class'] = '';
145 }
146
147 $placement = $data['settings']['label_placement'];
148 $placementClass = $placement ? 'ff-el-form-' . $placement : '';
149 $hasConditions = $this->hasConditions($data) ? ' has-conditions' : '';
150 $defaultContainerClass = $this->getDefaultContainerClass();
151 $containerClass = $data['settings']['container_class'];
152 $data['attributes']['class'] .= trim(implode(' ', array_map('trim', [
153 $defaultContainerClass, $containerClass, $placementClass, $hasConditions,
154 ])));
155
156 return $this;
157 }
158 }
159