UIX Documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • None
  • uix
    • data
    • ui
      • control

Classes

  • uix\data\data
  • uix\ui
  • uix\ui\box
  • uix\ui\control
  • uix\ui\control\autocomplete
  • uix\ui\control\button
  • uix\ui\control\checkbox
  • uix\ui\control\color
  • uix\ui\control\editor
  • uix\ui\control\email
  • uix\ui\control\file
  • uix\ui\control\hidden
  • uix\ui\control\number
  • uix\ui\control\post_relation
  • uix\ui\control\radio
  • uix\ui\control\select
  • uix\ui\control\separator
  • uix\ui\control\slider
  • uix\ui\control\template
  • uix\ui\control\text
  • uix\ui\control\textarea
  • uix\ui\control\toggle
  • uix\ui\footer
  • uix\ui\grid
  • uix\ui\header
  • uix\ui\help
  • uix\ui\metabox
  • uix\ui\modal
  • uix\ui\notice
  • uix\ui\page
  • uix\ui\panel
  • uix\ui\post_type
  • uix\ui\repeat
  • uix\ui\section
  • uix\ui\uix

Interfaces

  • uix\data\load
  • uix\data\save

Functions

  • uix
  • uix_autoload_class
  1 <?php
  2 /**
  3  * UIX Controls
  4  *
  5  * @package   controls
  6  * @author    David Cramer
  7  * @license   GPL-2.0+
  8  * @link
  9  * @copyright 2016 David Cramer
 10  */
 11 namespace uix\ui;
 12 
 13 /**
 14  * Base UIX Control class.
 15  *
 16  * @since       1.0.0
 17  */
 18 class control extends \uix\data\data{
 19 
 20     /**
 21      * The type of object
 22      *
 23      * @since       1.0.0
 24      * @access public
 25      * @var         string
 26      */
 27     public $type = 'control';
 28 
 29     /**
 30      * Register the UIX objects
 31      *
 32      * @since 1.0.0
 33      * @access public
 34      * @param string $slug Object slug
 35      * @param array $object object structure array
 36      * @return object|\uix object instance
 37      */
 38     public static function register( $slug, $object, $parent = null ) {
 39 
 40         $caller = get_called_class();
 41         // get the current instance
 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      * Sets the controls data
 52      *
 53      * @since 1.0.0
 54      * @see \uix\uix
 55      * @access public
 56      */
 57     public function setup() {
 58 
 59         // run parents to setup sanitization filters
 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         // base attributes defined
 76         $this->attributes['name']      =  $this->name();
 77         $this->attributes['id']        =  $this->id() . '-control';
 78 
 79     }
 80 
 81     /**
 82      * Handy method for setting data-* attributes using the setup parameter
 83      * @since  1.0.0
 84      * @access public
 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      * Gets the attributes for the control.
 97      *
 98      * @since  1.0.0
 99      * @access public
100      * @return array Attributes for the input field
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      * Define core page styles
115      *
116      * @since 1.0.0
117      * @access public
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      * Create and Return the control's input name
126      *
127      * @since 1.0.0
128      * @access public
129      * @return string The control name
130      */
131     public function name(){
132         return $this->id();
133     }
134 
135     /**
136      * get this controls value
137      *
138      * @since 1.0.0
139      * @access public
140      * @return mixed the controls value
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      * Gets the classes for the control input
155      *
156      * @since  1.0.0
157      * @access public
158      * @return array
159      */
160     public function classes() {
161 
162         return array(
163             'widefat'
164         );
165 
166     }
167 
168 
169     /**
170      * Returns the main input field for rendering
171      *
172      * @since 1.0.0
173      * @see \uix\ui\uix
174      * @access public
175      * @return string Input field HTML striung
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      * Returns the label for the control
184      *
185      * @since 1.0.0
186      * @access public
187      * @return string label of control
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      * Returns the description for the control
200      *
201      * @since 1.0.0
202      * @access public
203      * @return string description string 
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      * Render the Control
216      *
217      * @since 1.0.0
218      * @see \uix\ui\uix
219      * @access public
220      * @return string HTML of rendered control
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      * checks if the current control is active
239      *
240      * @since 1.0.0
241      * @access public
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 }
UIX Documentation API documentation generated by ApiGen