|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Administrator\Types; |
|
4
|
|
|
|
|
5
|
|
|
class TextSelect extends Element |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $options = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Except attributes to insert. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $exceptAttributes = [ 'stat', 'default' ]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Required attributes which can't be replaced. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $requiredAttributes = [ |
|
25
|
|
|
'style' => 'max-width: 300px', |
|
26
|
|
|
'data-input-type' => 'text_dropdown', |
|
27
|
|
|
'class' => 'ui fluid search selection dropdown' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Render input. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function renderInput() |
|
36
|
|
|
{ |
|
37
|
|
|
$options = $this->getOptions(); |
|
38
|
|
|
|
|
39
|
|
|
$html = sprintf('<div %s>', $this->renderAttributesAsHtml()); |
|
40
|
|
|
$html .= '<input name="'. $this->getName() .'" type="hidden" ' . $this->renderValueAsHtml() . '>'; |
|
41
|
|
|
$html .= '<i class="dropdown icon"></i>'; |
|
42
|
|
|
$html .= '<div class="default text">' . $this->getDefaultText() . '</div>'; |
|
43
|
|
|
$html .= '<div class="menu">'; |
|
44
|
|
|
array_walk($options, function($option) use (&$html){ |
|
45
|
|
|
return $html .= sprintf("<div class=\"item\" data-value=\"%s\">%s</div>", $option, $option); |
|
46
|
|
|
}); |
|
47
|
|
|
$html .= '</div>'; |
|
48
|
|
|
$html .= '</div>'; |
|
49
|
|
|
|
|
50
|
|
|
return $html; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get default text value. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $default |
|
57
|
|
|
* @return null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDefaultText($default = 'default') |
|
60
|
|
|
{ |
|
61
|
|
|
$attributes = $this->getAttributes(); |
|
62
|
|
|
|
|
63
|
|
|
return isset($attributes[$default]) ? $attributes[$default] : null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get options |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getOptions() |
|
72
|
|
|
{ |
|
73
|
|
|
if(is_callable($this->options)) |
|
74
|
|
|
return call_user_func_array($this->options, array($this->getRepository())); |
|
75
|
|
|
|
|
76
|
|
|
return $this->options; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Convert array attributes into html attributes. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
private function renderAttributesAsHtml() |
|
85
|
|
|
{ |
|
86
|
|
|
$attributes = $this->cleanAttributes(); |
|
87
|
|
|
$attributes = array_merge($attributes, $this->requiredAttributes); |
|
88
|
|
|
|
|
89
|
|
|
$string = ''; |
|
90
|
|
|
|
|
91
|
|
|
array_walk($attributes, function($attrVal, $attrKey) use (&$string, $attributes) { |
|
92
|
|
|
if(end($attributes) !== $attrKey) |
|
93
|
|
|
return $string .= sprintf("%s=\"%s\" ", $attrKey, $attrVal); |
|
94
|
|
|
|
|
95
|
|
|
return $string .= sprintf("%s=\"%s\"", $attrKey, $attrVal); |
|
96
|
|
|
}); |
|
97
|
|
|
|
|
98
|
|
|
return $string; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Clear attributes. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function cleanAttributes() |
|
107
|
|
|
{ |
|
108
|
|
|
$excepts = $this->exceptAttributes; |
|
109
|
|
|
|
|
110
|
|
|
$attributes = $this->getAttributes(); |
|
111
|
|
|
|
|
112
|
|
|
array_walk($excepts, function($except) use (&$attributes){ |
|
113
|
|
|
unset($attributes[$except]); |
|
114
|
|
|
}); |
|
115
|
|
|
|
|
116
|
|
|
return $attributes; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Render value as html attribute. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function renderValueAsHtml() |
|
125
|
|
|
{ |
|
126
|
|
|
$value = $this->getValue(); |
|
127
|
|
|
|
|
128
|
|
|
if($value !== null) |
|
129
|
|
|
return sprintf("value=\"%s\"", $value); |
|
130
|
|
|
|
|
131
|
|
|
return ''; |
|
132
|
|
|
} |
|
133
|
|
|
} |