• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/easy/element/DropDownList.php

00001 <?PHP
00017 include_once("InputElement.php");
00018 
00027 class DropDownList extends InputElement
00028 {
00029        
00030        public $m_ReadOnly;
00031        public $m_DefaultDisplayValue;
00032        public $m_cssHoverClass;
00033     public $m_SelectFrom;
00034     public $m_SelectFromSQL;
00035     public $m_SelectedList; 
00036        
00037        protected function readMetaData(&$xmlArr){
00038               parent::readMetaData($xmlArr);
00039               $this->m_cssClass = isset($xmlArr["ATTRIBUTES"]["CSSCLASS"]) ? $xmlArr["ATTRIBUTES"]["CSSCLASS"] : "input_select_w";
00040               $this->m_cssErrorClass = isset($xmlArr["ATTRIBUTES"]["CSSERRORCLASS"]) ? $xmlArr["ATTRIBUTES"]["CSSERRORCLASS"] : $this->m_cssClass."_error";
00041               $this->m_cssFocusClass = isset($xmlArr["ATTRIBUTES"]["CSSFOCUSCLASS"]) ? $xmlArr["ATTRIBUTES"]["CSSFOCUSCLASS"] : $this->m_cssClass."_focus";
00042               $this->m_cssHoverClass = isset($xmlArr["ATTRIBUTES"]["CSSHOVERCLASS"]) ? $xmlArr["ATTRIBUTES"]["CSSHOVERCLASS"] : $this->m_cssClass."_hover";
00043               $this->m_Value = isset($xmlArr["ATTRIBUTES"]["DEFAULTVALUE"]) ? $xmlArr["ATTRIBUTES"]["DEFAULTVALUE"] : null;        
00044               $this->m_ReadOnly = isset($xmlArr["ATTRIBUTES"]["READONLY"]) ? $xmlArr["ATTRIBUTES"]["READONLY"] : "N";
00045         $this->m_SelectFrom = isset($xmlArr["ATTRIBUTES"]["SELECTFROM"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROM"] : null;
00046         $this->m_SelectedList = isset($xmlArr["ATTRIBUTES"]["SELECTEDLIST"]) ? $xmlArr["ATTRIBUTES"]["SELECTEDLIST"] : null;
00047         $this->m_SelectFromSQL = isset($xmlArr["ATTRIBUTES"]["SELECTFROMSQL"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROMSQL"] : null;
00048        $this->m_BlankOption = isset($xmlArr["ATTRIBUTES"]["BLANKOPTION"]) ? $xmlArr["ATTRIBUTES"]["BLANKOPTION"] : null;
00049               $this->m_DefaultValueRename = isset($xmlArr["ATTRIBUTES"]["DEFAULTVALUERENAME"]) ? $xmlArr["ATTRIBUTES"]["DEFAULTVALUERENAME"] : "N";  
00050               $this->m_BlankOption = $this->translateString($this->m_BlankOption);
00051        }
00057     public function render()
00058     {
00059        if($this->m_FormPrefix){
00060               $formNameStr = str_replace(".","_", $this->getFormObj()->m_Name)."_";
00061        }        
00062        if($this->m_Value!=null){
00063               $value = $this->m_Value;
00064        }else{
00065               $value = $this->getText();
00066        }        
00067        if($value==null){
00068               $value = $this->getDefaultValue();
00069        }
00070        if(preg_match('/\{.*?@.*?\}/si',$value)){
00071               $formObj = $this->getFormObj();
00072               $value = Expression::evaluateExpression($value, $formObj);
00073        }
00074         $disabledStr = ($this->getEnabled() == "N") ? "READONLY=\"true\"" : "";
00075         $style = $this->getStyle();
00076         $func = $this->getFunction();
00077         $optionList= $this->renderList();
00078         
00079         $htmlClass = Expression::evaluateExpression($this->m_cssClass, $formobj);
00080         $htmlClass = "CLASS='$htmlClass'";
00081         
00082         $sHTML .= "<div class=\"div_".$this->m_cssClass."\">";                        
00083         if($this->m_ReadOnly=='Y')
00084         {
00085                $display_input = "style=\"display:none;\"";
00086                $display_span = "style=\"position:;\"";
00087         }
00088         else
00089         {
00090                $display_span = "style=\"display:none;\"";
00091                $display_input = "style=\"position:;\"";
00092         }
00093         $list = $this->getList();
00094         if ($this->m_BlankOption) // ADD a blank option
00095         {
00096             $entry = explode(",",$this->m_BlankOption);
00097             $text = $entry[0];
00098             $blkvalue = ($entry[1]!= "") ? $entry[1] : null;
00099             $display_value = $text;
00100             $entryList = array(array("val" => $blkvalue, "txt" => $text ));
00101             $list = array_merge($entryList, $list);
00102         }        
00103         
00104         $display_value=$this->getDisplayValue($value);
00105         
00106         foreach ($list as $item){
00107               if($item['val']==$value){
00108                      if(!preg_match("/</si",$item['txt'])){
00109                             $display_value = $item['txt'];
00110                      }else{
00111                             $display_value = $item['val'];
00112                      }
00113                      break;
00114               }
00115         }
00116         
00117         if($value==''){
00118             $defaultOpt = array_shift($list);
00119             if($display_value==''){
00120               $value = $defaultOpt['val'];
00121                      $display_value = $defaultOpt['txt'];
00122             }
00123               array_unshift($list,$defaultOpt);          
00124         }
00125                 
00126         $this->setValue($value);
00127         $display_value = strip_tags($display_value);
00128         
00129               $onchange_func = $this->getOnChangeFunction();
00130                $sHTML .= $optionList;
00131               $sHTML .= "<div $display_span>";
00132                
00133                $sHTML .= "<span ID=\"span_" . $formNameStr.$this->m_Name ."\"  $this->m_HTMLAttr $style
00134                                           onclick=\"if($('".$formNameStr.$this->m_Name."_list').visible()){\$('".$formNameStr.$this->m_Name."_list').hide();$('".$formNameStr.$this->m_Name."').className='".$this->m_cssClass."'}else{\$('".$formNameStr.$this->m_Name."_list').show();$('".$formNameStr.$this->m_Name."').className='".$this->m_cssFocusClass."'}\"
00135                                           onmouseover=\"$('span_".$formNameStr.$this->m_Name."').className='".$this->m_cssHoverClass."'\"
00136                                           onmouseout=\"$('span_".$formNameStr.$this->m_Name."').className='".$this->m_cssClass."'\"
00137                                           >$display_value</span>";
00138                $sHTML .= "</div>";
00139                $sHTML .= "<div $display_input>";
00140                $sHTML .= "<INPUT NAME=\"" . $formNameStr.$this->m_Name . "\" ID=\"" . $formNameStr.$this->m_Name ."\" VALUE=\"" . $display_value . "\" $disabledStr $this->m_HTMLAttr $style 
00141                                           onclick=\"if($('".$formNameStr.$this->m_Name."_list').visible()){\$('".$formNameStr.$this->m_Name."_list').hide();$('".$formNameStr.$this->m_Name."').className='".$this->m_cssClass."'}else{\$('".$formNameStr.$this->m_Name."_list').show();$('".$formNameStr.$this->m_Name."').className='".$this->m_cssFocusClass."'}\"
00142                                           onmouseover=\"$('".$formNameStr.$this->m_Name."').className='".$this->m_cssHoverClass."'\"
00143                                           onmouseout=\"$('".$formNameStr.$this->m_Name."').className='".$this->m_cssClass."'\"
00144                                           onchange=\"$('".$formNameStr.$this->m_Name."_hidden').setValue(this.value);$onchange_func;\"
00145                                           />";
00146                $sHTML .= "<INPUT NAME=\"" . $formNameStr.$this->m_Name . "\" ID=\"" . $formNameStr.$this->m_Name ."_hidden\" VALUE=\"" . $value . "\" type=\"hidden\" $func />";              
00147                $sHTML .= "</div>";         
00148               
00149         $sHTML .= "</div>";
00150         
00151         $sHTML .= "<script>$('".$formNameStr.$this->m_Name."_list').hide()</script>";
00152         return $sHTML;
00153     }
00154     
00155     public function getDisplayValue($value)
00156     {
00157        if($value===null)
00158        {
00159               return null;
00160        }
00161        $selectFrom = $this->m_SelectFrom;
00162        $selectFrom = substr($selectFrom,0,strpos($selectFrom,','));
00163         
00164        $list= array();        
00165         if (!$selectFrom) {
00166               $this->getSQLFromList($list);
00167         }
00168         else
00169         {
00170               $this->getXMLFromList($list, $selectFrom);                     
00171               if (!is_array($list) || count($list)==0){
00172                      $this->getDOFromList($list, $selectFrom);                      
00173                      if (!is_array($list) || count($list)==0){
00174                             $this->getSimpleFromList($list, $selectFrom);
00175                      }                           
00176               }
00177               
00178         }
00179             
00180               if(!is_array($list)||count($list)==0){
00181                      return $value;
00182               }
00183         
00184        foreach ($list as $item){
00185               if($item['val']==$value){
00186                      if(!preg_match("/</si",$item['txt'])){
00187                             $display_value = $item['txt'];
00188                             return $display_value;
00189                      }else{
00190                             $display_value = $item['val'];
00191                             return $display_value;
00192                      }
00193                      break;
00194               }
00195         }        
00196        return $display_value;
00197     }
00198     
00199     protected function renderList(){
00200        
00201        if($this->m_FormPrefix){
00202               $formNameStr = str_replace(".","_", $this->getFormObj()->m_Name)."_";
00203        } 
00204        $onchange_func = $this->getOnChangeFunction();
00205        $list = $this->getList();
00206        
00207         if ($this->m_BlankOption) // ADD a blank option
00208         {
00209             $entry = explode(",",$this->m_BlankOption);
00210             $text = $entry[0];
00211             $value = ($entry[1]!= "") ? $entry[1] : null;
00212             $entryList = array(array("val" => $value, "txt" => $text ));
00213             $list = array_merge($entryList, $list);
00214         }     
00215        
00216        $value = $this->m_Value!==null ? $this->m_Value : $this->getText();
00217        $sHTML = "<ul style=\"display:none;z-index:50\" id=\"".$formNameStr.$this->m_Name."_list\" class=\"dropdownlist\">";
00218        foreach($list as $item){
00219               $val = $item['val'];
00220               $txt = $item['txt'];
00221               $pic = $item['pic'];
00222               if($pic){
00223                      if(preg_match('/\{.*\}/si',$pic)){
00224                             $pic = Expression::evaluateExpression($pic,null);
00225                      }elseif(!preg_match('/\//si',$pic)){
00226                             $pic = Resource::getImageUrl()."/".$pic;
00227                      } 
00228                      $str_pic="<img src=\"$pic\" />";
00229                      
00230               }else{
00231                      $str_pic = "";
00232               }             
00233            if(!preg_match("/</si",$txt)){
00234                      $display_value = $txt;
00235            }else{
00236               $display_value = $val;
00237            }    
00238            if($str_pic){
00239               $li_option_value =  $str_pic."<span>".$txt."</span>";
00240            }
00241            else{
00242               $li_option_value =$txt ;
00243            }
00244            
00245            
00246            if($val==$value)
00247            {                
00248               $option_item_style=" class='selected' ";
00249            }else{
00250               $option_item_style=" onmouseover=\"this.className='hover'\" onmouseout=\"this.className=''\" ";
00251            }
00252            
00253               $sHTML .= "<li $option_item_style                       
00254                             onclick=\"$('".$formNameStr.$this->m_Name."_list').hide();
00255                                                  $('".$formNameStr.$this->m_Name."').setValue('".addslashes($display_value)."');
00256                                                  $('".$formNameStr.$this->m_Name."_hidden').setValue('".addslashes($val)."');
00257                                                  $('span_".$formNameStr.$this->m_Name."').innerHTML = this.innerHTML;                                            
00258                                                  $onchange_func ;
00259                                                  $('".$formNameStr.$this->m_Name."').className='".$this->m_cssClass."'
00260                                                  \"                                 
00261                             >$li_option_value</li>";
00262               
00263               if($val == $value){
00264                      $this->m_DefaultDisplayValue="".$str_pic."<span>".$txt."</span>";
00265               }             
00266        }
00267        $sHTML .= "</ul>";
00268        return $sHTML;
00269     }
00270     
00271     
00272     protected function getList(){
00273        $list= array();
00274         if (!$selectFrom) {
00275             $selectFrom = $this->getSelectFrom();
00276         }
00277         if (!$selectFrom) {
00278               return $this->getSQLFromList($list);
00279         }
00280         $this->getXMLFromList($list, $selectFrom);
00281         if ($list != null)
00282             return $list;            
00283         $this->getDOFromList($list, $selectFrom);
00284         if ($list != null)
00285             return $list;
00286         $this->getSimpleFromList($list, $selectFrom);
00287         if ($list != null)
00288             return $list;
00289                 
00290        return $list;
00291     }
00292 // below code is copied from OptionElement
00293     protected function getSelectFrom()
00294     {
00295         $formobj = $this->getFormObj();
00296         return Expression::evaluateExpression($this->m_SelectFrom, $formobj);
00297     }
00298 
00299     protected function getSelectedList()
00300     {
00301         $formobj = $this->getFormObj();
00302         return Expression::evaluateExpression($this->m_SelectedList, $formobj);
00303     }
00304     
00305        protected function getSelectFromSQL()
00306     {
00307         $formobj = $this->getFormObj();
00308         return Expression::evaluateExpression($this->m_SelectFromSQL, $formobj);
00309     }
00310     
00311     protected function getXMLFromList(&$list, $selectFrom)
00312     {
00313         $pos0 = strpos($selectFrom, "(");
00314         $pos1 = strpos($selectFrom, ")");
00315         if ($pos0>0 && $pos1 > $pos0)
00316         {  // select from xml file
00317             $xmlFile = substr($selectFrom, 0, $pos0);
00318             $tag = substr($selectFrom, $pos0 + 1, $pos1 - $pos0-1);
00319             $tag = strtoupper($tag);
00320             $xmlFile = BizSystem::GetXmlFileWithPath ($xmlFile);
00321             if (!$xmlFile) return false;
00322 
00323             $xmlArr = &BizSystem::getXmlArray($xmlFile);
00324             if ($xmlArr)
00325             {
00326                 $i = 0;
00327                 if (!key_exists($tag, $xmlArr["SELECTION"]))
00328                     return false;
00329                 if(!$xmlArr["SELECTION"][$tag][0]){
00330                      $array = $xmlArr["SELECTION"][$tag];
00331                      unset($xmlArr["SELECTION"][$tag]);
00332                      $xmlArr["SELECTION"][$tag][0]=$array;
00333                 }
00334                 foreach($xmlArr["SELECTION"][$tag] as $node)
00335                 {
00336                     $list[$i]['val'] = $node["ATTRIBUTES"]["VALUE"];
00337                     $list[$i]['pic'] = $node["ATTRIBUTES"]["PICTURE"];
00338                     if ($node["ATTRIBUTES"]["TEXT"])
00339                     {
00340                         $list[$i]['txt'] = $node["ATTRIBUTES"]["TEXT"];                        
00341                     }
00342                     else
00343                     {
00344                         $list[$i]['txt'] = $list[$i]['val'];
00345                     }
00346                     $i++;
00347                     
00348                 }
00349                 $this->translateList($list, $tag);      // supprot multi-language
00350             }
00351             return true;
00352         }
00353         return false;
00354     }
00355     
00356     protected function getDOFromList(&$list, $selectFrom)
00357     {
00358         $pos0 = strpos($selectFrom, "[");
00359         $pos1 = strpos($selectFrom, "]");
00360         if ($pos0 > 0 && $pos1 > $pos0)
00361         {  // select from bizObj
00362             // support BizObjName[BizFieldName] or 
00363             // BizObjName[BizFieldName4Text:BizFieldName4Value] or 
00364             // BizObjName[BizFieldName4Text:BizFieldName4Value:BizFieldName4Pic]
00365             $bizObjName = substr($selectFrom, 0, $pos0);
00366             $pos3 = strpos($selectFrom, ":");
00367             if($pos3 > $pos0 && $pos3 < $pos1)
00368             {
00369                 $fieldName = substr($selectFrom, $pos0 + 1, $pos3 - $pos0 - 1);
00370                 $fieldName_v = substr($selectFrom, $pos3 + 1, $pos1 - $pos3 - 1);
00371             }
00372             else
00373             {
00374                 $fieldName = substr($selectFrom, $pos0 + 1, $pos1 - $pos0 - 1);
00375                 $fieldName_v = $fieldName;
00376             }
00377             $pos4 = strpos($fieldName_v, ":");
00378             if($pos4){
00379               $fieldName_v_mixed = $fieldName_v;
00380               $fieldName_v = substr($fieldName_v_mixed,0,$pos4);
00381               $fieldName_p = substr($fieldName_v_mixed, $pos4+1, strlen($fieldName_v_mixed)-$pos4-1);
00382               unset($fieldName_v_mixed);
00383             }
00384             $commaPos = strpos($selectFrom, ",", $pos1);
00385             if ($commaPos > $pos1)
00386                 $searchRule = trim(substr($selectFrom, $commaPos + 1));
00387             
00388             /* @var $bizObj BizDataObj */
00389             $bizObj = BizSystem::getObject($bizObjName);
00390             if (!$bizObj)
00391                 return false;
00392 
00393             $recList = array();
00394             $oldAssoc = $bizObj->m_Association;
00395             $bizObj->m_Association = null;
00396             QueryStringParam::reset();
00397             $recList = $bizObj->directFetch($searchRule);
00398             $bizObj->m_Association = $oldAssoc;
00399 
00400             foreach ($recList as $rec)
00401             {
00402                 $list[$i]['val'] = $rec[$fieldName_v];
00403                 $list[$i]['txt'] = $rec[$fieldName];
00404                 $list[$i]['pic'] = $rec[$fieldName_p];
00405                 $i++;
00406             }
00407            
00408             return true;
00409         }
00410         return false;
00411     }
00412     
00413     protected function getSimpleFromList(&$list, $selectFrom)
00414     {
00415         // in case of a|b|c
00416         if (strpos($selectFrom, "[") > 0 || strpos($selectFrom, "(") > 0)
00417             return;
00418         $recList = explode('|',$selectFrom);
00419         foreach ($recList as $rec)
00420         {
00421             $list[$i]['val'] = $rec;
00422             $list[$i]['txt'] = $rec;
00423             $list[$i]['pic'] = $rec;
00424             $i++;
00425         }
00426     }
00427     
00428     public function getSQLFromList(&$list)
00429     {
00430        $sql = $this->getSelectFromSQL();
00431        if (!$sql) return;
00432        $formObj = $this->getFormObj();
00433        $do = $formObj->getDataObj();
00434        $db = $do->getDBConnection();
00435        try {
00436               $resultSet = $db->query($sql);
00437               $recList = $resultSet->fetchAll();
00438               foreach ($recList as $rec)
00439                {
00440                    $list[$i]['val'] = $rec[0];
00441                    $list[$i]['txt'] = isset($rec[1]) ? $rec[1] : $rec[0];
00442                    $i++;
00443                }
00444        }
00445        catch (Exception $e)
00446         {
00447             BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: ".$e->getMessage());
00448             $this->m_ErrorMessage = "Error in SQL query: ".$sql.". ".$e->getMessage();
00449             throw new BDOException($this->m_ErrorMessage);
00450             return null;
00451         }
00452     }    
00453 
00454     
00455     protected function getOnChangeFunction()
00456     {
00457         $name = $this->m_Name;
00458         // loop through the event handlers
00459         $func = "";
00460 
00461         if ($this->m_EventHandlers == null)
00462             return null;
00463         $formobj = $this->getFormObj();
00464         foreach ($this->m_EventHandlers as $eventHandler)
00465         {
00466             $ehName = $eventHandler->m_Name;
00467             $event = $eventHandler->m_Event;
00468             $type = $eventHandler->m_FunctionType;
00469             if (!$event) continue;
00470             if($events[$event]!=""){
00471               $events[$event]=array_merge(array($events[$event]),array($eventHandler->getFormedFunction()));
00472             }else{
00473               $events[$event]=$eventHandler->getFormedFunction();
00474             }
00475 
00476         }
00477               
00478                      $function=$events['onchange'];
00479                      if(is_array($function)){
00480                             foreach($function as $f){
00481                                    $function_str.=$f.";";
00482                             }
00483                             $func .= $function_str;
00484                      }else{
00485                             $func .= $function;
00486                      }
00487               
00488         return $func;
00489     }    
00490     
00491     protected function translateList(&$list, $tag)
00492     {
00493        $module = $this->getModuleName($this->m_SelectFrom);
00494         if (empty($module))
00495             $module = $this->getModuleName($this->m_FormName);
00496        for ($i=0; $i<count($list); $i++)
00497        {
00498               $key = 'SELECTION_'.strtoupper($tag).'_'.$i.'_TEXT';
00499               $list[$i]['txt'] = I18n::t($list[$i]['txt'], $key, $module);
00500        }
00501     }    
00502 }
00503 
00504 
00505 
00506 ?>

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2