| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Ui_Element_Table extends HC3_Ui_Abstract_Collection |
| 3 |
{ |
| 4 |
protected $htmlFactory = NULL; |
| 5 |
protected $striped = TRUE; |
| 6 |
protected $bordered = FALSE; |
| 7 |
protected $segments = array(); |
| 8 |
protected $labelled = FALSE; |
| 9 |
|
| 10 |
protected $forceColWidth = null; |
| 11 |
protected $forceRowHeaderWidth = null; |
| 12 |
|
| 13 |
public function __construct( HC3_Ui $htmlFactory, $header = NULL, $rows = array(), $striped = TRUE ) |
| 14 |
{ |
| 15 |
$this->htmlFactory = $htmlFactory; |
| 16 |
$this->striped = $striped; |
| 17 |
|
| 18 |
$final_rows = array(); |
| 19 |
|
| 20 |
if( NULL === $header ){ |
| 21 |
if( isset($rows[0]) && is_array($rows[0]) ){ |
| 22 |
$header = $rows[0]; |
| 23 |
foreach( array_keys($header) as $k ){ |
| 24 |
$header[$k] = NULL; |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if( $header ){ |
| 30 |
$final_rows[] = $this->htmlFactory->makeCollection($header); |
| 31 |
} |
| 32 |
|
| 33 |
foreach( $rows as $row ){ |
| 34 |
$final_row = $this->htmlFactory->makeCollection($row); |
| 35 |
$final_rows[] = $final_row; |
| 36 |
} |
| 37 |
|
| 38 |
parent::__construct( $final_rows ); |
| 39 |
} |
| 40 |
|
| 41 |
public function forceColWidth( $width ) |
| 42 |
{ |
| 43 |
$this->forceColWidth = $width; |
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
public function forceRowHeaderWidth( $width ) |
| 48 |
{ |
| 49 |
$this->forceRowHeaderWidth = $width; |
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function setBordered( $set = TRUE ) |
| 54 |
{ |
| 55 |
$this->bordered = $set; |
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
public function setLabelled( $set = TRUE ) |
| 60 |
{ |
| 61 |
$this->labelled = $set; |
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function setSegments( array $set ) |
| 66 |
{ |
| 67 |
$this->segments = $set; |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function setStriped( $striped = TRUE ) |
| 72 |
{ |
| 73 |
$this->striped = $striped; |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
public function render() |
| 78 |
{ |
| 79 |
// header |
| 80 |
$show = array(); |
| 81 |
|
| 82 |
$rows = $this->getChildren(); |
| 83 |
if( ! $rows ) return; |
| 84 |
|
| 85 |
$header = current( $rows ); |
| 86 |
// $header = array_shift( $rows ); |
| 87 |
$header = $header->getChildren(); |
| 88 |
|
| 89 |
// if all null then we don't need header |
| 90 |
$show_header = FALSE; |
| 91 |
foreach( $header as $k => $hv ){ |
| 92 |
if( $hv !== NULL ){ |
| 93 |
$show_header = TRUE; |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if( $this->labelled ){ |
| 99 |
if( $this->forceColWidth ){ |
| 100 |
$width = $this->forceColWidth; |
| 101 |
$firstWidth = $this->forceRowHeaderWidth ? $this->forceRowHeaderWidth : $this->forceColWidth; |
| 102 |
} |
| 103 |
else { |
| 104 |
$width = 'x-' . ( count($header) - 1 ); |
| 105 |
$firstWidth = 'x-x'; |
| 106 |
$firstWidth = $this->forceRowHeaderWidth ? $this->forceRowHeaderWidth : $firstWidth; |
| 107 |
} |
| 108 |
} |
| 109 |
else { |
| 110 |
if( $this->forceColWidth ){ |
| 111 |
$width = $this->forceColWidth; |
| 112 |
$firstWidth = $this->forceRowHeaderWidth ? $this->forceRowHeaderWidth : $this->forceColWidth; |
| 113 |
} |
| 114 |
else { |
| 115 |
$width = '1-' . count($header); |
| 116 |
$firstWidth = $width; |
| 117 |
$firstWidth = $this->forceRowHeaderWidth ? $this->forceRowHeaderWidth : $firstWidth; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// rows |
| 122 |
$rri = 0; |
| 123 |
|
| 124 |
foreach( $rows as $rid => $row ){ |
| 125 |
$row = $row->getChildren(); |
| 126 |
|
| 127 |
$rri++; |
| 128 |
$row_cells = array(); |
| 129 |
|
| 130 |
$ii = 0; |
| 131 |
$grid = array(); |
| 132 |
reset( $header ); |
| 133 |
foreach( $header as $k => $hv ){ |
| 134 |
$v = array_key_exists($k, $row) ? $row[$k] : NULL; |
| 135 |
$cell = $this->htmlFactory->makeBlock( $v ); |
| 136 |
|
| 137 |
if( (! $ii) && $show_header ){ |
| 138 |
if( defined('WPINC') && is_admin() ){ |
| 139 |
$cell |
| 140 |
->addAttr('class', 'hc-bg-white') |
| 141 |
; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if( (0 == $rid) && $show_header ){ |
| 146 |
if( defined('WPINC') && is_admin() ){ |
| 147 |
$cell |
| 148 |
->addAttr('class', 'hc-bg-white') |
| 149 |
; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if( $this->gutter ){ |
| 154 |
$cell |
| 155 |
->addAttr('class', 'hc-p' . $this->gutter) |
| 156 |
->addAttr('class', 'hc-px' . $this->gutter . '-xs') |
| 157 |
; |
| 158 |
} |
| 159 |
$cell |
| 160 |
->addAttr('class', 'hc-py1-xs') |
| 161 |
; |
| 162 |
|
| 163 |
if( $hv ){ |
| 164 |
$cell_header = $this->htmlFactory->makeBlock($hv) |
| 165 |
->addAttr('class', 'hc-fs1') |
| 166 |
->addAttr('class', 'hc-muted2') |
| 167 |
->addAttr('class', 'hc-lg-hide') |
| 168 |
->addAttr('class', 'hc-p1-xs') |
| 169 |
; |
| 170 |
$cell = $this->htmlFactory->makeCollection( array($cell_header, $cell) ); |
| 171 |
} |
| 172 |
|
| 173 |
if( $ii ){ |
| 174 |
$grid[] = array( $cell, $width, 12 ); |
| 175 |
} |
| 176 |
else { |
| 177 |
$grid[] = array( $cell, $firstWidth, 12 ); |
| 178 |
} |
| 179 |
$ii++; |
| 180 |
} |
| 181 |
|
| 182 |
$tr = $this->htmlFactory->makeGrid( $grid )->gutter(0); |
| 183 |
if( $this->bordered ){ |
| 184 |
$tr->setBordered( $this->bordered ); |
| 185 |
} |
| 186 |
if( $this->segments ){ |
| 187 |
$tr->setSegments( $this->segments ); |
| 188 |
} |
| 189 |
|
| 190 |
if( $this->striped ){ |
| 191 |
$tr = $this->htmlFactory->makeBlock( $tr ); |
| 192 |
if( defined('WPINC') && is_admin() ){ |
| 193 |
if( $rri % 2 ){ |
| 194 |
$tr->addAttr('class', 'hc-bg-wpsilver'); |
| 195 |
} |
| 196 |
else { |
| 197 |
$tr->addAttr('class', 'hc-bg-white'); |
| 198 |
} |
| 199 |
} |
| 200 |
else { |
| 201 |
// if( $rri % 2 ){ |
| 202 |
// $tr->addAttr('class', 'hc-bg-lightsilver'); |
| 203 |
// } |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// header |
| 208 |
if( (0 == $rid) && $show_header ){ |
| 209 |
$tr |
| 210 |
// ->addAttr('class', 'hc-full-width') |
| 211 |
->addAttr('class', 'hc-table-header') |
| 212 |
// ->addAttr('class', 'hc-bg-white') |
| 213 |
->addAttr('class', 'hc-xs-hide') |
| 214 |
->addAttr('class', 'hc-fs4') |
| 215 |
->addAttr('style', 'line-height: 1.5em;') |
| 216 |
; |
| 217 |
|
| 218 |
if( defined('WPINC') && is_admin() ){ |
| 219 |
$tr |
| 220 |
->addAttr('class', 'hc-table-header-wpadmin') |
| 221 |
; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
$show[] = $tr; |
| 226 |
} |
| 227 |
|
| 228 |
if( $this->bordered ){ |
| 229 |
$out = $this->htmlFactory->makeCollection( $show ); |
| 230 |
$out = $this->htmlFactory->makeBlock( $out ) |
| 231 |
->addAttr('class', 'hc-table') |
| 232 |
// ->addAttr('style', 'border: red 1px solid; overflow: auto;') |
| 233 |
; |
| 234 |
} |
| 235 |
else { |
| 236 |
$out = $this->htmlFactory->makeList( $show )->gutter(0); |
| 237 |
$out = $this->htmlFactory->makeBlock( $out ) |
| 238 |
->addAttr('class', 'hc-border') |
| 239 |
; |
| 240 |
} |
| 241 |
|
| 242 |
if( $this->forceColWidth ){ |
| 243 |
// $out = $this->htmlFactory->makeBlock( $out ) |
| 244 |
// ->addAttr( 'style', 'overflow-x: auto;') |
| 245 |
// ; |
| 246 |
} |
| 247 |
|
| 248 |
return $out; |
| 249 |
} |
| 250 |
} |