YAP  2.5
 All Data Structures Namespaces Files Functions Variables
CYap_clsSearch.php
Go to the documentation of this file.
1 <?PHP
12 /*
13  * +-------------------------------------------------------------------------+
14  * | Yap, Version 2.5.0 |
15  * +-------------------------------------------------------------------------+
16  * | Copyright (c) 2003-2013 Andrioli Darvin |
17  * | Email darvin@andrioli.com |
18  * | Web http://www.andrioli.com/en/yap.html |
19  * | Download http://www.phpclasses.org/browse.html/package/1391.html |
20  * | |
21  * +-------------------------------------------------------------------------+
22  * | This library is free software; you can redistribute it and/or modify |
23  * | it under the terms of the GNU Lesser General Public License as |
24  * | published by the Free Software Foundation; either version 2 of the |
25  * | License, or (at your option) any later version. |
26  * | |
27  * | This library is distributed in the hope that it will be useful, but |
28  * | WITHOUT ANY WARRANTY; without even the implied warranty of |
29  * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
30  * | Lesser General Public License for more details. |
31  * | |
32  * | You should have received a copy of the GNU Lesser General Public |
33  * | License along with this library; if not, write to the Free Software |
34  * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
35  * +-------------------------------------------------------------------------+
36  */
49 {
66 var $Lang;
67 
69 
71 
95 function CYap_clsSearch($search_field,$language)
96 {
97 /*
98  * Parse the configuration data. This is the startup configuration.
99  * Those parameters may be changed at runtime
100  */
101 $this->tblFields=array();
102 $this->tblFieldsOrder=array();
103 $this->LayoutObject=null;
104 $this->SubmitObject=null;
105 // Default value
106 $this->LayoutClass='CYAP_clsSearchLayout';
107 $this->SubmitClass='CYAP_ViewSubmitButton';
108 $this->Lang=$language;
109 $this->LoadCfg($search_field);
110 }
111 
120 function LoadCfg($info)
121 {
122 $index=count($this->tblFieldsOrder);
123 foreach($info as $FieldOrder => $FieldCfg)
124  {
125  if(is_array($FieldCfg))
126  {
127  // Field parameters
128  $FieldClass=(array_key_exists("fieldclass",$FieldCfg))?$FieldCfg['fieldclass']:'CYap_clsSearchTxt';
129  if(array_key_exists($FieldOrder,$this->tblFields))
130  {
131  // FieldName should be unique
132  die("Duplicated fieldname: " . $FieldOrder. ".Check the search parameters");
133  }
134  else
135  {
136  eval('$this->tblFields[$FieldOrder]=new '.$FieldClass.'($FieldCfg,$FieldOrder);');
137  $this->tblFieldsOrder[$index]=&$this->tblFields[$FieldOrder];
138  $this->tblFields[$FieldOrder]->SetProperty('FieldOrder',$index);
139  $index++;
140  }
141  }
142  else
143  {
144  // Filter's parameters
145  $this->LayoutClass=$this->GetParameter('layoutclass',$info,'CYAP_clsSearchLayout');
146  $this->SubmitClass=$this->GetParameter('submitclass',$info,'CYAP_ViewSubmitButton');
147  }
148  }
149 }
160 function GetParameter($paramName,$cfg,$default)
161 {
162 return((array_key_exists($paramName,$cfg))?
163  $cfg[$paramName]:
164  $default);
165 }
173 function &GetSearchField($FieldName)
174 {
175 if(array_key_exists($FieldName,$this->tblFields))
176  return($this->tblFields[$FieldName]);
177 else
178  return(null);
179 }
189 function AddSearchField(&$Field,$FieldName)
190 {
191 $index=count($this->tblFieldsOrder);
192 if(array_key_exists($FieldName,$this->tblFields))
193  {
194  // FieldName should be unique
195  die("Duplicated fieldname: " . $FieldName. ".Check the search parameters");
196  }
197 else
198  {
199  $this->tblFields[$FieldName]=&$Field;
200  $this->tblFieldsOrder[$index]=&$Field;
201  $this->tblFields[$FieldName]->SetProperty('FieldOrder',$index);
202  }
203 }
211 function RemoveSearchField($FieldName)
212 {
213 if(array_key_exists($FieldName,$this->tblFields))
214  {
215  // The field exist, remove it
216  $index=$this->tblFields[$FieldName]->GetProperty('FieldOrder');
217  unset($this->tblFieldsOrder[$index]);
218  unset($this->tblFields[$FieldName]);
219  }
220 }
229 function SetLayoutObject(&$layout)
230 {
231 $this->LayoutObject=&$layout;
232 }
238 function SetLayoutClass($ClassName)
239 {
240 $this->LayoutClass=$ClassName;
241 }
249 function SetSubmitObject(&$Submit)
250 {
251 $this->SubmitObject=&$Submit;
252 }
257 function SetSubmitClass($ClassName)
258 {
259 $this->SubmitClass=$ClassName;
260 }
261 
271 function NoSearchBox()
272 {
273 return(count($this->tblFieldsOrder)<2);
274 }
275 
283 function ReadFormData()
284 {
285 foreach($this->tblFields as $FieldName => $Field)
286  {
287  $this->tblFields[$FieldName]->ReadFormData();
288  }
289 }
296 function StoreInformation($prefix)
297 {
298 // Tell to all fields to store its own information.
299 foreach($this->tblFields as $FieldName => $Field)
300  {
301  $this->tblFields[$FieldName]->StoreInformation($prefix);
302  }
303 // I've not any informztion to save, yet...
304 }
311 function LoadInformation($prefix)
312 {
313 // Tell to all fields to store its own information.
314 foreach($this->tblFields as $FieldName => $Field)
315  {
316  $this->tblFields[$FieldName]->LoadInformation($prefix);
317  }
318 // I've not any informztion to load, yet...
319 }
326 function CleanSessionInfo($prefix)
327 {
328 foreach($this->tblFields as $FieldName => $Field)
329  {
330  $this->tblFields[$FieldName]->CleanSessionInfo($prefix);
331  }
332 }
341 function GetWhereCondition($db_class)
342 {
343 $where="";
344 foreach($this->tblFields as $FieldName => $Field)
345  {
346  $risp=$this->tblFields[$FieldName]->GetWhereCondition($db_class);
347  if($risp!='')
348  $where.=' and '.$risp;
349  }
350 return($where);
351 }
360 function GetPageSize()
361 {
362 // Set the number of rows to show. If
363 $nRows=($this->NoSearchBox())?1000:$this->tblFields['PagSize']->GetValue();
364 return($nRows);
365 }
366 
375 function AddRowPerPage()
376 {
377 $this->tblFields['PagSize']=new CYap_clsSearchPageSize(array(),"PagSize");
378 $this->tblFields['PagSize']->SetLabel($this->Lang['PageLen']);
379 $this->tblFields['PagSize']->SetDefaultValue(15);
380 $this->tblFieldsOrder[199]=&$this->tblFields['PagSize'];
381 }
382 
391 {
392 echo '
393 <script type="text/javascript">
394 function CheckSearchBox(TheForm)
395 {';
396 $JsCode='';
397 foreach($this->tblFields as $FieldName => $Field)
398  {
399  $JsCode.=$this->tblFields[$FieldName]->JsSearchFieldValidate();
400  }
401 echo $JsCode;
402 echo '
403 return true
404 }
405 </script>';
406 }
407 
417 function Draw()
418 {
419 // Place any required javascript code
420 // Start the form
421 $txt="<h3>".$this->Lang['NewSearch']."</h3>
422  <form action=".$_SERVER["PHP_SELF"] ." method=post onsubmit='return CheckSearchBox(this)' name='yapfilter'>\n
423  <input type=hidden name=daquery value=1>\n
424  <input type=hidden name=ev value=YAP_SEARCH>\n";
425 /*
426  * If any layout object is provided I use it, otherwise
427  * I instance a new one.
428  */
429 if (is_null($this->LayoutObject))
430  eval('$LayoutObj=new ' . $this->LayoutClass .'();');
431 else
432  $LayoutObj=$this->LayoutObject;
433 $txt.=$LayoutObj->Start();
434 ksort($this->tblFieldsOrder);
435 $txt.=$LayoutObj->Draw($this->tblFieldsOrder);
436 if (is_null($this->SubmitObject))
437  eval('$subObj=new ' . $this->SubmitClass .'();');
438 else
439  $subObj=$this->SubmitObject;
440 $subObj->SetControlLabel($this->Lang['Search']);
441 $txt.=$LayoutObj->DrawElement($subObj);
442 $txt.=$LayoutObj->End();
443 $txt.="</Form>";
444 $txt.=$LayoutObj->GetJsCode();
445 return ($txt);
446 }
447 
448 } // CYAP_clsSearch
449 
458 {
461 {
462 $this->JsCode='';
463 }
469 function Start()
470 {
471 return('<table border=0 align=center>');
472 }
478 function End()
479 {
480 return('</table>');
481 }
482 
491 function Draw($tblFields)
492 {
493 $Html='';
494 foreach($tblFields as $FieldName => $Field)
495  {
496  if(is_subclass_of($tblFields[$FieldName],'CYap_clsSearchField'))
497  $drawObject=$tblFields[$FieldName]->GetDrawObject();
498  else
499  $drawObject=$tblFields[$FieldName];
500  if(!is_subclass_of($drawObject,'CYAP_Control'))
501  die('Error: the control object given does not extend the class CYAP_Control');
502  $Html.=$this->_DoDraw($drawObject);
503  $this->JsCode.=$drawObject->GetJsCode();
504  }
505 return($Html);
506 }
515 function DrawElement(&$element)
516 {
517  if(is_subclass_of($element,'CYap_clsSearchField'))
518  $drawObject=$element->GetDrawObject();
519  else
520  $drawObject=$element;
521  if(!is_subclass_of($drawObject,'CYAP_Control'))
522  die('Error: the control object given does not extend the class CYAP_Control');
523  $Html=$this->_DoDraw($drawObject);
524  return($Html);
525 }
534 function _DoDraw($DrawObject)
535 {
536 $Html="<tr><td>\n";
537 $Html.=$DrawObject->DrawLabel(false)."\n";
538 $Html.="</td><td>\n";
539 $Html.=$DrawObject->Draw(false)."\n";
540 $Html.="</td></tr>\n";
541 return($Html);
542 }
543 
544 function GetJsCode()
545 {
546 return($this->JsCode);
547 }
548 }
549 
550 /***********************************************************************
551  * C Y A P _ c l s 2 C O L U M N L A Y O U T
552  ***********************************************************************/
556 class CYAP_cls2ColumnLayout extends CYAP_clsSearchLayout
557 {
565 {
566  parent::CYAP_clsSearchLayout();
567  $this->ElementNo=0;
568 }
569 
570 function _DoDraw($DrawObject)
571 {
572 $this->ElementNo++;
573 $Html='';
574 if(is_subclass_of($DrawObject,'CYAP_ViewSubmitButton'))
575  {
576  // submit button
577  if($this->ElementNo>1)
578  $Html.="</td></tr>\n";
579  $Html.="<tr><td colspan=4>\n";
580  $Html.=$DrawObject->Draw(false)."\n";
581  $Html.="</td></tr>\n";
582  }
583 else
584  {
585  // Filter field
586  if($this->ElementNo%2)
587  $Html.="<tr><td>\n";
588  else
589  $Html.="</td><td>\n";
590  $Html.=$DrawObject->DrawLabel(false)."\n";
591  $Html.="</td><td>\n";
592  $Html.=$DrawObject->Draw(false)."\n";
593  if(!$this->ElementNo%2)
594  $Html.="</td></tr>\n";
595  }
596 return($Html);
597 }
598 
599 }
600 
601 /***********************************************************************
602  * C Y A P _ c l s S E A R C H F I E L D
603  ***********************************************************************/
604 
612 {
613 
614 var $Type;
618 var $Label;
633 var $Size;
666 function CYap_clsSearchField($cfg,$FieldName)
667 {
668 $this->MyName=$FieldName;
669 $this->DefaultValue='';
670 $this->ViewObj=null;
671 $this->_Initialize();
672 $this->LoadCFg($cfg);
673 }
684 function GetParameter($paramName,$cfg,$default)
685 {
686 return((array_key_exists($paramName,$cfg))?
687  $cfg[$paramName]:
688  $default);
689 }
695 function _Initialize()
696 {
697 $this->Type='';
698 $this->DbField=' ';
699 $this->PaddingChar=' ';
700 $this->Padding='none';
701 $this->UseRegex='0';
702 $this->Size=0;
703 $this->ViewClass='CYAP_ViewTextBox';
704 $this->Label=' ';
705 }
713 function LoadCFg($cfg)
714 {
715 if(array_key_exists("type",$cfg))
716  $this->Type=$cfg["type"];
717 if(array_key_exists("fieldname",$cfg))
718  $this->DbField=$cfg["fieldname"];
719 if(array_key_exists("paddingchar",$cfg))
720  $this->PaddingChar=$cfg["paddingchar"];
721 if(array_key_exists("padding",$cfg))
722  $this->Padding=$cfg["padding"];
723 if(array_key_exists("useregex",$cfg))
724  $this->UseRegex=$cfg["useregex"];
725 if(array_key_exists("size",$cfg))
726  $this->Size=$cfg["size"];
727 if(array_key_exists("viewclass",$cfg))
728  $this->ViewClass=$cfg["viewclass"];
729 if(array_key_exists("description",$cfg))
730  $this->Label=$cfg["description"];
731 }
740 function SetDefaultValue($value)
741 {
742 $this->DefaultValue=$value;
743 }
744 function SetLabel($value)
745 {
746 $this->Label=$value;
747 }
755 function SetProperty($Property,$value)
756 {
757 // TODO check whether the property exists
758 eval('$this->'.$Property.'=$value;');
759 }
767 function GetProperty($Property)
768 {
769 // TODO check whether the property exists
770 eval('return($this->'.$Property.');');
771 }
779 function GetWhereCondition($db_class)
780 {
781 Return("");
782 }
790 function GetValue()
791 {
792 if($this->RequestValue!='' && !is_null($this->RequestValue))
793  return($this->RequestValue);
794 else
795  return($this->DefaultValue);
796 }
797 
806 function StoreInformation($prefix)
807 {
808 $_SESSION[$prefix."F".$this->MyName]=$this->RequestValue;
809 }
817 function LoadInformation($prefix)
818 {
819 if(array_key_exists($prefix."F".$this->MyName,$_SESSION))
820  {
821  $this->RequestValue=$_SESSION[$prefix."F".$this->MyName];
822  if(is_null($this->RequestValue))
823  $this->RequestValue=$this->DefaultValue;
824  }
825 else
826  $this->RequestValue=$this->DefaultValue; // if any
827 }
828 
829 function CleanSessionInfo($prefix)
830 {
831 if(isset($_SESSION[$prefix."F".$this->MyName]))
832  unset($_SESSION[$prefix."F".$this->MyName]);
833 }
838 function ReadFormData()
839 {
840 if(isset($_REQUEST["F".$this->MyName])&&$_REQUEST["F".$this->MyName]!='')
841  {
842  $this->RequestValue=$_REQUEST["F".$this->MyName];
843  if(is_null($this->RequestValue))
844  $this->RequestValue=$this->DefaultValue;
845  }
846 else
847  $this->RequestValue=$this->DefaultValue;
848 }
849 
858 {
859 return('');
860 }
866 function &GetDrawObject()
867 {
868 }
869 
875 function _MakeViewObj()
876 {
877 if(is_null($this->ViewObj))
878  {
879  $this->ViewObj=new $this->ViewClass();
880  $this->ViewObj->SetControlLabel($this->Label);
881  $this->ViewObj->SetControlName('F'.$this->MyName);
882  $this->ViewObj->SetValue($this->GetValue());
883  }
884 return($this->ViewObj);
885 }
886 } // CYAP_clsSearchField
887 
888 
893 {
894 
895 
896 function CYap_clsSearchTxt($cfg,$FieldName)
897 {
898 parent::CYap_clsSearchField($cfg,$FieldName);
899 }
907 function GetWhereCondition($db_class)
908 {
909 $RetValue="";
910 // Field not required
911 if($this->RequestValue=='')
912  {
913  return($RetValue);
914  }
915 
916 // variabile inserita
917 if ($this->Type=="A")
918  {
919  // Uso regex? Se si alzo il flag del campo FieldUseRegex, se no eseguo il padding del campo
920  if($this->UseRegex=="1")
921  {
922  $RetValue=$db_class->DbRegexCond($this->DbField,$db_class->DBEscape($this->RequestValue)).' ';
923  }
924  else
925  {
926  $LookFor=$this->var_padding($this->Size,$this->Padding,$this->PaddingChar,$this->RequestValue);
927  $RetValue=$this->DbField.'=\''.$db_class->DBEscape($LookFor).'\' ';
928  }
929  }
930 else
931  {
932  // numeric field
933  $LookFor=$this->RequestValue;
934  $RetValue=$this->DbField.'='.$db_class->DBEscape($LookFor).' ';
935  }
936 return($RetValue);
937 }
938 
949 function var_padding($size,$padding,$paddingchar,$text)
950 {
951 if($padding=='none') { return($text); }
952 switch($padding)
953  {
954  case 'left': $cmd=STR_PAD_LEFT;
955  break;
956  case 'right': $cmd=STR_PAD_RIGHT;
957  break;
958  case 'center': $cmd=STR_PAD_BOTH;
959  break;
960  default:
961  trigger_error('ERROR: invalid padding value '.$padding,E_USER_ERROR);
962  break;
963  }
964 $dimTesto=strlen($text);
965 // se la dimensione è sufficiente restituisco la stringa
966 if ($dimTesto<$size) { return(str_pad($text, $size, $paddingchar, $cmd)); }
967 else {return($text); }
968 }
974 function &GetDrawObject()
975 {
976 $ViewObj=$this->_MakeViewObj();
977 $ViewObj->SetProperty('Size',$this->Size);
978 return($ViewObj);
979 }
980 }
984 class CYap_clsSearchPageSize extends CYap_clsSearchField
985 {
986 function CYap_clsSearchPageSize($cfg,$FieldName)
987 {
988 parent::CYap_clsSearchField($cfg,$FieldName);
989 $this->Size=4;
990 }
1000 function GetWhereCondition($db_class)
1001 {
1002 return('');
1003 }
1004 
1010 function &GetDrawObject()
1011 {
1012 $ViewObj=$this->_MakeViewObj();
1013 $ViewObj->SetProperty('Size',$this->Size);
1014 return($ViewObj);
1015 }
1016 
1017 }
1018 
1030 class CYap_clsSearchRegexOpt extends CYap_clsSearchTxt
1031 {
1032 
1033 var $RegexValue;
1034 function CYap_clsSearchRegexOpt($cfg,$FieldName)
1035 {
1036 parent::CYap_clsSearchTxt($cfg,$FieldName);
1037 }
1043 function _Initialize()
1044 {
1045 parent::_Initialize();
1046 $this->ViewClass='CYap_ViewOptRegex';
1047 }
1055 function LoadCfg($cfg)
1056 {
1057 parent::LoadCFg($cfg);
1058 if(!is_subclass($this->ViewClass,'CYap_ViewOptRegex'))
1059  die('Error: the provided view class '.$this->ViewClass.' is not subclass of CYap_ViewOptRegex');
1060 $this->RegexValue=$this->UseRegex;
1061 }
1065 function GetWhereCondition($Db_Class)
1066 {
1067 // Field not required
1068 if($this->RequestValue=='')
1069  {
1070  return('');
1071  }
1072 
1073 // variabile inserita
1074 if ($this->Type=="A")
1075  {
1076  // Uso regex? Se si alzo il flag del campo FieldUseRegex, se no eseguo il padding del campo
1077  if($this->RegexValue=="1")
1078  return($Db_Class->DbRegexCond($this->DbField,$Db_Class->DBEscape($this->RequestValue)).' ');
1079  else
1080  {
1081  $LookFor=$this->var_padding($this->Size,$this->Padding,$this->PaddingChar,$this->RequestValue);
1082  return($this->DbField.'=\''.$Db_Class->DBEscape($LookFor).'\' ');
1083  }
1084  }
1085 else
1086  {
1087  // numeric field
1088  $LookFor=$this->RequestValue;
1089  return($this->DbField.'='.$Db_Class->DBEscape($LookFor).' ');
1090  }
1091 }
1096 function ReadFormData()
1097 {
1098 // The text is retrieved by the standard function
1099 parent::ReadFormData();
1100 // Now I retrieve the 'complete word flag
1101 if(isset($_REQUEST["F".$this->MyName."_compWord"])&&$_REQUEST["F".$this->MyName."_compWord"]!='')
1102  {
1108  $this->RegexValue=($_REQUEST["F".$this->MyName."_compWord"]=='on')?'0':'1';
1109  }
1110 else
1111  $this->RegexValue=$this->UseRegex;
1112 }
1118 function &GetDrawObject()
1119 {
1120 $ViewObj=$this->_MakeViewObj();
1121 $ViewObj->SetProperty('Size',$this->Size);
1122 $ViewObj->SetProperty('UseRegex',$this->RegexValue);
1123 return($ViewObj);
1124 }
1125 
1134 function StoreInformation($prefix)
1135 {
1136 parent::StoreInformation($prefix);
1137 $_SESSION[$prefix."F".$this->MyName.'_regex']=$this->RegexValue;
1138 }
1146 function LoadInformation($prefix)
1147 {
1148 parent::LoadInformation($prefix);
1149 if(array_key_exists($prefix."F".$this->MyName.'_regex',$_SESSION))
1150  {
1151  $this->RegexValue=$_SESSION[$prefix."F".$this->MyName.'_regex'];
1152  if(is_null($this->RegexValue))
1153  $this->RegexValue=$this->UseRegex;
1154  }
1155 else
1156  $this->RegexValue=$this->UseRegex; // if any
1157 }
1158 
1159 function CleanSessionInfo($prefix)
1160 {
1161 parent::CleanSessionInfo($prefix);
1162 if(isset($_SESSION[$prefix."F".$this->MyName.'_regex']))
1163  unset($_SESSION[$prefix."F".$this->MyName.'_regex']);
1164 }
1165 }
1166 
1167 
1168 
1173 {
1180 
1181 function CYap_clsSearchSelectBox($cfg,$FieldName)
1182 {
1183 parent::CYap_clsSearchField($cfg,$FieldName);
1184 }
1190 function _Initialize()
1191 {
1192 parent::_Initialize();
1193 $this->OptionList=array();
1194 $this->ViewClass='CYap_SelectBox';
1195 $this->DefaultValue='NOFILTER';
1196 }
1204 function LoadCfg($cfg)
1205 {
1206 parent::LoadCFg($cfg);
1207 if(array_key_exists("optionlist",$cfg))
1208  $this->OptionList=$cfg["optionlist"];
1209 if(!is_subclass($this->ViewClass,'CYap_SelectBox'))
1210  die('Error: the provided view class '.$this->ViewClass.' is not subclass of CYap_SelectBox');
1211 $this->RegexValue=$this->UseRegex;
1212 }
1221 function AddOption($Text,$Value)
1222 {
1223 $this->OptionList[$Value]=$Text;
1224 }
1231 function GetWhereCondition($Db_Class)
1232 {
1233 // Field not required
1237 if($this->RequestValue==''||$this->RequestValue=='ALL'||$this->RequestValue=='NOFILTER')
1238  {
1239  return('');
1240  }
1241 $LookFor=$this->RequestValue;
1242 if ($this->Type=="A")
1243  // Search as text
1244  return($this->DbField.'=\''.$Db_Class->DBEscape($LookFor).'\' ');
1245 else
1246  // Search as number
1247  return($this->DbField.'='.$Db_Class->DBEscape($LookFor).' ');
1248 }
1254 function &GetDrawObject()
1255 {
1256 $ViewObj=$this->_MakeViewObj();
1257 $ViewObj->SetProperty('Choices',$this->OptionList);
1258 return($ViewObj);
1259 }
1260 }
1261 
1265 class CYap_clsSearchRangeTxt extends CYap_clsSearchField
1266 {
1267 /*
1268  * Some rules:
1269  *
1270  * $RequestValue is assumed as from,
1271  * $RequestValueTo is the to field
1272  * $DefaultValue is assumed as from
1273  * $DefaultValueTo is the 'to' default
1274  * if only the from, or only the to RequestValue (or DefaultValue) is filled
1275  * the 'to' and the 'from' values are set equal
1276  */
1277 
1278 var $RequestValueTo;
1279 var $DefaultValueTo;
1280 
1281 function CYap_clsSearchRangeTxt($cfg,$FieldName)
1282 {
1283 parent::CYap_clsSearchField($cfg,$FieldName);
1284 $this->UseRegex='0';
1285 if(is_null($this->DefaultValueTo))
1286  $this->DefaultValueTo=$this->DefaultValue;
1287 }
1293 function _Initialize()
1294 {
1295 parent::_Initialize();
1296 $this->DefaultValueTo='';
1297 $this->ViewClass='CYAP_ViewTextRange';
1298 }
1306 function LoadCfg($cfg)
1307 {
1308 parent::LoadCFg($cfg);
1309 if(array_key_exists("defaultto",$cfg))
1310  $this->DefaultValueTo=$cfg["defaultto"];
1311 if(!is_subclass($this->ViewClass,'CYAP_ViewTextRange'))
1312  die('Error: the provided view class '.$this->ViewClass.' is not subclass of CYAP_ViewTextRange');
1313 }
1320 function GetWhereCondition($Db_Class)
1321 {
1322 // Field not required
1323 if($this->RequestValue=='')
1324  {
1325  return('');
1326  }
1327 
1328 // variabile inserita
1329 if ($this->Type=="A")
1330  {
1331  $LookForFrom=$this->var_padding($this->Size,$this->Padding,$this->PaddingChar,$this->RequestValue);
1332  $LookForTo=$this->var_padding($this->Size,$this->Padding,$this->PaddingChar,$this->RequestValueTo);
1333  return($Db_Class->DbBetweenCond($this->DbField,$Db_Class->DBEscape($this->RequestValue),$Db_Class->DBEscape($this->RequestValueTo),'DB_TEXT').' ');
1334  }
1335 else
1336  {
1337  // numeric field
1338  $LookForFrom=$this->RequestValue;
1339  $LookForTo=$this->RequestValueTo;
1340  return($Db_Class->DbBetweenCond($this->DbField,$Db_Class->DBEscape($this->RequestValue),$Db_Class->DBEscape($this->RequestValueTo),'DB_NUMBER').' ');
1341  }
1342 }
1343 
1354 function var_padding($size,$padding,$paddingchar,$text)
1355 {
1356 if($padding=='none') { return($text); }
1357 switch($padding)
1358  {
1359  case 'left': $cmd=STR_PAD_LEFT;
1360  break;
1361  case 'right': $cmd=STR_PAD_RIGHT;
1362  break;
1363  case 'center': $cmd=STR_PAD_BOTH;
1364  break;
1365  default:
1366  trigger_error('ERROR: invalid padding value '.$padding,E_USER_ERROR);
1367  break;
1368  }
1369 $dimTesto=strlen($text);
1370 // se la dimensione è sufficiente restituisco la stringa
1371 if ($dimTesto<$size) { return(str_pad($text, $size, $paddingchar, $cmd)); }
1372 else {return($text); }
1373 }
1374 
1379 function ReadFormData()
1380 {
1381 if(isset($_REQUEST["F".$this->MyName])&&$_REQUEST["F".$this->MyName]!='')
1382  {
1383  $this->RequestValue=$_REQUEST["F".$this->MyName];
1384  if(is_null($this->RequestValue))
1385  $this->RequestValue=$this->DefaultValue;
1386  }
1387 else
1388  $this->RequestValue=$this->DefaultValue;
1389 if(isset($_REQUEST["F".$this->MyName."_To"])&&$_REQUEST["F".$this->MyName."_To"]!='')
1390  {
1391  $this->RequestValueTo=$_REQUEST["F".$this->MyName."_To"];
1392  if(is_null($this->RequestValueTo))
1393  $this->RequestValueTo=$this->DefaultValueTo;
1394  }
1395 else
1396  $this->RequestValueTo=$this->DefaultValueTo;
1397 if((is_null($this->RequestValue)||$this->RequestValue=='')&&!is_null($this->RequestValueTo))
1398  $this->RequestValue=$this->RequestValueTo;
1399 elseif(!is_null($this->RequestValue)&&(is_null($this->RequestValueTo)||$this->RequestValueTo==''))
1400  $this->RequestValueTo=$this->RequestValue;
1401 }
1409 function GetValueTo()
1410 {
1411 if($this->RequestValueTo!='' && !is_null($this->RequestValueTo))
1412  return($this->RequestValueTo);
1413 else
1414  return($this->DefaultValueTo);
1415 }
1424 function SetDefaultValueTo($value)
1425 {
1426 $this->DefaultValueTo=$value;
1427 }
1436 function StoreInformation($prefix)
1437 {
1438 parent::StoreInformation($prefix);
1439 $_SESSION[$prefix."F".$this->MyName."_To"]=$this->RequestValueTo;
1440 }
1448 function LoadInformation($prefix)
1449 {
1450 parent::LoadInformation($prefix);
1451 if(array_key_exists($prefix."F".$this->MyName."_To",$_SESSION))
1452  {
1453  $this->RequestValueTo=$_SESSION[$prefix."F".$this->MyName."_To"];
1454  if(is_null($this->RequestValueTo))
1455  $this->RequestValueTo=$this->DefaultValueTo;
1456  }
1457 else
1458  $this->RequestValueTo=$this->DefaultValueTo; // if any
1459 }
1460 
1461 function CleanSessionInfo($prefix)
1462 {
1463 parent::CleanSessionInfo($prefix);
1464 if(isset($_SESSION[$prefix."F".$this->MyName."_To"]))
1465  unset($_SESSION[$prefix."F".$this->MyName."_To"]);
1466 }
1472 function &GetDrawObject()
1473 {
1474 $ViewObj=$this->_MakeViewObj();
1475 $ViewObj->SetValueTo($this->GetValueTo());
1476 $ViewObj->SetProperty('Size',$this->Size);
1477 $ViewObj->SetControlNameTo('F'.$this->MyName.'_To');
1478 return($ViewObj);
1479 }
1480 }
1481 
1486 {
1487 /*
1488  * Some rules:
1489  * The value is archived as timestamp, but it is shown according to the
1490  * given format
1491  */
1492 
1495 
1496 function CYap_clsSearchDate($cfg,$FieldName)
1497 {
1498 parent::CYap_clsSearchField($cfg,$FieldName);
1499 }
1505 function _Initialize()
1506 {
1507 parent::_Initialize();
1508 $this->ShowFormat='d-m-Y';
1509 $this->WhereFormat='Ymd';
1510 $this->ViewClass='CYAP_ViewDate';
1511 }
1519 function LoadCfg($cfg)
1520 {
1521 parent::LoadCFg($cfg);
1522 if(array_key_exists("showformat",$cfg))
1523  $this->ShowFormat=$cfg["showformat"];
1524 if(array_key_exists("showformat",$cfg))
1525  $this->WhereFormat=$cfg["filterformat"];
1526 if(!is_subclass($this->ViewClass,'CYap_ViewDate'))
1527  die('Error: the provided view class '.$this->ViewClass.' is not subclass of CYAP_ViewDate');
1528 }
1535 function GetWhereCondition($Db_Class)
1536 {
1537 // Field not required
1538 if($this->RequestValue=='')
1539  {
1540  return('');
1541  }
1542 // variabile inserita
1543 if ($this->Type=="A")
1544  {
1545  if($this->WhereFormat==strtoupper('TIMESTAMP'))
1546  $cond= $this->DbField.'= \''.$this->RequestValue.'\'';
1547  else
1548  $cond= $this->DbField.'= \''.date($this->WhereFormat,$this->RequestValue).'\'';
1549  }
1550 else
1551  {
1552  if($this->WhereFormat==strtoupper('TIMESTAMP'))
1553  $cond= $this->DbField.'='.$this->RequestValue.' ';
1554  else
1555  $cond= $this->DbField.'= '.date($this->WhereFormat,$this->RequestValue).' ';
1556  }
1557 return($cond);
1558 }
1559 
1564 function ReadFormData()
1565 {
1566 if(isset($_REQUEST["F".$this->MyName])&&$_REQUEST["F".$this->MyName]!='')
1567  {
1568  $InsertValue=$_REQUEST["F".$this->MyName];
1569  if(is_null($InsertValue))
1570  $this->RequestValue=$this->DefaultValue;
1571  else
1572  {
1573  // Convert $this->ShowFormat to Format string accepted by parseDate
1574  $cond='/([dmYHM])/';
1575  $replace='%${1}';
1576  $Format=preg_replace($cond,$replace,$this->ShowFormat);
1577  $this->RequestValue=@strtotime($this->parseDate($InsertValue,$Format));
1578  if($this->RequestValue===false)
1579  {
1580  // Invalid date . TODO show some message
1581  $this->RequestValue=$this->DefaultValue;
1582  }
1583  }
1584  }
1585 else
1586  $this->RequestValue=$this->DefaultValue;
1587 }
1588 
1606 function parseDate( $date, $format ) {
1607  // Builds up date pattern from the given $format, keeping delimiters in place.
1608  if( !preg_match_all( "/%([YmdHMsu])([^%])*/", $format, $formatTokens, PREG_SET_ORDER ) ) {
1609  return false;
1610  }
1611  $datePattern='';
1612  foreach( $formatTokens as $formatToken ) {
1613  if(array_key_exists(2,$formatToken))
1614  $delimiter = preg_quote( $formatToken[2], "/" );
1615  else
1616  $delimiter='';
1617  if($formatToken[1] == 'Y') {
1618  $datePattern .= '(.{1,4})'.$delimiter;
1619  } elseif($formatToken[1] == 'u') {
1620  $datePattern .= '(.{1,5})'.$delimiter;
1621  } else {
1622  $datePattern .= '(.{1,2})'.$delimiter;
1623  }
1624  }
1625 
1626  // Splits up the given $date
1627  if( !preg_match( "/".$datePattern."/", $date, $dateTokens) ) {
1628  return false;
1629  }
1630  $dateSegments = array();
1631  for($i = 0; $i < count($formatTokens); $i++) {
1632  $dateSegments[$formatTokens[$i][1]] = $dateTokens[$i+1];
1633  }
1634 
1635  // Reformats the given $date into rfc3339
1636 
1637  if( $dateSegments["Y"] && $dateSegments["m"] && $dateSegments["d"] ) {
1638  if( ! checkdate ( $dateSegments["m"], $dateSegments["d"], $dateSegments["Y"] )) { return false; }
1639  $dateReformated =
1640  str_pad($dateSegments["Y"], 4, '0', STR_PAD_LEFT)
1641  ."-".str_pad($dateSegments["m"], 2, '0', STR_PAD_LEFT)
1642  ."-".str_pad($dateSegments["d"], 2, '0', STR_PAD_LEFT);
1643  } else {
1644  return false;
1645  }
1646  /*
1647  if( $dateSegments["H"] && $dateSegments["M"] ) {
1648  $dateReformated .=
1649  "T".str_pad($dateSegments["H"], 2, '0', STR_PAD_LEFT)
1650  .':'.str_pad($dateSegments["M"], 2, '0', STR_PAD_LEFT);
1651 
1652  if( $dateSegments["s"] ) {
1653  $dateReformated .=
1654  ":".str_pad($dateSegments["s"], 2, '0', STR_PAD_LEFT);
1655  if( $dateSegments["u"] ) {
1656  $dateReformated .=
1657  '.'.str_pad($dateSegments["u"], 5, '0', STR_PAD_RIGHT);
1658  }
1659  }
1660  } */
1661 
1662  return $dateReformated;
1663 }
1664 
1665 
1671 function &GetDrawObject()
1672 {
1673 $ViewObj=$this->_MakeViewObj();
1674 $ViewObj->SetProperty('ShowFormat',$this->ShowFormat);
1675 return($ViewObj);
1676 }
1677 }
1678 ?>