Table.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. require_once dirname(__FILE__) . '/' . 'Node.php';
  3. class UI_Table extends UI_Node { // TODO:??? implements UITagInterface {
  4. public $tagName, $props, $childrens;
  5. function __construct($tagName = null, $props = null, $childrens = null) {
  6. // parent::__construct($tagName, $props, $childrens);
  7. if (!empty($childrens)) throw new Exception("Childrens not allowed for Node 'p5:Table'");
  8. $this->tagName = 'table';
  9. $this->props = [
  10. 'class' => 'table table-bordered table-hover',
  11. ];
  12. $args = [ // default values
  13. 'empty_msg' => 'Brak danych',
  14. 'disable_lp' => false,
  15. 'cell_padding' => 2,
  16. 'cols_help' => [],
  17. 'hidden_cols' => [],
  18. 'tbody_attributes' => [],
  19. 'list_col_class' => [], // [ colName => class ] from $props['@class[{$colName}]']
  20. 'list_col_style' => [], // [ colName => style ] from $props['@style[{$colName}]']
  21. ];
  22. foreach ($props as $key => $value) {
  23. switch ($key) {
  24. case 'cols': $args['cols'] = $value; break;
  25. case 'rows': $args['rows'] = $value; break;
  26. case 'cols_help': $args['cols_help'] = $value; break;
  27. case 'cols_label': $args['cols_label'] = $value; break;
  28. case 'caption': $args['caption'] = $value; break;
  29. case 'cell_padding': $args['cell_padding'] = (int)$value; break;
  30. case 'disable_lp': $args['disable_lp'] = (bool)$value; break;
  31. case 'hidden_cols': $args['hidden_cols'] = $value; break;
  32. case 'empty_msg': $args['empty_msg'] = $value; break;
  33. case '@tbody.id': $args['tbody_attributes']['id'] = $value; break;
  34. case '@class': {
  35. $this->props['class'] = $value;
  36. } break;
  37. case '__html_id': {
  38. $this->props['id'] = $value;
  39. } break;
  40. default: {
  41. if ('@class[' === substr($key, 0, strlen('@class['))) {
  42. $colName = substr($key, strlen('@class['), -1);
  43. $args['list_col_class'][$colName] = $value;
  44. } else if ('@style[' === substr($key, 0, strlen('@style['))) {
  45. $colName = substr($key, strlen('@style['), -1);
  46. $args['list_col_style'][$colName] = $value;
  47. } else {
  48. $this->props[$key] = $value;
  49. }
  50. } break;
  51. }
  52. }
  53. $this->childrens = null;
  54. $countCols = 1;
  55. if (empty($args['cols']) && !empty($args['rows'])) {
  56. $firstRow = array();
  57. foreach ($args['rows'] as $row) {
  58. $firstRow = $row;
  59. break;
  60. }
  61. $args['cols'] = array_filter(
  62. array_keys((array)$firstRow),
  63. function ($col) {
  64. return ('@' != substr($col, 0, 1));
  65. }
  66. );
  67. }
  68. $countCols = count($args['cols']);
  69. $countCols = ($args['disable_lp']) ? $countCols : $countCols + 1;
  70. { // $help
  71. $help = array();
  72. foreach ($args['cols'] as $name) {
  73. $helpMsg = V::get($name, '', $args['cols_help']);
  74. if (empty($helpMsg)) continue;
  75. $help[$name] = UI::node('i', [
  76. 'class' => "glyphicon glyphicon-question-sign",
  77. 'title' => $helpMsg
  78. ]);
  79. }
  80. }
  81. { // $label
  82. $label = array();
  83. foreach ($args['cols'] as $name) {
  84. $label[$name] = V::get($name, $name, $args['cols_label']);
  85. }
  86. }
  87. if (!empty($args['caption'])) $this->childrens[] = UI::node('caption', [], $args['caption']);
  88. if (!empty($args['cols'])) {
  89. $this->childrens[] = UI::node('thead', null, [
  90. UI::node('tr', null, array_merge(
  91. $args['disable_lp']
  92. ? []
  93. : [ UI::node('th', [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ] ], "Lp.") ]
  94. ,
  95. array_map(function ($colName) use ($args, $label, $help) {
  96. if (in_array($colName, $args['hidden_cols'])) return null;
  97. return UI::node('th', [
  98. 'class' => (empty($args['list_col_class'][$colName])) ? '' : $args['list_col_class'][$colName],
  99. 'style' => "padding:{$args['cell_padding']}px" . (empty($args['list_col_style'][$colName]) ? '' : ";{$args['list_col_style'][$colName]}"),
  100. // 'style' => array_merge([ 'padding' => "{$args['cell_padding']}px" ],
  101. // empty($args['list_col_style'][$colName]) ? [] : $args['list_col_style'][$colName] // TODO: fix style to Array
  102. // )
  103. ], [
  104. $label[$colName],
  105. (!empty($help[$colName])) ? ' ' . $help[$colName] : '',
  106. ]);
  107. }, $args['cols'])
  108. )),
  109. ]);
  110. }
  111. $this->childrens[] = UI::node('tbody', $args['tbody_attributes'],
  112. (empty($args['rows']))
  113. ? [
  114. UI::node('tr', [], [
  115. UI::node('td', [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ], 'colspan' => $countCols ], $args['empty_msg']),
  116. ]),
  117. ]
  118. : array_map(function ($row, $lp) use ($args) {
  119. $trAttrs = array();
  120. if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
  121. if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
  122. if (!empty($row['@style'])) $trAttrs['style'] = $row['@style']; // TODO: fix style to Array
  123. if (!empty($row['@data'])) foreach ($row['@data'] as $k => $v) $trAttrs["data-{$k}"] = $v;
  124. return UI::node('tr', $trAttrs, array_merge(
  125. ($args['disable_lp'])
  126. ? []
  127. : [
  128. UI::node('th', [ 'style' => [ 'padding' => "{$args['cell_padding']}px", 'color' => "#ccc" ] ], $lp),
  129. ]
  130. ,
  131. array_map(function ($colName) use ($row, $args) {
  132. if (in_array($colName, $args['hidden_cols'])) return null;
  133. $rowAttrs = [ 'style' => "padding:{$args['cell_padding']}px" ];
  134. // $rowAttrs = [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ] ];
  135. if (!empty($row["@onClick[{$colName}]"])) $rowAttrs['onClick'] = $row["@onClick[{$colName}]"];
  136. if (!empty($row["@class[{$colName}]"])) $rowAttrs['class'] = $row["@class[{$colName}]"];
  137. if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] .= "; " . $row["@style[{$colName}]"];
  138. // if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] = array_merge($rowAttrs['style'], $row["@style[{$colName}"]); // TODO: fix style to Array
  139. return UI::node('td', $rowAttrs, V::get($colName, '', $row));
  140. }, $args['cols'])
  141. ));
  142. }, $args['rows'], range(1, count($args['rows'])))
  143. );
  144. }
  145. }