1: <?php
2:
3: namespace webfilesframework\io\form\formItem;
4:
5: use webfilesframework\MSite;
6:
7: 8: 9: 10: 11: 12:
13: class MDropdownMenueFormItem extends MAbstractFormItem
14: {
15:
16: protected $possibleValues;
17: protected $filtered;
18:
19: protected $initialized = false;
20:
21: public function __construct($name, $value, $localizedName = "", $filtered = false)
22: {
23:
24: $this->filtered = $filtered;
25: parent::__construct($name, $value, $localizedName);
26: }
27:
28: public function setPossibleValues($possibleValues)
29: {
30: $this->possibleValues = $possibleValues;
31: }
32:
33: public function getPossibleValues()
34: {
35: return $this->possibleValues;
36: }
37:
38: public function init($useLabel = true)
39: {
40:
41: $this->code = "";
42:
43: if ($useLabel) {
44: $this->code .= "<div style=\"margin-top:4px;\">";
45:
46: if (!empty($this->localizedName)) {
47: $this->code .= $this->localizedName;
48: } else {
49: $this->code .= $this->name;
50: }
51:
52: $this->code = "<div style=\"margin-top:4px;\">
53: <label style=\"width:" . $this->getLabelWidth() . "px;display:block;float:left;\">";
54: if (!empty($this->localizedName)) {
55: $this->code .= $this->localizedName;
56: } else {
57: $this->code .= $this->name;
58: }
59: $this->code .= " </label>
60: ";
61: }
62:
63: if (!$this->filtered) {
64: $this->code .= "<select name=\"" . $this->name . "\" dojoType=\"dijit.form.Select\"";
65: $this->code .= ">";
66:
67: if (is_array($this->possibleValues)) {
68: foreach ($this->possibleValues as $value) {
69: $this->code .= "<option value=\"" . $value->getId() . "\"";
70: if ($value->getId() == $this->value) {
71: $this->code .= " selected=\"selected\"";
72: }
73: $this->code .= ">" . $value . "</option>";
74: }
75: }
76: $this->code .= " </select>";
77: } else {
78:
79:
80: if (!$this->initialized) {
81: MSite::getInstance()->addHeader('<script type="text/javascript">
82:
83: require([
84: "dijit/form/ComboBox", "dijit/form/FilteringSelect", "dojo/on","dojo/dom"
85: ], function(ComboBox, FilteringSelect, on, dom){
86:
87: new dijit.form.FilteringSelect({
88: id: "' . $this->name . '",
89: name: "' . $this->name . '",
90: autoComplete: true,
91: style: "width: 300px;",
92: searchDelay: 1000
93: }, "' . $this->name . '").startup();
94: on(dom.byId("' . $this->name . '"), "keyup", function(event) {
95:
96: require([
97: "dojo/store/Memory","dojo/request/xhr", "dojo/dom", "dojo/dom-construct", "dojo/json","dojo/domReady!"
98: ], function(Memory, xhr, dom, domConst, JSON){
99:
100: xhr("index.php?site=makeAjaxRequest&searchstring=" + encodeURIComponent(dijit.byId("' . $this->name . '").get(\'displayedValue\')), {
101: preventCache: "true"
102: }).then(function(data){
103: var datastore = new Memory(JSON.parse(data));
104: dijit.byId("' . $this->name . '").set("store",datastore);
105: }, function(err){
106: domConst.place("<p>error: <p>" + err.response.text + "</p></p>", "output");
107: });
108: })
109: });
110: });
111: </script>');
112: }
113:
114: $this->initialized = true;
115:
116: $this->code .= "<input id=\"" . $this->name . "\" />";
117: }
118:
119: if ($useLabel) {
120: $this->code .= "
121: <div style=\"clear:both;\"></div>
122: </div>";
123: }
124:
125: }
126:
127: public function getCode($useLabel = true)
128: {
129: $this->init($useLabel);
130: return parent::getCode();
131: }
132:
133: }