1: | <?php |
2: | |
3: | namespace Zippy\Html\Form; |
4: | |
5: | use Zippy\WebApplication; |
6: | use Zippy\Interfaces\Binding; |
7: | use Zippy\Interfaces\ChangeListener; |
8: | use Zippy\Interfaces\Requestable; |
9: | use Zippy\Interfaces\AjaxRender; |
10: | use Zippy\Interfaces\EventReceiver; |
11: | use Zippy\Event; |
12: | |
13: | |
14: | |
15: | |
16: | class DropDownChoice extends HtmlFormDataElement implements ChangeListener, Requestable, AjaxRender |
17: | { |
18: | private $optionlist; |
19: | private $event; |
20: | private $defvalue; |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | public function __construct($id, $optionlist = array(), $value = -1, $bgupdate = false) { |
29: | parent::__construct($id); |
30: | $this->setValue($value); |
31: | |
32: | $this->defvalue = $value; |
33: | $this->optionlist = $optionlist; |
34: | $this->bgupdate = $bgupdate; |
35: | } |
36: | |
37: | protected function onAdded() { |
38: | if ($this->bgupdate) { |
39: | $page = $this->getPageOwner(); |
40: | $this->onChange($page, 'OnBackgroundUpdate', true); |
41: | } |
42: | } |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | public function RenderImpl() { |
49: | |
50: | |
51: | |
52: | $this->setAttribute("name", $this->id); |
53: | $this->setAttribute("id", $this->id); |
54: | |
55: | if ($this->event != null) { |
56: | $formid = $this->getFormOwner()->id; |
57: | |
58: | $url = $this->owner->getURLNode() . '::' . $this->id; |
59: | $url = substr($url, 2 + strpos($url, 'q=')); |
60: | |
61: | if ($this->event->isajax == false) { |
62: | |
63: | $this->setAttribute("onchange", "javascript:{if(beforeZippy('{$this->id}') ==false) return false; $('#" . $formid . "_q').attr('value','" . $url . "');$('#" . $formid . "').submit();}"); |
64: | } else { |
65: | $_BASEURL = WebApplication::$app->getResponse()->getHostUrl(); |
66: | $this->setAttribute("onchange", "if(beforeZippy('{$this->id}') ==false) return false; var old=$('#" . $formid . "_q').attr('value') ; $('#" . $formid . "_q').attr('value','" . $url . "'); submitForm('{$formid}','{$_BASEURL}/?ajax=true');$('#" . $formid . "_q').attr('value',old);"); |
67: | } |
68: | } |
69: | |
70: | |
71: | $this->setResponseData(); |
72: | } |
73: | |
74: | private function setResponseData() { |
75: | |
76: | $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
77: | if(is_array($list)==false) { |
78: | $list = array(); |
79: | } |
80: | $tag = $this->getTag(); |
81: | $options = ""; |
82: | foreach ($list as $key => $value) { |
83: | |
84: | $option = "<option value=\"{$key}\" "; |
85: | |
86: | if ($key == $this->getValue()) { |
87: | $option .= " selected "; |
88: | } |
89: | |
90: | $option .= ">{$value}</option>"; |
91: | $options .= $option; |
92: | |
93: | } |
94: | $tag->appendWith($options); |
95: | |
96: | |
97: | if (count($list) == 0) { |
98: | |
99: | $js = "$('#" . $this->id . " option').each(function() { if($(this).val() == '" . $this->getValue() . "') { $(this).prop(\"selected\", true); }});"; |
100: | |
101: | WebApplication::$app->getResponse()->addJavaScript($js, true); |
102: | |
103: | |
104: | |
105: | } |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | public function getRequestData() { |
112: | if(!isset($_REQUEST[$this->id])) { |
113: | return; |
114: | } |
115: | $this->setValue($_REQUEST[$this->id]); |
116: | |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | public function RequestHandle() { |
123: | $this->OnEvent(); |
124: | } |
125: | |
126: | |
127: | |
128: | |
129: | public function AjaxAnswer() { |
130: | |
131: | $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
132: | |
133: | $js = "$('#{$this->id}').empty();"; |
134: | foreach ($list as $key => $value) { |
135: | $js .= "$('#{$this->id}').append('<option value=\"{$key}\">{$value}</option>');"; |
136: | } |
137: | return $js; |
138: | } |
139: | |
140: | public function setValue($value) { |
141: | |
142: | parent::setValue($value) ; |
143: | |
144: | if(\Zippy\WebApplication::$app->getRequest()->isAjaxRequest()) { |
145: | $js= "$('#{$this->id}').val('{$value}')" ; |
146: | \Zippy\WebApplication::$app->getResponse()->addAjaxResponse($js) ; |
147: | } |
148: | |
149: | } |
150: | |
151: | |
152: | |
153: | |
154: | public function onChange(EventReceiver $receiver, $handler, $ajax = false) { |
155: | |
156: | $this->event = new Event($receiver, $handler); |
157: | $this->event->isajax = $ajax; |
158: | } |
159: | |
160: | |
161: | |
162: | |
163: | public function OnEvent() { |
164: | if ($this->event != null) { |
165: | $this->event->onEvent($this); |
166: | } |
167: | } |
168: | |
169: | |
170: | |
171: | public function setOptionList($optionlist) { |
172: | if (is_array($optionlist)) { |
173: | $this->optionlist = $optionlist; |
174: | $this->setValue(-1); |
175: | } |
176: | |
177: | if(\Zippy\WebApplication::$app->getRequest()->isAjaxRequest()) { |
178: | $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
179: | |
180: | $js = "$('#{$this->id}').empty();"; |
181: | foreach ($list as $key => $value) { |
182: | $js .= "$('#{$this->id}').append('<option value=\"{$key}\">{$value}</option>');"; |
183: | } |
184: | |
185: | \Zippy\WebApplication::$app->getResponse()->addAjaxResponse($js) ; |
186: | } |
187: | |
188: | } |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | public function getOptionList() { |
195: | return $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
196: | } |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | public function addOption($value, $text) { |
205: | if ($this->optionlist instanceof Binding) { |
206: | return; |
207: | } |
208: | $this->optionlist[$value] = $text; |
209: | } |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | public function getValueName() { |
216: | $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
217: | return $list[$this->getValue()] ??''; |
218: | } |
219: | |
220: | public function clean() { |
221: | $this->setValue($this->defvalue); |
222: | } |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | public function selectFirst() { |
229: | $list = $this->optionlist instanceof Binding ? $this->optionlist->getValue() : $this->optionlist; |
230: | if (count($list) == 0) { |
231: | return; |
232: | } |
233: | $k = array_keys($list); |
234: | $this->setValue($k[0]); |
235: | } |
236: | |
237: | |
238: | public function getIntValue() { |
239: | |
240: | return intval($this->getValue()); |
241: | |
242: | } |
243: | |
244: | } |
245: | |