|
@@ -0,0 +1,159 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+require_once dirname(__FILE__) . '/' . 'Node.php';
|
|
|
|
|
+
|
|
|
|
|
+class UI_Table extends UI_Node { // TODO:??? implements UITagInterface {
|
|
|
|
|
+
|
|
|
|
|
+ public $tagName, $props, $childrens;
|
|
|
|
|
+
|
|
|
|
|
+ function __construct($tagName = null, $props = null, $childrens = null) {
|
|
|
|
|
+ // parent::__construct($tagName, $props, $childrens);
|
|
|
|
|
+ if (!empty($childrens)) throw new Exception("Childrens not allowed for Node 'p5:Table'");
|
|
|
|
|
+
|
|
|
|
|
+ $this->tagName = 'table';
|
|
|
|
|
+ $this->props = [
|
|
|
|
|
+ 'class' => 'table table-bordered table-hover',
|
|
|
|
|
+ ];
|
|
|
|
|
+ $args = [ // default values
|
|
|
|
|
+ 'empty_msg' => 'Brak danych',
|
|
|
|
|
+ 'disable_lp' => false,
|
|
|
|
|
+ 'cell_padding' => 2,
|
|
|
|
|
+ 'cols_help' => [],
|
|
|
|
|
+ 'hidden_cols' => [],
|
|
|
|
|
+ 'tbody_attributes' => [],
|
|
|
|
|
+ 'list_col_class' => [], // [ colName => class ] from $props['@class[{$colName}]']
|
|
|
|
|
+ 'list_col_style' => [], // [ colName => style ] from $props['@style[{$colName}]']
|
|
|
|
|
+ ];
|
|
|
|
|
+ foreach ($props as $key => $value) {
|
|
|
|
|
+ switch ($key) {
|
|
|
|
|
+ case 'cols': $args['cols'] = $value; break;
|
|
|
|
|
+ case 'rows': $args['rows'] = $value; break;
|
|
|
|
|
+ case 'cols_help': $args['cols_help'] = $value; break;
|
|
|
|
|
+ case 'cols_label': $args['cols_label'] = $value; break;
|
|
|
|
|
+ case 'caption': $args['caption'] = $value; break;
|
|
|
|
|
+ case 'cell_padding': $args['cell_padding'] = (int)$value; break;
|
|
|
|
|
+ case 'disable_lp': $args['disable_lp'] = (bool)$value; break;
|
|
|
|
|
+ case 'hidden_cols': $args['hidden_cols'] = $value; break;
|
|
|
|
|
+ case 'empty_msg': $args['empty_msg'] = $value; break;
|
|
|
|
|
+ case '@tbody.id': $args['tbody_attributes']['id'] = $value; break;
|
|
|
|
|
+ case '@class': {
|
|
|
|
|
+ $this->props['class'] = $value;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case '__html_id': {
|
|
|
|
|
+ $this->props['id'] = $value;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ default: {
|
|
|
|
|
+ if ('@class[' === substr($key, 0, strlen('@class['))) {
|
|
|
|
|
+ $colName = substr($key, strlen('@class['), -1);
|
|
|
|
|
+ $args['list_col_class'][$colName] = $value;
|
|
|
|
|
+ } else if ('@style[' === substr($key, 0, strlen('@style['))) {
|
|
|
|
|
+ $colName = substr($key, strlen('@style['), -1);
|
|
|
|
|
+ $args['list_col_style'][$colName] = $value;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->props[$key] = $value;
|
|
|
|
|
+ }
|
|
|
|
|
+ } break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->childrens = null;
|
|
|
|
|
+
|
|
|
|
|
+ $countCols = 1;
|
|
|
|
|
+ if (empty($args['cols']) && !empty($args['rows'])) {
|
|
|
|
|
+ $firstRow = array();
|
|
|
|
|
+ foreach ($args['rows'] as $row) {
|
|
|
|
|
+ $firstRow = $row;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ $args['cols'] = array_filter(
|
|
|
|
|
+ array_keys((array)$firstRow),
|
|
|
|
|
+ function ($col) {
|
|
|
|
|
+ return ('@' != substr($col, 0, 1));
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ $countCols = count($args['cols']);
|
|
|
|
|
+ $countCols = ($args['disable_lp']) ? $countCols : $countCols + 1;
|
|
|
|
|
+ { // $help
|
|
|
|
|
+ $help = array();
|
|
|
|
|
+ foreach ($args['cols'] as $name) {
|
|
|
|
|
+ $helpMsg = V::get($name, '', $args['cols_help']);
|
|
|
|
|
+ if (empty($helpMsg)) continue;
|
|
|
|
|
+ $help[$name] = UI::node('i', [
|
|
|
|
|
+ 'class' => "glyphicon glyphicon-question-sign",
|
|
|
|
|
+ 'title' => $helpMsg
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ { // $label
|
|
|
|
|
+ $label = array();
|
|
|
|
|
+ foreach ($args['cols'] as $name) {
|
|
|
|
|
+ $label[$name] = V::get($name, $name, $args['cols_label']);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($args['caption'])) $this->childrens[] = UI::node('caption', [], $args['caption']);
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($args['cols'])) {
|
|
|
|
|
+ $this->childrens[] = UI::node('thead', null, [
|
|
|
|
|
+ UI::node('tr', null, array_merge(
|
|
|
|
|
+ $args['disable_lp']
|
|
|
|
|
+ ? []
|
|
|
|
|
+ : [ UI::node('th', [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ] ], "Lp.") ]
|
|
|
|
|
+ ,
|
|
|
|
|
+ array_map(function ($colName) use ($args, $label, $help) {
|
|
|
|
|
+ if (in_array($colName, $args['hidden_cols'])) return null;
|
|
|
|
|
+
|
|
|
|
|
+ return UI::node('th', [
|
|
|
|
|
+ 'class' => (empty($args['list_col_class'][$colName])) ? '' : $args['list_col_class'][$colName],
|
|
|
|
|
+ 'style' => "padding:{$args['cell_padding']}px" . (empty($args['list_col_style'][$colName]) ? '' : ";{$args['list_col_style'][$colName]}"),
|
|
|
|
|
+ // 'style' => array_merge([ 'padding' => "{$args['cell_padding']}px" ],
|
|
|
|
|
+ // empty($args['list_col_style'][$colName]) ? [] : $args['list_col_style'][$colName] // TODO: fix style to Array
|
|
|
|
|
+ // )
|
|
|
|
|
+ ], [
|
|
|
|
|
+ $label[$colName],
|
|
|
|
|
+ (!empty($help[$colName])) ? ' ' . $help[$colName] : '',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }, $args['cols'])
|
|
|
|
|
+ )),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->childrens[] = UI::node('tbody', $args['tbody_attributes'],
|
|
|
|
|
+ (empty($args['rows']))
|
|
|
|
|
+ ? [
|
|
|
|
|
+ UI::node('tr', [], [
|
|
|
|
|
+ UI::node('td', [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ], 'colspan' => $countCols ], $args['empty_msg']),
|
|
|
|
|
+ ]),
|
|
|
|
|
+ ]
|
|
|
|
|
+ : array_map(function ($row, $lp) use ($args) {
|
|
|
|
|
+ $trAttrs = array();
|
|
|
|
|
+ if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
|
|
|
|
|
+ if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
|
|
|
|
|
+ if (!empty($row['@style'])) $trAttrs['style'] = $row['@style']; // TODO: fix style to Array
|
|
|
|
|
+ if (!empty($row['@data'])) foreach ($row['@data'] as $k => $v) $trAttrs["data-{$k}"] = $v;
|
|
|
|
|
+
|
|
|
|
|
+ return UI::node('tr', $trAttrs, array_merge(
|
|
|
|
|
+ ($args['disable_lp'])
|
|
|
|
|
+ ? []
|
|
|
|
|
+ : [
|
|
|
|
|
+ UI::node('th', [ 'style' => [ 'padding' => "{$args['cell_padding']}px", 'color' => "#ccc" ] ], $lp),
|
|
|
|
|
+ ]
|
|
|
|
|
+ ,
|
|
|
|
|
+ array_map(function ($colName) use ($row, $args) {
|
|
|
|
|
+ if (in_array($colName, $args['hidden_cols'])) return null;
|
|
|
|
|
+
|
|
|
|
|
+ $rowAttrs = [ 'style' => "padding:{$args['cell_padding']}px" ];
|
|
|
|
|
+ // $rowAttrs = [ 'style' => [ 'padding' => "{$args['cell_padding']}px" ] ];
|
|
|
|
|
+ if (!empty($row["@onClick[{$colName}]"])) $rowAttrs['onClick'] = $row["@onClick[{$colName}]"];
|
|
|
|
|
+ if (!empty($row["@class[{$colName}]"])) $rowAttrs['class'] = $row["@class[{$colName}]"];
|
|
|
|
|
+ if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] .= "; " . $row["@style[{$colName}]"];
|
|
|
|
|
+ // if (!empty($row["@style[{$colName}]"])) $rowAttrs['style'] = array_merge($rowAttrs['style'], $row["@style[{$colName}"]); // TODO: fix style to Array
|
|
|
|
|
+
|
|
|
|
|
+ return UI::node('td', $rowAttrs, V::get($colName, '', $row));
|
|
|
|
|
+ }, $args['cols'])
|
|
|
|
|
+ ));
|
|
|
|
|
+ }, $args['rows'], range(1, count($args['rows'])))
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|