Issues (63)

Elements/Select.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sjhc1170
5
 * Date: 18/05/2018
6
 * Time: 10:57
7
 */
8
9
namespace Iriven\Plugins\Form\Elements;
10
11
12
use \Iriven\Plugins\Form\Core\Libs\AttributesBuilder;
0 ignored issues
show
The type \Iriven\Plugins\Form\Core\Libs\AttributesBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class Select extends Checkbox
15
{
16
    private $selected = false;
17
18
    /**
19
     * Select constructor.
20
     * @param $label
21
     * @param array $options
22
     * @param array $attributes
23
     */
24
    public function __construct($label, array $options, array $attributes =[])
25
    {
26
        parent::__construct($label, $options, $attributes);
27
        $this->Attributes()->set('type','select');
28
        $this->Attributes()->set('placeholder',$this->Attributes()->get('placeholder','Make a choise ...'));
29
        $this->Attributes()->Ignore('select');
30
    }
31
    /**
32
     * @return string
33
     */
34
    public function RenderHtml()
35
    {
36
37
        $html = '';
38
        $multiple=false;
39
40
        if (!$this->Attributes()->has('id'))
41
            $this->Attributes()->createElementID($this->Attributes()->get('name', $this->label()->getItemID()));
42
        $this->label()->Attribute()->set('fieldtype', $this->Attributes()->get('type'));
43
        $this->label()->Attribute()->set('for', $this->Attributes()->get('id'));
44
45
46
        if($this->Attributes()->has('value'))
47
        {
48
            if(!is_array($this->Attributes()->get('value')))
49
                $this->Attributes()->set('value',[$this->Attributes()->get('value')]);
50
        }
51
        else
52
            $this->Attributes()->set('value',[]);
53
54
        if($this->Attributes()->has('multiple') AND $this->Attributes()->get('multiple')==='multiple')
55
            $multiple=true;
56
57
        if($multiple)
58
        {   $this->Attributes()->set('size',$this->Attributes()->get('size',3));
59
            if(substr($this->Attributes()->get('name'), -2) !== '[]')
60
                $this->Attributes()->set('name',$this->Attributes()->get('name').'[]');
61
        }
62
        $html .= $this->Label()->RenderHtml();
63
        $this->Attributes()->Ignore(['value','selected','optgroup-attributes','option-attributes','placeholder']);
64
65
        $html .= '<select'.$this->Attributes()->RenderHtml().'>';
66
        if(empty($this->Attributes()->get('value')[0]) and $this->Attributes()->has('placeholder'))
67
            $html .= '<option value="" disabled selected>'.$this->Attributes()->get('placeholder').'</option>';
68
69
        foreach($this->options as $index=>$datas):
70
71
            if(is_array($datas))
72
                $html .= $this->createOptgroup($index,$datas);
73
            else
74
            {
75
                $optionAttr = null;
76
                if($this->Attributes()->has('option-attributes'))
77
                {
78
                    $oOption = new AttributesBuilder($this->Attributes()->get('option-attributes'));
79
                    $oOption->Ignore(['value','selected','placeholder']);
80
                    $oOption->set('type','option');
81
                    $optionAttr .= $oOption->RenderHtml();
82
                }
83
                $html .= '<option value="'.$index.'"';
84
                if(!$this->selected and in_array($index,$this->Attributes()->get('value')))
85
                {
86
                    $html .= ' selected="selected"';
87
                    $this->selected = true;
88
                }
89
                if($optionAttr) $html .= ' '.$optionAttr ;
90
                $html .= '>';
91
                $html .= $datas.'</option>';
92
            }
93
        endforeach;
94
        $html .= '</select>';
95
        return $html;
96
    }
97
98
    /**
99
     * @param string $label
100
     * @param array $options
101
     * @return string
102
     */
103
    private function createOptgroup($label,$options = [])
104
    {
105
        $groupAttr = null;
106
        $optionAttr = null;
107
        if($this->Attributes()->has('optgroup-attributes'))
108
        {
109
            $oGroup = new AttributesBuilder($this->Attributes()->get('optgroup-attributes'));
110
            $oGroup->set('type','optgroup');
111
            $oGroup->Ignore('label');
112
            $groupAttr .= $oGroup->RenderHtml();
113
        }
114
        if($this->Attributes()->has('option-attributes'))
115
        {
116
            $oOption = new AttributesBuilder($this->Attributes()->get('option-attributes'));
117
            $oOption->Ignore(['value','selected','placeholder']);
118
            $oOption->set('type','option');
119
            $optionAttr .= $oOption->RenderHtml();
120
        }
121
        $output='<optgroup label="'.$label.'"';
122
        if($groupAttr) $output .= ' '.$groupAttr ;
123
        $output .= '>';
124
        foreach($options as $key=>$optLabel)
125
        {
126
            if(is_array($optLabel))
127
                $output .= call_user_func_array([$this,__METHOD__],[$optLabel]);
128
            else
129
            {
130
                $output .= '<option value="'.$key.'"';
131
                if(!$this->selected and in_array($key,$this->Attributes()->get('value')))
132
                {
133
                    $output .= ' selected="selected"';
134
                    $this->selected = true;
135
                }
136
                if($optionAttr) $output .= ' '.$optionAttr ;
137
                $output .= '>';
138
                $output .= $optLabel.'</option>';
139
            }
140
        }
141
        $output.='</optgroup>';
142
        return$output;
143
        
144
    }
145
}