1 <?php
 2 
 3 /**
 4  * @package redaxo\core\form
 5  */
 6 class rex_form_select_element extends rex_form_element
 7 {
 8     protected $select;
 9 
10     private $separator;
11 
12     // 1. Parameter nicht genutzt, muss aber hier stehen,
13     // wg einheitlicher Konstrukturparameter
14     public function __construct($tag = '', rex_form_base $table = null, array $attributes = [])
15     {
16         parent::__construct('', $table, $attributes);
17 
18         $this->select = new rex_select();
19         $this->separator = '|';
20     }
21 
22     public function formatElement()
23     {
24         $multipleSelect = false;
25 
26         // Hier die Attribute des Elements an den Select weitergeben, damit diese angezeigt werden
27         foreach ($this->getAttributes() as $attributeName => $attributeValue) {
28             $this->select->setAttribute($attributeName, $attributeValue);
29         }
30 
31         if ($this->select->hasAttribute('multiple')) {
32             $multipleSelect = true;
33         }
34 
35         if ($multipleSelect) {
36             $this->setAttribute('name', $this->getAttribute('name') . '[]');
37 
38             $selectedOptions = explode($this->separator, trim($this->getValue(), $this->separator));
39             if (is_array($selectedOptions) && $selectedOptions[0] != '') {
40                 foreach ($selectedOptions as $selectedOption) {
41                     $this->select->setSelected($selectedOption);
42                 }
43             }
44         } else {
45             $this->select->setSelected($this->getValue());
46         }
47 
48         $this->select->setName($this->getAttribute('name'));
49         return $this->select->get();
50     }
51 
52     public function setSeparator($separator)
53     {
54         $this->separator = $separator;
55     }
56 
57     /**
58      * @return rex_select
59      */
60     public function getSelect()
61     {
62         return $this->select;
63     }
64 
65     public function setSelect(rex_select $selectObj)
66     {
67         $this->select = $selectObj;
68         if ($selectObj->hasAttribute('multiple')) {
69             $this->setAttribute('multiple', $selectObj->getAttribute('multiple'));
70         }
71     }
72 }
73