PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
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 4.3.18, at app/Services/FormBuilder/Components/TabularGrid.php

125 lines 4.8 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('fluentform_rendering_field_data_' . $elementName, $data, $form);
21
22 $checked = $data['settings']['selected_grids'];
23 $columnLabels = $data['settings']['grid_columns'];
24
25 $fieldType = $data['settings']['tabular_field_type'];
26 $columnHeaders = implode('</th><th>', array_values($columnLabels));
27 $elementHelpMessage = $this->getElementHelpMessage($data, $form);
28 $elementLabel = $this->setClasses($data)->buildElementLabel($data, $form);
29
30 $elMarkup = "<table class='ff-table ff-checkable-grids ff_flexible_table'><thead><tr><th></th><th>" . fluentform_sanitize_html($columnHeaders, false) . '</th></tr></thead><tbody>';
31
32 $tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex();
33 foreach ($this->makeTabularData($data) as $index => $row) {
34 $elMarkup .= '<tr>';
35 $elMarkup .= "<td class='ff_grid_header'>" . fluentform_sanitize_html($row['label'], false) . '</td>';
36 $isRowChecked = in_array($row['name'], $checked) ? 'checked' : '';
37 foreach ($row['columns'] as $column) {
38 $name = $data['attributes']['name'] . '[' . $row['name'] . ']';
39 $name = 'checkbox' == $fieldType ? ($name . '[]') : $name;
40 $isColChecked = in_array($column['name'], $checked) ? 'checked' : '';
41 $isChecked = $isRowChecked ? $isRowChecked : $isColChecked;
42
43 $atts = [
44 'name' => $name,
45 'type' => $fieldType,
46 'value' => $column['name'],
47 ];
48 if ($tabIndex) {
49 $atts['tabindex'] = $tabIndex;
50 }
51 $attributes = $this->buildAttributes($atts, $form);
52
53 $input = '<input ' . $attributes . " {$isChecked}>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $attributes is escaped before being passed in.
54 $elMarkup .= "<td data-label='" . fluentform_sanitize_html($column['label'], false) . "'>{$input}</td>";
55 }
56 $elMarkup .= '</tr>';
57 }
58
59 $elMarkup .= '</tbody></table>';
60
61 $elMarkup = "<div class='ff-el-input--content'>{$elMarkup}" . fluentform_sanitize_html($elementHelpMessage, false) . '</div>';
62
63 $html = sprintf(
64 "<div data-type='%s' data-name='%s' class='%s'>{$elementLabel}{$elMarkup}</div>",
65 $data['attributes']['data-type'],
66 $data['attributes']['name'],
67 $data['attributes']['class']
68 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $elementLabel is escaped before being passed in.
69
70 $this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form);
71 }
72
73 public function makeTabularData($data)
74 {
75 $table = [];
76 $rows = $data['settings']['grid_rows'];
77 $columns = $data['settings']['grid_columns'];
78
79 foreach ($rows as $rowKey => $rowValue) {
80 $table[$rowKey] = [
81 'name' => $rowKey,
82 'label' => $rowValue,
83 'columns' => [],
84 ];
85
86 foreach ($columns as $columnKey => $columnValue) {
87 $table[$rowKey]['columns'][] = [
88 'name' => $columnKey,
89 'label' => $columnValue,
90 ];
91 }
92 }
93
94 return $table;
95 }
96
97 protected function getElementHelpMessage($data, $form)
98 {
99 $elementHelpMessage = '';
100 if ('under_input' == $form->settings['layout']['helpMessagePlacement']) {
101 $elementHelpMessage = $this->getInputHelpMessage($data);
102 }
103
104 return $elementHelpMessage;
105 }
106
107 protected function setClasses(&$data)
108 {
109 if (! isset($data['attributes']['class'])) {
110 $data['attributes']['class'] = '';
111 }
112
113 $placement = $data['settings']['label_placement'];
114 $placementClass = $placement ? 'ff-el-form-' . $placement : '';
115 $hasConditions = $this->hasConditions($data) ? ' has-conditions' : '';
116 $defaultContainerClass = $this->getDefaultContainerClass();
117 $containerClass = $data['settings']['container_class'];
118 $data['attributes']['class'] .= trim(implode(' ', array_map('trim', [
119 $defaultContainerClass, $containerClass, $placementClass, $hasConditions,
120 ])));
121
122 return $this;
123 }
124 }
125