PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / ui / element / grid.php

grid.php in ShiftController Employee Shift Scheduling 4.9.24, at hc3/ui/element/grid.php

261 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 foreach( $this->children as $child ){
66 list( $width, $mobile_width, $offset ) = $this->widths[$key];
67 $key++;
68
69 $classes = array();
70 if( $mobile_width == 12 ){
71 if( $this->gutter ){
72 $classes[] = 'hc-xs-mb' . $this->gutter;
73 }
74 }
75 else {
76 if( ! $this->bordered ){
77 $classes[] = 'hc-xs-col';
78 }
79
80 if( strpos($mobile_width, '%') === FALSE ){
81 $classes[] = 'hc-xs-col-' . $mobile_width;
82 }
83 }
84
85 if( ! $this->bordered ){
86 $classes[] = 'hc-col';
87 }
88 else {
89 $classes[] = 'hc-table-cell';
90 $classes[] = 'hc-border';
91 if( $this->bordered !== TRUE ){
92 $classes[] = 'hc-border-'. $this->bordered;
93 }
94 }
95
96 if( strpos($width, '%') === FALSE ){
97 $widthClass = 'hc-col-' . $width;
98 $classes[] = $widthClass;
99 }
100
101 if( $this->gutter ){
102 $classes[] = 'hc-px' . $this->gutter;
103 }
104
105 if( $this->gutter ){
106 if( $current_row > 0 ){
107 $classes[] = 'hc-mt' . $this->gutter;
108 }
109 if( $current_mobile_row > 0 ){
110 $classes[] = 'hc-xs-mt' . $this->gutter;
111 }
112 }
113
114 $slot = $this->htmlFactory->makeBlock( $child );
115 foreach( $classes as $class ){
116 $slot->addAttr('class', $class);
117 }
118
119 // more border, for weeks for example
120 if( in_array($key, $this->segments) ){
121 $slot
122 ->addAttr('class', 'hc-lg-prominent-border-left')
123 // ->addAttr('style', 'border-left: gray 2px solid;')
124 ;
125 }
126
127 if( strpos($width, '%') !== FALSE ){
128 $slot
129 ->addAttr('style', 'width: ' . $width . ';')
130 // ->addAttr('style', 'border-left: gray 2px solid;')
131 ;
132 }
133
134 if( $offset ){
135 $slot
136 ->addAttr('style', 'margin-left: ' . $offset . ';')
137 ;
138 }
139
140 $children[] = $slot;
141
142 if( (strpos($width, '-') === FALSE) && (strpos($width, '%') === FALSE) ){
143 $taken_width += $width;
144 if( $taken_width >= 12 ){
145 $current_row++;
146 $taken_width = 0;
147
148 $sep = $this->htmlFactory->makeBlock()
149 ->addAttr('class', 'hc-clearfix')
150 ;
151 $children[] = $sep;
152 }
153
154 if( $mobile_width < 12 ){
155 $taken_mobile_width += $mobile_width;
156 if( $taken_mobile_width >= 12 ){
157 $current_mobile_row++;
158 $taken_mobile_width = 0;
159
160 $sep = $this->htmlFactory->makeBlock()
161 ->addAttr('class', 'hc-xs-clearfix')
162 ;
163 $children[] = $sep;
164 }
165 }
166 }
167 }
168
169 $out = $this->htmlFactory->makeCollection( $children );
170 $out = $this->htmlFactory->makeBlock($out)
171 ->addAttr('class', 'hc-clearfix')
172 ;
173
174 if( $this->gutter ){
175 $out->addAttr('class', 'hc-mxn' . $this->gutter);
176 }
177
178 if( $this->bordered ){
179 $out
180 ->addAttr('class', 'hc-table-row')
181 ;
182 }
183
184 foreach( $this->attr as $k => $v ){
185 $out->addAttr( $k, $v );
186 }
187
188 return $out;
189 }
190
191 public function setAttr( $key, $value )
192 {
193 unset( $this->attr[$key] );
194 return $this->addAttr( $key, $value );
195 }
196
197 public function addAttr( $key, $value, $escape = TRUE )
198 {
199 if( is_array($value) ){
200 foreach( $value as $v ){
201 $this->addAttr( $key, $v );
202 }
203 return $this;
204 }
205
206 switch( $key ){
207 case 'title':
208 if( is_string($value) ){
209 $value = strip_tags($value);
210 $value = trim($value);
211 }
212 break;
213
214 case 'class':
215 if( isset($this->attr[$key]) && in_array($value, $this->attr[$key]) ){
216 return $this;
217 }
218 break;
219 }
220
221 if( $value === NULL ){
222 return $this;
223 }
224
225 if( ! in_array($key, $this->no_array) ){
226 if( ! is_array($value) )
227 $value = array( $value );
228 }
229
230 if( $escape && in_array($key, array('alt', 'value', 'title')) ){
231 for( $ii = 0; $ii < count($value); $ii++ ){
232 $value[$ii] = $this->esc_attr( $value[$ii] );
233 }
234 }
235
236 if( in_array($key, $this->no_array) ){
237 $this->attr[$key] = $value;
238 }
239 else {
240 if( isset($this->attr[$key]) ){
241 $this->attr[$key] = array_merge( $this->attr[$key], $value );
242 }
243 else {
244 $this->attr[$key] = $value;
245 }
246 }
247
248 return $this;
249 }
250
251 public function esc_attr( $value )
252 {
253 if( function_exists('esc_attr') ){
254 return esc_attr( $value );
255 }
256 else {
257 $return = htmlspecialchars( $value );
258 return $return;
259 }
260 }
261 }