| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Ui_Element_Input_Checkbox extends HC3_Ui_Abstract_Input |
| 3 |
{ |
| 4 |
protected $el = 'input'; |
| 5 |
protected $uiType = 'input/checkbox'; |
| 6 |
|
| 7 |
protected $checked = false; |
| 8 |
protected $readonly = false; |
| 9 |
protected $asList = false; |
| 10 |
public $value; |
| 11 |
|
| 12 |
public function __construct( $htmlFactory, $name, $label = null, $value = null, $checked = false ) |
| 13 |
{ |
| 14 |
parent::__construct( $htmlFactory, $name, $label, $value ); |
| 15 |
$this->value = $value; |
| 16 |
$this->checked = $checked; |
| 17 |
} |
| 18 |
|
| 19 |
public function setValue( $value ) |
| 20 |
{ |
| 21 |
$this->checked = $value ? true : false; |
| 22 |
$this->value = $value; |
| 23 |
return $this; |
| 24 |
} |
| 25 |
|
| 26 |
public function setChecked( $set = true ) |
| 27 |
{ |
| 28 |
$this->checked = $set; |
| 29 |
return $this; |
| 30 |
} |
| 31 |
|
| 32 |
public function setReadonly( $set = true ) |
| 33 |
{ |
| 34 |
$this->readonly = $set; |
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
public function asList( $set = TRUE ) |
| 39 |
{ |
| 40 |
$this->asList = $set; |
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function render() |
| 45 |
{ |
| 46 |
$out = $this->htmlFactory->makeElement('input') |
| 47 |
->addAttr('type', 'checkbox' ) |
| 48 |
->addAttr('name', $this->htmlName() ) |
| 49 |
// ->addAttr('class', 'hc-field') |
| 50 |
; |
| 51 |
|
| 52 |
if( (null !== $this->value) && strlen($this->value) ){ |
| 53 |
$out->addAttr('value', $this->value); |
| 54 |
} |
| 55 |
|
| 56 |
if( $this->checked ){ |
| 57 |
$out->addAttr('checked', 'checked'); |
| 58 |
} |
| 59 |
|
| 60 |
if( $this->readonly ){ |
| 61 |
$out |
| 62 |
->addAttr('readonly', 'readonly') |
| 63 |
->addAttr('disabled', 'disabled') |
| 64 |
; |
| 65 |
} |
| 66 |
|
| 67 |
$attr = $this->getAttr(); |
| 68 |
foreach( $attr as $k => $v ){ |
| 69 |
$out->addAttr($k, $v); |
| 70 |
} |
| 71 |
|
| 72 |
if( (null !== $this->label) && strlen($this->label) ){ |
| 73 |
$htmlId = 'hc3_' . mt_rand( 100000, 999999 ); |
| 74 |
|
| 75 |
$out->addAttr('id', $htmlId); |
| 76 |
$label = $this->htmlFactory->makeElement('label', $this->label) |
| 77 |
->addAttr('for', $htmlId ) |
| 78 |
->addAttr('title', esc_attr($this->label) ) |
| 79 |
// ->addAttr('class', 'hc-fs2') |
| 80 |
; |
| 81 |
|
| 82 |
if( $this->asList ){ |
| 83 |
$out = $this->htmlFactory->makeList( array($out, $label) )->gutter(0); |
| 84 |
$out = $this->htmlFactory->makeBlock( $out ) |
| 85 |
->addAttr('class', 'hc-align-center') |
| 86 |
; |
| 87 |
} |
| 88 |
else { |
| 89 |
$out = $this->htmlFactory->makeListInline( array($out, $label) )->gutter(1); |
| 90 |
$out = $this->htmlFactory->makeBlock( $out ) |
| 91 |
->addAttr('class', 'hc-nowrap') |
| 92 |
; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $out; |
| 97 |
} |
| 98 |
} |