openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

CheckboxComponent.class.php (1912B)


      1 <?php
      2 
      3 namespace template_engine\components\html\component_checkbox;
      4 
      5 use template_engine\components\html\Component;
      6 use template_engine\element\CMSElement;
      7 use template_engine\element\PHPBlockElement;
      8 use template_engine\element\Value;
      9 use template_engine\element\ValueExpression;
     10 
     11 class CheckboxComponent extends Component
     12 {
     13 	
     14 	public $default = false;
     15 	public $name;
     16 	public $readonly = false;
     17 	public $required = false;
     18 	public $remember = false;
     19 	public $label;
     20 
     21 	public function createElement()
     22 	{
     23 		$checkbox = (new CMSElement('input'))
     24 			->addAttribute('type','checkbox')
     25 			->addStyleClass('form-checkbox');
     26 
     27 		if    ( $this->remember )
     28 			$checkbox->addStyleClass('remember');
     29 
     30 		if ( !$this->label )
     31 			$checkbox->addAttribute('name',$this->name);
     32 		else
     33 			$checkbox->addAttribute('data-name',$this->name);
     34 
     35         if	( $this->readonly )
     36 			$checkbox->addAttribute('disabled','disabled');
     37 		$checkbox->addAttribute('value','1');
     38 
     39 		if   ( $this->default )
     40 			$condition = ''.PHPBlockElement::value($this->default);
     41 		else
     42 			$condition = '@$'.PHPBlockElement::value($this->name);
     43 		$checkbox->addConditionalAttribute('checked', $condition, 'checked');
     44 
     45 		if   ( $this->required )
     46 			$checkbox->addAttribute( 'required','required');
     47 
     48 		if ( $this->readonly && $this->required ) {
     49 			$hidden = (new CMSElement('input'))->addAttribute('type','hidden')->addAttribute('name',$this->name)->addAttribute('value','1');
     50 			$checkbox->addChild( $hidden );
     51 		}
     52 
     53 		if   ( $this->label ) {
     54 			$label = (new CMSElement('label'))
     55 				//->addStyleClass('form-checkbox')
     56 				->addChild($checkbox)
     57 				->addChild( (new CMSElement('input'))->addAttribute('type','hidden')->addAttribute('name',$this->name)->addConditionalAttribute('value', $condition, '1') )
     58 				->addChild( (new CMSElement('span'))
     59 					->addStyleClass('form-label')
     60 					->content($this->label));
     61 			return $label;
     62 		}
     63 
     64 		return $checkbox;
     65     }
     66 }