# shiftcontroller/4.9.95/hc3/ui/element/input/checkbox.php

ShiftController Employee Shift Scheduling, version 4.9.95. 98 lines.

- Page: https://pluginprobe.com/plugins/shiftcontroller/4.9.95/code/hc3/ui/element/input/checkbox.php
- Raw: https://pluginprobe.com/plugins/shiftcontroller/4.9.95/raw/hc3/ui/element/input/checkbox.php
- Modified: 2024-12-09T07:03:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/shiftcontroller/4.9.95/code/hc3/ui/element/input/checkbox.php#L10-L20`.

```php
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
class HC3_Ui_Element_Input_Checkbox extends HC3_Ui_Abstract_Input
{
	protected $el = 'input';
	protected $uiType = 'input/checkbox';

	protected $checked = false;
	protected $readonly = false;
	protected $asList = false;
	public $value;

	public function __construct( $htmlFactory, $name, $label = null, $value = null, $checked = false )
	{
		parent::__construct( $htmlFactory, $name, $label, $value );
		$this->value = $value;
		$this->checked = $checked;
	}

	public function setValue( $value )
	{
		$this->checked = $value ? true : false;
		$this->value = $value;
		return $this;
	}

	public function setChecked( $set = true )
	{
		$this->checked = $set;
		return $this;
	}

	public function setReadonly( $set = true )
	{
		$this->readonly = $set;
		return $this;
	}

	public function asList( $set = TRUE )
	{
		$this->asList = $set;
		return $this;
	}

	public function render()
	{
		$out = $this->htmlFactory->makeElement('input')
			->addAttr('type', 'checkbox' )
			->addAttr('name', $this->htmlName() )
			// ->addAttr('class', 'hc-field')
			;

		if( (null !== $this->value) && strlen($this->value) ){
			$out->addAttr('value', $this->value);
		}

		if( $this->checked ){
			$out->addAttr('checked', 'checked');
		}

		if( $this->readonly ){
			$out
				->addAttr('readonly', 'readonly')
				->addAttr('disabled', 'disabled')
				;
		}

		$attr = $this->getAttr();
		foreach( $attr as $k => $v ){
			$out->addAttr($k, $v);
		}

		if( (null !== $this->label) && strlen($this->label) ){
			$htmlId = 'hc3_' . mt_rand( 100000, 999999 );

			$out->addAttr('id', $htmlId);
			$label = $this->htmlFactory->makeElement('label', $this->label)
				->addAttr('for', $htmlId )
				->addAttr('title', esc_attr($this->label) )
				// ->addAttr('class', 'hc-fs2')
				;

			if( $this->asList ){
				$out = $this->htmlFactory->makeList( array($out, $label) )->gutter(0);
				$out = $this->htmlFactory->makeBlock( $out )
					->addAttr('class', 'hc-align-center')
					;
			}
			else {
				$out = $this->htmlFactory->makeListInline( array($out, $label) )->gutter(1);
				$out = $this->htmlFactory->makeBlock( $out )
					->addAttr('class', 'hc-nowrap')
					;
			}
		}

		return $out;
	}
}
```
