Source for file DBMS_FIELD_DBSELECTLIST.phpclass

Documentation is available at DBMS_FIELD_DBSELECTLIST.phpclass

  1. <?php
  2. /**
  3.   * Class file DBMS_FIELD_DBSELECTLIST.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    dbms_field
  7.   * @category   mixed
  8.   *
  9.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  10.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  11.   *
  12.   * @version $Id: DBMS_FIELD_DBSELECTLIST.phpclass,v 1.11 2008/08/08 02:00:42 pitlinz Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('DBMS_FIELD_SELECTLIST',dirname(__FILE__"/DBMS_FIELD_SELECTLIST.phpclass");
  20.     pcf_require_class('OCSP_TEMPLATE',"common/");
  21.     
  22. /**
  23.   * Class DBMS_FIELD_DBSELECTLIST
  24.   *
  25.   * handels select fields (&lt;SELECT&gt;) with DB Values
  26.   * extends DBMS_FIELD_SELECTLIST to get the values from the database
  27.   *
  28.   * @project    Open CSP-Management
  29.   * @package    dbms_field
  30.   * @category   mixed
  31.   *
  32.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  33.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  34.   *
  35.   * @version pk-06-07-24 getPopupLinkJs() added
  36.   * @version $Id: DBMS_FIELD_DBSELECTLIST.phpclass,v 1.11 2008/08/08 02:00:42 pitlinz Exp $
  37.   * 
  38.   ***/
  39.  
  40.     // ---------------------------------------------------------------------------
  41.     // constants
  42.     // ---------------------------------------------------------------------------
  43.     
  44.     /**
  45.      * @constant string CLASS_SRC_FILE
  46.      */
  47.     const CLASS_SRC_FILE = __FILE__;
  48.  
  49.     // ---------------------------------------------------------------------------
  50.     // class (static)
  51.     // ---------------------------------------------------------------------------
  52.     
  53.     /*** class vars ------------------------------------------------------ */
  54.  
  55.     /**
  56.      * multidimensonal array of already loaded values
  57.      * 
  58.      * array[$this->getClassCacheKey()][$value] = {query result}
  59.      * 
  60.      * if self::$classValPopulateTs[$this->getClassCacheKey()] > (time() - ini_get('max_execution_time'))
  61.      * the array should be up to date and there is no need to query the db
  62.      * 
  63.      * for list modes simply check if the value is set
  64.      *
  65.      * @var array $classCache 
  66.      */
  67.     protected static $classCache array();
  68.     
  69.     /**
  70.      * array of class value populate time stemps
  71.      *
  72.      * @var array $classValPopulateTs 
  73.      */
  74.     protected static $classValPopulateTs array();
  75.     
  76.     /*** class methods --------------------------------------------------- */    
  77.     
  78.     // ---------------------------------------------------------------------------
  79.     // object vars
  80.     // ---------------------------------------------------------------------------
  81.     
  82.     /*** compostion --------------------------------------------------- */
  83.     
  84.     /*** attributes  -------------------------------------------------- */    
  85.     
  86.     /**
  87.      * @deprecated
  88.      */
  89.     protected $classSrcFile=__FILE__;
  90.  
  91.      
  92.     /**
  93.       * a sql query to get data array
  94.       * @var string $query 
  95.       */
  96.     protected $query           = "";
  97.  
  98.     // ---------------------------------------------------------------------------
  99.     // factory / construct
  100.     // ---------------------------------------------------------------------------
  101.     
  102.     // ---------------------------------------------------------------------------
  103.     // getter / setter
  104.     // ---------------------------------------------------------------------------    
  105.  
  106.     protected function getClassCacheKey()
  107.     {
  108.         return md5($this->query);
  109.     }
  110.     
  111.     // ---------------------------------------------------------------------------
  112.     // OBJECT SETTINGS METHODS
  113.     // ---------------------------------------------------------------------------
  114.  
  115.     /**
  116.       * returns an array of field to bie shown in the edit form
  117.       * by using editTbl_echoForm
  118.       *
  119.       * the array is in the form:
  120.       *
  121.       * <code>
  122.       * [_sectionName_]
  123.       *      [TITLE] -> string
  124.       *      [_rowName_]
  125.       *         [FLDLAB]  (complete html code with <td></td>)
  126.       *         [FLDVAL] (complete html code with <td></td>)
  127.       * </code>
  128.       *
  129.       * @param boolean $debug 
  130.       *
  131.       * @returns array
  132.       *
  133.       * @since pk-06-04-26
  134.       *
  135.       */
  136.     function editTbl_getFieldEditArr($debug=FALSE
  137.     {
  138.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_FIELD_DBSELECTLIST::editTbl_getFieldEditArr","",0);
  139.         $a_ret=parent::editTbl_getFieldEditArr($debug);
  140.  
  141.         $a_ret['SELECT']['FLD_LSTVALS']=NULL// unset parent field
  142.         $a_ret['SELECT']['FLD_VALQUERY']=array(
  143.             'FLDLAB'=>"Query (SELECT KEY,VALUE ..)",
  144.             'FLDVAL'=>"<textarea name=\"SELECTQUERY\" cols=\"40\" rows=\"5\" wrap=\"off\">".htmlspecialchars($this->query)."</textarea>"
  145.         );
  146.  
  147.         return $a_ret;
  148.     }
  149.  
  150.     /**
  151.       * save the filed definition form
  152.       *
  153.       * @return bool 
  154.       *
  155.       * @version pk-04-10-11
  156.       * @version pk-07-05-17
  157.       * @version pk-08-06-13 use OCSP_OBJ::defaultReadDBObj()
  158.       *
  159.       */
  160.     function save(
  161.     {
  162.         $b_ret=parent::save();
  163.         if (isset($_POST['SELECTQUERY'])) // <pk-07-05-17> E_ALL
  164.         {
  165.             $this->query = OCSP_OBJ::defaultReadDBObj()->checkQuery($_POST['SELECTQUERY'],PQT_SELECT);
  166.         }
  167.         return $b_ret;
  168.     }
  169.  
  170.     // -----------------------------------------------------------
  171.     // DB value methods
  172.     // -----------------------------------------------------------
  173.     
  174.     /**
  175.       * sets populates $this->myValues array from the database
  176.       *
  177.       * @param string $arrName 
  178.       * @param bool $debug 
  179.       * 
  180.       * @global string ${$arrName} 
  181.       *
  182.       * @version pk-04-10-11
  183.       * @version pk-05-02-24
  184.       * @version pk-07-10-10
  185.       * @version pk-08-06-13 use OCSP_OBJ::defaultReadDBObj()
  186.       * @version pk-08-08-01 use self::$classCache
  187.       */
  188.     function setMyValues($arrName="DBVAL",$debug=False{
  189.         global ${$arrName};
  190.         
  191.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_FIELD_DBSELECTLIST::setMyValues($arrName,...)","ClassCacheKey: "$this->getClassCacheKey());
  192.  
  193.         if (
  194.             isset(DBMS_FIELD_DBSELECTLIST::$classValPopulateTs[$this->getClassCacheKey()]&& 
  195.             (DBMS_FIELD_DBSELECTLIST::$classValPopulateTs[$this->getClassCacheKey()(time(ini_get('max_execution_time')))
  196.            )
  197.         {
  198.             $this->myValues =  DBMS_FIELD_DBSELECTLIST::$classCache[$this->getClassCacheKey()];
  199.             return True;
  200.         }
  201.         
  202.         $str_query OCSP_TEMPLATE::parse($this->query,${$arrName});                
  203.           $dbVals=OCSP_OBJ::defaultReadDBObj()->queryArray($str_query,0,1);
  204.  
  205.         if (!is_array($dbVals)) 
  206.         {
  207.             $dbVals=array("NULL"=>"keine Datens&auml;tzegefunden");
  208.             return False;
  209.         }
  210.  
  211.         $this->myValues=$dbVals;
  212.         
  213.         DBMS_FIELD_DBSELECTLIST::$classCache[$this->getClassCacheKey()&$this->myValues;
  214.         if (!is_array(DBMS_FIELD_DBSELECTLIST::$classValPopulateTs))
  215.         {
  216.             if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>" print_r(DBMS_FIELD_DBSELECTLIST::$classValPopulateTs,True" - " time("\n {$this->getKey()} - {$this->getClassCacheKey()}</pre>");
  217.         else {
  218.             DBMS_FIELD_DBSELECTLIST::$classValPopulateTs[$this->getClassCacheKey()time();
  219.             if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>" print_r(DBMS_FIELD_DBSELECTLIST::$classValPopulateTs,True" - " time("\n {$this->getKey()} </pre>");
  220.         }
  221.         return True;        
  222.     }
  223.  
  224.     // ###########################################################
  225.     // OBJECT HTML FORM METHODS
  226.     // ###########################################################
  227.  
  228.     /**
  229.       * return the link to the new or edit form
  230.       * the link is a template which is parsed with global ${$this->myDataArrName}
  231.       * also ##VALUE## is replaced with the current value
  232.       *
  233.       * @param string $url 
  234.       * @param int $mode 
  235.       * @param boolean $debug 
  236.       *
  237.       * @returns string
  238.       *
  239.       * @since pk-06-07-24
  240.       *
  241.       */
  242.     function getPopupLinkJs($url,$mode=FRM_MODE_NEW,$debug=FALSE{
  243.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_FIELD_SELECTLIST::getPopupLinkJs()");
  244.  
  245.         require_once __OCSP_PHPINCPATH__."common"._OCSP_DIRSEP_."pcf_templates.phpinc";
  246.  
  247.         $jCmd="\n";
  248.         global ${$this->myDataArrName};
  249.         $valArr=${$this->myDataArrName};
  250.  
  251.         if ($debugecho "<pre>\$valArr:\n".print_r($valArr,TRUE)."</pre>";
  252.         $url=str_replace("##VALUE##",$valArr[$this->myName],$url);
  253.         $url=pcf_tmpl_parse($url,$valArr);                      // parse the field setting
  254.         if (empty($url)) return "";                             // empty url stop
  255.  
  256.         switch($mode{
  257.             case FRM_MODE_NEW:
  258.                 unset($valArr[$this->myName]);  // do not add own values to new link
  259.                 break;
  260.             case FRM_MODE_EDIT:
  261.                 if ($this->enableEdit{        // user can change value -> get the current value from the select
  262.                     $jCmd.="var fldVal_".$this->myName."=getSelectedOptionByName(document.forms['".$this->getMyFormName()."'],'".$this->myDataArrName."[".$this->myName."]');\n";
  263.                     $valArr[$this->myName]="'+fldVal_".$this->myName."+'";
  264.                     break;
  265.                 }
  266.         }
  267.  
  268.         if (!strstr($url,"?")) $url.="?";                       // ensure we are adding get values from now
  269.         if (!empty($this->myURIQueryArrgs)) {                   // add aditional args
  270.             if ($debugecho "<blockquote><p>Args: ".$this->myURIQueryArrgs."</p>";
  271.             if ($addArgs=explode(";",$this->myURIQueryArrgs)) {
  272.                 unset($addArgs[$this->myName]);                 // do not add field value as it is added later
  273.                 foreach($addArgs as $key{
  274.                     if ($debugecho "<p>$key:".htmlspecialchars($valArr[$key])."</p>";
  275.                     if (!empty($valArr[$key])) {    // only not empty values
  276.                         $url.="&$key=".$valArr[$key];
  277.                     }
  278.                 }
  279.             else if (!empty($valArr[$this->myURIQueryArrgs])) {
  280.                 $url.="&".$this->myURIQueryArrgs."=".$valArr[$this->myURIQueryArrgs];
  281.             }
  282.             if ($debugecho "</blockquote>";
  283.         }
  284.  
  285.         if ($mode != FRM_MODE_NEW{                         // ensure the field value is in the param list
  286.             if (!strstr($url,$this->myName."=")) {
  287.                 $url.="&".$this->myName."=".$valArr[$this->myName];
  288.             }
  289.         }
  290.  
  291.         // generate the fields to set update the field from the popup
  292.  
  293.         $myForm=&$this->getMyForm();
  294.         $oArgs['FORMID']    =$myForm->getId();          // id of the form
  295.         $oArgs['FORMNAME']  =$this->getMyFormName();    // html name of the form (document.forms[...])
  296.         $oArgs['ARRNAME']   =$this->myDataArrName;      // name of the data array
  297.         $oArgs['FIELD']     =$this->myName;             // name of the field
  298.  
  299.         $url.="&OPENERURL=".base64_encode(serialize($oArgs));
  300.  
  301.         if ($this->reloadFormOnChange$url.="&OPENERRELOAD=1";
  302.  
  303.         if (!intval($this->popupWndWidth)) {
  304.             switch ($mode{
  305.                 case FRM_MODE_NEW:
  306.                     $this->popupWndWidth=$this->newWndWidth;
  307.                     break;
  308.                 case FRM_MODE_EDIT:
  309.                     $this->popupWndWidth=$this->editWndWidth;
  310.                     break;
  311.                 default:
  312.                     $this->popupWndWidth=400;
  313.             }
  314.         }
  315.         if (!intval($this->popupWndpopupWndHeight)) {
  316.             switch ($mode{
  317.                 case FRM_MODE_NEW:
  318.                     $this->popupWndpopupWndHeight=$this->editWndHeight;
  319.                     break;
  320.                 case FRM_MODE_EDIT:
  321.                     $this->popupWndpopupWndHeight=$this->editWndpopupWndHeight;
  322.                     break;
  323.                 default:
  324.                     $this->popupWndpopupWndHeight=300;
  325.             }
  326.         }
  327.  
  328.  
  329.         $jCmd.="var url_".$this->myName."='".$url."';\n";
  330.         $jCmd.="var opt_".$this->myName."='width=".$this->popupWndWidth.",height=".$this->popupWndHeight.",toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes';\n";
  331.         $jCmd.="hWnd_".$this->myName."=window.open(url_".$this->myName.",'".$this->myName."', opt_".$this->myName.");\n";
  332.         $jCmd.="hWnd_".$this->myName.".focus();\n";
  333.  
  334.         if ($debugecho "<blockquote><pre>".htmlspecialchars($jCmd)."</pre></blockquote>";
  335.  
  336.         return $jCmd;
  337.  
  338.     }
  339.  
  340.     
  341.     // -------------------------------------------------
  342.     // jOCSP rpc commands
  343.     // -------------------------------------------------
  344.  
  345.     function jOCSP_reloadOptionLst($options,$debug=FALSE)
  346.     {
  347.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_FIELD_DBSELECTLIST::jOCSP_reloadOptionLst()");
  348.         
  349.         if (isset($options['FRMARRAYNAME']&& (!empty($options['FRMARRAYNAME'])))
  350.         {
  351.             $this->myDataArrName = $options['FRMARRAYNAME'];
  352.         
  353.         
  354.         global ${$this->myDataArrName};
  355.  
  356.         if (is_array($options[$this->myDataArrName]))
  357.         {
  358.             ${$this->myDataArrName$options[$this->myDataArrName];
  359.         else {
  360.             ${$this->myDataArrNamearray();
  361.             if (isset($options['fldValue']))
  362.             {
  363.                 ${$this->myDataArrName}[$this->getName()$options['fldValue']
  364.             }
  365.         }
  366.         
  367.         if (isset($options['fldValue']&& !empty($options['fldValue']))
  368.         {
  369.             $arr_values array();
  370.             $arr_values[$options['fldValue']
  371.         else if (is_array(${$this->myDataArrName}[$this->getName()]))
  372.         {
  373.             $arr_values = ${$this->myDataArrName}[$this->getName()];
  374.         else {
  375.             $arr_values array();
  376.             if (!empty(${$this->myDataArrName}[$this->getName()]))
  377.             {
  378.                 $arr_values[= ${$this->myDataArrName}[$this->getName()];
  379.             }
  380.         }
  381.         
  382.         $this->setMyValues($this->myDataArrName,$debug);
  383.         
  384.         $str_ret "
  385.             if (obj_fld = document.getElementById('" $options['domId'"'))
  386.             {
  387.                 var i=0;
  388.                 var defaultValues = {};
  389.                 
  390.             ";
  391.         if ($this->showNullOption
  392.         {
  393.             $str_ret .= "
  394.                 if (obj_fld.options[i].value != '')
  395.                 {
  396.                     if (obj_fld.options[i].defaultSelected)
  397.                     {
  398.                         defaultValues[obj_fld.options[i].value] = obj_fld.options[i].value; 
  399.                     }
  400.                     
  401.                     obj_fld.options[i] = new Option('" $this->nullLabel . "','',false," (sizeof($arr_values"false" "true"");                    
  402.                 }
  403.                 i++;
  404.             "
  405.         }
  406.         
  407.         if (sizeof($this->myValues))
  408.         {
  409.             foreach($this->myValues as $str_val => $str_text)
  410.             {
  411.                 $str_ret .= "
  412.                         if (obj_fld.options[i].value != '" pcf_js_escape($str_val"')
  413.                         {
  414.                             if (obj_fld.options[i].defaultSelected)                            
  415.                             {
  416.                                 defaultValues[obj_fld.options[i].value] = obj_fld.options[i].value; 
  417.                             }
  418.                             obj_fld.options[i] = new Option('" pcf_js_escape($str_text"','" pcf_js_escape($str_val"',false," (in_array($str_val,$arr_values"true" "false"");
  419.                         } else {
  420.                             obj_fld.options[i].text = '" pcf_js_escape($str_text"';
  421.                         }
  422.                         i++;
  423.                     ";
  424.             }
  425.         }
  426.         
  427.         $str_ret .= "
  428.                 int_len = obj_fld.options.length;
  429.                 while(i < int_len)
  430.                 {
  431.                     obj_fld.options[i] = null;
  432.                     i++;
  433.                 }
  434.                 
  435.                 // restore default values
  436.                 if (defaultValues.length)
  437.                 {
  438.                     int_len = obj_fld.options.length;
  439.                     for (i = 0;i < int_len;i++)
  440.                     {
  441.                         if (defaultValues[obj_fld.options[i].value] == obj_fld.options[i].value)
  442.                         {
  443.                             obj_fld.options[i].defaultSelected = true;
  444.                         } 
  445.                     }
  446.                 }                
  447.             }
  448.             ";
  449.         
  450.         return $str_ret;
  451.     }
  452.  
  453. // end class DBMS_FIELD_DBSELECTLIST
  454. ?>

Documentation generated on Thu, 08 Jan 2009 17:41:18 +0100 by phpDocumentor 1.4.0a2