YAP  2.5
 All Data Structures Namespaces Files Functions Variables
CYap.php
Go to the documentation of this file.
1 <?php
32 /*
33  * Debug include DON'T Change it !
34  */
35 define('DEBUG',0);
36 // include_once('debug.inc');
41 class CYap {
42 
43 // var $search_field; // see below
57 var $Prefix;
73 var $Lang;
86 // var $Query_Field;
106 var $db;
121 var $Mode;
154 
156 // var $FieldUseRegex; // Se presente per il campo x, forza l'uso di regex nella query di ricerca
157 var $OrderField; // Nome del campo sui cui ordinare l'output. Passato da db_info, puņ essere
158  // modificato cliccando sulla colonna
159 var $OrderDirection; // direzione dell'ordinamento. A=ascendente, D=discendente
160 var $NoSearchBox; // Non visualizzare il box delle ricerche
161 var $NeedOpenDb; // Do I need to open the db connecction? Yes if true.
162 var $DetailMode; // Do I show the full detail of the rows? (True)
163  // Or do I show a table with many rows? (False)
195 function CYap($search_field=array(), $show_field=array(), $db_info=array(), $show_info=array(),$modify_info=array())
196 {
197 // $this->NoSearchBox=FALSE;
198 $this->ShowFirstPage=FALSE;
199 $this->show_field=array();
200 // $this->search_field=array();
201 $this->db_info=array();
202 $this->show_info=array();
203 $this->modify_info=array();
204 $this->SearchCfg=array();
205 $this->Pre_1_0_Style=true;
206 $this->Db_Class=NULL;
207 $this->_SetDefaultHandler();
208 $this->PagSize=-1; // Dummy value. It should be set by CYap_Search object
209 
210 // Check the configuration file (if set)
211 if(array_key_exists('cfgfile',$show_info))
212  $this->_LoadCfg($show_info['cfgfile']);
214 $this->check_db($db_info);
217 // Made the search object. Load and parse the configuration
218 // add the field rows per page and submit button. At the right moment all this
219 // mess will be shown.
220 // If no search fields are configurated, the searchbox will be disabled,
221 // NoSearchBox set to true
222 $this->LoadSearchCfg($search_field);
223 $this->SearchObj=new CYap_clsSearch($this->SearchCfg,$this->Lang);
224 // $this->NoSearchBox=$this->SearchObj->NoSearchBox();
225 // $this->PagSize=($this->NoSearchBox)?1000:15;
226 $this->SearchObj->AddRowPerPage();
227 
228 
229 // $this->FieldUseRegex=array();
230 // check if detail mode is on or off.
231 // Off by default, to set on, I look for the session parameter or the request
232 // parameter (see load_previous_data)
233 $this->DetailMode=FALSE;
234 $this->WrongFields=array();
235 
236 }
237 
243 function _LoadCfg($FileName)
244 {
245 require_once($FileName);
246 if(isset($SearchFieldCfg))
247  {
248  if(is_array($SearchFieldCfg))
249  $this->SearchCfg=$SearchFieldCfg;
250  else
251  trigger_error('Configuratin error. $SearchFieldCfg should be an array'.E_USER_ERROR);
252  }
253 if(isset($ShowFieldCfg))
254  {
255  if(is_array($ShowFieldCfg))
256  $this->show_field=$ShowFieldCfg;
257  else
258  trigger_error('Configuratin error. $ShowFieldCfg should be an array'.E_USER_ERROR);
259  }
260 if(isset($DbInfoCfg))
261  {
262  if(is_array($DbInfoCfg))
263  $this->db_info=$DbInfoCfg;
264  else
265  trigger_error('Configuratin error. $DbInfoCfg should be an array'.E_USER_ERROR);
266  }
267 if(isset($ShowInfoCfg))
268  {
269  if(is_array($ShowInfoCfg))
270  $this->show_info=$ShowInfoCfg;
271  else
272  trigger_error('Configuratin error. $ShowInfoCfg should be an array'.E_USER_ERROR);
273  }
274 if(isset($ModifyInfoCfg))
275  {
276  if(is_array($ModifyInfoCfg))
277  $this->modify_info=$ModifyInfoCfg;
278  else
279  trigger_error('Configuratin error. $ModifyInfoCfg should be an array'.E_USER_ERROR);
280  }
281 }
282 
283 
284 
292 function check_showfield($info)
293 {
294 foreach($info as $key => $value)
295  $this->show_field[$key]=$value;
296 }
297 
305 function check_db($info)
306 {
307 foreach($info as $key => $value)
308  $this->db_info[$key]=$value;
309 $this->NeedOpenDb=true;
310 
311 /*
312  * Setting the order field.
313  * If nothing is indicated, use the field id with the ascending order
314  */
315 if(isset($this->db_info["orderfield"])) { $this->OrderField=$this->db_info["orderfield"]; }
316 else { $this->OrderField="id"; }
317 
318 if(isset($this->db_info["orderdirection"])) { $this->OrderDirection=$this->db_info["orderdirection"]; }
319 else { $this->OrderDirection="A"; }
320 
321 if(isset($this->db_info["needopendb"])) { $this->NeedOpenDb=$this->db_info["needopendb"]; }
322 
323 if($this->NeedOpenDb) $this->DbOpened=false;
324 else $this->DbOpened=true;
325 if(array_key_exists('db_class',$this->db_info))
326  {
327  // one class is set, do it exists?
328  if(!class_exists($this->db_info['db_class']))
329  trigger_error('ERROR: the class '.$this->db_info['db_class'].' does not exists',E_USER_ERROR);
330  eval('$this->Db_Class=new '.$this->db_info['db_class'].'();');
331  }
332 else
333  {
334  // no preference, use the default: mysql
335  $this->Db_Class=new CYap_mysql();
336  }
337 }
346 function LoadSearchCfg($info)
347 {
348 foreach($info as $key => $value)
349  $this->SearchCfg[$key]=$value;
350 }
351 
359 function check_show_info($info)
360 {
361 foreach($info as $key => $value)
362  $this->show_info[$key]=$value;
363 if(array_key_exists('prefix',$this->show_info))
364  $this->Prefix=$this->show_info['prefix'];
365 $fileName='./language.en';
366 if(array_key_exists('languagefile',$this->show_info))
367  $fileName=$this->show_info['languagefile'];
368 $this->show_info['languagefile']=$fileName;
369 $this->Lang=parse_ini_file($fileName);
370 if(array_key_exists('showfirstcallrow',$this->show_info))
371  $this->ShowFirstPage=$this->show_info['showfirstcallrow'];
372 $defMode=(array_key_exists('detailmode',$this->show_info))?(($this->show_info['detailmode'])?'DisplayDetail':'DisplayTable'):'DisplayTable';
373 $tmpMode=(array_key_exists('mode',$this->show_info))?$this->show_info['mode']:$defMode;
374 switch(strtolower($tmpMode))
375  {
376  case 'displaytable':
377  $this->Mode='DisplayTable';
378  break;
379  case 'displaydetail':
380  $this->Mode='DisplayDetail';
381  break;
382  case 'modify':
383  $this->Mode='Modify';
384  if(!array_key_exists('keyid',$this->show_info))
385  trigger_error('Modify mode requested, but keyid is not supplied',E_USER_ERROR);
386  break;
387  case 'add':
388  $this->Mode='Add';
389  break;
390  default:
391  trigger_error('Invalid mode',E_USER_ERROR);
392  break;
393  }
394 if(!array_key_exists('onokhref',$this->show_info))
395  $this->show_info['onokhref']=$_SERVER['PHP_SELF'];
396 if(!array_key_exists('onaborthref',$this->show_info))
397  {
398  // by default, set the event to YAP_ABORTMODIFY and submit the form
399  $this->show_info['onaborthref']='javascript:this.form.ev.value=\'YAP_ABORTMODIFY\';this.form.submit()';
400  }
401 else
402  {
403  // If request any other page, set the abort event and call that page
404  $this->show_info['onaborthref']='javascript:this.form.ev.value=\'YAP_ABORTMODIFY\';
405  this.form.action=\''.$info['onaborthref'].'\';
406  this.form.submit()';
407  }
408 // rendering class
409 if(!array_key_exists('boxclass',$this->show_info))
410  $this->show_info['boxclass']='CYapBox';
411 if(!array_key_exists('detailviewclass',$this->show_info))
412  $this->show_info['detailviewclass']='CYap_DetailView';
413 if(!array_key_exists('tableviewclass',$this->show_info))
414  $this->show_info['tableviewclass']='CYap_TableView';
415 if(!array_key_exists('skippageclass',$this->show_info))
416  $this->show_info['skippageclass']='CYap_MoveToPage';
417 if(!array_key_exists('pagenumberclass',$this->show_info))
418  $this->show_info['pagenumberclass']='CYap_PageNumber';
419 if(!array_key_exists('formclass',$this->show_info))
420  $this->show_info['formclass']='CYap_Form';
421 // check whether the classes exist
422 if(!class_exists($this->show_info['boxclass']))
423  trigger_error('Undefined class '.$this->show_info['boxclass'],E_USER_ERROR);
424 if(!class_exists($this->show_info['detailviewclass']))
425  trigger_error('Undefined class '.$this->show_info['detailviewclass'],E_USER_ERROR);
426 if(!class_exists($this->show_info['tableviewclass']))
427  trigger_error('Undefined class '.$this->show_info['tableviewclass'],E_USER_ERROR);
428 if(!class_exists($this->show_info['skippageclass']))
429  trigger_error('Undefined class '.$this->show_info['skippageclass'],E_USER_ERROR);
430 if(!class_exists($this->show_info['pagenumberclass']))
431  trigger_error('Undefined class '.$this->show_info['pagenumberclass'],E_USER_ERROR);
432 if(!class_exists($this->show_info['formclass']))
433  trigger_error('Undefined class '.$this->show_info['formclass'],E_USER_ERROR);
434 if(!array_key_exists('customdetpage',$this->show_info))
435  $this->show_info['customdetpage']=$_SERVER['PHP_SELF'];
436 if(!array_key_exists('showpagenumber',$this->show_info))
437  $this->show_info['showpagenumber']=true;
438 if(!array_key_exists('showmovepage',$this->show_info))
439  $this->show_info['showmovepage']=true;
440 $this->_ManageFormats();
441 if(array_key_exists('pre_1_0_style',$this->show_info))
442  $this->Pre_1_0_Style=$this->show_info['pre_1_0_style'];
443 }
444 
452 function check_modify_info($info)
453 {
454 foreach($info as $key => $value)
455  $this->modify_info[$key]=$value;
456 //
457 if(!array_key_exists('showadd',$this->modify_info))
458  $this->modify_info['showadd']=false;
459 if(!array_key_exists('showdelete',$this->modify_info))
460  $this->modify_info['showdelete']=false;
461 if(!array_key_exists('showmodify',$this->modify_info))
462  $this->modify_info['showmodify']=false;
463 // If the modify functions are requested, the index table and keyfield
464 // should be filled
465 if($this->modify_info['showmodify']
466  ||$this->modify_info['showdelete']
467  ||$this->modify_info['showadd'])
468  {
469  if(!array_key_exists('table',$this->modify_info))
470  trigger_error('ERROR: index table not found in modify_info array',E_USER_ERROR);
471  if(!array_key_exists('keyfield',$this->modify_info))
472  trigger_error('ERROR: index keyfield not found in modify_info array',E_USER_ERROR);
473  }
474 if(!array_key_exists('fields',$this->modify_info))
475  $this->modify_info['fields']=array();
476 else
477  $this->modify_info['fields']=$this->modify_info['fields'];
478 if(array_key_exists('fieldvalidator',$this->modify_info))
479  $validator=$this->modify_info['fieldvalidator'];
480 else
481  $validator=array();
482 if(array_key_exists('fieldtorequest',$this->modify_info))
483  {
484  // convert the old structure into the new one
485  foreach($this->modify_info['fieldtorequest'] as $FieldName)
486  {
487  if(!array_key_exists($FieldName,$this->modify_info['fields']))
488  $this->modify_info['fields'][$FieldName]=array();
489  if(array_key_exists($FieldName,$validator))
490  $this->modify_info['fields'][$FieldName]['fieldvalidator']=$validator[$FieldName];
491  }
492  }
493 if(!array_key_exists('customaddpage',$this->modify_info))
494  $this->modify_info['customaddpage']=$_SERVER['PHP_SELF'];
495 if(!array_key_exists('custommodifypage',$this->modify_info))
496  $this->modify_info['custommodifypage']=$_SERVER['PHP_SELF'];
497 if(!array_key_exists('customdeletepage',$this->modify_info))
498  $this->modify_info['customdeletepage']=$_SERVER['PHP_SELF'];
499 }
507 function &GetSearchObject()
508 {
509  $ret=&$this->SearchObj;
510  return($ret);
511 }
520 function SetSearchObject(&$newObject)
521 {
522 $this->SearchObj=$newObject;
523 }
539 function _ManageFormats()
540 {
541 // Preset formats
542 $PresetFormat=array (
543  'NONE' => '$txt_out=$txt_in;',
544  'NUMERIC_IT' => '$txt_out=number_format($txt_in,0,",",".");',
545  'NUMERIC_EN' => '$txt_out=number_format($txt_in,2);',
546  'MONEY_IT' => '$txt_out=number_format($txt_in,2,",",".");',
547  'MONEY_EN' => '$txt_out=number_format($txt_in,2,".",",");',
548  'DATE_IT' => '$txt_out=date("d/m/Y",$txt_in);',
549  'DATE_US' => '$txt_out=date("m/d/Y",$txt_in);',
550  );
551 $this->OutFormats=array();
552 // Load the preset formats
553 foreach ($PresetFormat as $key => $value)
554  $this->OutFormats[$key]=$value;
555 // Load the user formats
556 if(array_key_exists('outformats',$this->show_info))
557  {
558  foreach($this->show_info['outformats'] as $formatName => $formatParam)
559  {
560  $cmd='$txt_in';
561  if(array_key_exists('number',$formatParam))
562  { $cmd='number_format($txt_in,'.$formatParam['number'].')'; }
563  if(array_key_exists('date',$formatParam))
564  { $cmd='date("'.$formatParam['date'].'",'.$cmd.')'; }
565  if(array_key_exists('sprintf',$formatParam))
566  { $cmd='sprintf("'.$formatParam['sprintf'].'",'.$cmd.')'; }
567  if(array_key_exists('custom',$formatParam))
568  { $cmd=$formatParam['custom'].'('.$cmd.')'; }
569  $cmd='$txt_out='.$cmd.';';
570  $this->OutFormats[$formatName]=$cmd;
571  }
572  }
573 }
574 
581 function ForgetAll()
582 {
583  if(isset($_SESSION[$this->Prefix."DatiPerQuery"])) {unset($_SESSION[$this->Prefix."DatiPerQuery"]); }
584  if(isset($_SESSION[$this->Prefix."TotRow"])) { unset($_SESSION[$this->Prefix."TotRow"]); }
585  if(isset($_SESSION[$this->Prefix."OrderDirection"])) { unset($_SESSION[$this->Prefix."OrderDirection"]); }
586  if(array_key_exists($this->Prefix."DetailMode",$_SESSION)) {unset($_SESSION[$this->Prefix."DetailMode"]);}
587  if(isset($_SESSION[$this->Prefix."OrderField"])) { unset($_SESSION[$this->Prefix."OrderField"]);
588  unset($_SESSION["OrderField"]);
589  $this->SearchObj->CleanSessionInfo($this->Prefix);
590  }
591 }
592 
599 {
600  if(DEBUG) $GLOBALS['debug']->f_in('_InitializeSessionVar');
601  // Remove any previous istance of the class, if it exists in the current session
602  $this->ForgetAll();
603  // save the referer for the exit link (if not set from cfg parameters)
604  if(isset($this->show_info["exit"])) { $this->PagerExit=$this->show_info["exit"];}
605  else { if(array_key_exists('HTTP_REFERER',$_SERVER))
606  $this->PagerExit=$_SERVER['HTTP_REFERER'];
607  else
608  $this->PagerExit='';
609  }
610  $_SESSION[$this->Prefix."PagerExit"]=$this->PagerExit;
611  // Pre-set of the numebr of row
612  // $this->PagSize=($this->NoSearchBox)?1000:15;
613  // $_SESSION[$this->Prefix."PagSize"]=$this->PagSize;
614  if(DEBUG) $GLOBALS['debug']->f_out('_InitializeSessionVar');
615 }
616 
617 
618 /**************************************************************************************************************
619  * E V E N T H A N D L E R *
620  **************************************************************************************************************
621  * Event related functions
622  */
628 function _DoEvent($ev)
629 {
630 // Yap's event name start with yap_
631 if(substr($ev,0,4)!='YAP_')
632  return(0);
633 if(!array_key_exists($ev,$this->EventTable))
634  trigger_error('Unknown event '.$ev,E_USER_ERROR);
635 // Some evant may have not an associated handler
636 if($this->EventTable[$ev]!='')
637  {
638  if(method_exists($this,$this->EventTable[$ev]))
639  call_user_func(array(&$this,$this->EventTable[$ev]));
640  elseif(function_exists($this->EventTable[$ev]))
641  call_user_func($this->EventTable[$ev]);
642  else
643  trigger_error('ERROR: the requested handler '.$this->EventTable[$ev].' does not exist',E_USER_ERROR);
644  }
645 return(0);
646 }
647 
654 {
655 $this->EventTable=array(
656  'YAP_ORDERUP' => '_OnOrderUp',
657  'YAP_ORDERDOWN' => '_OnOrderDown',
658  'YAP_DETAILCLICK' => '_OnDetailClick',
659  'YAP_MODIFYCLICK' => '_OnModifyClick',
660  'YAP_DOMODIFY' => '_OnDoModify',
661  'YAP_ABORTMODIFY' => '_OnAbortModify',
662  'YAP_REMOVECLICK' => '_OnRemoveClick',
663  'YAP_DOREMOVE' => '_OnDoRemove',
664  'YAP_ABORTREMOVE' => '_OnAbortRemove',
665  'YAP_CMDEXECUTED' => '_OnCmdExecuted',
666  'YAP_TOPAGE' => '_OnToPage',
667  'YAP_EXIT' => '_OnExit',
668  'YAP_TABLEVIEW' => '_OnTableView',
669  'YAP_NEWROW' => '_OnNewRow',
670  'YAP_DOSAVEROW' => '_OnDoSaveRow',
671  'YAP_ABORTSAVEROW' => '_OnAbortSaveRow',
672  'YAP_SEARCH' => '_OnSearch',
673  'YAP_FIRSTTIME' => '_OnFirstTime',
674  'YAP_DRAWCURRENT' => '_OnDrawCurrent',
675  );
676 }
684 function RegisterEvent($EventName,$Handler)
685 {
686 $this->EventTable[$EventName]=$Handler;
687 return(true);
688 }
689 
694 function _OnOrderUp()
695 {
696 $this->_RunDisplayFunc();
697 }
702 function _OnOrderDown()
703 {
704 $this->_RunDisplayFunc();
705 }
710 function _OnDetailClick()
711 {
712 $this->DetailMode=TRUE;
713 $this->Mode='DisplayDetail';
714 $this->_RunDisplayFunc();
715 }
720 function _OnModifyClick()
721 {
722 // modify the row
723 $this->_ModifyInputForm(false);
724 }
730 function _OnDoModify()
731 {
732 // apply the modify
733 // If some field is invalid, I show the form again
734 if(!$this->_ModifyExecute(false))
735  $this->_ModifyInputForm(false,$_SESSION[$this->Prefix.'KeyToUpdate']);
736 else
737  $this->_ModifySuccessed();
738 }
744 function _OnAbortModify()
745 {
746 $this->_RunDisplayFunc();
747 }
752 function _OnRemoveClick()
753 {
754 $this->_DeleteRequestConfirm();
755 }
760 function _OnDoRemove()
761 {
762 $this->_DeleteExecute();
763 }
768 function _OnAbortRemove()
769 {
770 $this->_RunDisplayFunc();
771 }
776 function _OnCmdExecuted()
777 {
778 $this->_RunDisplayFunc();
779 }
785 function _OnToPage()
786 {
787 // exist the pag parameter? The 'skip to new page' event, requires the pag parameter
788 if(!array_key_exists('pag',$_REQUEST))
789  trigger_error('ERROR: the parameter pag doesn\'t exist',E_USER_ERROR);
790 $this->_RunDisplayFunc();
791 }
792 
793 function _OnExit()
794 {
795 $this->_RunDisplayFunc();
796 }
801 function _OnTableView()
802 {
803 $this->DetailMode=FALSE;
804 $this->Mode='DisplayTable';
805 $this->_RunDisplayFunc();
806 }
812 function _OnNewRow()
813 {
814  // add a new row
815 $this->_ModifyInputForm(true);
816 }
821 function _OnDoSaveRow()
822 {
823 // execute the addition
824 if(!$this->_ModifyExecute(true))
825  $this->_ModifyInputForm(true,$_SESSION[$this->Prefix.'KeyToUpdate']);
826 else
827  $this->_ModifySuccessed();
828 }
833 function _OnAbortSaveRow()
834 {
835 $this->_RunDisplayFunc();
836 }
842 function _OnSearch()
843 {
844 $this->analize_request();
845 $this->_RunDisplayFunc();
846 }
852 function _OnFirstTime()
853 {
854 $this->_InitializeSessionVar();
855 // If DetailMode is requested by config array
856 if($this->Mode=='DisplayDetail') $this->DetailMode=TRUE;
857 /*
858  * If I don't need to show the search box, I assume that the query from cfg parms is complete
859  * so I parse it.
860  */
861 $NoSearchBox=$this->SearchObj->NoSearchBox();
862 if($NoSearchBox||$this->ShowFirstPage)
863  $this->analize_request();
864 $_SESSION[$this->Prefix."daform"]=1;
865 $this->_RunDisplayFunc();
866 }
871 function _OnDrawCurrent()
872 {
873 $this->_RunDisplayFunc();
874 }
875 
876 /**************************************************************************************************************
877  * P A G E S H O W *
878  **************************************************************************************************************
879  */
886 function showpage($ForcedEvent='')
887 {
888 if(DEBUG)
889  {
890  $GLOBALS['debug']->f_in('showpage');
891  $GLOBALS['debug']->startTimer();
892  $GLOBALS['debug']->DumpVar($_SESSION,'$_SESSION');
893  }
894 $ev='YAP_FIRSTTIME';
895 if($ForcedEvent!='')
896  $ev=$ForcedEvent;
897 else
898  {
899  // Parse the event, if exists
900  if(!array_key_exists('ev',$_REQUEST))
901  // No event, start from the begin
902  $ev='YAP_FIRSTTIME';
903  else
904  {
905  if(substr($_REQUEST['ev'],0,4)=='YAP_')
906  // My event
907  $ev=$_REQUEST['ev'];
908  else
909  // Application event, not mine, simply draw from the current state
910  $ev='YAP_DRAWCURRENT';
911  }
912  }
913 
914 // first step, Is the first time that I run?. If so save the referrer and initialize
915 // session's variable
916 // $FirstTime=true;
917 // If I find the mode store into session, I suppose not to be the first time the module run
918 if(!array_key_exists($this->Prefix."DetailMode",$_SESSION))
919  $ev='YAP_FIRSTTIME';
920 // If the reset command is request, clear all session var and restart all
921 if(array_key_exists("reset",$_REQUEST)) $ev='YAP_FIRSTTIME';
922 
923 if($ev!='YAP_FIRSTTIME')
924  {
925  // It not the first time the script run, Check if I'm able to find my session vars
926  if(!array_key_exists($this->Prefix."PagerExit",$_SESSION))
927  {
928  /* Something goes wrong, its the second o more time I run, but I haven't
929  * my session vars. Probably session time-out, or, worst someone try to hack the script!
930  */
931  trigger_error("Error: I'm not able to find the session vars!",E_USER_ERROR);
932  }
933  // Get the exit link from the session vars
934  $this->PagerExit=$_SESSION[$this->Prefix."PagerExit"];
935  }
936 
937 // Load information from previous execution from session vars.
938 if($ev!='YAP_FIRSTTIME'
939  &&$ev!='YAP_QUERY')
940  $this->load_previous_data();
941 /*
942  * Output the javascript required to run
943  */
944 $this->_JsOut();
945 
946 // Event handler
947 $this->_DoEvent($ev);
948 
949 // Store the module status into session
950 $this->_SaveToSession();
951 
952 if(DEBUG)
953  {
954  $GLOBALS['debug']->stopTimer();
955  $GLOBALS['debug']->getTime();
956  $GLOBALS['debug']->DumpVar($_SESSION,'$_SESSION');
957  $GLOBALS['debug']->f_out('showpage');
958  $GLOBALS['debug']->ShowTrace();
959  $GLOBALS['debug']->SaveTrace('trace.txt');
960  }
961 }
962 
967 function _SaveToSession()
968 {
969 $_SESSION[$this->Prefix."DetailMode"]=$this->DetailMode;
970 }
971 
976 function _RunDisplayFunc()
977 {
978  // I execute the mode option only without command from POST parameter
979  // That command override the mode suggested by the configuration info
980  switch($this->Mode)
981  {
982  case 'DisplayTable':
983  case 'DisplayDetail':
984  $this->_DisplayData();
985  // add row link, if requested
986  /*
987  if(array_key_exists('showadd',$this->modify_info))
988  {
989  if($this->modify_info['showadd'])
990  $this->_AddHrefLink();
991  }
992  */
993  /*
994  * Show the search box
995  */
996  $NoSearchBox=$this->SearchObj->NoSearchBox();
997  if(!$NoSearchBox)
998  {
999  $this->_JsSearchBoxCheck();
1000  $this->show_form();
1001  }
1002  break;
1003  case 'Add':
1004  $this->_ModifyInputForm(true);
1005  break;
1006  case 'Modify':
1007  $this->_ModifyInputForm(false,$this->show_info['keyid']);
1008  break;
1009  default:
1010  trigger_error('Internal Error: invalid mode',E_USER_ERROR);
1011  break;
1012  }
1013 
1014 }
1015 
1020 function _DisplayData()
1021 {
1022 /*
1023  * if I've a previous query, I show the requested page
1024  */
1025 if(isset($this->PageRequest)&&$this->PageRequest>0)
1026  {
1027  // Save the current page. In the new page it will be override by
1028  // $_REQUEST['pag'] if specified
1029  $_SESSION[$this->Prefix.'pag']=$this->PageRequest;
1030  // Has someone clicked on column header to change the table's field order?
1031  $this->check_orderfield();
1032 
1033  // perform the query to the database
1034  $this->perform_query();
1035 
1036  // Show the founded rows
1037  $this->show_table_rows();
1038 
1039  // Show up the selectors
1040  $this->show_selectors();
1041  $this->select_page();
1042  }
1043 }
1044 
1045 /********************************************************************************
1046  * PARAMETERS PARSING FUNCTIONS
1047  ********************************************************************************/
1054 {
1055 if(DEBUG) $GLOBALS['debug']->f_in('analize_request');
1056 
1057 // Retrieve data from search form
1058 $this->SearchObj->ReadFormData();
1059 // Save data into session for later usage
1060 $this->SearchObj->StoreInformation($this->Prefix);
1061 $this->PagSize=$this->SearchObj->GetPageSize();
1062 $_SESSION[$this->Prefix."PagSize"]=$this->PagSize;
1063 
1064 /*
1065  * Request to show the first page. If I have got a new request (=someone has filled
1066  * the search box) I must start from the begin
1067  */
1068 $this->PageRequest=1;
1069 
1070 /*
1071  * Nuova query, azzero il contatore di record nella query
1072  */
1073 if(isset($_SESSION[$this->Prefix."TotRow"])) { unset($_SESSION[$this->Prefix."TotRow"]); }
1074 if(DEBUG) $GLOBALS['debug']->f_out('analize_request');
1075 }
1076 
1084 {
1085 if(DEBUG) $GLOBALS['debug']->f_in('load_previous_data');
1086 /*
1087  * Page requested from the previous execution. This will be the
1088  * shown page, if I haven't any page request from $_REQUEST
1089  */
1090 if(array_key_exists($this->Prefix."pag",$_SESSION))
1091  $this->PageRequest=$_SESSION[$this->Prefix."pag"];
1092 if(isset($_REQUEST["pag"])) {$this->PageRequest=$_REQUEST["pag"]; }
1093 
1094 $this->SearchObj->LoadInformation($this->Prefix);
1095 if(array_key_exists($this->Prefix."PagSize",$_SESSION))
1096  $this->PagSize=$_SESSION[$this->Prefix."PagSize"];
1097 
1098 /*
1099  * Se sono stati impostati dei parametri di ordinamento diversi dal default,
1100  * li carico ora
1101  */
1102 if(isset($_SESSION[$this->Prefix."OrderDirection"])) { $this->OrderDirection=$_SESSION[$this->Prefix."OrderDirection"]; }
1103 if(isset($_SESSION[$this->Prefix."OrderField"])) { $this->OrderField=$_SESSION[$this->Prefix."OrderField"]; }
1104 
1105 
1106 // Detail mode setup
1107 // Parametr saved by the previous page
1108 if(array_key_exists($this->Prefix."DetailMode",$_SESSION)) {$this->DetailMode=$_SESSION[$this->Prefix."DetailMode"];}
1109 $this->Mode=($this->DetailMode)?'DisplayDetail':'DisplayTable';
1110 // New request coming from the user
1111 // if(array_key_exists("DetailMode",$_REQUEST)) {$this->DetailMode=TRUE;}
1112 // If DetailMode is requested by config array, it overwrite all previous set
1113 // if($this->Mode=='DisplayDetail') $this->DetailMode=TRUE;
1114 // Handle the request to exit from the detailmode
1115 // if(array_key_exists("NoDetailMode",$_REQUEST)) {$this->DetailMode=FALSE;}
1116 if(DEBUG) $GLOBALS['debug']->f_out('load_previous_data');
1117 }
1118 
1128 {
1129 $this->SearchObj->JsSearchFormValidate();
1130 }
1131 
1139 function show_form()
1140 {
1141 // Get the seach form from the Search object
1142 $txt=$this->SearchObj->Draw();
1143 // preparazione del msgbox
1144 echo "<table align=center>
1145  <tr><td>";
1146 $this->_ShowSearchBox($txt);
1147 echo "</td></tr></table>";
1148 }
1149 
1156 function _ShowSearchBox($txt)
1157 {
1158 eval('$box=new '.$this->show_info['boxclass'].'($txt,\'#000000\',\'#EFEF52\');');
1159 // $box=new CYapBox($txt,'#000000','#EFEF52');
1160 $box->Show();
1161 }
1162 
1175 {
1176 if(isset($_REQUEST["of"]))
1177  {
1178  $this->OrderField=$_REQUEST["of"];
1179  $_SESSION[$this->Prefix."OrderField"]=$_REQUEST["of"];
1180  }
1181 
1182 if(isset($_REQUEST["ot"]))
1183  {
1184  $this->OrderDirection=$_REQUEST["ot"];
1185  $_SESSION[$this->Prefix."OrderDirection"]=$_REQUEST["ot"];
1186  }
1187 
1188 }
1189 
1190 
1197 function perform_query()
1198 {
1199 if(!$this->DbOpened) $this->_OpenDb();
1200 /*
1201  * Determinazione del numero di record nella query. Se il dato č memorizzato,
1202  * non eseguo pił la query
1203  */
1204 if(isset($_SESSION[$this->Prefix."TotRow"])) { $this->TotRow=$_SESSION[$this->Prefix."TotRow"]; }
1205 else
1206  {
1207  // How many rows will return this query?
1208  $sql=$this->db_info["selectquery"] . "1=1 "; // inserita condizione stupida per avere almeno
1209  // una condizione in caso non se ne impostino
1210  $sql.=$this->SearchObj->GetWhereCondition($this->Db_Class);
1211  // and now perform the query
1212  if(($result=$this->Db_Class->DbExecSql($sql))===FALSE)
1213  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
1214  $this->TotRow=$this->Db_Class->DbGetNumRow($result);
1215  $_SESSSION["TotRow"]=$this->TotRow;
1216  $this->Db_Class->DbFreeResult($result);
1217  } // end if
1218 }
1219 
1220 
1221 /********************************************************************************
1222  * SHOW ROW FUNCTIONS
1223  ********************************************************************************/
1224 
1231 {
1232 if(DEBUG) $GLOBALS['debug']->f_in('show_table_rows');
1233 // Query sul database (gią aperto da perform_query)
1234 $sql=$this->db_info["selectquery"]. "1=1 "; // inserita condizione stupida per avere almeno
1235  // una condizione in caso non se ne impostino
1236 $sql.=$this->SearchObj->GetWhereCondition($this->Db_Class);
1237 
1238 // Definita la condizione where, imposto la chiave di ordinamento
1239 $sql.=" order by " . $this->OrderField;
1240 if($this->OrderDirection=="D") { $sql.=" DESC"; }
1241 
1242 // Limiti di visualizzazione in base alla pagina corrente
1243 if($this->DetailMode)
1244  {
1245  $StartLimit=($this->PageRequest-1);
1246  $EndLimit=1;
1247  }
1248 else
1249  {
1250  $StartLimit=($this->PageRequest-1)*$this->PagSize;
1251  $EndLimit=$this->PagSize;
1252  }
1253 // $sql.=" limit ".$StartLimit.",".$EndLimit;
1254 
1255 // var_dump($sql);
1256 if(DEBUG) $GLOBALS['debug']->ToTrace($sql);
1257 // ToTrace("sql:".$sql);
1258 // ed ora eseguo la query
1259 if(($result=$this->Db_Class->QueryLimit($sql,$StartLimit,$EndLimit))==FALSE)
1260  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
1261 $Fields=$this->_RetrieveFieldType($result,false);
1262 /*
1263  * Split the display function between normal mode and detailMode
1264  */
1265 if($this->DetailMode)
1266  $this->_ShowDetailMode($Fields,$result);
1267 else
1268  $this->_ShowTableMode($Fields,$result);
1269 $this->Db_Class->DbFreeResult($result);
1270 if(DEBUG) $GLOBALS['debug']->f_out('show_table_rows');
1271 }
1272 
1273 
1274 function _ShowDetailMode($Fields,$result)
1275 {
1276 // DetailMode
1277  eval('$clsTable=new '.$this->show_info['detailviewclass'].'($this->show_info[\'languagefile\']);');
1278  //$clsTable=new CYap_DetailView($this->show_info['languagefile']);
1279  $clsTable->LoadFormats($this->OutFormats);
1280  $clsTable->BeginTable();
1281  $clsTable->HeaderStart();
1282  // Data
1283  // $line = mysql_fetch_array($result, MYSQL_ASSOC);
1284  $line=$this->Db_Class->DbGetValue($result);
1285 
1286  // ShowDetail
1287  $this->show_detail_row($line,true,$Fields,$clsTable);
1288  // chiusura del ciclo
1289  $clsTable->EndTable();
1290  $clsTable->FlushCode();
1291 }
1292 
1293 function _ShowTableMode($Fields,$result)
1294 {
1295 eval('$clsTable=new '.$this->show_info['tableviewclass'].'($this->show_info[\'languagefile\']);');
1296  // $clsTable=new CYap_TableView($this->show_info['languagefile']);
1297  // 'Normal Mode'
1298  $clsTable->LoadFormats($this->OutFormats);
1299  $clsTable->BeginTable();
1300  // Visualizzazione delle intestazioni di colonna
1301  $this->show_header($clsTable);
1302 
1303  // Ciclo sui record della query
1304  $counter=0;
1305  while ($line = $this->Db_Class->DbGetValue($result))
1306  {
1307  $counter++;
1308  $this->show_single_row($line,$counter,$clsTable);
1309  }
1310  // chiusura del ciclo
1311  if(array_key_exists('showadd',$this->modify_info))
1312  {
1313  if($this->modify_info['showadd'])
1314  $this->_AddHrefLink($clsTable);
1315  }
1316  $clsTable->EndTable();
1317  $clsTable->FlushCode();
1318 }
1319 
1320 
1321 
1330 function show_header(&$clsTable)
1331 {
1332 /* @var $clsTable CYap_TableView */
1333 $clsTable->HeaderStart();
1334 // How many column should I show?
1335 $NItem=count($this->show_field);
1336 
1337 // var_dump($this->OrderField);
1338 
1339 for($i=0;$i<$NItem;$i++)
1340  {
1341  // May I show this column?
1342  $ToShow=(array_key_exists('showtable',$this->show_field[$i]))?
1343  $this->show_field[$i]['showtable']: // paramater exists, check it
1344  true; // it doesn't exist, show the field
1345  if($ToShow)
1346  {
1347  // New order key or new direction
1348  $Href= $_SERVER["PHP_SELF"] ."?pag=".$this->PageRequest."&amp;of=".$this->show_field[$i]["fieldname"]."&amp;ot=";
1349  if($this->show_field[$i]["fieldname"]==$this->OrderField)
1350  {
1351  $Href.= ($this->OrderDirection=="A")?"D":"A";
1352  $Href.='&amp;ev=YAP_ORDER';
1353  $Href.= ($this->OrderDirection=="A")?"DOWN":"UP";
1354  }
1355  else { $Href.= "A&amp;ev=YAP_ORDERUP"; }
1356  $Label=$this->show_field[$i]["header"];
1357  $clsTable->HeaderCell($Href,$Label);
1358  }
1359  }
1360 $clsTable->HeaderClose();
1361 }
1362 
1372 function show_single_row($RowData,$RowNumber,&$clsTable)
1373 {
1374 /* @var $clsTable CYap_TableView */
1375 $clsTable->NewRow();
1376 // Quante colonne devo visualizzare
1377 $NItem=count($this->show_field);
1378 for($i=0;$i<$NItem;$i++)
1379  {
1380  // May I show this column?
1381  $ToShow=(array_key_exists('showtable',$this->show_field[$i]))?
1382  $this->show_field[$i]['showtable']: // paramater exists, check it
1383  true; // it doesn't exist, show the field
1384  if($ToShow)
1385  {
1386  $fmt=(array_key_exists('format',$this->show_field[$i]))?$this->show_field[$i]["format"]:'NONE';
1387  $clsTable->Cell($RowData[$this->show_field[$i]["fieldname"]],$this->show_field[$i]["fieldname"],-1,$fmt);
1388  }
1389  }
1390 
1391 // Check whether I must show the link to the DetailMode
1392 if(isset($this->show_info["showdethref"]))
1393  {
1394  if($this->show_info["showdethref"])
1395  $clsTable->SpecialCell($this->_DetHrefLink($RowNumber,$RowData),'DETAIL');
1396  }
1397 
1398 // modify row
1399 if(array_key_exists('showmodify',$this->modify_info))
1400  {
1401  if($this->modify_info['showmodify'])
1402  $clsTable->SpecialCell($this->_ModifyHrefLink($RowData),'MODIFY');
1403  }
1404 
1405 // delete row
1406 if(array_key_exists('showdelete',$this->modify_info))
1407  {
1408  if($this->modify_info['showdelete'])
1409  $clsTable->SpecialCell($this->_DeleteHrefLink($RowData),'DELETE');
1410  }
1411 
1412 // Prima di chiudere la riga eseguo la eventuale funzione di callback per
1413 // l'aggiunta di altri dati sulla riga
1414 // var_dump($this->show_info);
1415 if(isset($this->show_info["rowcallback"]))
1416  {
1417  // The callback function may return a string (that will be placed into one cell)
1418  // or an array. In the last case each array's item will be place into its own cell
1419  // No format will be applied
1420  $CustomCell=$this->show_info["rowcallback"]($RowData);
1421  if(is_array($CustomCell))
1422  {
1423  foreach($CustomCell as $key => $value)
1424  $clsTable->Cell($value,'callback');
1425  }
1426  else
1427  $clsTable->Cell($CustomCell,'callback');
1428  }
1429 }
1430 
1439 function _DetHrefLink($RowNumber,$RowData)
1440 {
1441 /* If call CYAp, it needs only numebr of page (it knows the query
1442  * and the order.
1443  * But if it calls another page, that page doesn't knows the query, the search inputs, ...
1444  * so I need to give it an unique identifier to the row. The solution is to use
1445  * the modify_info's keyfield.
1446  */
1447 
1448 if($this->show_info['customdetpage']==$_SERVER['PHP_SELF']&&$this->EventTable['YAP_DETAILCLICK']=='_OnDetailClick')
1449  $RowId='';
1450 else
1451  {
1452  if(!array_key_exists('keyfield',$this->modify_info))
1453  trigger_error('Error: if you set the custom detail page, you must set the row key field ',E_USER_ERROR);
1454  if(!array_key_exists($this->modify_info['keyfield'],$RowData))
1455  trigger_error('Error: row key field '.$this->modify_info['keyfield'].' not found',E_USER_ERROR);
1456  $RowId=$RowData[$this->modify_info['keyfield']];
1457  }
1458 $PageNum=$this->PagSize*($this->PageRequest-1)+$RowNumber;
1459 $c=(strpos($this->show_info['customdetpage'],'?')===FALSE)?'?':'&amp;';
1460 return($this->show_info['customdetpage'].$c."ev=YAP_DETAILCLICK&amp;pag=".$PageNum."&amp;id=".$RowId);
1461 }
1462 
1469 function _DeleteHrefLink($RowData)
1470 {
1471 if(!array_key_exists($this->modify_info['keyfield'],$RowData))
1472  trigger_error('Error: row key field '.$this->modify_info['keyfield'].' not found',E_USER_ERROR);
1473 $KeyValue=$RowData[$this->modify_info['keyfield']];
1474 if($this->_CheckAuthorization($KeyValue,'RemoveRow'))
1475  {
1476  $c=(strpos($this->modify_info['customdeletepage'],'?')===FALSE)?'?':'&amp;';
1477  return($this->modify_info['customdeletepage'].$c.'ev=YAP_REMOVECLICK&amp;id='.urlencode($KeyValue));
1478  }
1479 else
1480  return('');
1481 }
1482 
1489 function _ModifyHrefLink($RowData)
1490 {
1491 if(!array_key_exists($this->modify_info['keyfield'],$RowData))
1492  trigger_error('Error: row key field '.$this->modify_info['keyfield'].' not found',E_USER_ERROR);
1493 $KeyValue=$RowData[$this->modify_info['keyfield']];
1494 if($this->_CheckAuthorization($KeyValue,'ModifyRow'))
1495  {
1496  $c=(strpos($this->modify_info['custommodifypage'],'?')===FALSE)?'?':'&amp;';
1497  return($this->modify_info['custommodifypage'].$c.'ev=YAP_MODIFYCLICK&amp;id='.urlencode($KeyValue));
1498  }
1499 else
1500  return('');
1501 }
1502 
1507 function _AddHrefLink(&$clsTable)
1508 {
1509 /* @var $clsTable CYap_TableView */
1510 if($this->_CheckAuthorization('','AddRow'))
1511  {
1512  $c=(strpos($this->modify_info['customaddpage'],'?')===FALSE)?'?':'&amp;';
1513  $text=$this->modify_info['customaddpage'].$c."ev=YAP_NEWROW";
1514  $clsTable->AddRowCell($text);
1515  }
1516 }
1517 
1528 function show_detail_row($DataRow,$Show=true, $Fields,&$clsTable)
1529 {
1530 /* @var $clsTable CYap_DetailView */
1531 $lclShowField=array();
1532 $fmt=array();
1533 for($i=0;$i<count($this->show_field);$i++)
1534  {
1535  $lclShowField[$this->show_field[$i]['fieldname']]=(array_key_exists('showdetail',$this->show_field[$i]))?
1536  $this->show_field[$i]['showdetail']: // paramater exists, check it
1537  true;
1538  $fmt[$this->show_field[$i]['fieldname']]=(array_key_exists('format',$this->show_field[$i]))?
1539  $this->show_field[$i]["format"]:
1540  'NONE';
1541  }
1542 
1543 foreach($Fields as $Key => $Value)
1544  {
1545 
1546  if(array_key_exists($Key,$DataRow))
1547  $special_chars = htmlspecialchars($DataRow[$Key]);
1548  else
1549  $special_chars = htmlspecialchars(" ");
1550  // May I show this column?
1551  $ToShow=(array_key_exists($Key,$lclShowField))?
1552  $lclShowField[$Key]:
1553  false;
1554  if($ToShow||$this->Pre_1_0_Style)
1555  {
1556  if(array_key_exists($Key,$fmt))
1557  $clsTable->Cell($Key,$special_chars,$Key,$fmt[$Key]);
1558  else
1559  $clsTable->Cell($Key,$special_chars,$Key,'NONE');
1560  }
1561  }
1562 }
1563 
1564 
1569 function show_selectors()
1570 {
1571 // Check whether I must show those link or not
1572 if($this->show_info['showmovepage']==false)
1573  return;
1574 
1575 // number of rows to show in each page (1 in detailmode)
1576 $rowsXpage=($this->DetailMode)?1:$this->PagSize;
1577 
1578 /*
1579  * Determinazine dei numeri di pagina in base ai selettori
1580  */
1581 
1582 // Prima pagina
1583 $FirstPage=($this->PageRequest>1)?1:0;
1584 if($FirstPage)
1585  $p['FirstPage']=$_SERVER["PHP_SELF"]."?ev=YAP_TOPAGE&amp;pag=".$FirstPage;
1586 
1587 // Pagina Precedente
1588 $PrevPage=($this->PageRequest>1)?$this->PageRequest-1:0;
1589 if($PrevPage)
1590  $p['PrevPage']=$_SERVER["PHP_SELF"]."?ev=YAP_TOPAGE&amp;pag=".$PrevPage;
1591 
1592 // Pagine previste:
1593 // $TotPag=1+(($this->TotRow-1)/$this->PagSize);
1594 $TotPag=ceil($this->TotRow/$rowsXpage);
1595 
1596 // Pag Successiva
1597 $NextPage=($this->PageRequest<$TotPag)?$this->PageRequest+1:0;
1598 if($NextPage)
1599  $p['NextPage']=$_SERVER["PHP_SELF"]."?ev=YAP_TOPAGE&amp;pag=".$NextPage;
1600 
1601 // Pagina finale
1602 $LastPage=($this->PageRequest<$TotPag)?$TotPag:0;
1603 if($LastPage)
1604  $p['LastPage']=$_SERVER["PHP_SELF"]."?ev=YAP_TOPAGE&amp;pag=".$LastPage;
1605 
1606 if($this->DetailMode)
1607  $p['DetailMode']=$_SERVER["PHP_SELF"]."?ev=YAP_TABLEVIEW&amp;pag=".ceil($this->PageRequest/$this->PagSize);
1608 
1609 $p['Exit']=($this->PagerExit!='')?$this->PagerExit:$_SERVER['PHP_SELF'];
1610 
1611 
1612 eval('$draw=new '.$this->show_info['skippageclass'].'($this->show_info[\'languagefile\']);');
1613 // $draw=new CYap_MoveToPage($this->show_info['languagefile']);
1614 $draw->Draw($p);
1615 }
1616 
1622 function select_page()
1623 {
1624 // Check whether I must show the following links or not
1625 if($this->show_info['showpagenumber']==false)
1626  return;
1627 
1628 // number of rows to show in each page (1 in detailmode)
1629 $rowsXpage=($this->DetailMode)?1:$this->PagSize;
1630 
1631 
1632 // Pagina iniziale
1633 $min=($this->PageRequest-5>0)?$this->PageRequest-5:1;
1634 
1635 // Pagine previste:
1636 $TotPag=ceil($this->TotRow/$rowsXpage);
1637 
1638 // Pagina finale
1639 $max=($min+10>$TotPag)?$TotPag:$min+10;
1640 
1641 eval('$draw=new '.$this->show_info['pagenumberclass'].'($this->show_info[\'languagefile\']);');
1642 // $draw=new CYap_PageNumber($this->show_info['languagefile']);
1643 $draw->Draw($min,$max,$this->PageRequest);
1644 }
1645 
1652 function InDetailMode()
1653 {
1654 return($this->DetailMode);
1655 }
1656 
1657 
1658 /*********************************************************************************
1659  * JAVASCRIPT
1660  *********************************************************************************/
1665 function _JsOut()
1666 {
1667 echo '
1668 <script language="JavaScript1.2" type="text/javascript">
1669 ';
1670 $this->_ToolTipJs();
1671 echo '
1672 </script>
1673 ';
1674 $this->_ToolTipsDiv();
1675 }
1676 
1681 function _ToolTipJs()
1682 {
1683 echo '
1684 var ns4 = document.layers;
1685 var ns6 = document.getElementById && !document.all;
1686 var ie4 = document.all;
1687 offsetX = 0;
1688 offsetY = 20;
1689 var YapToolTipSTYLE="";
1690 function initYapToolTips()
1691 {
1692  if(ns4||ns6||ie4)
1693  {
1694  if(ns4) YapToolTipSTYLE = document.YapToolTipLayer;
1695  else if(ns6) YapToolTipSTYLE = document.getElementById("YapToolTipLayer").style;
1696  else if(ie4) YapToolTipSTYLE = document.all.YapToolTipLayer.style;
1697  if(ns4) document.captureEvents(Event.MOUSEMOVE);
1698  else
1699  {
1700  YapToolTipSTYLE.visibility = "visible";
1701  YapToolTipSTYLE.display = "none";
1702  }
1703  document.onmousemove = YapMoveToMouseLoc;
1704  }
1705 }
1706 /*
1707  * Show/Hide the tooltip.
1708  * msg: tooltip text,
1709  * fg: foreground color
1710  * bg: background color
1711  * Call the function without parameters to hide the tooltip
1712  */
1713 function YapToolTip(msg, fg, bg)
1714 {
1715  if(YapToolTip.arguments.length < 1) // hide
1716  {
1717  if(ns4) YapToolTipSTYLE.visibility = "hidden";
1718  else YapToolTipSTYLE.display = "none";
1719  }
1720  else // show
1721  {
1722  if(!fg) fg = "#777777";
1723  if(!bg) bg = "#FFFFFF";
1724  var content =
1725  \'<table border="0" cellspacing="0" cellpadding="1" bgcolor="\' + fg + \'"><td>\' +
1726  \'<table border="0" cellspacing="0" cellpadding="1" bgcolor="\' + bg +
1727  \'"><td align="center"><font face="sans-serif" color="\' + fg +
1728  \'" size="-2">&nbsp\;\' + msg +
1729  \'&nbsp\;</font></td></table></td></table>\';
1730  if(ns4)
1731  {
1732  YapToolTipSTYLE.document.write(content);
1733  YapToolTipSTYLE.document.close();
1734  YapToolTipSTYLE.visibility = "visible";
1735  }
1736  if(ns6)
1737  {
1738  document.getElementById("YapToolTipLayer").innerHTML = content;
1739  YapToolTipSTYLE.display=\'block\'
1740  }
1741  if(ie4)
1742  {
1743  document.all("YapToolTipLayer").innerHTML=content;
1744  YapToolTipSTYLE.display=\'block\'
1745  }
1746  }
1747 }
1748 function YapMoveToMouseLoc(e)
1749 {
1750  if(ns4||ns6)
1751  {
1752  x = e.pageX;
1753  y = e.pageY;
1754  }
1755  else
1756  {
1757  x = event.x + document.body.scrollLeft;
1758  y = event.y + document.body.scrollTop;
1759  }
1760  YapToolTipSTYLE.left = x + offsetX;
1761  YapToolTipSTYLE.top = y + offsetY;
1762  return true;
1763 }
1764 ';
1765 }
1766 
1774 function _ToolTipsDiv()
1775 {
1776 echo '
1777 <div id="YapToolTipLayer" style="position:absolute; visibility: hidden"></div>
1778 <script language="JavaScript" type="text/javascript">
1779 initYapToolTips();
1780 </script>
1781 ';
1782 }
1783 
1784 /********************************************************************************
1785  * MODIFY ROW FUNCTIONS
1786  ********************************************************************************/
1796 {
1797 // Argument Parsing
1798 $numargs = func_num_args();
1799 if($numargs >=1)
1800  $Add=func_get_arg(0);
1801 if($numargs >=2)
1802  $KeyValue=func_get_arg(1);
1803 if(array_key_exists('id',$_REQUEST))
1804  $KeyValue=urldecode($_REQUEST['id']);
1805 
1806 if(!isset($KeyValue)&&!$Add)
1807  trigger_error('ERRROR: request to modify a row, but no key value is specified',E_USER_ERROR);
1808 if($Add)
1809  {
1810  if(!$this->_CheckAuthorization('','AddRow'))
1811  {
1812  $this->_DontAllow();
1813  return(false);
1814  }
1815  $row=array();
1816  $KeyValue='';
1817  $fields=$this->_RetrieveFieldType();
1818  }
1819 else
1820  {
1821  // Am I authorized to perform this job?
1822  if(!$this->_CheckAuthorization($KeyValue,'ModifyRow'))
1823  {
1824  $this->_DontAllow();
1825  return(false);
1826  }
1827  $fields=$this->_RetrieveFieldType();
1828  // Retrieve the requested row
1829  $row=$this->_RetrieveRow($KeyValue,$fields);
1830  }
1831 
1832 $field_controls=$this->_BuildControlTable($fields);
1833 // If I came from an invalid input, I fill the field with the information
1834 // coming from the previous form
1835 $FilledRow=$this->_FillFromRequest($row,$fields);
1836 foreach ($field_controls as $fieldname => $control)
1837  {
1838  if(array_key_exists($fieldname,$FilledRow))
1839  $field_controls[$fieldname]->SetValue($FilledRow[$fieldname]);
1840  }
1841 $this->_ShowModifyBox($FilledRow,$fields,$Add,$KeyValue,$field_controls);
1842 $_SESSION[$this->Prefix.'KeyToUpdate']=$KeyValue;
1843 return(true);
1844 }
1845 
1852 function _ModifyExecute($Add)
1853 {
1854 if(!array_key_exists($this->Prefix.'KeyToUpdate',$_SESSION)&&!$Add)
1855  trigger_error('ERRROR: request to modify a row, but no key value is specified',E_USER_ERROR);
1856 if($Add)
1857  {
1858  if(!$this->_CheckAuthorization('','AddRow'))
1859  {
1860  $this->_DontAllow();
1861  return(false);
1862  }
1863  }
1864 else
1865  {
1866  // $KeyValue=urldecode($_REQUEST['id']);
1867  $KeyValue=$_SESSION[$this->Prefix.'KeyToUpdate'];
1868  // Am I authorized to perform this job?
1869  if(!$this->_CheckAuthorization($KeyValue,'ModifyRow'))
1870  {
1871  $this->_DontAllow();
1872  return(false);
1873  }
1874  }
1875 
1876 $fields=$this->_RetrieveFieldType();
1877 $field_controls=$this->_BuildControlTable($fields);
1878 $row=$this->_FillFromRequest(array(),$fields);
1879 $this->WrongFields=array();
1880 if($Add)
1881  $sql='insert into ' . $this->modify_info['table'];
1882 else
1883  $sql='update ' . $this->modify_info['table'];
1884 foreach ($field_controls as $fieldName => $control)
1885  {
1886  //
1887  $field_controls[$fieldName]->ParseResponse();
1888  if(!$field_controls[$fieldName]->IsValid())
1889  $this->WrongFields[$fieldName]='*';
1890  if(array_key_exists($fieldName,$row))
1891  {
1892  if(!$this->_CheckFieldCallBack($row,$fieldName))
1893  $this->WrongFields[$fieldName]='*';
1894  }
1895  }
1896 
1897 // I don't update anything if I get some wrogn field
1898 $DoUpdate=!count($this->WrongFields);
1899 //var_dump($DoUpdate);
1900 if($DoUpdate)
1901  {
1902  $sql.=($Add)?$this->Db_Class->DbaddSql($row,$fields,$this->modify_info['keyfield']):$this->Db_Class->DbmodifySql($row,$fields);
1903  if(!$Add)
1904  {
1905  $apice=$fields[$this->modify_info['keyfield']]['apice'];
1906  $sql.=' where ' . $this->modify_info['keyfield'] . '= '.$apice.$this->Db_Class->DbEscape($KeyValue).$apice;
1907  }
1908  if(DEBUG) $GLOBALS['debug']->DumpVar($sql,'$sql');
1909  if(!$this->Db_Class->DbExecSql($sql))
1910  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
1911  unset($_SESSION[$this->Prefix.'KeyToUpdate']);
1912  // $this->_ExecuteSql($sql);
1913  }
1914 return($DoUpdate);
1915 }
1921 {
1922 $msg='<p>'.$this->Lang['OperationExecuted'].'!</p>';
1923 $cmdOk=$this->show_info['onokhref'].'?ev=YAP_CMDEXECUTED';
1924 $msg.='<br><form>
1925  <input type=button value=\''.$this->Lang['Continue'].'\' onclick="window.location.href=\''.$cmdOk.'\'">
1926  </form>';
1927 eval('$msgbox=new '.$this->show_info['boxclass'].'($msg);');
1928 //$msgbox=new CYapBox($msg);
1929 $msgbox->Show();
1930 }
1931 
1938 function _BuildControlTable($tblFields)
1939 {
1940 $OutTable=array();
1941 $array_in=$tblFields;
1942 // print_r($array_in);
1943 foreach($array_in as $FieldName => $field)
1944  {
1945  if(!$field['autoincrement'])
1946  {
1947  if(array_key_exists('classname',$field))
1948  eval('$OutTable[$FieldName]=new '.$field['classname'].';');
1949  else
1950  {
1951  switch($field['type'])
1952  {
1953  case 'SET':
1954  case 'ENUM':
1955  // Enum/set type, use a select control
1956  if(array_key_exists('classname',$field))
1957  $class=$field['classname'];
1958  else
1959  $class='CYap_SelectBox';
1960  if(!class_exists($class))
1961  trigger_error('The requested class '.$class.' doesn\'t exist',E_USER_ERROR);
1962  eval('$OutTable[$FieldName]=new '.$class.';');
1963  $OutTable[$FieldName]->SetChoices($field['values']);
1964  break;
1965  default:
1966  // no specs, use the default
1967  if(array_key_exists('classname',$field))
1968  $class=$field['classname'];
1969  else
1970  $class='CYap_TextBox';
1971  if(!class_exists($class))
1972  trigger_error('The requested class '.$class.' doesn\'t exist',E_USER_ERROR);
1973  eval('$OutTable[$FieldName]=new '.$class.';');
1974  // 14/04/2009
1975  // If selectbox object, I set the choices from configuration file, if any provided
1976  if (is_subclass_of($OutTable[$FieldName], 'CYap_SelectBox'))
1977  {
1978  if(array_key_exists('choices',$field))
1979  $OutTable[$FieldName]->SetChoices($field['choices']);
1980  }
1981  break;
1982  }
1983  }
1984  if(array_key_exists('default',$field))
1985  $OutTable[$FieldName]->SetValue($field['default']);
1986  if(array_key_exists('custom',$field))
1987  $OutTable[$FieldName]->CustomParam($field['custom']);
1988  $OutTable[$FieldName]->BindField($field);
1989  }
1990  }
1991 // var_dump($OutTable);
1992 return($OutTable);
1993 }
1994 
2005 function _ShowModifyBox($Row,$Fields,$Add,$KeyValue,$Controls)
2006 {
2007 eval('$TheForm=new '.$this->show_info['formclass'].'($Controls,$this->modify_info,$this->show_info[\'languagefile\']);');
2008 // $TheForm=new CYap_form($Controls,$this->modify_info,$this->show_info['languagefile']);
2009 $TheForm->MakeForm($Add,$KeyValue,$this->WrongFields,$this->show_info['onaborthref']);
2010 }
2018 function _FillFromRequest($row,$Fields)
2019 {
2020 $OutRow=$row;
2021 $FieldToCheck=$this->modify_info['fields'];
2022 // If no field is request, ask all fields from database
2023 if(!count($FieldToCheck))
2024  $FieldToCheck=$Fields;
2025 foreach($FieldToCheck as $Key => $Value)
2026  {
2027  // if(array_key_exists('if_'.$Key,$_REQUEST)) Yap 2.1.0
2028  if(array_key_exists($Key,$_REQUEST))
2029  $OutRow[$Key]=$_REQUEST[$Key];
2030  }
2031 return($OutRow);
2032 }
2033 
2041 function _RetrieveRow($KeyValue,$Fields)
2042 {
2043 if(!$this->DbOpened) $this->_OpenDb();
2044 $apice=$Fields[$this->modify_info['keyfield']]['apice'];
2045 $sql='select * from ' .$this->modify_info['table'];
2046 $sql.=' where ' . $this->modify_info['keyfield'] . '= '.$apice.$this->Db_Class->DbEscape($KeyValue).$apice;
2047 
2048 if(($result=$this->Db_Class->DbExecSql($sql))===FALSE)
2049  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
2050 $found=$this->Db_Class->DbGetNumRow($result);
2051 if($found!=1)
2052  {
2053  // I'm expecting only one row!
2054  trigger_error('ERROR: found many rows having key = '.$KeyValue,E_USER_ERROR);
2055  }
2056 // $row=$this->_dbGetRow($result);
2057 $row=$this->Db_Class->DbGetValue($result);
2058 return($row);
2059 }
2060 
2069 function _RetrieveFieldType($res=NULL, $ModifyMode=true)
2070 {
2071 $free=false;
2072 if($res==NULL)
2073  {
2074  // No resource passed, I'll get the information only from
2075  // the modify_info table
2076  if(!$this->DbOpened) $this->_OpenDb();
2077  if(($res=$this->Db_Class->QueryLimit('select * FROM '.$this->modify_info['table'],0,1))===FALSE)
2078  trigger_error($this->dberror('Invalid query:SHOW FIELDS FROM '.$this->modify_info['table']),E_USER_ERROR);
2079  $free=true;
2080  }
2081 $i=0;
2082 $GetModifyInfo=false;
2083 if(array_key_exists('fields',$this->modify_info))
2084  if(count($this->modify_info['fields'])>0)
2085  $GetModifyInfo=true;
2086 
2087 while ($i < $this->Db_Class->DbGetNumFields($res))
2088  {
2089  $meta = $this->Db_Class->DbFetchField($res,$i);
2090  if (!$meta)
2091  trigger_error( "No information available",E_USER_ERROR);
2092  $flags = $this->Db_Class->DbFieldFlags($res, $i);
2093  if($meta->type!='IGNORE')
2094  {
2095  if(!$ModifyMode)
2096  $fields[$meta->name]=$this->_CompileFieldType($meta,$flags,array());
2097  else
2098  {
2099  // modify mode
2100  if(!$GetModifyInfo)
2101  {
2102  $fields[$meta->name]=$this->_CompileFieldType($meta,$flags,array());
2103  }
2104  else
2105  {
2106  if(array_key_exists($meta->name,$this->modify_info['fields']))
2107  $fields[$meta->name]=$this->_CompileFieldType($meta,$flags,$this->modify_info['fields'][$meta->name]);
2108  else
2109  {
2110  // look for the rowkey. If that field is not included into modfy_info, it will not loaded, so
2111  // I added it.
2112  if($meta->name==$this->modify_info['keyfield'])
2113  $fields[$meta->name]=$this->_CompileFieldType($meta,$flags,array());
2114  }
2115  }
2116  } // Modify mode
2117  } // Skip the field IGNORE
2118  $i++;
2119  } // end while
2120 
2121 if($free)
2122  $this->Db_Class->DbFreeResult($res);
2123  //mysql_free_result($res);
2124 return($fields);
2125 }
2126 
2135 function _CompileFieldType($meta,$flags,$CfgParam)
2136 {
2137  $out=$CfgParam;
2138  $out['DbType']=$meta->type;
2139  $out['autoincrement']=(preg_match('/auto_increment/',$flags))?true:false;
2140  $out['name']=$meta->name;
2141  $out['values']=array();
2142  // $out['table']=$meta->table; not used
2143  $out['type']=$meta->type;
2144  $out['apice']=$this->_Apice($meta->type);
2145  $out['size']=$meta->max_length;
2146  $out['decimal']=0;
2147 
2148  if($meta->numeric)
2149  $out['size']=6;
2150  switch($meta->type)
2151  {
2152  case 'SET':
2153  $out['size']=10;
2154  if (ereg('set.(.*).',$meta->type,$match))
2155  $out['values'] = explode(',',$match[1]);
2156  break;
2157  case 'ENUM':
2158  $out['size']=10;
2159  if (ereg('enum.(.*).',$meta->type,$match))
2160  $out['values'] = explode(',',$match[1]);
2161  break;
2162  case 'DATE':
2163  $out['size']=10;
2164  break;
2165  case 'DATETIME':
2166  $out['size']=19;
2167  break;
2168  case 'TIME':
2169  $out['size']=10;
2170  break;
2171  case 'BOOL':
2172  case 'CHAR':
2173  case 'BIT':
2174  $out['size']=1;
2175  break;
2176  case 'TEXT':
2177  case 'BLOB':
2178  case 'TINYTEXT':
2179  case 'TINYBLOB':
2180  case 'MEDIUMTEXT':
2181  case 'MEDIUMBLOB':
2182  case 'LONGTEXT':
2183  case 'LONGBLOB':
2184  $out['size']=50;
2185  break;
2186  default:
2187  $out['size']=50;
2188  break;
2189  } // end switch based on the field type
2190  return($out);
2191 }
2192 
2199 function _Apice($fieldType)
2200 {
2201 $OutText='';
2202 switch(strtolower($fieldType))
2203  {
2204  case 'bool':
2205  case 'bit':
2206  case 'string':
2207  case 'date':
2208  case 'time':
2209  case 'datetime':
2210  case 'tinytext':
2211  case 'text':
2212  case 'mediumtext':
2213  case 'longtext':
2214  case 'char':
2215  case 'varchar':
2216  case 'blob':
2217  case 'tinyblob':
2218  case 'mediumblob':
2219  case 'longblob':
2220 
2221  $OutText='\''; // \' make the editor happy!
2222  break;
2223  }
2224 return($OutText);
2225 }
2226 /********************************************************************************
2227  * DELETE ROW FUNCTIONS
2228  ********************************************************************************/
2234 function _DeleteRequestConfirm()
2235 {
2236 if(!array_key_exists('id',$_REQUEST))
2237  trigger_error('ERRROR: request to remove a row, but no key value is specified',E_USER_ERROR);
2238 $KeyValue=urldecode($_REQUEST['id']);
2239 // Am I authorized to perform this job?
2240 if(!$this->_CheckAuthorization($KeyValue,'RemoveRow'))
2241  {
2242  $this->_DontAllow();
2243  return(false);
2244  }
2245 // show the box
2246 $msg='<table border=0 cellpadding=0 cellspacing=0>
2247  <tr><td colspan=2><strong>'.$this->Lang['SureToRemove'].'?</strong></td></tr>
2248  <tr><td>';
2249 if(!$this->DbOpened) $this->_OpenDb();
2250 $Fields=$this->_RetrieveFieldType();
2251 $row=$this->_RetrieveRow($KeyValue,$Fields);
2252 //$this->_DetOpenTable();
2253 // DetailMode
2254  eval('$clsTable=new '.$this->show_info['detailviewclass'].'($this->show_info[\'languagefile\']);');
2255  //$clsTable=new CYap_DetailView($this->show_info['languagefile']);
2256  $clsTable->LoadFormats($this->OutFormats);
2257  $clsTable->BeginTable();
2258  $clsTable->HeaderStart();
2259  $this->show_detail_row($row,false,$Fields,$clsTable);
2260  $clsTable->EndTable();
2261  $clsTable->FlushCode();
2262 // $msg.=$this->show_detail_row($row,false,$Fields);
2263 //$this->_DetCloseTable();
2264 $cmdRemove=$_SERVER['PHP_SELF'].'?ev=YAP_DOREMOVE';
2265 $cmdAbort=$_SERVER['PHP_SELF'].'?ev=YAP_ABORTREMOVE';
2266 $msg.='</td></tr>
2267  <tr><td>
2268  <form>
2269  <input type=button value=\''.$this->Lang['Remove'].'\' onclick="window.location.href=\''.$cmdRemove.'\'">
2270  <input type=button value=\''.$this->Lang['Abort'].'\' onclick="window.location.href=\''.$cmdAbort.'\'">
2271  </form>
2272  </td></tr>
2273  </table>';
2274 eval('$msgbox=new '.$this->show_info['boxclass'].'($msg);');
2275 //$msgbox=new CYapBox($msg);
2276 $msgbox->Show();
2277 $_SESSION[$this->Prefix.'KeyToRemove']=$KeyValue;
2278 return(true);
2279 }
2285 function _DeleteExecute()
2286 {
2287 if(!$this->DbOpened) $this->_OpenDb();
2288 if(!array_key_exists($this->Prefix.'KeyToRemove',$_SESSION))
2289  trigger_error('ERRROR: request to remove a row, but no key value is specified',E_USER_ERROR);
2290 $KeyValue=$_SESSION[$this->Prefix.'KeyToRemove'];
2291 // Am I authorized to perform this job?
2292 if(!$this->_CheckAuthorization($KeyValue,'RemoveRow'))
2293  {
2294  $this->_DontAllow();
2295  return(false);
2296  }
2297 if(!$this->DbOpened) $this->_OpenDb();
2298 $field=$this->_RetrieveFieldType();
2299 $apice=$field[$this->modify_info['keyfield']]['apice'];
2300 $sql='delete from ' .$this->modify_info['table'] . '
2301  where ' . $this->modify_info['keyfield'] . '= '.$apice.$this->Db_Class->DbEscape($KeyValue).$apice;
2302 $this->Db_Class->DbExecSql($sql);
2303 // $this->_ExecuteSql($sql);
2304 $msg='<p>'.$this->Lang['RowDeleted'].'!</p>';
2305 $cmdOk=$this->show_info['onokhref'].'?ev=YAP_CMDEXECUTED';
2306 $msg.='<br><form>
2307  <input type=button value=\''.$this->Lang['Continue'].'\' onclick="window.location.href=\''.$cmdOk.'\'">
2308  </form>';
2309 eval('$msgbox=new '.$this->show_info['boxclass'].'($msg);');
2310 // $msgbox=new CYapBox($msg);
2311 $msgbox->Show();
2312 unset($_SESSION[$this->Prefix.'KeyToRemove']);
2313 return(true);
2314 }
2315 
2328 function _CheckAuthorization($Key,$Action)
2329 {
2330 switch($Action)
2331  {
2332  case 'RemoveRow':
2333  if(array_key_exists('deleteallow',$this->modify_info))
2334  $ret=call_user_func($this->modify_info['deleteallow'],$Key);
2335  else
2336  $ret=true;
2337  // Stop it if I don't show the link
2338  if($ret)
2339  $ret=$this->modify_info['showdelete'];
2340  break;
2341  case 'AddRow':
2342  if(array_key_exists('addallow',$this->modify_info))
2343  $ret=call_user_func($this->modify_info['addallow']);
2344  else
2345  $ret=true;
2346  // Stop it if I don't show the link
2347  if($ret)
2348  $ret=$this->modify_info['showadd'];
2349  break;
2350  case 'ModifyRow':
2351  if(array_key_exists('modifyallow',$this->modify_info))
2352  $ret=call_user_func($this->modify_info['modifyallow'],$Key);
2353  else
2354  $ret=true;
2355  // Stop it if I don't show the link
2356  if($ret)
2357  $ret=$this->modify_info['showmodify'];
2358  break;
2359  default:
2360  trigger_error('Internal Error:_CheckAuthorization, unespected action',E_USER_ERROR);
2361  break;
2362  }
2363 return($ret);
2364 }
2365 
2371 function _DontAllow()
2372 {
2373 $text='<p>'.$this->Lang['AccessDenied'].'.</p>';
2374 eval('$box=new '.$this->show_info['boxclass'].'($text);');
2375 //$box=new CYapBox($text);
2376 $box->Show();
2377 }
2378 
2388 function _CheckFieldCallBack($row,$Key)
2389 {
2390 if(!array_key_exists($Key,$this->modify_info['fields']))
2391  return(true);
2392 if(array_key_exists('fieldvalidator',$this->modify_info['fields'][$Key]))
2393  $ret=call_user_func($this->modify_info['fields'][$Key]['fieldvalidator'],$row,$Key);
2394 else
2395  $ret=true;
2396 return($ret);
2397 }
2398 
2399 /********************************************************************************
2400  * DATABASE FUNCTIONS
2401  ********************************************************************************/
2402 
2406 function _OpenDb()
2407 {
2408 if(!$this->Db_Class->OpenDb($this->db_info["servername"],$this->db_info["user"],$this->db_info["password"]))
2409  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
2410 if(!$this->Db_Class->SelectDb($this->db_info["dbname"]))
2411  trigger_error($this->Db_Class->DbError(),E_USER_ERROR);
2412 $this->DbOpened=true;
2413 }
2414 
2415 } // that's all folk!
2428 function is_subclass($sClass, $sExpectedParentClass)
2429 {
2430  do
2431  if( strtolower($sExpectedParentClass) === strtolower($sClass) ) return true;
2432  while( false != ($sClass = get_parent_class($sClass)) );
2433  return false;
2434 }
2435 ?>