| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Ui_Element_Input_Select extends HC3_Ui_Abstract_Input |
| 3 |
{ |
| 4 |
protected $el = 'select'; |
| 5 |
protected $uiType = 'input/select'; |
| 6 |
protected $options = array(); |
| 7 |
|
| 8 |
public function __construct( $htmlFactory, $name, $label = NULL, $options = array(), $value = NULL ) |
| 9 |
{ |
| 10 |
parent::__construct( $htmlFactory, $name, $label, $value ); |
| 11 |
$this->options = $options; |
| 12 |
} |
| 13 |
|
| 14 |
public function render() |
| 15 |
{ |
| 16 |
$options = array(); |
| 17 |
foreach( $this->options as $k => $v ){ |
| 18 |
if( ! strlen($k) ){ |
| 19 |
if( $this->getAttr('required') ){ |
| 20 |
} |
| 21 |
else { |
| 22 |
$k = ' '; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
$this_input = $this->htmlFactory->makeElement('option', $v) |
| 27 |
->addAttr('value', $k ) |
| 28 |
; |
| 29 |
|
| 30 |
if( $k == $this->value ){ |
| 31 |
$this_input |
| 32 |
->addAttr('selected', 'selected') |
| 33 |
; |
| 34 |
} |
| 35 |
$options[] = $this_input; |
| 36 |
} |
| 37 |
|
| 38 |
$options = $this->htmlFactory->makeCollection( $options ); |
| 39 |
|
| 40 |
$out = $this->htmlFactory->makeElement('select', $options) |
| 41 |
->addAttr('name', $this->htmlName() ) |
| 42 |
->addAttr('class', 'hc-field') |
| 43 |
// ->addAttr('class', 'hc-block') |
| 44 |
->addAttr('class', 'hc-full-width') |
| 45 |
; |
| 46 |
|
| 47 |
$attr = $this->getAttr(); |
| 48 |
foreach( $attr as $k => $v ){ |
| 49 |
$out->addAttr( $k, $v ); |
| 50 |
} |
| 51 |
|
| 52 |
if( strlen($this->label) ){ |
| 53 |
$out = $this->htmlFactory->makeLabelled( $this->label, $out, $this->htmlId() ); |
| 54 |
} |
| 55 |
|
| 56 |
return $out; |
| 57 |
} |
| 58 |
} |