| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: |
|
| 7: | namespace Sphp\comp\html{
|
| 8: |
|
| 9: | class DateField extends \Sphp\tools\Control{
|
| 10: | public $datemin = '';
|
| 11: | public $datemax = '';
|
| 12: | private $appendText = 'dd-mm-yy';
|
| 13: | private $image = "";
|
| 14: | private $nomonth = "";
|
| 15: | private $errmsg = '';
|
| 16: | private $formName = '';
|
| 17: | private $msgName = '';
|
| 18: | private $req = false;
|
| 19: |
|
| 20: | public function __construct($name='',$fieldName='',$tableName='') {
|
| 21: | $this->init($name,$fieldName,$tableName);
|
| 22: | if($this->value!=''){
|
| 23: | $this->value = $this->dateToMySQLDate($this->value) ;
|
| 24: | }
|
| 25: | $this->unsetEndTag();
|
| 26: | }
|
| 27: | public function onit() {
|
| 28: | if($this->getAttribute("msgname") != ""){
|
| 29: | $this->msgName = $this->getAttribute("msgname");
|
| 30: | }
|
| 31: |
|
| 32: | }
|
| 33: | protected function genhelpPropList() {
|
| 34: | $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$val');
|
| 35: | $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
|
| 36: | $this->addHelpPropFunList('setRequired','Can not submit Empty','','');
|
| 37: | $this->addHelpPropFunList('setDateMin','Set Minimum Date can Select, check jquery ui date','','$val');
|
| 38: | $this->addHelpPropFunList('setDateMax','Set Max Date can Select, check jquery ui date','','$val');
|
| 39: | $this->addHelpPropFunList('setAppendText','Set Append Text with Date','','$val');
|
| 40: | $this->addHelpPropFunList('setButtonImage','Button image path ','','$filepath');
|
| 41: | $this->addHelpPropFunList('setNumMonths','Max Months to Display','','$val');
|
| 42: | }
|
| 43: |
|
| 44: | public function setErrMsg($msg){
|
| 45: | $this->errmsg .= '<strong class="alert-danger">' . $msg . '</strong>';
|
| 46: | setErr($this->name, $msg);
|
| 47: | }
|
| 48: | public function setForm($val) { $this->formName = $val;}
|
| 49: | public function setMsgName($val) { $this->msgName = $val; $this->setAttribute('placeholder', $val);}
|
| 50: | public function setRequired() {
|
| 51: | if($this->issubmit){
|
| 52: | if(strlen($this->value) < 1){
|
| 53: | $this->setErrMsg($this->getAttribute("msgname") .' ' . "Can not submit Empty");
|
| 54: | }
|
| 55: | }
|
| 56: | $this->req = true;
|
| 57: | }
|
| 58: | private function dateToMySQLDate($date){
|
| 59: | $date1 = $this->createDate($this->appendText, $date, 'Y-m-d', 0, 0, 0);
|
| 60: | return $date1;
|
| 61: | }
|
| 62: | public function createDate($dformat, $beginDate, $outformat, $offsetd, $offsetm, $offsety)
|
| 63: | {
|
| 64: |
|
| 65: | $dsep = ' ';
|
| 66: | if(strpos($dformat, '/')>0){$dsep = '/';}
|
| 67: | if(strpos($dformat, '-')>0){$dsep = '-';}
|
| 68: | $date_parts2 = explode($dsep, $beginDate);
|
| 69: | $date_parts3 = explode($dsep, $dformat);
|
| 70: | if($date_parts3[0]=='mm' && $date_parts3[1]=='dd'){
|
| 71: | $date_parts1[0] = $date_parts2[1];
|
| 72: | $date_parts1[1] = $date_parts2[0];
|
| 73: | $date_parts1[2] = $date_parts2[2];
|
| 74: | }
|
| 75: | else if($date_parts3[0]=='mm' && $date_parts3[1]=='yy'){
|
| 76: | $date_parts1[0] = $date_parts2[2];
|
| 77: | $date_parts1[1] = $date_parts2[0];
|
| 78: | $date_parts1[2] = $date_parts2[1];
|
| 79: | }
|
| 80: | else if($date_parts3[0]=='yy' && $date_parts3[1]=='dd'){
|
| 81: | $date_parts1[0] = $date_parts2[1];
|
| 82: | $date_parts1[1] = $date_parts2[2];
|
| 83: | $date_parts1[2] = $date_parts2[0];
|
| 84: | }
|
| 85: | else if($date_parts3[0]=='yy' && $date_parts3[1]=='mm'){
|
| 86: | $date_parts1[0] = $date_parts2[2];
|
| 87: | $date_parts1[1] = $date_parts2[1];
|
| 88: | $date_parts1[2] = $date_parts2[0];
|
| 89: | }
|
| 90: | else if($date_parts3[0]=='dd' && $date_parts3[1]=='yy'){
|
| 91: | $date_parts1[0] = $date_parts2[0];
|
| 92: | $date_parts1[1] = $date_parts2[2];
|
| 93: | $date_parts1[2] = $date_parts2[1];
|
| 94: | }else{
|
| 95: | $date_parts1 = $date_parts2;
|
| 96: | }
|
| 97: |
|
| 98: | $date1 = date($outformat, mktime(0, 0, 0, $date_parts1[1] + $offsetm, $date_parts1[0] + $offsetd, $date_parts1[2] + $offsety));
|
| 99: | return $date1;
|
| 100: | }
|
| 101: |
|
| 102: | public function mysqlDateToDate($df)
|
| 103: | {
|
| 104: | if($df!=''){
|
| 105: | $dformat = $this->appendText;
|
| 106: | $dformat = str_replace('dd', 'd', $dformat);
|
| 107: | $dformat = str_replace('mm', 'm', $dformat);
|
| 108: | $dformat = str_replace('yy', 'Y', $dformat);
|
| 109: |
|
| 110: | $date1 = date($dformat,strtotime($df));
|
| 111: | return $date1;
|
| 112: | }
|
| 113: | }
|
| 114: |
|
| 115: | public function setDateMin($val){$this->datemin = $val;}
|
| 116: | public function setDateMax($val){$this->datemax = $val;}
|
| 117: | public function setAppendText($val){$this->appendText=$val;}
|
| 118: | public function setButtonImage($val){$this->image=$val;}
|
| 119: | public function setNumMonths($val){$this->nomonth=$val;}
|
| 120: |
|
| 121: | public function onjsrender(){
|
| 122: | $sphp_settings = \SphpBase::sphp_settings();
|
| 123: |
|
| 124: |
|
| 125: |
|
| 126: |
|
| 127: | $str = '';
|
| 128: | if($this->appendText!=''){$str .= ",appendText: '$this->appendText', dateFormat: '$this->appendText'";}
|
| 129: | if($this->image==''){$this->image = $sphp_settings->slib_res_path . "/comp/html/res/calendar.gif";}
|
| 130: | if($this->image!=''){$str .= ",showOn: 'button',buttonImageOnly: true, buttonImage: '$this->image'";}
|
| 131: | if($this->datemax!=''){$str .= ",maxDate: '$this->datemax'";}
|
| 132: | if($this->datemin!=''){$str .= ",minDate: '$this->datemin'";}
|
| 133: | if($this->nomonth!=''){$str .= ",numberOfMonths: $this->nomonth";}
|
| 134: | $this->setParameterA('onfocus','$(\'#'.$this->name.'\').datepicker(\'show\');');
|
| 135: | addHeaderJSFunctionCode('ready', $this->name, "
|
| 136: | $('#$this->name').datepicker({ changeMonth: true,changeYear: true $str});
|
| 137: | ");
|
| 138: | if($this->formName!=''){
|
| 139: | if($this->req){
|
| 140: | addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
|
| 141: | ctlReq['$this->name']= Array('$this->msgName','TextField');");
|
| 142: | }
|
| 143: | }
|
| 144: | }
|
| 145: |
|
| 146: | public function onrender(){
|
| 147: | if($this->errmsg!=""){
|
| 148: | $this->setPostTag($this->errmsg);
|
| 149: | }
|
| 150: | if($this->getAttribute('class')==''){
|
| 151: | $this->class = "form-control";
|
| 152: | }
|
| 153: | if($this->value!=''){
|
| 154: | $this->setAttribute('value', $this->mysqlDateToDate($this->value));
|
| 155: | }
|
| 156: | $this->setAttribute('type', 'text');
|
| 157: | $this->setAttribute('readonly', 'readonly');
|
| 158: |
|
| 159: | }
|
| 160: |
|
| 161: |
|
| 162: |
|
| 163: | public function getJSValue(){
|
| 164: | return "document.getElementById('$this->name').value" ;
|
| 165: | }
|
| 166: |
|
| 167: | public function setJSValue($exp){
|
| 168: | $jsOut = "document.getElementById('$this->name').value = $exp;" ;
|
| 169: | }
|
| 170: |
|
| 171: | }
|
| 172: | }
|
| 173: | |