1: <?php
2:
3: namespace Zippy\Html\Form;
4:
5: use Zippy\WebApplication;
6: use Zippy\Interfaces\ChangeListener;
7: use Zippy\Interfaces\Requestable;
8:
9: use Zippy\Interfaces\EventReceiver;
10: use Zippy\Event;
11:
12: /**
13: * Компонент тэга &lt;input type=&quot;text&quot;&gt;
14: */
15: class TextInput extends HtmlFormDataElement implements ChangeListener, Requestable
16: {
17: private $defvalue;
18: private $dlist;
19: private $event;
20:
21: /**
22: * Конструктор
23: * @param mixed ID
24: * @param Значение элемента или поле привязанного объекта
25: */
26: public function __construct($id, $value = '') {
27: parent::__construct($id);
28: $this->setValue($value);
29: $this->setAttribute("name", $this->id);
30: $this->defvalue = $value;
31: }
32:
33: /**
34: * Возвращает текстовое значение
35: * @return string
36: */
37: public function getText() {
38: return $this->getValue();
39: }
40:
41: final public function getInt() {
42: return intval(trim($this->getValue()));
43: }
44:
45: final public function getDouble() {
46: return doubleval(trim($this->getValue()));
47: }
48:
49: /**
50: * Устанавливает текстовое значение
51: * @param string
52: */
53: public function setText($text='') {
54: $this->setValue($text);
55:
56:
57: if(\Zippy\WebApplication::$app->getRequest()->isAjaxRequest()) {
58: $js= "$('#{$this->id}').val('{$text}')" ;
59:
60:
61: \Zippy\WebApplication::$app->getResponse()->addAjaxResponse($js) ;
62: }
63:
64: }
65:
66: /**
67: * @see HtmlComponent
68: */
69: public function RenderImpl() {
70: // $this->checkInForm();
71:
72: $this->setResponseData();
73: $this->setAttribute("list", null) ;
74: $list="";
75: if(is_array($this->dlist) && count($this->dlist)>0) {
76: $this->setAttribute("list", $this->id.'_list') ;
77:
78: $list = " <datalist id=\"".$this->id.'_list'."\">" ;
79: foreach($this->dlist as $option) {
80: $list .= " <option label='{$option}' value='{$option}'> ";
81: }
82: $list .= " </datalist>" ;
83: $HtmlTag = $this->getTag();
84: $HtmlTag->follow($list);
85:
86:
87: }
88:
89: if ($this->event != null) {
90: $formid = $this->getFormOwner()->id;
91:
92: $url = $this->owner->getURLNode() . '::' . $this->id;
93: $url = substr($url, 2 + strpos($url, 'q='));
94:
95: if ($this->event->isajax == false) {
96:
97: $this->setAttribute("onblur", "javascript:{if(beforeZippy('{$this->id}') ==false) return false; $('#" . $formid . "_q').attr('value','" . $url . "');$('#" . $formid . "').submit();}");
98: } else {
99: $_BASEURL = WebApplication::$app->getResponse()->getHostUrl();
100: $this->setAttribute("onblur", "if(beforeZippy('{$this->id}') ==false) return false; $('#" . $formid . "_q').attr('value','" . $url . "'); submitForm('{$formid}','{$_BASEURL}/?ajax=true');");
101: }
102: }
103:
104: }
105:
106: protected function setResponseData() {
107: $this->setAttribute("value", ($this->getValue()));
108: }
109:
110: /**
111: * @see SubmitDataRequest
112: */
113: public function getRequestData() {
114: if(!isset($_REQUEST[$this->id])) {
115: return;
116: }
117:
118: $this->setValue($_REQUEST[$this->id]);
119:
120: }
121:
122:
123:
124:
125: public function clean() {
126: $this->setText($this->defvalue);
127: }
128:
129: /**
130: * выпадающий список
131: *
132: * @param mixed $list
133: */
134: final public function setDataList($list) {
135: if(!is_array($list)) {
136: $list = array();
137: }
138: $this->dlist = $list;
139:
140: }
141: public function RequestHandle() {
142: $this->OnEvent();
143: }
144: /**
145: * @see ChangeListener
146: */
147: public function onChange(EventReceiver $receiver, $handler, $ajax = false) {
148:
149: $this->event = new Event($receiver, $handler);
150: $this->event->isajax = $ajax;
151: }
152:
153: /**
154: * @see ChangeListener
155: */
156: public function OnEvent() {
157: if ($this->event != null) {
158: $this->event->onEvent($this);
159: }
160: }
161:
162: }
163: