Selectbox.class.php (2554B)
1 <?php 2 3 namespace template_engine\components; 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 SelectboxComponent extends Component 12 { 13 14 public $list; 15 16 public $name; 17 18 public $default; 19 20 /** 21 * Titel 22 * @var unknown 23 */ 24 public $title; 25 26 /** 27 * Style-Klasse 28 */ 29 public $class; 30 31 /** 32 * Leere Auswahlmöglichkeit hinzufügen. 33 * @var string 34 */ 35 public $addempty = false; 36 37 /** 38 * Mehrfachauswahl möglich? 39 * @var string 40 */ 41 public $multiple = false; 42 43 /** 44 * Größe des Eingabefeldes 45 * @var integer 46 */ 47 public $size = 1; 48 49 /** 50 * Ob es sich bei den Option-Werten um Sprachschlüssel handelt. 51 * @var string 52 */ 53 public $lang = false; 54 55 56 public $label; 57 58 59 60 public function createElement() 61 { 62 $selectbox = (new CMSElement('select')); 63 64 65 $selectbox->addAttribute('name',$this->name); 66 //$selectbox->addAttribute('disabled',$this->readonly); 67 68 69 if ( $this->class ) 70 $selectbox->addStyleClass($this->class); 71 72 if ( $this->title ) 73 $selectbox->addAttribute('title',$this->title); 74 75 if ( $this->multiple ) 76 $selectbox->addAttribute('multiple','multiple'); 77 78 $selectbox->addAttribute('size',$this->size); 79 80 if ( $this->addempty ) 81 $selectbox->addChild( (new CMSElement('option'))->addAttribute('value','')->content( Value::createExpression(ValueExpression::TYPE_MESSAGE,'LIST_ENTRY_EMPTY'))); 82 83 $optionLoop = (new PHPBlockElement())->asChildOf($selectbox); 84 85 if ( $this->default ) 86 $value = $optionLoop->value($this->default); 87 elseif ( isset($this->default) ) 88 $value = '\'\''; 89 else 90 $value = '$'.$this->name; 91 92 // Create the option list. 93 $optionLoop->beforeBlock = 'foreach($'.$this->list.' as $_key=>$_value)'; 94 $option = (new CMSElement('option')) 95 ->addAttribute('value',Value::createExpression( ValueExpression::TYPE_DATA_VAR,'_key')) 96 ->content(Value::createExpression( ValueExpression::TYPE_DATA_VAR,'_value')) 97 ->addConditionalAttribute('selected','$_key=='.$value,'selected'); 98 99 $optionLoop->addChild($option); 100 101 $selectbox->addStyleClass('input'); 102 103 // Wrap into a label, if necessary. 104 if ( $this->label ) { 105 $label = new CMSElement('label'); 106 $label->addStyleClass('form-row')->addStyleClass('form-input'); 107 $label->addChild( (new CMSElement('span'))->addStyleClass('form-label')->content($this->label)); 108 $label->addChild( $selectbox ); 109 return $label; 110 } 111 else { 112 return $selectbox; 113 } 114 } 115 }