1: <?php
2:
3: /**
4: * Description of DateField
5: *
6: * @author SARTAJ
7: */
8:
9: namespace Sphp\Comp\Form {
10:
11: class DateField extends \Sphp\tools\Component {
12:
13: public $datemin = '';
14: public $datemax = '';
15: private $appendText = 'dd-mm-yy';
16: private $image = "";
17: private $nomonth = "";
18: private $errmsg = '';
19: private $formName = '';
20: private $msgName = '';
21: private $req = false;
22:
23: protected function oninit() {
24: \SphpJsM::addjQueryUI();
25: if ($this->value != '') {
26: $this->value = $this->dateToMySQLDate($this->value);
27: }
28: if(!$this->element->hasAttribute("name")){
29: $this->HTMLName = $this->name;
30: }else{
31: $this->HTMLName = $this->getAttribute("name");
32: }
33:
34: if ($this->getAttribute("msgname") != "") {
35: $this->msgName = $this->getAttribute("msgname");
36: }
37: $this->unsetEndTag();
38: }
39:
40: protected function genhelpPropList() {
41: $this->addHelpPropFunList('setForm', 'Bind with Form JS Event', '', '$val');
42: $this->addHelpPropFunList('setMsgName', 'Name Display in placeholder and Error', '', '$val');
43: $this->addHelpPropFunList('setRequired', 'Can not submit Empty', '', '');
44: $this->addHelpPropFunList('setDateMin', 'Set Minimum Date can Select, check jquery ui date', '', '$val');
45: $this->addHelpPropFunList('setDateMax', 'Set Max Date can Select, check jquery ui date', '', '$val');
46: $this->addHelpPropFunList('setAppendText', 'Set Append Text with Date', '', '$val');
47: $this->addHelpPropFunList('setButtonImage', 'Button image path ', '', '$filepath');
48: $this->addHelpPropFunList('setNumMonths', 'Max Months to Display', '', '$val');
49: }
50:
51: public function setErrMsg($msg) {
52: $this->errmsg .= '<strong class="alert-danger">' . $msg . '</strong>';
53: if(\SphpBase::sphp_request()->isAJAX()){
54: \SphpBase::JSServer()->addJSONJSBlock('$("#'. $this->name .'").after("<strong class=\"alert-danger\">' . $msg . '! </strong>");');
55: }
56: setErr($this->name, $msg);
57: }
58:
59: public function fi_setForm($val) {
60: $this->formName = $val;
61: }
62:
63: public function fu_setMsgName($val) {
64: $this->msgName = $val;
65: $this->setAttribute('placeholder', $val);
66: }
67:
68: public function fi_setRequired() {
69: if ($this->issubmit) {
70: if (strlen($this->value) < 1) {
71: $this->setErrMsg($this->getAttribute("msgname") . ' ' . "Can not submit Empty");
72: }
73: }
74: $this->req = true;
75: }
76:
77: private function dateToMySQLDate($date) {
78: $date1 = $this->createDate($this->appendText, $date, 'Y-m-d', 0, 0, 0);
79: return $date1;
80: }
81:
82: public function createDate($dformat, $beginDate, $outformat, $offsetd, $offsetm, $offsety) {
83: // find date separator
84: $dsep = ' ';
85: if (strpos($dformat, '/') > 0) {
86: $dsep = '/';
87: }
88: if (strpos($dformat, '-') > 0) {
89: $dsep = '-';
90: }
91: $date_parts2 = explode($dsep, $beginDate);
92: $date_parts3 = explode($dsep, $dformat);
93: if ($date_parts3[0] == 'mm' && $date_parts3[1] == 'dd') {
94: $date_parts1[0] = $date_parts2[1];
95: $date_parts1[1] = $date_parts2[0];
96: $date_parts1[2] = $date_parts2[2];
97: } else if ($date_parts3[0] == 'mm' && $date_parts3[1] == 'yy') {
98: $date_parts1[0] = $date_parts2[2];
99: $date_parts1[1] = $date_parts2[0];
100: $date_parts1[2] = $date_parts2[1];
101: } else if ($date_parts3[0] == 'yy' && $date_parts3[1] == 'dd') {
102: $date_parts1[0] = $date_parts2[1];
103: $date_parts1[1] = $date_parts2[2];
104: $date_parts1[2] = $date_parts2[0];
105: } else if ($date_parts3[0] == 'yy' && $date_parts3[1] == 'mm') {
106: $date_parts1[0] = $date_parts2[2];
107: $date_parts1[1] = $date_parts2[1];
108: $date_parts1[2] = $date_parts2[0];
109: } else if ($date_parts3[0] == 'dd' && $date_parts3[1] == 'yy') {
110: $date_parts1[0] = $date_parts2[0];
111: $date_parts1[1] = $date_parts2[2];
112: $date_parts1[2] = $date_parts2[1];
113: } else {
114: $date_parts1 = $date_parts2;
115: }
116:
117: $date1 = date($outformat, mktime(0, 0, 0, $date_parts1[1] + $offsetm, $date_parts1[0] + $offsetd, $date_parts1[2] + $offsety));
118: return $date1;
119: }
120:
121: public function mysqlDateToDate($df) {
122: if ($df != '') {
123: $dformat = $this->appendText;
124: $dformat = str_replace('dd', 'd', $dformat);
125: $dformat = str_replace('mm', 'm', $dformat);
126: $dformat = str_replace('yy', 'Y', $dformat);
127:
128: $date1 = date($dformat, strtotime($df));
129: return $date1;
130: }
131: }
132:
133: public function fu_setDateMin($val) {
134: $this->datemin = $val;
135: }
136:
137: public function fu_setDateMax($val) {
138: $this->datemax = $val;
139: }
140:
141: public function fu_setAppendText($val) {
142: $this->appendText = $val;
143: }
144:
145: public function fu_setButtonImage($val) {
146: $this->image = $val;
147: }
148:
149: public function fu_setNumMonths($val) {
150: $this->nomonth = $val;
151: }
152:
153: protected function onjsrender() {
154: $sphp_settings = \SphpBase::sphp_settings();
155: //addFileLink("$jquerypath/themes/base/jquery.ui.all.css");
156: //addFileLink("$jquerypath/ui/jquery.ui.core.min.js");
157: //addFileLink("$jquerypath/ui/jquery.ui.widget.min.js");
158: //addFileLink("$jquerypath/ui/jquery.ui.datepicker.min.js");
159: $str = '';
160: if ($this->appendText != '') {
161: $str .= ",appendText: '$this->appendText', dateFormat: '$this->appendText'";
162: }
163: if ($this->image == '') {
164: $this->image = $sphp_settings->slib_res_path . "/comp/html/res/calendar.gif";
165: }
166: if ($this->image != '') {
167: $str .= ",showOn: 'button',buttonImageOnly: true, buttonImage: '$this->image'";
168: }
169: if ($this->datemax != '') {
170: $str .= ",maxDate: '$this->datemax'";
171: }
172: if ($this->datemin != '') {
173: $str .= ",minDate: '$this->datemin'";
174: }
175: if ($this->nomonth != '') {
176: $str .= ",numberOfMonths: $this->nomonth";
177: }
178: $this->setParameterA('onfocus', '$(\'#' . $this->name . '\').datepicker(\'show\');');
179: addHeaderJSFunctionCode('ready', $this->name, "
180: $('#$this->name').datepicker({ changeMonth: true,changeYear: true $str});
181: ");
182: if ($this->formName != '') {
183: if ($this->req) {
184: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
185: ctlReq['$this->name']= Array('$this->msgName','TextField');");
186: }
187: }
188: }
189:
190: protected function onrender() {
191: if ($this->errmsg != "") {
192: $this->setPostTag($this->errmsg);
193: }
194: if ($this->getAttribute('class') == '') {
195: $this->class = "form-control";
196: }
197: if ($this->value != '') {
198: $this->setAttribute('value', $this->mysqlDateToDate($this->value));
199: }
200: $this->setAttribute('type', 'text');
201: $this->setAttribute('readonly', 'readonly');
202: }
203:
204: // javascript functions
205: public function getJSValue() {
206: return "document.getElementById('$this->name').value";
207: }
208:
209: public function setJSValue($exp) {
210: $jsOut = "document.getElementById('$this->name').value = $exp;";
211: }
212:
213: }
214:
215: }
216: