PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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 / abstract / element.php

element.php in ShiftController Employee Shift Scheduling 4.9.26, at hc3/ui/abstract/element.php

226 lines 4.2 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 abstract class HC3_Ui_Abstract_Element
3 {
4 protected $el = 'input';
5 protected $attr = array();
6 private $no_array = array('id', 'action', 'href', 'cols', 'rows');
7
8 protected $uiType = NULL;
9 protected $tags = array();
10 protected $content = NULL;
11
12 public function __construct( $el = NULL, $content = NULL )
13 {
14 $this->el = $el;
15 $this->content = $content;
16 }
17
18 public function __toString()
19 {
20 return '' . $this->render();
21 }
22
23 public function tag( $tag, $details = NULL )
24 {
25 $details = ($details === NULL) ? $tag : $details;
26 $this->tags[ $tag ] = $details;
27 return $this;
28 }
29
30 public function getTags()
31 {
32 return $this->tags;
33 }
34
35 public function getUiType()
36 {
37 return $this->uiType;
38 }
39
40 public function getChildren()
41 {
42 $return = array( $this->content );
43 return $return;
44 }
45
46 public function getContent()
47 {
48 return $this->content;
49 }
50
51 public function setChild( $key, $child )
52 {
53 $this->content = $child;
54 return $this;
55 }
56
57 public function margin( $margin )
58 {
59 $this->addAttr('class', 'hc-m' . $margin);
60 return $this;
61 }
62
63 public function marginX( $margin )
64 {
65 $this->addAttr('class', 'hc-mx' . $margin);
66 return $this;
67 }
68
69 public function marginY( $margin )
70 {
71 $this->addAttr('class', 'hc-my' . $margin);
72 return $this;
73 }
74
75 public function padding( $padding )
76 {
77 $this->addAttr('class', 'hc-p' . $padding);
78 return $this;
79 }
80
81 public function paddingX( $padding )
82 {
83 $this->addAttr('class', 'hc-px' . $padding);
84 return $this;
85 }
86
87 public function paddingY( $padding )
88 {
89 $this->addAttr('class', 'hc-py' . $padding);
90 return $this;
91 }
92
93 public function render()
94 {
95 $el = $this->el;
96
97 $return = '';
98 $add_newline = FALSE;
99
100 if( $el !== NULL ){
101 $return .= '<' . $el;
102
103 if( in_array($el, array('script', 'meta', 'link', 'head', 'body')) ){
104 $add_newline = TRUE;
105 }
106
107 $attr = $this->getAttr();
108 foreach( $attr as $key => $val ){
109 if( is_array($val) ){
110 $val = join(' ', $val);
111 }
112
113 if( strlen($val) OR ( substr($key, 0, strlen('data-')) == 'data-') ){
114 $return .= ' ' . esc_attr($key) . '="' . esc_attr($val) . '"';
115 }
116 }
117 }
118
119 if( in_array($el, array('br', 'input', 'link', 'meta')) ){
120 $return .= '/>';
121 }
122 else {
123 if( $el !== NULL ){
124 $return .= '>';
125 }
126
127 $return .= $this->content;
128
129 if( $el !== NULL ){
130 $return .= '</' . $el . '>';
131 }
132 }
133
134 if( $add_newline ){
135 $return .= "\n";
136 }
137
138 return $return;
139 }
140
141 // attribute related functions
142 public function getAttr( $key = NULL )
143 {
144 if( $key === NULL ){
145 $return = $this->attr;
146 }
147 elseif( isset($this->attr[$key]) ){
148 $return = $this->attr[$key];
149 }
150 else {
151 $return = array();
152 }
153 return $return;
154 }
155
156 public function setAttr( $key, $value )
157 {
158 unset( $this->attr[$key] );
159 return $this->addAttr( $key, $value );
160 }
161
162 public function addAttr( $key, $value, $escape = TRUE )
163 {
164 if( is_array($value) ){
165 foreach( $value as $v ){
166 $this->addAttr( $key, $v );
167 }
168 return $this;
169 }
170
171 switch( $key ){
172 case 'title':
173 if( is_string($value) ){
174 $value = strip_tags($value);
175 $value = trim($value);
176 }
177 break;
178
179 case 'class':
180 if( isset($this->attr[$key]) && in_array($value, $this->attr[$key]) ){
181 return $this;
182 }
183 break;
184 }
185
186 if( $value === NULL ){
187 return $this;
188 }
189
190 if( ! in_array($key, $this->no_array) ){
191 if( ! is_array($value) )
192 $value = array( $value );
193 }
194
195 if( $escape && in_array($key, array('alt', 'value', 'title')) ){
196 for( $ii = 0; $ii < count($value); $ii++ ){
197 $value[$ii] = $this->esc_attr( $value[$ii] );
198 }
199 }
200
201 if( in_array($key, $this->no_array) ){
202 $this->attr[$key] = $value;
203 }
204 else {
205 if( isset($this->attr[$key]) ){
206 $this->attr[$key] = array_merge( $this->attr[$key], $value );
207 }
208 else {
209 $this->attr[$key] = $value;
210 }
211 }
212
213 return $this;
214 }
215
216 public function esc_attr( $value )
217 {
218 if( function_exists('esc_attr') ){
219 return esc_attr( $value );
220 }
221 else {
222 $return = htmlspecialchars( $value );
223 return $return;
224 }
225 }
226 }