| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Ui_Element_Grid extends HC3_Ui_Abstract_Collection |
| 3 |
{ |
| 4 |
protected $htmlFactory = NULL; |
| 5 |
protected $widths = array(); |
| 6 |
protected $bordered = FALSE; |
| 7 |
protected $segments = array(); |
| 8 |
|
| 9 |
protected $attr = array(); |
| 10 |
private $no_array = array('id', 'action', 'href', 'cols', 'rows'); |
| 11 |
|
| 12 |
public function __construct( HC3_Ui $htmlFactory, array $items = array(), $itemWidth = NULL ) |
| 13 |
{ |
| 14 |
$this->htmlFactory = $htmlFactory; |
| 15 |
|
| 16 |
$count = count($items); |
| 17 |
|
| 18 |
for( $i = 0; $i < $count; $i++ ){ |
| 19 |
if( ! is_array($items[$i]) ){ |
| 20 |
$items[$i] = array( $items[$i] ); |
| 21 |
} |
| 22 |
if( ! isset($items[$i][1]) ){ // desktop width |
| 23 |
$thisItemWidth = ( null == $itemWidth ) ? '1-' . $count : (12 / $itemWidth); |
| 24 |
$items[$i][1] = $thisItemWidth; |
| 25 |
} |
| 26 |
if( ! isset($items[$i][2]) ){ // mobile width |
| 27 |
$items[$i][2] = 12; |
| 28 |
} |
| 29 |
|
| 30 |
$this->add( $items[$i][0], $items[$i][1], $items[$i][2] ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function setSegments( array $set ) |
| 35 |
{ |
| 36 |
$this->segments = $set; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function setBordered( $set = TRUE ) |
| 41 |
{ |
| 42 |
$this->bordered = $set; |
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function add( $child, $width = 12, $mobile_width = 12, $offset = 0 ) |
| 47 |
{ |
| 48 |
parent::add( $child ); |
| 49 |
$this->widths[] = array( $width, $mobile_width, $offset ); |
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function render() |
| 54 |
{ |
| 55 |
$taken_width = 0; |
| 56 |
$taken_mobile_width = 0; |
| 57 |
$current_row = 0; |
| 58 |
$current_mobile_row = 0; |
| 59 |
|
| 60 |
$children = array(); |
| 61 |
$key = 0; |
| 62 |
|
| 63 |
// $this->segments = array( 8, 15, 22, 29 ); |
| 64 |
|
| 65 |
$ii = -1; |
| 66 |
foreach( $this->children as $child ){ |
| 67 |
$ii++; |
| 68 |
list( $width, $mobile_width, $offset ) = $this->widths[$key]; |
| 69 |
$key++; |
| 70 |
|
| 71 |
$classes = array(); |
| 72 |
if( $mobile_width == 12 ){ |
| 73 |
if( $this->gutter ){ |
| 74 |
$classes[] = 'hc-xs-mb' . $this->gutter; |
| 75 |
} |
| 76 |
} |
| 77 |
else { |
| 78 |
if( ! $this->bordered ){ |
| 79 |
$classes[] = 'hc-xs-col'; |
| 80 |
} |
| 81 |
|
| 82 |
if( (strpos($mobile_width, '%') === false) && (strpos($mobile_width, 'em') === false) ){ |
| 83 |
$classes[] = 'hc-xs-col-' . $mobile_width; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if( ! $this->bordered ){ |
| 88 |
$classes[] = 'hc-col'; |
| 89 |
} |
| 90 |
else { |
| 91 |
$classes[] = 'hc-table-cell'; |
| 92 |
$classes[] = 'hc-border'; |
| 93 |
if( $this->bordered !== TRUE ){ |
| 94 |
$classes[] = 'hc-border-'. $this->bordered; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if( (strpos($width, '%') === false) && (strpos($width, 'em') === false) ){ |
| 99 |
$widthClass = 'hc-col-' . $width; |
| 100 |
$classes[] = $widthClass; |
| 101 |
} |
| 102 |
|
| 103 |
if( $this->gutter ){ |
| 104 |
$classes[] = 'hc-px' . $this->gutter; |
| 105 |
} |
| 106 |
|
| 107 |
if( $this->gutter ){ |
| 108 |
if( $current_row > 0 ){ |
| 109 |
$classes[] = 'hc-mt' . $this->gutter; |
| 110 |
} |
| 111 |
if( $current_mobile_row > 0 ){ |
| 112 |
$classes[] = 'hc-xs-mt' . $this->gutter; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$slot = $this->htmlFactory->makeBlock( $child ); |
| 117 |
foreach( $classes as $class ){ |
| 118 |
$slot->addAttr('class', $class); |
| 119 |
} |
| 120 |
|
| 121 |
// more border, for weeks for example |
| 122 |
if( in_array($key, $this->segments) ){ |
| 123 |
$slot |
| 124 |
->addAttr('class', 'hc-lg-prominent-border-left') |
| 125 |
// ->addAttr('style', 'border-left: gray 2px solid;') |
| 126 |
; |
| 127 |
} |
| 128 |
|
| 129 |
if( (strpos($width, '%') !== false) OR (strpos($width, 'em') !== false) ){ |
| 130 |
$slot |
| 131 |
->addAttr('style', 'width: ' . $width . ';') |
| 132 |
// ->addAttr('style', 'border-left: gray 2px solid;') |
| 133 |
; |
| 134 |
} |
| 135 |
|
| 136 |
if( $offset ){ |
| 137 |
$slot |
| 138 |
->addAttr('style', 'margin-left: ' . $offset . ';') |
| 139 |
; |
| 140 |
} |
| 141 |
|
| 142 |
if( strpos($width, 'em') !== false ){ |
| 143 |
if( ! $ii ){ |
| 144 |
$slot |
| 145 |
->addAttr('class', 'hc-table-row-header') |
| 146 |
; |
| 147 |
; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$children[] = $slot; |
| 152 |
|
| 153 |
if( (strpos($width, '-') === FALSE) && (strpos($width, '%') === false) && (strpos($width, 'em') === false) ){ |
| 154 |
$taken_width += $width; |
| 155 |
if( $taken_width >= 12 ){ |
| 156 |
$current_row++; |
| 157 |
$taken_width = 0; |
| 158 |
|
| 159 |
$sep = $this->htmlFactory->makeBlock() |
| 160 |
->addAttr('class', 'hc-clearfix') |
| 161 |
; |
| 162 |
$children[] = $sep; |
| 163 |
} |
| 164 |
|
| 165 |
if( $mobile_width < 12 ){ |
| 166 |
$taken_mobile_width += $mobile_width; |
| 167 |
if( $taken_mobile_width >= 12 ){ |
| 168 |
$current_mobile_row++; |
| 169 |
$taken_mobile_width = 0; |
| 170 |
|
| 171 |
$sep = $this->htmlFactory->makeBlock() |
| 172 |
->addAttr('class', 'hc-xs-clearfix') |
| 173 |
; |
| 174 |
$children[] = $sep; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
$out = $this->htmlFactory->makeCollection( $children ); |
| 181 |
$out = $this->htmlFactory->makeBlock($out) |
| 182 |
->addAttr('class', 'hc-clearfix') |
| 183 |
; |
| 184 |
|
| 185 |
if( $this->gutter ){ |
| 186 |
$out->addAttr('class', 'hc-mxn' . $this->gutter); |
| 187 |
} |
| 188 |
|
| 189 |
if( $this->bordered ){ |
| 190 |
$out |
| 191 |
->addAttr('class', 'hc-table-row') |
| 192 |
; |
| 193 |
} |
| 194 |
|
| 195 |
foreach( $this->attr as $k => $v ){ |
| 196 |
$out->addAttr( $k, $v ); |
| 197 |
} |
| 198 |
|
| 199 |
return $out; |
| 200 |
} |
| 201 |
|
| 202 |
public function setAttr( $key, $value ) |
| 203 |
{ |
| 204 |
unset( $this->attr[$key] ); |
| 205 |
return $this->addAttr( $key, $value ); |
| 206 |
} |
| 207 |
|
| 208 |
public function addAttr( $key, $value, $escape = TRUE ) |
| 209 |
{ |
| 210 |
if( is_array($value) ){ |
| 211 |
foreach( $value as $v ){ |
| 212 |
$this->addAttr( $key, $v ); |
| 213 |
} |
| 214 |
return $this; |
| 215 |
} |
| 216 |
|
| 217 |
switch( $key ){ |
| 218 |
case 'title': |
| 219 |
if( is_string($value) ){ |
| 220 |
$value = strip_tags($value); |
| 221 |
$value = trim($value); |
| 222 |
} |
| 223 |
break; |
| 224 |
|
| 225 |
case 'class': |
| 226 |
if( isset($this->attr[$key]) && in_array($value, $this->attr[$key]) ){ |
| 227 |
return $this; |
| 228 |
} |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
if( $value === NULL ){ |
| 233 |
return $this; |
| 234 |
} |
| 235 |
|
| 236 |
if( ! in_array($key, $this->no_array) ){ |
| 237 |
if( ! is_array($value) ) |
| 238 |
$value = array( $value ); |
| 239 |
} |
| 240 |
|
| 241 |
if( $escape && in_array($key, array('alt', 'value', 'title')) ){ |
| 242 |
for( $ii = 0; $ii < count($value); $ii++ ){ |
| 243 |
$value[$ii] = $this->esc_attr( $value[$ii] ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if( in_array($key, $this->no_array) ){ |
| 248 |
$this->attr[$key] = $value; |
| 249 |
} |
| 250 |
else { |
| 251 |
if( isset($this->attr[$key]) ){ |
| 252 |
$this->attr[$key] = array_merge( $this->attr[$key], $value ); |
| 253 |
} |
| 254 |
else { |
| 255 |
$this->attr[$key] = $value; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return $this; |
| 260 |
} |
| 261 |
|
| 262 |
public function esc_attr( $value ) |
| 263 |
{ |
| 264 |
if( function_exists('esc_attr') ){ |
| 265 |
return esc_attr( $value ); |
| 266 |
} |
| 267 |
else { |
| 268 |
$return = htmlspecialchars( $value ); |
| 269 |
return $return; |
| 270 |
} |
| 271 |
} |
| 272 |
} |