openrat-cms

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

SelectboxComponent.class.php (2758B)


      1 <?php
      2 
      3 namespace template_engine\components\html\component_selectbox;
      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( $this->lang
     97 				? Value::createExpression( ValueExpression::TYPE_MESSAGE, Value::createExpression( ValueExpression::TYPE_DATA_VAR,'_value')) // Translate the option value
     98 				: Value::createExpression( ValueExpression::TYPE_DATA_VAR,'_value') )
     99 			->addConditionalAttribute('selected','$_key=='.$value,'selected');
    100 
    101 		$optionLoop->addChild($option);
    102 
    103 		$selectbox->addStyleClass('input');
    104 
    105 		// Wrap into a label, if necessary.
    106 		if   ( $this->label ) {
    107 			$label = new CMSElement('label');
    108 			$label->addStyleClass('form-row')->addStyleClass('form-input');
    109 			$label->addChild( (new CMSElement('span'))->addStyleClass('form-label')->content($this->label));
    110 			$label->addChild( $selectbox );
    111 			return $label;
    112 		}
    113 		else {
    114 			return $selectbox;
    115 		}
    116 	}
    117 }