| 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 |
* @param array $data [element data] |
| 12 |
* @param stdClass $form [Form Object] |
| 13 |
* @return viod |
| 14 |
*/ |
| 15 |
public function compile($data, $form) |
| 16 |
{ |
| 17 |
$elementName = $data['element']; |
| 18 |
$data = apply_filters('fluenform_rendering_field_data_'.$elementName, $data, $form); |
| 19 |
|
| 20 |
$checked = $data['settings']['selected_grids']; |
| 21 |
$columnLabels = $data['settings']['grid_columns']; |
| 22 |
|
| 23 |
|
| 24 |
$fieldType = $data['settings']['tabular_field_type']; |
| 25 |
$columnHeaders = implode('</th><th>', array_values($columnLabels)); |
| 26 |
$elementHelpMessage = $this->getElementHelpMessage($data, $form); |
| 27 |
$elementLabel = $this->setClasses($data)->buildElementLabel($data, $form); |
| 28 |
|
| 29 |
|
| 30 |
$elMarkup = "<table class='ff-table ff-checkable-grids ff_flexible_table'><thead><tr><th></th><th>{$columnHeaders}</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'>{$row['label']}</td>"; |
| 36 |
$isRowChecked = in_array($row['name'], $checked) ? 'checked' : ''; |
| 37 |
foreach ($row['columns'] as $column) { |
| 38 |
$name = $data['attributes']['name'] . '['.$row['name'].']'; |
| 39 |
$name = $fieldType == 'checkbox' ? ($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}>"; |
| 54 |
$elMarkup .= "<td data-label='".$column['label']."'>{$input}</td>"; |
| 55 |
} |
| 56 |
$elMarkup .= "</tr>"; |
| 57 |
} |
| 58 |
|
| 59 |
$elMarkup .= "</tbody></table>"; |
| 60 |
|
| 61 |
$elMarkup = "<div class='ff-el-input--content'>{$elMarkup}{$elementHelpMessage}</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 |
); |
| 69 |
|
| 70 |
echo apply_filters('fluenform_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 ($form->settings['layout']['helpMessagePlacement'] == 'under_input') { |
| 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 |
|