Merge branch 'ui-refacto' into develop-unstable

pull/72/head
mokaddem 2021-09-17 16:38:48 +02:00
commit 36f6866bd3
4 changed files with 248 additions and 71 deletions

View File

@ -65,10 +65,16 @@ class BootstrapHelper extends Helper
public function table($options, $data)
{
$bsTable = new BoostrapTable($options, $data);
$bsTable = new BoostrapTable($options, $data, $this);
return $bsTable->table();
}
public function listTable($options, $data)
{
$bsListTable = new BoostrapListTable($options, $data, $this);
return $bsListTable->table();
}
public function button($options)
{
$bsButton = new BoostrapButton($options);
@ -203,6 +209,7 @@ class BootstrapTabs extends BootstrapGeneric
'card' => false,
'header-variant' => 'light',
'body-variant' => '',
'body-class' => [],
'nav-class' => [],
'nav-item-class' => [],
'content-class' => [],
@ -321,7 +328,13 @@ class BootstrapTabs extends BootstrapGeneric
$html .= $this->genNav();
if ($this->options['card']) {
$html .= $this->closeNode('div');
$html .= $this->openNode('div', ['class' => array_merge(['card-body'], ["bg-{$this->options['body-variant']}", "text-{$this->options['body-text-variant']}"])]);
$html .= $this->openNode('div', [
'class' => array_merge(
['card-body'],
$this->options['body-class'] ?? [],
["bg-{$this->options['body-variant']}", "text-{$this->options['body-text-variant']}"]
)
]);
}
$html .= $this->genContent();
if ($this->options['card']) {
@ -337,7 +350,13 @@ class BootstrapTabs extends BootstrapGeneric
$html .= $this->openNode('div', ['class' => array_merge(['col-' . $this->options['vertical-size'], ($this->options['card'] ? 'card-header border-right' : '')], ["bg-{$this->options['header-variant']}", "text-{$this->options['header-text-variant']}", "border-{$this->options['header-border-variant']}"])]);
$html .= $this->genNav();
$html .= $this->closeNode('div');
$html .= $this->openNode('div', ['class' => array_merge(['col-' . (12 - $this->options['vertical-size']), ($this->options['card'] ? 'card-body2' : '')], ["bg-{$this->options['body-variant']}", "text-{$this->options['body-text-variant']}"])]);
$html .= $this->openNode('div', [
'class' => array_merge(
['col-' . (12 - $this->options['vertical-size']), ($this->options['card'] ? 'card-body2' : '')],
$this->options['body-class'] ?? [],
["bg-{$this->options['body-variant']}", "text-{$this->options['body-text-variant']}"]
)
]);
$html .= $this->genContent();
$html .= $this->closeNode('div');
$html .= $this->closeNode('div');
@ -487,7 +506,7 @@ class BoostrapTable extends BootstrapGeneric {
private $bsClasses = null;
function __construct($options, $data) {
function __construct($options, $data, $btHelper) {
$this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, [''])
];
@ -495,6 +514,7 @@ class BoostrapTable extends BootstrapGeneric {
$this->fields = $data['fields'];
$this->items = $data['items'];
$this->caption = !empty($data['caption']) ? $data['caption'] : '';
$this->btHelper = $btHelper;
}
private function processOptions($options)
@ -603,6 +623,11 @@ class BoostrapTable extends BootstrapGeneric {
{
if (isset($field['formatter'])) {
$cellContent = $field['formatter']($value, $row);
} else if (isset($field['element'])) {
$cellContent = $this->btHelper->getView()->element($field['element'], [
'data' => [$value],
'field' => ['path' => '0']
]);
} else {
$cellContent = h($value);
}
@ -619,6 +644,145 @@ class BoostrapTable extends BootstrapGeneric {
}
}
class BoostrapListTable extends BootstrapGeneric {
private $defaultOptions = [
'striped' => true,
'bordered' => false,
'borderless' => false,
'hover' => true,
'small' => false,
'variant' => '',
'tableClass' => [],
'bodyClass' => [],
];
private $bsClasses = null;
function __construct($options, $data, $btHelper) {
$this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, [''])
];
$this->processOptions($options);
$this->fields = $data['fields'];
$this->item = $data['item'];
$this->caption = !empty($data['caption']) ? $data['caption'] : '';
$this->btHelper = $btHelper;
}
private function processOptions($options)
{
$this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity();
}
public function table()
{
return $this->genTable();
}
private function genTable()
{
$html = $this->openNode('table', [
'class' => [
'table',
"table-{$this->options['variant']}",
$this->options['striped'] ? 'table-striped' : '',
$this->options['bordered'] ? 'table-bordered' : '',
$this->options['borderless'] ? 'table-borderless' : '',
$this->options['hover'] ? 'table-hover' : '',
$this->options['small'] ? 'table-sm' : '',
!empty($this->options['variant']) ? "table-{$this->options['variant']}" : '',
!empty($this->options['tableClass']) ? (is_array($this->options['tableClass']) ? implode(' ', $this->options['tableClass']) : $this->options['tableClass']) : ''
],
'id' => $this->options['id'] ?? ''
]);
$html .= $this->genCaption();
$html .= $this->genBody();
$html .= $this->closeNode('table');
return $html;
}
private function genBody()
{
$body = $this->openNode('tbody', [
'class' => [
!empty($this->options['bodyClass']) ? (is_array($this->options['bodyClass']) ? implode(' ', $this->options['bodyClass']) : $this->options['bodyClass']) : ''
],
]);
foreach ($this->fields as $i => $field) {
$body .= $this->genRow($field);
}
$body .= $this->closeNode('tbody');
return $body;
}
private function genRow($field)
{
$rowValue = $this->genCell($field);
$rowKey = $this->genNode('th', [
'class' => [
'col-sm-2'
],
'scope' => 'row'
], h($field['key']));
$row = $this->genNode('tr',[
'class' => [
'd-flex',
!empty($field['_rowVariant']) ? "table-{$field['_rowVariant']}" : ''
]
], implode('', [$rowKey, $rowValue]));
return $row;
}
private function genCell($field=[])
{
if (isset($field['raw'])) {
$cellContent = h($field['raw']);
} else if (isset($field['formatter'])) {
$cellContent = $field['formatter']($this->getValueFromObject($field), $this->item);
} else if (isset($field['type'])) {
$cellContent = $this->btHelper->getView()->element($this->getElementPath($field['type']), [
'data' => $this->item,
'field' => $field
]);
} else {
$cellContent = h($this->getValueFromObject($field));
}
return $this->genNode('td', [
'class' => [
'col-sm-10',
!empty($row['_cellVariant']) ? "bg-{$row['_cellVariant']}" : ''
]
], $cellContent);
}
private function getValueFromObject($field)
{
if (is_array($field)) {
$key = $field['path'];
} else {
$key = $field;
}
$cellValue = Hash::get($this->item, $key);
return $cellValue;
}
private function getElementPath($type)
{
return sprintf('%s%sField',
$this->options['elementsRootPath'] ?? '',
$type
);
}
private function genCaption()
{
return !empty($this->caption) ? $this->genNode('caption', [], h($this->caption)) : '';
}
}
class BoostrapButton extends BootstrapGeneric {
private $defaultOptions = [
'id' => '',

View File

@ -45,6 +45,7 @@ echo $this->element(
]
],
'metaTemplates' => empty($metaFields) ? [] : $metaFields,
'combinedFieldsView' => true,
'children' => []
]
);

View File

@ -0,0 +1,47 @@
<?php
$tabData = [
'navs' => [],
'content' => []
];
foreach($data['metaTemplates'] as $metaTemplate) {
if (!empty($metaTemplate->meta_template_fields)) {
if ($metaTemplate->is_default) {
$tabData['navs'][] = [
'html' => $this->element('/genericElements/MetaTemplates/metaTemplateNav', ['metaTemplate' => $metaTemplate])
];
} else {
$tabData['navs'][] = [
'text' => $metaTemplate->name
];
}
$fields = [];
foreach ($metaTemplate->meta_template_fields as $metaTemplateField) {
$metaField = $metaTemplateField->meta_fields[0];
$fields[] = [
'key' => $metaField->field,
'raw' => $metaField->value
];
}
$listTable = $this->Bootstrap->listTable([
'hover' => false,
'elementsRootPath' => '/genericElements/SingleViews/Fields/'
],[
'item' => false,
'fields' => $fields
]);
$tabData['content'][] = $listTable;
}
}
if (!empty($additionalTabs)) {
$tabData['navs'] = array_merge($additionalTabs['navs'], $tabData['navs']);
$tabData['content'] = array_merge($additionalTabs['content'], $tabData['content']);
}
if (!empty($tabData['navs'])) {
$metaTemplateTabs = $this->Bootstrap->Tabs([
'pills' => true,
'card' => true,
'body-class' => ['p-1'],
'data' => $tabData
]);
}
echo $metaTemplateTabs;

View File

@ -25,74 +25,42 @@
* ],
* ],
* 'skip_meta_templates' => false // should the meta templates not be displayed
* 'combinedFieldsView' => false // should the default fields and meta fields displayed in a merged interface
* ]);
*
*/
$tableRandomValue = Cake\Utility\Security::randomString(8);
$listElements = '';
if (!empty($fields)) {
foreach ($fields as $field) {
if (empty($field['type'])) {
$field['type'] = 'generic';
}
$listElements .= sprintf(
"<tr class=\"row\">
<td class=\"col-sm-2 font-weight-bold\">%s</td>
<td class=\"col-sm-10\">%s</td>
</tr>",
h($field['key']),
$this->element(
'/genericElements/SingleViews/Fields/' . $field['type'] . 'Field',
['data' => $data, 'field' => $field]
)
);
}
$listTableOptions = [
'id' => "single-view-table-{$tableRandomValue}",
'hover' => false,
'tableClass' => 'col-sm-8',
'elementsRootPath' => '/genericElements/SingleViews/Fields/'
];
if (!empty($data['metaTemplates']) && (empty($skip_meta_templates)) && !empty($combinedFieldsView)) {
$listTableOptions['tableClass'] = '';
}
$metaTemplateTabs = '';
$listTable = $this->Bootstrap->listTable($listTableOptions,[
'item' => $entity,
'fields' => $fields
]);
$metafieldsPanel = '';
if (!empty($data['metaTemplates']) && (empty($skip_meta_templates))) {
$tabData = [
'navs' => [],
'content' => []
$metaFieldsData = [
'data' => $data,
];
foreach($data['metaTemplates'] as $metaTemplate) {
if (!empty($metaTemplate->meta_template_fields)) {
if ($metaTemplate->is_default) {
$tabData['navs'][] = [
'html' => $this->element('/genericElements/MetaTemplates/metaTemplateNav', ['metaTemplate' => $metaTemplate])
];
} else {
$tabData['navs'][] = [
'text' => $metaTemplate->name
];
}
$fieldsHtml = '<table class="table table-striped">';
foreach ($metaTemplate->meta_template_fields as $metaTemplateField) {
$metaField = $metaTemplateField->meta_fields[0];
$fieldsHtml .= sprintf(
'<tr class="row"><td class="col-sm-2 font-weight-bold">%s</td><td class="col-sm-10">%s</td></tr>',
h($metaField->field),
$this->element(
'/genericElements/SingleViews/Fields/genericField',
[
'data' => $metaField->value,
'field' => [
'raw' => $metaField->value
]
]
)
);
}
$fieldsHtml .= '</table>';
$tabData['content'][] = $fieldsHtml;
}
}
if (!empty($tabData['navs'])) {
$metaTemplateTabs = $this->Bootstrap->Tabs([
'pills' => true,
'card' => true,
'data' => $tabData
]);
if (!empty($combinedFieldsView)) {
$metaFieldsData['additionalTabs'] = [
'navs' => [
['text' => __('Default')]
],
'content' => [
$listTable
]
];
$listTable = '';
}
$metafieldsPanel = $this->element('/genericElements/SingleViews/metafields_panel', $metaFieldsData);
}
$ajaxLists = '';
if (!empty($children)) {
@ -113,19 +81,16 @@
"<div id=\"single-view-table-container-%s\">
<h2>%s</h2>
%s%s
<div class=\"px-3\">
<table id=\"single-view-table-%s\" class=\"table table-striped col-sm-8\">%s</table>
</div>
<div id=\"metaTemplates\" class=\"col-lg-8 px-0\">%s</div>
<div>%s</div>
<div id=\"metafieldsPanel\" class=\"col-lg-8 px-0\">%s</div>
<div id=\"accordion\">%s</div>
</div>",
$tableRandomValue,
h($title),
empty($description) ? '' : sprintf('<p>%s</p>', h($description)),
empty($description_html) ? '' : sprintf('<p>%s</p>', $description_html),
$tableRandomValue,
$listElements,
$metaTemplateTabs,
$listTable,
$metafieldsPanel,
$ajaxLists
);
?>