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