1: <?php
2:
3: namespace Zippy\Html\Form;
4:
5: use Zippy\WebApplication;
6: use Zippy\Event;
7: use Zippy\Interfaces\ChangeListener;
8: use Zippy\Interfaces\EventReceiver;
9: use Zippy\Interfaces\Requestable;
10:
11: /**
12: * Компонент тэга &lt;input type=&quot;date&quot;&gt;
13: */
14: class Date extends TextInput implements Requestable, ChangeListener
15: {
16: private $event;
17: private $min=0;
18: private $max=0;
19:
20: public function __construct($id, $value = null, $bgupdate = false) {
21: parent::__construct($id);
22: $this->setDate($value);
23: $this->bgupdate = $bgupdate;
24: }
25:
26: protected function onAdded() {
27: if ($this->bgupdate) {
28: $page = $this->getPageOwner();
29: $this->onChange($page, 'OnBackgroundUpdate', true);
30: }
31: }
32:
33: /**
34: * Установка минимальной или максимальной дат. Если не используется передать 0
35: *
36: * @param mixed $min
37: * @param mixed $max
38: */
39: public function setMinMax($min, $max) {
40: if($min > $max) {
41: return;
42: }
43: $this->min = $min;
44: $this->max = $max;
45: }
46:
47: public function RenderImpl() {
48: TextInput::RenderImpl();
49:
50: /*
51: $min='';
52: $max='';
53: // $url = $this->owner->getURLNode() . "::" . $this->id . "&ajax=true";
54: if ($this->min > 0) {
55: $min = ", min: new Date(" . date("Y,m-1,d", $this->min) . ")";
56: }
57: if ($this->max > 0) {
58: $max = ", max: new Date(" . date("Y,m-1,d", $this->max) . ")";
59: }
60:
61: $js = "$('#{$this->id}').pickadate( { format: 'yyyy-mm-dd' {$min} {$max} });";
62: if ($this->event != null) {
63: $formid = $this->getFormOwner()->id;
64:
65: if ($this->event->isajax == false) {
66: $url = $this->owner->getURLNode() . '::' . $this->id;
67: $url = substr($url, 2 + strpos($url, 'q='));
68: $this->setAttribute("onchange", "javascript:{ $('#" . $formid . "_q').attr('value','" . $url . "');$('#" . $formid . "').submit(); }");
69: $js = "$('#{$this->id}').pickadate( {format: 'yyyy-mm-dd',onSet: function() { ;
70: $('#" . $formid . "_q').attr('value','" . $url . "');$('#" . $formid . "').submit()
71: } } );";
72: } else {
73: $url = $this->owner->getURLNode() . "::" . $this->id;
74: $url = substr($url, 2 + strpos($url, 'q='));
75: $_BASEURL = WebApplication::$app->getResponse()->getHostUrl();
76:
77: $js = "$('#{$this->id}').pickadate( {format: 'yyyy-mm-dd' ,onSet: function() {
78: $('#" . $formid . "_q').attr('value','" . $url . "'); submitForm('{$formid}','{$_BASEURL}/?ajax=true')
79: } } );";
80: }
81: }
82: WebApplication::$app->getResponse()->addJavaScript($js, true);
83: */
84: $this->setAttribute('type', 'date') ;
85: if($this->min >0) {
86: $this->setAttribute('min', date('Y-m-d', $this->min)) ;
87: }
88: if($this->max >0) {
89: $this->setAttribute('max', date('Y-m-d', $this->max)) ;
90: }
91:
92: }
93:
94: /**
95: * Возвращает дату в виде timestamp
96: * @param mixed $endday - установить конец дня
97: */
98: public function getDate($endday = false) {
99: $date = strtotime($this->getText());
100: if ($endday == true) {
101: $d = date('Y-m-d', $date);
102: $date = strtotime($d . ' 23:59:59');
103: }
104: return $date;
105: }
106:
107: /**
108: * Устанавливает дату
109: * Если параметр не задан - текущая дата
110: *
111: * @param mixed $t - timestamp
112: */
113: public function setDate($t = 0) {
114: if ($t > 0) {
115: $this->setText(date('Y-m-d', $t));
116: } else {
117: $this->setText("");
118: }
119: }
120:
121: /**
122: * @see ChangeListener
123: */
124: public function onChange(EventReceiver $receiver, $handler, $ajax = true) {
125:
126: $this->event = new Event($receiver, $handler);
127: $this->event->isajax = $ajax;
128: }
129:
130: /**
131: * @see ChangeListener
132: */
133: public function OnEvent() {
134: if ($this->event != null) {
135: $this->event->onEvent($this);
136: }
137: }
138:
139: /**
140: * @see Requestable
141: */
142: public function RequestHandle() {
143: $this->OnEvent();
144: }
145:
146: }
147: