1 <?php
2 3 4 5 6 7 8 9 10
11 namespace uix\ui;
12
13 14 15 16 17
18 class control extends \uix\data\data{
19
20 21 22 23 24 25 26
27 public $type = 'control';
28
29 30 31 32 33 34 35 36 37
38 public static function register( $slug, $object, $parent = null ) {
39
40 $caller = get_called_class();
41
42 if( empty( $object['type'] ) || !uix()->is_callable( 'control\\' . $object['type'] ) )
43 $object['type'] = 'text';
44
45 $caller = $caller . '\\' . $object['type'];
46 return new $caller( $slug, $object, $parent );
47
48 }
49
50 51 52 53 54 55 56
57 public function setup() {
58
59
60 parent::setup();
61
62 $value = array( $this->slug, '' );
63
64 $data = uix()->request_vars( 'post' );
65
66 if( !empty( $this->struct['value'] ) )
67 $value[ $this->slug ] = $this->struct['value'];
68
69 if( isset( $data[ $this->id() ] ) ){
70 $value[$this->slug] = $data[$this->id()];
71 }
72
73 $this->set_data( $value );
74
75
76 $this->attributes['name'] = $this->name();
77 $this->attributes['id'] = $this->id() . '-control';
78
79 }
80
81 82 83 84 85
86 public function set_config() {
87
88 foreach( $this->struct['config'] as $key=>$setting )
89 $this->attributes['data-' . $key ] = $setting;
90
91 }
92
93
94
95 96 97 98 99 100 101
102 public function set_attributes() {
103
104 if( !empty( $this->struct['config'] ) )
105 $this->set_config();
106
107 $this->attributes['class'] = implode( ' ', $this->classes() );
108
109 parent::set_attributes();
110
111 }
112
113 114 115 116 117 118
119 public function set_assets() {
120 $this->assets['style']['controls'] = $this->url . 'assets/css/control' . UIX_ASSET_DEBUG . '.css';
121 parent::set_assets();
122 }
123
124 125 126 127 128 129 130
131 public function name(){
132 return $this->id();
133 }
134
135 136 137 138 139 140 141
142 public function get_value(){
143 $value = null;
144 $data = $this->get_data();
145
146 if( null !== $data )
147 $value = $data[ $this->slug ];
148
149 return $value;
150 }
151
152
153 154 155 156 157 158 159
160 public function classes() {
161
162 return array(
163 'widefat'
164 );
165
166 }
167
168
169 170 171 172 173 174 175 176
177 public function input(){
178
179 return '<input type="' . esc_attr( $this->type ) . '" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
180 }
181
182 183 184 185 186 187 188
189 public function label(){
190 $output = null;
191 if( isset( $this->struct['label'] ) )
192 $output .= '<label for="' . esc_attr( $this->id() ) . '-control"><span class="uix-control-label">' . esc_html( $this->struct['label'] ) . '</span></label>';
193
194 return $output;
195 }
196
197
198 199 200 201 202 203 204
205 public function description(){
206 $output = null;
207 if( isset( $this->struct['description'] ) )
208 $output .= '<span class="uix-control-description">' . esc_html( $this->struct['description'] ) . '</span>';
209
210 return $output;
211 }
212
213
214 215 216 217 218 219 220 221
222 public function render(){
223
224 $output = '<div id="' . esc_attr( $this->id() ) . '" class="uix-control uix-control-' . esc_attr( $this->type ) . ' ' . esc_attr( $this->id() ) . '">';
225
226 $output .= $this->label();
227 $output .= '<div class="uix-control-input">';
228 $output .= $this->input();
229 $output .= '</div>';
230 $output .= $this->description();
231
232 $output .= '</div>';
233
234 return $output;
235 }
236
237 238 239 240 241 242
243 public function is_active(){
244 if( !empty( $this->parent ) )
245 return $this->parent->is_active();
246
247 return parent::is_active();
248 }
249
250 }