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

E:/E/GEAMP/www/openbiz/openbiz/bin/easy/EasyForm.php

00001 <?php
00017 include_once(OPENBIZ_BIN."/easy/Panel.php");
00018 include_once(OPENBIZ_BIN."/util/QueryStringParam.php");
00019 
00028 class EasyForm extends MetaObject implements iSessionObject
00029 {
00030     public $DATAFORMAT = 'RECORD';
00031 
00032     // metadata vars are public, necessary for metadata inheritance
00033     public $m_Title;
00034     public $m_Icon;
00035     public $m_Description;
00036     public $m_jsClass;
00037     public $m_DataObjName;
00038     public $m_Height;
00039     public $m_Width;
00040     public $m_DefaultForm;
00041   
00042     public $m_CanUpdateRecord;
00043     public $m_DirectMethodList = null; //list of method that can directly from browser
00044 
00045     public $m_Panels; 
00046 
00052     public $m_InheritFrom;
00053     
00059     public $m_DataPanel;
00064     public $m_ActionPanel;
00069     public $m_NavPanel;
00074     public $m_SearchPanel;
00075 
00076     public $m_TemplateEngine;
00077     public $m_TemplateFile;
00078     public $m_FormType;
00079     public $m_SubForms = null;
00080     public $m_EventName;
00081     public $m_Range = 10;
00082     public $m_CacheLifeTime = 0;
00083     public $m_FormParams;
00084 
00085     // parent form is the form that trigger the popup. "this" form is a popup form
00086     public $m_ParentFormName;
00087     // the form that drives navigation - the 1st form deplayed in the view
00088     public $m_DefaultFormName = null;
00089 
00090     // query helper
00091     public $m_QueryStringParam;
00092 
00093     public $m_Errors;   // errors array (error_element, error_message)
00094     public $m_Notices;  // list of notice messages
00095 
00096     // basic form vars
00097     protected $m_DataObj;
00098     protected $m_RecordId = null;
00099     public $m_ActiveRecord = null;
00100     public $m_FormInputs = null;
00101     public $m_SearchRule = null;
00102     public $m_FixSearchRule = null; // FixSearchRule is the search rule always applying on the search
00103     
00104     public $m_SortRule = null;
00105     
00106     protected $m_DefaultFixSearchRule = null;
00107     protected $m_SearchRuleBindValues;
00108     protected $m_Referer = "";
00109     public $m_MessageFile = null;
00110     protected $m_hasError = false;
00111     protected $m_ValidateErrors = array();
00112 
00113     // vars for grid(list)
00114     protected $m_CurrentPage = 1;
00115     protected $m_StartItem = 1;
00116     public $m_TotalPages = 1;
00117     protected $m_TotalRecords = 0;
00118     protected $m_RecordSet = null;
00119     protected $m_RefreshData = false;
00120     protected $m_Resource = "";
00121 
00122     protected $m_Messages;
00123     protected $m_InvokingElement = null;
00124     
00125     public $m_AutoRefresh=0;
00126     
00127     public $m_ReferenceFormName; //switch from which form
00128     protected $m_RecordAllowAccess = true;
00129 
00136     function __construct(&$xmlArr)
00137     {
00138         $this->readMetadata($xmlArr);
00139         //echo $_GET['referer'];
00140         $this->inheritParentObj();
00141     }
00142 
00143     public function allowAccess($access=null)
00144     {
00145        if(!$this->m_RecordAllowAccess)
00146        {
00151               return false; 
00152        }
00153        $result = parent::allowAccess($access);          
00154        return $result ;
00155     }
00156     
00163     protected function readMetadata(&$xmlArr)
00164     {
00165         parent::readMetaData($xmlArr);
00166         $this->m_InheritFrom = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["INHERITFROM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["INHERITFROM"] : null;        
00167         $this->m_Title = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TITLE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TITLE"] : null;
00168         $this->m_Icon = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["ICON"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["ICON"] : null;        
00169         $this->m_Description = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DESCRIPTION"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["DESCRIPTION"] : null;
00170         $this->m_jsClass = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["JSCLASS"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["JSCLASS"] : null;
00171         $this->m_Height = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["HEIGHT"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["HEIGHT"] : null;
00172         $this->m_Width = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["WIDTH"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["WIDTH"] : null;
00173         $this->m_DefaultForm = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DEFAULTFORM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["DEFAULTFORM"] : null;
00174         $this->m_TemplateEngine = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEENGINE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEENGINE"] : null;
00175         $this->m_TemplateFile = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEFILE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEFILE"] : null;
00176         $this->m_FormType = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["FORMTYPE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["FORMTYPE"] : null;
00177         $this->m_Range = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["PAGESIZE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["PAGESIZE"] : $this->m_Range;
00178         $this->m_FixSearchRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"] : null;
00179         $this->m_SortRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SORTRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SORTRULE"] : null;
00180               $this->m_DefaultFixSearchRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"] : null;
00181         
00182         $this->m_Name = $this->prefixPackage($this->m_Name);
00183         if ($this->m_InheritFrom == '@sourceMeta') $this->m_InheritFrom = '@'.$this->m_Name;
00184         else $this->m_InheritFrom = $this->prefixPackage($this->m_InheritFrom);
00185         $this->m_DataObjName = $this->prefixPackage($xmlArr["EASYFORM"]["ATTRIBUTES"]["BIZDATAOBJ"]);
00186 
00187         if (isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DIRECTMETHOD"]))
00188             $this->m_DirectMethodList = explode(",", strtolower(str_replace(" ", "",$xmlArr["EASYFORM"]["ATTRIBUTES"]["DIRECTMETHOD"])));
00189 
00190         $this->m_DataPanel = new Panel($xmlArr["EASYFORM"]["DATAPANEL"]["ELEMENT"],"",$this);
00191         $this->m_ActionPanel = new Panel($xmlArr["EASYFORM"]["ACTIONPANEL"]["ELEMENT"],"",$this);
00192         $this->m_NavPanel = new Panel($xmlArr["EASYFORM"]["NAVPANEL"]["ELEMENT"],"",$this);
00193         $this->m_SearchPanel = new Panel($xmlArr["EASYFORM"]["SEARCHPANEL"]["ELEMENT"],"",$this);
00194         $this->m_Panels = array($this->m_DataPanel, $this->m_ActionPanel, $this->m_NavPanel, $this->m_SearchPanel);
00195 
00196         $this->m_FormType = strtoupper($this->m_FormType);
00197 
00198         $this->m_EventName = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["EVENTNAME"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["EVENTNAME"] : null;
00199 
00200         $this->m_MessageFile = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["MESSAGEFILE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["MESSAGEFILE"] : null;
00201         $this->m_Messages = Resource::loadMessage($this->m_MessageFile , $this->m_Package);
00202 
00203         $this->m_CacheLifeTime = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["CACHELIFETIME"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["CACHELIFETIME"] : "0";
00204 
00205         $this->m_CurrentPage = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTPAGE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTPAGE"] : 1;
00206         $this->m_StartItem = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTITEM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTITEM"] : 1;
00207 
00208         $this->m_AutoRefresh = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["AUTOREFRESH"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["AUTOREFRESH"] : 0;
00209         
00210         // parse access
00211         if ($this->m_Access)
00212         {
00213             $arr = explode (".", $this->m_Access);
00214             $this->m_Resource = $arr[0];
00215         }
00216         if ($this->m_jsClass == "jbForm" && strtoupper($this->m_FormType) == "LIST") $this->m_jsClass = "Openbiz.TableForm";
00217         if ($this->m_jsClass == "jbForm") $this->m_jsClass = "Openbiz.Form";
00218         
00219               $this->translate();  // translate for multi-language support
00220     }
00221 
00227     protected function inheritParentObj()
00228     {
00229         if (!$this->m_InheritFrom) return;
00230         $parentObj = BizSystem::getObject($this->m_InheritFrom);
00231 
00232         $this->m_Title = $this->m_Title ? $this->m_Title : $parentObj->m_Title;
00233         $this->m_Icon = $this->m_Icon ? $this->m_Icon : $parentObj->m_Icon;        
00234         $this->m_Description  = $this->m_Description ? $this->m_Description : $parentObj->m_Description;
00235         $this->m_jsClass   = $this->m_jsClass ? $this->m_jsClass : $parentObj->m_jsClass;
00236         $this->m_Height   = $this->m_Height ? $this->m_Height : $parentObj->m_Height;
00237         $this->m_Width   = $this->m_Width ? $this->m_Width : $parentObj->m_Width;
00238         $this->m_DefaultForm   = $this->m_DefaultForm ? $this->m_DefaultForm : $parentObj->m_DefaultForm;        
00239         $this->m_TemplateEngine   = $this->m_TemplateEngine ? $this->m_TemplateEngine : $parentObj->m_TemplateEngine;
00240         $this->m_TemplateFile   = $this->m_TemplateFile ? $this->m_TemplateFile : $parentObj->m_TemplateFile;        
00241         $this->m_FormType   = $this->m_FormType ? $this->m_FormType : $parentObj->m_FormType;
00242         $this->m_Range   = $this->m_Range ? $this->m_Range : $parentObj->m_Range;
00243         $this->m_FixSearchRule   = $this->m_FixSearchRule ? $this->m_FixSearchRule : $parentObj->m_FixSearchRule;
00244         $this->m_DefaultFixSearchRule   = $this->m_DefaultFixSearchRule ? $this->m_DefaultFixSearchRule : $parentObj->m_DefaultFixSearchRule;                     
00245         $this->m_DataObjName   = $this->m_DataObjName ? $this->m_DataObjName : $parentObj->m_DataObjName;
00246         $this->m_DirectMethodList   = $this->m_DirectMethodList ? $this->m_DirectMethodList : $parentObj->m_DirectMethodList;
00247         $this->m_EventName   = $this->m_EventName ? $this->m_EventName : $parentObj->m_EventName;
00248         $this->m_MessageFile   = $this->m_MessageFile ? $this->m_MessageFile : $parentObj->m_MessageFile;
00249         $this->m_Messages = Resource::loadMessage($this->m_MessageFile , $this->m_Package);
00250               $this->m_CacheLifeTime   = $this->m_CacheLifeTime ? $this->m_CacheLifeTime : $parentObj->m_CacheLifeTime;
00251               $this->m_CurrentPage   = $this->m_CurrentPage ? $this->m_CurrentPage : $parentObj->m_CurrentPage;
00252               $this->m_StartItem   = $this->m_StartItem ? $this->m_StartItem : $parentObj->m_StartItem;        
00253         
00254         $this->m_DataPanel->merge($parentObj->m_DataPanel);
00255         $this->m_ActionPanel->merge($parentObj->m_ActionPanel);
00256         $this->m_NavPanel->merge($parentObj->m_NavPanel);
00257         $this->m_SearchPanel->merge($parentObj->m_SearchPanel);        
00258 
00259         if($this->m_DataPanel->current()){
00260                foreach ($this->m_DataPanel as $elem)
00261                    $elem->adjustFormName($this->m_Name);
00262         }
00263         if($this->m_ActionPanel->current()){
00264                foreach ($this->m_ActionPanel as $elem)
00265                    $elem->adjustFormName($this->m_Name);
00266         }
00267         if($this->m_NavPanel->current()){                
00268                foreach ($this->m_NavPanel as $elem)
00269                    $elem->adjustFormName($this->m_Name);
00270         }
00271         if($this->m_SearchPanel->current()){
00272                foreach ($this->m_SearchPanel as $elem)
00273                    $elem->adjustFormName($this->m_Name);            
00274         }   
00275               $this->m_Panels = array($this->m_DataPanel, $this->m_ActionPanel, $this->m_NavPanel, $this->m_SearchPanel);            
00276     }    
00277     
00285     protected function getMessage($messageId, $params=array())
00286     {
00287         $message = isset($this->m_Messages[$messageId]) ? $this->m_Messages[$messageId] : constant($messageId);
00288         //$message = I18n::getInstance()->translate($message);
00289         $message = I18n::t($message, $messageId, $this->getModuleName($this->m_Name));        
00290         $msg = @vsprintf($message,$params);
00291         if(!$msg){ //maybe in translation missing some %s can cause it returns null
00292               $msg = $message;
00293         }
00294         return $msg;
00295     }
00296 
00297     public function canDisplayForm()
00298     {
00299        
00300        if($this->getDataObj()->m_DataPermControl=='Y')
00301         {
00302               switch(strtolower($this->m_FormType))
00303               {
00304                      default:
00305                      case 'list':
00306                             return true;
00307                             break;
00308                      case 'detail':
00309                             $permCode=1;
00310                             break;  
00311                             
00312                      case 'edit':
00313                             $permCode=2;
00314                             break;                                    
00315               }
00316                $svcObj = BizSystem::GetService(DATAPERM_SERVICE);
00317                $result = $svcObj->checkDataPerm($this->fetchData(),$permCode,$this->getDataObj());
00318                if($result == false)
00319                {
00320                      return false;
00321                }
00322         }     
00323         return true;
00324     }
00325 
00326     public function canDeleteRecord($rec)
00327     {
00328        
00329        if($this->getDataObj()->m_DataPermControl=='Y')
00330         {                          
00331                $svcObj = BizSystem::GetService(DATAPERM_SERVICE);
00332                $result = $svcObj->checkDataPerm($rec,3,$this->getDataObj());
00333                if($result == false)
00334                {
00335                      return false;
00336                }
00337         }     
00338         return true;
00339     }
00340     
00341     
00348     public function getSessionVars($sessionContext)
00349     {
00350         $sessionContext->getObjVar($this->m_Name, "RecordId", $this->m_RecordId);
00351         $sessionContext->getObjVar($this->m_Name, "FixSearchRule", $this->m_FixSearchRule);
00352         $sessionContext->getObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);
00353         $sessionContext->getObjVar($this->m_Name, "SearchRuleBindValues", $this->m_SearchRuleBindValues);
00354         $sessionContext->getObjVar($this->m_Name, "SubForms", $this->m_SubForms);
00355         $sessionContext->getObjVar($this->m_Name, "ParentFormName", $this->m_ParentFormName);
00356         $sessionContext->getObjVar($this->m_Name, "DefaultFormName", $this->m_DefaultFormName);
00357         $sessionContext->getObjVar($this->m_Name, "CurrentPage", $this->m_CurrentPage);
00358         $sessionContext->getObjVar($this->m_Name, "PageSize", $this->m_Range);
00359         $sessionContext->getObjVar($this->m_Name, "ReferenceFormName", $this->m_ReferenceFormName);
00360         $sessionContext->getObjVar($this->m_Name, "SearchPanelValues", $this->m_SearchPanelValues);
00361     }
00362 
00369     public function setSessionVars($sessionContext)
00370     {
00371         $sessionContext->setObjVar($this->m_Name, "RecordId", $this->m_RecordId);
00372         $sessionContext->setObjVar($this->m_Name, "FixSearchRule", $this->m_FixSearchRule);
00373         $sessionContext->setObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);        
00374         $sessionContext->setObjVar($this->m_Name, "SearchRuleBindValues", $this->m_SearchRuleBindValues);
00375         $sessionContext->setObjVar($this->m_Name, "SubForms", $this->m_SubForms);
00376         $sessionContext->setObjVar($this->m_Name, "ParentFormName", $this->m_ParentFormName);
00377         $sessionContext->setObjVar($this->m_Name, "DefaultFormName", $this->m_DefaultFormName);
00378         $sessionContext->setObjVar($this->m_Name, "CurrentPage", $this->m_CurrentPage);
00379         $sessionContext->setObjVar($this->m_Name, "PageSize", $this->m_Range);
00380         $sessionContext->setObjVar($this->m_Name, "ReferenceFormName", $this->m_ReferenceFormName);
00381         $sessionContext->setObjVar($this->m_Name, "SearchPanelValues", $this->m_SearchPanelValues);        
00382     }
00383 
00389     public function invoke()
00390     {
00391         $argList = func_get_args();
00392         $param1 = array_shift($argList);
00393         // first one is element:eventhandler
00394         list ($elementName, $eventHandlerName) = explode(":", $param1);        
00395         $element = $this->getElement($elementName);
00396         $eventHandler = $element->m_EventHandlers->get($eventHandlerName);
00397         $this->m_InvokingElement = array($element, $eventHandler);
00398         // find the matching function
00399         list($funcName, $funcParams) = $eventHandler->parseFunction($eventHandler->m_OrigFunction);
00400         // call the function with rest parameters
00401         return call_user_func_array(array($this, $funcName), $argList);
00402     }
00403     
00410     public function validateRequest($methodName)
00411     {
00412         $methodName = strtolower($methodName);
00413         
00414         if ($methodName == "selectrecord" || $methodName == "invoke" || $methodName="sortrecord") 
00415             return true;
00416         // element, eventhandler
00417         list($element, $eventHandler) = $this->getInvokingElement();
00418         if ($element && $eventHandler)
00419         {
00420             if (stripos($eventHandler->m_OrigFunction, $methodName)===0)
00421                 return true;
00422         }
00423         // scan elements to match method
00424         foreach ($this->m_Panels as $panel)
00425         {
00426             foreach ($panel as $elem) 
00427                 if ($elem->matchRemoteMethod($methodName)) return true;
00428         }
00429 
00430         if (is_array($this->m_DirectMethodList))
00431         {
00432             foreach ($this->m_DirectMethodList as $value)
00433             {
00434                 if ($methodName == $value) return true;
00435             }
00436         }
00437 
00438         return false;
00439     }
00440 
00448     public function getProperty($propertyName)
00449     {
00450         $ret = parent::getProperty($propertyName);
00451         if ($ret !== null) return $ret;
00452 
00453         $pos1 = strpos($propertyName, "[");
00454         $pos2 = strpos($propertyName, "]");
00455         if ($pos1>0 && $pos2>$pos1)
00456         {
00457             $propType = substr($propertyName, 0, $pos1);
00458             $elementName = substr($propertyName, $pos1+1,$pos2-$pos1-1);
00459             switch(strtolower($propType))
00460             {
00461                             case 'param':               
00462               case 'params':
00463                      $result = $this->m_FormParams[$elementName];
00464                      break;
00465               default:
00466                      
00467                      $result = $this->getElement($elementName);
00468                      break;
00469             }            
00470             return $result;
00471         }
00472     }
00473 
00479     public function getDataObj()
00480     {
00481         if (!$this->m_DataObj)
00482         {
00483             if ($this->m_DataObjName)
00484                 $this->m_DataObj = BizSystem::objectFactory()->getObject($this->m_DataObjName);
00485             if($this->m_DataObj)
00486                 $this->m_DataObj->m_BizFormName = $this->m_Name;
00487             else
00488             {
00489                 //BizSystem::clientProxy()->showErrorMessage("Cannot get DataObj of ".$this->m_DataObjName.", please check your metadata file.");
00490                 return null;
00491             }
00492         }
00493         return $this->m_DataObj;
00494     }
00495 
00502     final public function setDataObj($dataObj)
00503     {
00504         $this->m_DataObj = $dataObj;
00505     }
00506 
00513     public function outputAttrs()
00514     {
00515         $output['name'] = $this->m_Name;
00516         $output['title'] = Expression::evaluateExpression($this->m_Title, $this);
00517         $output['icon'] = $this->m_Icon;
00518         $output['hasSubform'] = $this->m_SubForms ? 1 : 0;
00519         $output['currentPage'] = $this->m_CurrentPage;
00520         $output['currentRecordId'] = $this->m_RecordId;
00521         $output['totalPages'] = $this->m_TotalPages;
00522         $output['totalRecords'] = $this->m_TotalRecords;
00523         $output['description'] = str_replace('\n', "<br />", Expression::evaluateExpression($this->m_Description,$this));
00524         $output['elementSets'] = $this->getElementSet();
00525         $output['tabSets'] = $this->getTabSet();
00526         $output['ActionElementSets'] = $this->getElementSet($this->m_ActionPanel);    
00527         if($output['icon'])
00528         {   
00529                if(preg_match("/{.*}/si",$output['icon']))
00530                {
00531                      $output['icon'] = Expression::evaluateExpression($output['icon'], null);
00532                }
00533                else
00534                {
00535                      $output['icon'] = THEME_URL . "/" . Resource::getCurrentTheme() . "/images/".$output['icon'];
00536                }
00537         }
00538         return $output;
00539     }
00540 
00548     public function processDataObjError($errCode = 0)
00549     {
00550         $errorMsg = $this->getDataObj()->getErrorMessage();
00551         BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
00552         BizSystem::clientProxy()->showErrorMessage($errorMsg);
00553     }
00554 
00561     public function processFormObjError($errors)
00562     {
00563         $this->m_Errors = $errors;
00564         $this->m_hasError = true;
00565         return $this->rerender();
00566     }
00567 
00575     public function processBDOException($e)
00576     {
00577         $errorMsg = $e->getMessage();
00578         BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
00579         BizSystem::clientProxy()->showClientAlert($errorMsg);   //showErrorMessage($errorMsg);
00580     }
00581 
00588     final public function setSubForms($subForms)
00589     {
00590         // sub controls string with format: ctrl1;ctrl2...
00591         if (!$subForms || strlen($subForms) < 1)
00592         {
00593             $this->m_SubForms = null;
00594             return;
00595         }
00596         $subFormArr = explode(";", $subForms);
00597         unset($this->m_SubForms);
00598         foreach ($subFormArr as $subForm)
00599         {
00600             $this->m_SubForms[] = $this->prefixPackage($subForm);
00601         }
00602     }
00603 
00610     public function getViewObject()
00611     {
00612         global $g_BizSystem;
00613         $viewName = $g_BizSystem->getCurrentViewName();
00614         if (!$viewName) return null;
00615         $viewObj = BizSystem::getObject($viewName);
00616         return $viewObj;
00617     }
00618 
00624     public function getSubForms()
00625     {
00626         // ask view to give its subforms if not set yet
00627         return $this->m_SubForms;
00628     }
00629 
00636     public function getElement($elementName)
00637     {
00638         if ($this->m_DataPanel->get($elementName)) return $this->m_DataPanel->get($elementName);
00639         if ($this->m_ActionPanel->get($elementName)) return $this->m_ActionPanel->get($elementName);
00640         if ($this->m_NavPanel->get($elementName)) return $this->m_NavPanel->get($elementName);
00641         if ($this->m_SearchPanel->get($elementName)) return $this->m_SearchPanel->get($elementName);
00642     }
00643     
00644     public function getElementSet($panel = null)
00645     {
00646        if(!$panel){
00647               $panel = $this->m_DataPanel;
00648        }
00649        $setArr = array();
00650        $panel->rewind();
00651         while($panel->valid())            
00652         {      
00653               $elem = $panel->current();
00654               $panel->next();    
00655               if($elem->m_ElementSet && $elem->canDisplayed()){
00656                      //is it in array
00657                      if(in_array($elem->m_ElementSet,$setArr)){
00658                             continue;
00659                      }else{
00660                             array_push($setArr,$elem->m_ElementSet);
00661                      }
00662               }                          
00663         }
00664         return $setArr;
00665     }
00666     
00667        public function getTabSet($panel = null)
00668     {
00669        if(!$panel){
00670               $panel = $this->m_DataPanel;
00671        }
00672        $setArr = array();
00673        $tabSetArr = array();
00674        $panel->rewind();
00675         while($panel->valid())            
00676         {      
00677               $elem = $panel->current();
00678               $panel->next();    
00679               if($elem->m_TabSet && $elem->canDisplayed()){
00680                      //is it in array
00681                      if(in_array($elem->m_TabSet,$setArr)){
00682                             continue;
00683                      }else{
00684                             $setArr[$elem->m_TabSetCode]=$elem->m_TabSet;
00685                      }
00686               }                                               
00687         }
00688         foreach($setArr as $tabsetCode=>$tabset)
00689         {
00690               $elemSetArr = array();
00691               $panel->rewind();
00692                while($panel->valid())            
00693                {      
00694                      $elem = $panel->current();
00695                      $panel->next();    
00696                      if($elem->m_ElementSet && $elem->canDisplayed()){
00697                             //is it in array
00698                             if( $elem->m_TabSetCode!= $tabsetCode || 
00699                                    in_array($elem->m_ElementSet,$elemSetArr)){
00700                                    continue;
00701                             }else{
00702                                    array_push($elemSetArr,$elem->m_ElementSet);
00703                             }
00704                      }                                               
00705                }
00706                $tabSetArr[$tabsetCode]['SetName'] = $tabset;
00707                $tabSetArr[$tabsetCode]['Elems'] = $elemSetArr;
00708         }
00709         return $tabSetArr;
00710     }    
00711 
00718     public function getErrorElements($fields)
00719     {
00720         $errElements = array();
00721         foreach ($fields as $field=>$error)
00722         {
00723             $element = $this->m_DataPanel->getByField($field);
00724             $errElements[$element->m_Name]=$error;
00725         }
00726         return $errElements;
00727     }
00728 
00738     public function loadPicker($formName, $elementName="")
00739     {
00740         // set the ParentFormName and ParentCtrlName of the popup form
00741         /* @var $pickerForm EasyForm */
00742         $pickerForm = BizSystem::objectFactory()->getObject($formName);
00743 
00744         if ($elementName != "")
00745         {
00746             // set the picker map as well
00747             $element = $this->getElement($elementName);
00748             $pickerMap = $element->m_PickerMap;
00749         }
00750               $currentRecord = $this->readInputRecord();
00751         $pickerForm->setParentFormData($this->m_Name, $elementName, $pickerMap);
00752         $pickerForm->m_ParentFormRecord = $currentRecord;
00753         BizSystem::clientProxy()->redrawForm("DIALOG", $pickerForm->render());
00754     }
00755     
00756     public function loadDialog($formName, $id=null,$transId=false)
00757     {
00758        $paramFields = array();
00759         if ($id!=null)
00760             $paramFields["Id"] = $id;
00761         if ($transId!=false)
00762             $paramFields["Id"] = $this->m_RecordId;
00763         $this->_showForm($formName, "Dialog", $paramFields);
00764     }
00765 
00774     public function callService($class, $method, $param = null)
00775     {
00776         $service = BizSystem::getService($class);
00777         if($param){
00778               return $service->$method($param);
00779         }else{
00780               return $service->$method($this->m_Name);
00781         }
00782     }
00783 
00790     public function setRequestParams($paramFields)
00791     {                
00792         if ($paramFields)
00793         {
00794               $this->m_FixSearchRule=null; // reset fixsearchrule to clean the previous one in session
00795             foreach($paramFields as $fieldName=>$val)
00796             {
00797                 $element = $this->m_DataPanel->getByField($fieldName);
00798                 if($element->m_AllowURLParam=='Y')
00799                 {
00800                      if(!$this->getDataObj())return;
00801                     if($this->getDataObj()->getField($fieldName)->checkValueType($val))
00802                     {
00803                         //$this->setFixSearchRule("[$fldName]='$val'");
00804                         $queryString = QueryStringParam::formatQueryString("[$fieldName]", "=", $val);
00805                         $this->setFixSearchRule($queryString,false);
00806                         $this->m_SearchRuleBindValues = QueryStringParam::getBindValues();
00807                     }
00808                 }
00809             }
00810         }
00811     }
00812 
00813     public function setCurrentPage($pageid)
00814     {
00815        $this->m_CurrentPage = $pageid;
00816     }
00822     public function close()
00823     {
00824         BizSystem::clientProxy()->closePopup();
00825     }
00826 
00832     public function renderParent()
00833     {
00834         /* @var $parentForm EasyForm */
00835         $parentForm = BizSystem::objectFactory()->getObject($this->m_ParentFormName);
00836         $parentForm->rerender();
00837     }
00838 
00847     public function setFixSearchRule($rule = null, $cleanActualRule = true)
00848     {
00849         if ($cleanActualRule)
00850             $this->m_FixSearchRule = $this->m_DefaultFixSearchRule;
00851             
00852         if ($this->m_FixSearchRule && $rule)
00853         {            
00854             if (strpos($this->m_FixSearchRule, $rule) === false)
00855             {
00856                 $this->m_FixSearchRule = $this->m_FixSearchRule . " AND " . $rule;
00857             }
00858         }
00859         if (!$this->m_FixSearchRule && $rule){
00860             $this->m_FixSearchRule = $rule;
00861         }
00862     }
00863 
00869     public function fetchDataSet()
00870     {
00871         $dataObj = $this->getDataObj();
00872 
00873         if (!$dataObj) return null;
00874         
00875         QueryStringParam::setBindValues($this->m_SearchRuleBindValues);
00876 
00877         if ($this->m_RefreshData)
00878             $dataObj->resetRules();
00879         else
00880             $dataObj->clearSearchRule();
00881 
00882         if ($this->m_FixSearchRule)
00883         {
00884             if ($this->m_SearchRule)
00885                 $searchRule = $this->m_SearchRule . " AND " . $this->m_FixSearchRule;
00886             else
00887                 $searchRule = $this->m_FixSearchRule;
00888         }
00889         else
00890             $searchRule = $this->m_SearchRule;
00891 
00892         $dataObj->setSearchRule($searchRule);        
00893         if($this->m_StartItem>1)
00894         {
00895             $dataObj->setLimit($this->m_Range, $this->m_StartItem);
00896         }
00897         else
00898         {
00899             $dataObj->setLimit($this->m_Range, ($this->m_CurrentPage-1)*$this->m_Range);
00900         }      
00901         if($this->m_SortRule && $this->m_SortRule != $this->getDataObj()->m_SortRule)
00902         {
00903                      $dataObj->setSortRule($this->m_SortRule);
00904         }          
00905         $resultRecords = $dataObj->fetch();
00906         $this->m_TotalRecords = $dataObj->count();
00907         if ($this->m_Range && $this->m_Range > 0)
00908             $this->m_TotalPages = ceil($this->m_TotalRecords/$this->m_Range);
00909         $selectedIndex = 0;
00910         
00911         //if current page is large than total pages ,then reset current page to last page
00912         if($this->m_CurrentPage>$this->m_TotalPages && $this->m_TotalPages>0)
00913         {
00914               $this->m_CurrentPage = $this->m_TotalPages;
00915               $dataObj->setLimit($this->m_Range, ($this->m_CurrentPage-1)*$this->m_Range);
00916               $resultRecords = $dataObj->fetch();
00917         }
00918         
00919         $this->getDataObj()->setActiveRecord($resultRecords[$selectedIndex]);
00920 
00921         QueryStringParam::ReSet();
00922               if(!$this->m_RecordId)
00923               {
00924                      $this->m_RecordId = $resultRecords[0]["Id"];
00925               }else{
00926                      $foundRecordId = false;
00927                      foreach($resultRecords as $record)
00928                      {
00929                             if($this->m_RecordId==$record['Id'])
00930                             {
00931                                    $foundRecordId = true;
00932                             }
00933                      }
00934                      if($foundRecordId == false)
00935                      {
00936                             $this->m_RecordId=$result[0]['Id'];
00937                      }                    
00938               }
00939               
00940         return $resultRecords;
00941     }
00942 
00948     public function fetchData()
00949     {         
00950         // if has valid active record, return it, otherwise do a query
00951         if ($this->m_ActiveRecord != null)
00952             return $this->m_ActiveRecord;
00953         
00954         $dataObj = $this->getDataObj();
00955         if ($dataObj == null) return;
00956 
00957         if (strtoupper($this->m_FormType) == "NEW")
00958             return $this->getNewRecord();
00959               
00960         if (!$this->m_FixSearchRule && !$this->m_SearchRule){ 
00961               //if its a default sub form,even no search rule, but can still fetch a default record 
00962               if(!is_array($this->getDataObj()->m_Association)){
00963                      //only if its a default sub form and without any association then return emply array
00964                      return array();
00965               }
00966         }else{       
00967               QueryStringParam::setBindValues($this->m_SearchRuleBindValues);
00968                
00969                      
00970                if ($this->m_RefreshData)   $dataObj->resetRules();
00971                else $dataObj->clearSearchRule();
00972        
00973                if ($this->m_FixSearchRule)
00974                {
00975                    if ($this->m_SearchRule)
00976                        $searchRule = $this->m_SearchRule . " AND " . $this->m_FixSearchRule;
00977                    else
00978                        $searchRule = $this->m_FixSearchRule;
00979                }
00980                
00981                $dataObj->setSearchRule($searchRule);
00982                QueryStringParam::setBindValues($this->m_SearchRuleBindValues);
00983                $dataObj->setLimit(1);
00984         }
00985         $resultRecords = $dataObj->fetch();
00986               if(!count($resultRecords))
00987               {
00988                      $this->m_RecordAllowAccess=false;
00989               }
00990 
00991         $this->m_RecordId = $resultRecords[0]['Id'];
00992         $this->setActiveRecord($resultRecords[0]);      
00993         
00994         QueryStringParam::ReSet();
00995        if($this->getDataObj()){
00996                      $this->m_CanUpdateRecord = (int)$this->getDataObj()->canUpdateRecord();
00997        }        
00998         return $resultRecords[0];
00999     }
01000 
01007     public function gotoPage($page=1)
01008     {
01009         $tgtPage = intval($page);
01010         if ($tgtPage == 0) $tgtPage = 1;
01011         $this->m_CurrentPage = $tgtPage;
01012         $this->rerender();
01013     }
01014     public function gotoSelectedPage($elemName)
01015     {
01016         $page = BizSystem::clientProxy()->getFormInputs(str_replace(".","_", $this->m_Name).'_'.$elemName);
01017        $this->gotoPage($page);
01018     }
01019     public function setPageSize($elemName)
01020     {
01021         $pagesize = BizSystem::clientProxy()->getFormInputs(str_replace(".","_", $this->m_Name).'_'.$elemName);
01022        $this->m_Range=$pagesize;
01023        $this->UpdateForm();
01024     }    
01033     public function sortRecord($sortCol, $order='ASC')
01034     {
01035         $element = $this->getElement($sortCol);
01036         // turn off the OnSort flag of the old onsort field
01037         $element->setSortFlag(null);
01038         // turn on the OnSort flag of the new onsort field
01039         if ($order == "ASC")
01040             $order = "DESC";
01041         else
01042             $order = "ASC";
01043         $element->setSortFlag($order);
01044 
01045         // change the sort rule and issue the query
01046         $this->getDataObj()->setSortRule("[" . $element->m_FieldName . "] " . $order);
01047 
01048         // move to 1st page
01049         $this->m_CurrentPage = 1;
01050         $this->m_SortRule = "";
01051 
01052         $this->rerender();
01053     }
01054 
01060     public function runSearch()
01061     {
01062         include_once(OPENBIZ_BIN."/easy/SearchHelper.php");
01063         $searchRule = "";
01064         foreach ($this->m_SearchPanel as $element)
01065         {
01066             $searchStr = '';
01067               if(method_exists($element,"getSearchRule")){
01068                      $searchStr = $element->getSearchRule();                 
01069               }else{               
01070                    if (!$element->m_FieldName)
01071                        continue;
01072        
01073                    $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);            
01074                    if($element->m_FuzzySearch=="Y")
01075                    {
01076                        $value="*$value*";
01077                    }
01078                    if ($value!='')
01079                    {
01080                        $searchStr = inputValToRule($element->m_FieldName, $value, $this);                 
01081                    }
01082               }
01083               if($searchStr){
01084                      if ($searchRule == "")
01085                     $searchRule .= $searchStr;
01086                 else
01087                     $searchRule .= " AND " . $searchStr;
01088               }
01089         }
01090         $this->m_SearchRule = $searchRule;
01091         $this->m_SearchRuleBindValues = QueryStringParam::getBindValues();
01092         
01093 
01094         $this->m_RefreshData = true;
01095 
01096         $this->m_CurrentPage = 1;
01097 
01098         BizSystem::log(LOG_DEBUG,"FORMOBJ",$this->m_Name."::runSearch(), SearchRule=".$this->m_SearchRule);
01099 
01100               $recArr = $this->readInputRecord();              
01101               
01102               $this->m_SearchPanelValues = $recArr;
01103               
01104         
01105         $this->runEventLog();
01106         $this->rerender();
01107     }
01108 
01114     public function resetSearch()
01115     {
01116         $this->m_SearchRule = "";
01117         $this->m_RefreshData = true;
01118         $this->m_CurrentPage = 1;
01119         $this->runEventLog();
01120         $this->rerender();
01121     }
01122     
01123     public function setSearchRule($searchRule, $searchRuleBindValues=null)
01124     {
01125        $this->m_SearchRule = $searchRule;
01126        $this->m_SearchRuleBindValues = $searchRuleBindValues;
01127        $this->m_RefreshData = true;
01128         $this->m_CurrentPage = 1;
01129     }
01130     
01136     public function newRecord()
01137     {
01138         $this->processPostAction();
01139     }
01140 
01148     public function copyRecord($id=null)
01149     {
01150         if ($id==null || $id=='')
01151             $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
01152 
01153         if (!$id)
01154         {
01155             BizSystem::clientProxy()->showClientAlert($this->getMessage("PLEASE_EDIT_A_RECORD"));
01156             return;
01157         }
01158         $this->getActiveRecord($id);
01159         $this->processPostAction();
01160     }
01161 
01169     public function editRecord($id=null)
01170     {
01171         if ($id==null || $id=='')
01172             $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
01173               
01174         if (!isset($id))
01175         {
01176             BizSystem::clientProxy()->showClientAlert($this->getMessage("PLEASE_EDIT_A_RECORD"));
01177             return;
01178         }
01179 
01180         // update the active record with new update record
01181         $this->getActiveRecord($id);
01182 
01183         $this->processPostAction();
01184     }
01185 
01194     protected function _showForm($formName, $target, $paramFields)
01195     {
01196        
01197        $formName_org = $formName;
01198         if (!$this->m_DefaultFormName)
01199               $this->m_DefaultFormName = $this->m_Name;
01200        if ($formName == null)
01201        {
01202               if($this->m_ReferenceFormName == null)
01203               {
01204                      $formName = $this->m_DefaultFormName;
01205               }else{
01206                      if($formName = $this->m_ReferenceFormName){
01207                             //this judgement is for anti endless loop between swtich forms
01208                             $formObj = BizSystem::objectFactory()->getObject($this->m_ReferenceFormName);
01209                             if($formObj->m_ReferenceFormName == $this->m_Name){                                 
01210                                    $formName = $this->m_DefaultFormName;
01211                             }else{                             
01212                                    $formName = $this->m_ReferenceFormName;
01213                             }
01214                      }
01215               }
01216        }
01217        //if($this->getViewObject()->isInFormRefLibs($formName))
01218         {
01219             // get the form object
01220             /* @var $formObj EasyForm */
01221             $formObj = BizSystem::objectFactory()->getObject($formName);
01222             $formObj->m_DefaultFormName = $this->m_DefaultFormName;
01223             if($formName_org){
01224               //RefenerenceForm records where the from switch from
01225                      if( $this->m_FormType!='EDIT' &&
01226                             $this->m_FormType!='NEW' &&
01227                             $this->m_FormType!='COPY' ){
01228                      $formObj->m_ReferenceFormName = $this->m_Name;
01229                      } 
01230             }
01231             
01232             //if has more than Id field as params then $clearFixSearchRule is false, means join all where rules
01233             $paramTemp = $paramFields;           
01234             unset($paramTemp['Id']);
01235             if(count($paramTemp)){
01236               $clearFixSearchRule = false;
01237             }else{
01238               $clearFixSearchRule = true;
01239             }
01240             foreach($paramFields as $fieldName=>$val){                
01241               $formObj->m_FormParams[$fieldName] = $val;
01242                 $formObj->setFixSearchRule("[$fieldName]='$val'",$clearFixSearchRule);                
01243                 if($fieldName=="Id"){
01244                      $formObj->setRecordId($val);
01245                 }
01246             }
01247 
01248             if(!$formObj->canDisplayForm())
01249             {
01250               $formObj->m_ErrorMessage = $this->getMessage("FORM_OPERATION_NOT_PERMITTED",$formObj->m_Name);
01251          
01252                      if (strtoupper($this->m_FormType) == "LIST"){
01253                             BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
01254                             BizSystem::clientProxy()->showClientAlert($formObj->m_ErrorMessage);
01255                      }else{
01256                             $this->processFormObjError(array($formObj->m_ErrorMessage));   
01257                      }
01258                    
01259                    return false;
01260             }           
01261              
01262             switch ($target)
01263             {
01264                 case "Popup":
01265                     $formObj->m_ParentFormName = $this->m_Name;
01266                     echo $formObj->render();
01267                     break;
01268                 case "Dialog":
01269                     $formObj->m_ParentFormName = $this->m_Name;
01270                     BizSystem::clientProxy()->redrawForm("DIALOG", $formObj->render());
01271                     break;
01272                 default:
01273                     BizSystem::clientProxy()->redrawForm($this->m_Name, $formObj->render());
01274             }
01275         }
01276     }
01277 
01285     public function deleteRecord($id=null)
01286     {
01287         if ($id==null || $id=='')
01288             $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
01289 
01290         $selIds = BizSystem::clientProxy()->getFormInputs('row_selections', false);
01291         if ($selIds == null)
01292             $selIds[] = $id;
01293         foreach ($selIds as $id)
01294         {            
01295             $dataRec = $this->getDataObj()->fetchById($id);
01296             $this->getDataObj()->setActiveRecord($dataRec);
01297             
01298             if(!$this->canDeleteRecord($dataRec))
01299             {
01300               $this->m_ErrorMessage = $this->getMessage("FORM_OPERATION_NOT_PERMITTED",$this->m_Name);         
01301                      if (strtoupper($this->m_FormType) == "LIST"){
01302                             BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
01303                             BizSystem::clientProxy()->showClientAlert($this->m_ErrorMessage);
01304                      }else{
01305                             $this->processFormObjError(array($this->m_ErrorMessage));      
01306                      }      
01307                      return;
01308             }
01309             
01310             // take care of exception
01311             try
01312             {
01313                 $dataRec->delete();
01314             } catch (BDOException $e)
01315             {
01316                 // call $this->processBDOException($e);
01317                 $this->processBDOException($e);
01318                 return;
01319             }
01320         }
01321         if (strtoupper($this->m_FormType) == "LIST")
01322             $this->rerender();
01323 
01324         $this->runEventLog();
01325         $this->processPostAction();
01326     }
01327 
01333     public function removeRecord ()
01334     {
01335        if ($id==null || $id=='')
01336             $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
01337 
01338         $selIds = BizSystem::clientProxy()->getFormInputs('row_selections', false);
01339         if ($selIds == null)
01340             $selIds[] = $id;
01341         foreach ($selIds as $id)
01342         { 
01343               
01344               $rec = $this->getDataObj()->fetchById($id);
01345                $ok = $this->getDataObj()->removeRecord($rec, $bPrtObjUpdated);
01346                if (! $ok)
01347                    return $this->processDataObjError($ok);
01348         }        
01349 
01350         $this->runEventLog();
01351         $this->rerender();
01352               if($this->m_ParentFormName)
01353               {
01354                      $this->renderParent();
01355               }
01356      }
01357 
01365     public function selectRecord($recId)
01366     {
01367         if ($recId==null || $recId=='')
01368             $recId = BizSystem::clientProxy()->getFormInputs('_selectedId');
01369         $this->m_RecordId = $recId;
01370         if($this->getDataObj()){
01371               $this->getDataObj()->setActiveRecordId($this->m_RecordId);
01372         }
01373         $this->rerender(false); // not redraw the this form, but draw the subforms
01374         //$this->rerender(); 
01375     }
01376 
01382     public function getElementID()
01383     {
01384         $id = $this->m_DataPanel->getByField('Id')->getValue();
01385         if($id)
01386         {
01387             return (int)$id;
01388         }
01389         else
01390         {
01391             return (int)$this->m_RecordId;
01392         }
01393     }
01394 
01402     public function saveRecord()
01403     {
01404         if (strtoupper($this->m_FormType) == "NEW")
01405         {
01406             $this->insertRecord();
01407         }
01408         else
01409         {
01410             $this->updateRecord();
01411         }
01412     }
01413 
01419     public function updateRecord()
01420     {
01421         $currentRec = $this->fetchData();
01422         $recArr = $this->readInputRecord();
01423         $this->setActiveRecord($recArr);
01424         if (count($recArr) != 0){
01425               
01426                try
01427                {
01428                    $this->ValidateForm();
01429                }
01430                catch (ValidationException $e)
01431                {
01432                    $this->processFormObjError($e->m_Errors);
01433                    return;
01434                }
01435        
01436                if ($this->_doUpdate($recArr, $currentRec) == false)
01437                    return;
01438         
01439         }
01440         // in case of popup form, close it, then rerender the parent form
01441         if ($this->m_ParentFormName)
01442         {
01443             $this->close();
01444 
01445             $this->renderParent();
01446         }
01447 
01448         
01449         $this->processPostAction();
01450 
01451     }
01452 
01453     
01454        public function updateFieldValueAdd($id,$fld_name,$value,$min,$max)
01455     {         
01456        if($value>=$max){
01457               $value = $min;
01458        }else{
01459               $value++;
01460        }
01461               return $this->updateFieldValue($id,$fld_name,$value);
01462 
01463     }        
01464     
01465        public function updateFieldValueXor($id,$fld_name,$value)
01466     {         
01467        if($value>0){
01468               $value_xor = 0;
01469        }else{
01470               $value_xor = 1;
01471        }
01472               return $this->updateFieldValue($id,$fld_name,$value_xor);
01473 
01474     }    
01475     
01481     public function updateFieldValue($Id,$fld_name,$value)
01482     {
01483        
01484               $element = $this->m_DataPanel->get($fld_name);
01485               $fieldname = $element->m_FieldName;
01486         $currentRec = $this->getActiveRecord($Id);
01487         $recArr = $this->getActiveRecord($Id);
01488               $recArr[$fieldname]=$value;
01489         if ($this->_doUpdate($recArr, $currentRec) == false)
01490             return;
01491               $this->UpdateForm();
01492     }
01500     protected function _doUpdate($inputRecord, $currentRecord)
01501     {
01502 
01503 
01504         $dataRec = new DataRecord($currentRecord, $this->getDataObj());
01505 
01506         foreach ($inputRecord as $k => $v){
01507               $dataRec[$k] = $v; // or $dataRec->$k = $v;
01508         }
01509 
01510         try
01511         {
01512             $dataRec->save();
01513         }
01514         catch (ValidationException $e)
01515         {
01516             $errElements = $this->getErrorElements($e->m_Errors);           
01517               if(count($e->m_Errors)==count($errElements)){
01518               $this->processFormObjError($errElements);
01519             }else{                 
01520               $errmsg = implode("<br />",$e->m_Errors);
01521                       BizSystem::clientProxy()->showErrorMessage($errmsg);
01522             }
01523             return false;
01524         }
01525         catch (BDOException $e)
01526         {
01527             $this->processBDOException($e);
01528             return false;
01529         }
01530               $this->m_ActiveRecord = null;
01531         $this->getActiveRecord($dataRec["Id"]);
01532 
01533         $this->runEventLog();
01534         return true;
01535     }
01536 
01542     public function insertRecord()
01543     {
01544         $recArr = $this->readInputRecord();
01545         $this->setActiveRecord($recArr);
01546         if (count($recArr) == 0)
01547             return;
01548 
01549         try
01550         {
01551             $this->ValidateForm();
01552         }
01553         catch (ValidationException $e)
01554         {
01555             $this->processFormObjError($e->m_Errors);
01556             return;
01557         }
01558 
01559         $this->_doInsert($recArr);
01560         
01561         
01562 
01563         // in case of popup form, close it, then rerender the parent form
01564         if ($this->m_ParentFormName)
01565         {
01566             $this->close();
01567 
01568             $this->renderParent();
01569         }
01570 
01571         $this->processPostAction();
01572     }
01573 
01580     protected function _doInsert($inputRecord)
01581     {
01582        
01583         $dataRec = new DataRecord(null, $this->getDataObj());
01584 
01585         // $inputRecord['Id'] = null; // comment it out for name PK case 
01586         foreach ($inputRecord as $k => $v)
01587             $dataRec[$k] = $v; // or $dataRec->$k = $v;
01588 
01589         try
01590         {
01591            $dataRec->save();
01592         }
01593         catch (ValidationException $e)
01594         {
01595             $errElements = $this->getErrorElements($e->m_Errors);
01596             if(count($e->m_Errors)==count($errElements)){
01597               $this->processFormObjError($errElements);
01598             }else{                 
01599               $errmsg = implode("<br />",$e->m_Errors);
01600                       BizSystem::clientProxy()->showErrorMessage($errmsg);
01601             }
01602             return;
01603         }
01604         catch (BDOException $e)
01605         {
01606             $this->processBDOException($e);
01607             return;
01608         }
01609               $this->m_ActiveRecord = null;
01610         $this->getActiveRecord($dataRec["Id"]);
01611 
01612         $this->runEventLog();
01613         return $dataRec["Id"];
01614     }
01615 
01621     public function cancel()
01622     {
01623         $this->processPostAction();
01624     }
01625 
01632     public function updateForm()
01633     {
01634         // read the input to form controls
01635         //@todo: read inputs but should be skipp uploaders elements
01636         $recArr = $this->readInputRecord();        
01637         $this->setActiveRecord($recArr);        
01638         $this->rerender();
01639     }
01640 
01649     public function autoSuggest($input)
01650     {
01651        $foo = $_POST;
01652        $hidden_flag = FALSE;
01653         if (strpos($input, '_hidden'))
01654         {
01655             $realInput = str_replace('_hidden', '', $input);
01656                      $hidden_flag = TRUE;            
01657         } else
01658         {
01659             $realInput = $input;
01660         }
01661 
01662         $value = BizSystem::clientProxy()->getFormInputs($input);
01663 
01664         // get the select from list of the element
01665         $element = $this->getElement($realInput);
01666         $element->setValue($value);
01667         $fromlist = array();
01668         $element->getFromList($fromlist);
01669         echo "<ul>";
01670         if ($fromlist)
01671         {
01672             if ($hidden_flag = TRUE)
01673             {
01674               $count=0;
01675                 foreach ($fromlist as $item)
01676                 {                  
01677                     echo "<li id=" . $item['txt'] . ">" . $item['val'] . "</li>";
01678                     $count++;
01679                     if($count>=5) break;
01680                 }
01681             }
01682             else
01683             {
01684               $count=0;
01685                 foreach ($fromlist as $item)
01686                 {
01687                     echo "<li>" . $item['txt'] . "</li>";
01688                     $count++;
01689                     if($count>=5) break;
01690                 }
01691             }
01692         }
01693         echo "</ul>";
01694     }
01695 
01703     protected function validateForm($cleanError = true)
01704     {
01705         if($cleanError == true)
01706         {
01707             $this->m_ValidateErrors = array();
01708         }
01709         $this->m_DataPanel->rewind();
01710         while($this->m_DataPanel->valid())
01711         {
01712             /* @var $element Element */
01713             $element = $this->m_DataPanel->current();
01714             if($element->m_Label)
01715             {
01716                 $elementName = $element->m_Label;
01717             }
01718             else
01719             {
01720                 $elementName = $element->m_Text;
01721             }
01722             if ($element->checkRequired() === true &&
01723                     ($element->m_Value==null || $element->m_Value == ""))
01724             {
01725                 $errorMessage = $this->getMessage("FORM_ELEMENT_REQUIRED",array($elementName));
01726                 $this->m_ValidateErrors[$element->m_Name] = $errorMessage;
01727                 //return false;
01728             }
01729             elseif ($element->m_Value!==null && $element->Validate() == false)
01730             {
01731                 $validateService = BizSystem::getService(VALIDATE_SERVICE);
01732                 $errorMessage = $this->getMessage("FORM_ELEMENT_INVALID_INPUT",array($elementName,$value,$element->m_Validator));                
01733                 if ($errorMessage == false)
01734                 { //Couldn't get a clear error message so let's try this
01735                     $errorMessage = $validateService->getErrorMessage($element->m_Validator, $elementName);
01736                 }
01737                 $this->m_ValidateErrors[$element->m_Name] = $errorMessage;
01738                 //return false;
01739             }
01740             $this->m_DataPanel->next() ;
01741         }
01742         if (count($this->m_ValidateErrors) > 0)
01743         {
01744             throw new ValidationException($this->m_ValidateErrors);
01745             return false;
01746         }
01747         return true;
01748     }
01749     
01755     protected function readInputRecord()
01756     {
01757         $recArr = array();
01758         foreach ($this->m_DataPanel as $element)
01759         {
01760             $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
01761             if ($value ===null && (
01762                  !is_a($element,"FileUploader")
01763               && !is_a($element,"FormElement")
01764               && !is_subclass_of($element,"FileUploader")
01765               && !is_a($element,"Checkbox")             
01766               )){           
01767               continue;
01768             }
01769             $element->setValue($value);
01770             $this->m_FormInputs[$element->m_Name] = $value;
01771             $value = $element->getValue();
01772             if ( $element->m_FieldName)
01773                 $recArr[$element->m_FieldName] = $value;
01774         }
01775 
01776         foreach ($this->m_SearchPanel as $element)
01777         {
01778             $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
01779             $element->setValue($value);
01780             $this->m_FormInputs[$element->m_Name] = $value;
01781             $value = $element->getValue();
01782             if ($value !== null && $element->m_FieldName)
01783                 $recArr[$element->m_FieldName] = $value;
01784         }
01785         return $recArr;
01786     }
01787 
01793     protected function readInputs()
01794     {
01795         $inputArr = array();
01796         foreach ($this->m_DataPanel as $element)
01797         {
01798             $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
01799             $element->setValue($value);
01800             $inputArr[$element->m_Name] = $value;
01801         }
01802 
01803         foreach ($this->m_SearchPanel as $element)
01804         {
01805             $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
01806             $element->setValue($value);
01807             $inputArr[$element->m_Name] = $value;
01808         }
01809         return $inputArr;
01810     }
01811 
01812 
01813     public function setFormInputs($inputArr=null)
01814     {
01815         if(!$inputArr){
01816               $inputArr = $this->m_FormInputs;
01817         } 
01818        if(!is_array($inputArr)){
01819               $inputArr = array();
01820         }        
01821         foreach ($this->m_DataPanel as $element)
01822         {
01823             if (isset($inputArr[$element->m_Name]))
01824             {             
01825               $element->setValue($inputArr[$element->m_Name]);                          
01826             }
01827         }
01828 
01829         foreach ($this->m_SearchPanel as $element)
01830         {
01831             if (isset($inputArr[$element->m_Name]))
01832             {
01833               $element->setValue($inputArr[$element->m_Name]);
01834             }
01835         }
01836         return $inputArr;
01837     }    
01838 
01844     protected function getNewRecord()
01845     {
01846         $recArr = $this->getDataObj()->newRecord();
01847         if (! $recArr)
01848             return null;
01849         // load default values if new record value is empty
01850         $defaultRecArr = array();
01851         foreach ($this->m_DataPanel as $element)
01852         {
01853             if ($element->m_FieldName)
01854             {
01855                 $defaultRecArr[$element->m_FieldName] = $element->getDefaultValue();
01856             }
01857         }
01858         foreach ($recArr as $field => $val)
01859         {
01860             if ($val == "" && $defaultRecArr[$field] != "")
01861             {
01862                 $recArr[$field] = $defaultRecArr[$field];
01863             }
01864         }
01865         return $recArr;
01866     }
01867 
01875     public function render()
01876     {
01877         if (!$this->allowAccess())
01878             return "";
01879         $this->setClientScripts();
01880 
01881         if($this->m_CacheLifeTime>0 && $this->m_SubForms == null)
01882         {
01883             $cache_id = md5($this->m_Name);
01884             //try to process cache service.
01885             $cacheSvc = BizSystem::getService(CACHE_SERVICE,1);
01886             $cacheSvc->init($this->m_Name,$this->m_CacheLifeTime);
01887             if($cacheSvc->test($cache_id))
01888             {
01889                 BizSystem::log(LOG_DEBUG, "FORM", "Cache Hit. form name = ".$this->m_Name);
01890                 $output = $cacheSvc->load($cache_id);
01891             }
01892             else
01893             {
01894                 BizSystem::log(LOG_DEBUG, "FORM", "Set cache. form name = ".$this->m_Name);
01895                 $output = $this->renderHTML();
01896                 $cacheSvc->save($output, $cache_id);
01897             }
01898             return $output;
01899         }
01900 
01901         //Moved the renderHTML function infront of declaring subforms
01902         $renderedHTML = $this->renderHTML();
01903 
01904         // prepare the subforms' dataobjs, since the subform relates to parent form by dataobj association
01905         if ($this->m_SubForms && $this->getDataObj())
01906         {
01907             foreach ($this->m_SubForms as $subForm)
01908             {
01909                 $formObj = BizSystem::objectFactory()->getObject($subForm);
01910                 $dataObj = $this->getDataObj()->getRefObject($formObj->m_DataObjName);
01911                 if ($dataObj)
01912                     $formObj->setDataObj($dataObj);
01913             }
01914         }
01915               if (!$this->allowAccess())
01916               {
01917            return "";
01918               }
01919         return $renderedHTML;
01920     }
01921 
01927     protected function renderContextMenu ()
01928     {
01929         $menuList = array();
01930         foreach ($this->m_Panels as $panel)
01931         {
01932             $panel->rewind();
01933             while ($element = $panel->current())
01934             {
01935                 $panel->next();
01936                 if (method_exists($element,'getContextMenu') && $menus = $element->getContextMenu())
01937                 {
01938                     foreach ($menus as $m)
01939                         $menuList[] = $m;
01940                 }
01941             }
01942         }
01943         if (count($menuList) == 0)
01944             return "";
01945         $str = "<div  class='contextMenu' id='" . $this->m_Name . "_contextmenu'>\n";
01946         $str .= "<div class=\"contextMenu_header\" ></div>\n";
01947         $str .= "<ul>\n";
01948         foreach ($menuList as $m)
01949         {
01950             $func = $m['func'];
01951             $shortcutKey = isset($m['key']) ? " (".$m['key'].")" : "";
01952             $str .= "<li><a href=\"javascript:void(0)\" onclick=\"$func\">".$m['text'].$shortcutKey."</a></li>\n";
01953         }
01954         $str .= "</ul>\n";
01955         $str .= "<div class=\"contextMenu_footer\" ></div>\n";
01956         $str .= "</div>\n";
01957         $str .= "
01958 <script>
01959 $('".$this->m_Name."').removeAttribute('onContextMenu');
01960 $('".$this->m_Name."').oncontextmenu=function(event){return Openbiz.Menu.show(event, '".$this->m_Name."_contextmenu');};
01961 $('".$this->m_Name."').observe('click',Openbiz.Menu.hide);
01962 </script>";
01963         return $str;
01964     }
01965 
01973     public function rerender($redrawForm=true, $hasRecordChange=true)
01974     {
01975         if ($redrawForm)
01976         {
01977             BizSystem::clientProxy()->redrawForm($this->m_Name, $this->renderHTML());
01978         }
01979 
01980         if ($hasRecordChange)
01981         {
01982             $this->rerenderSubForms();
01983         }
01984     }
01985 
01992     protected function rerenderSubForms()
01993     {
01994         if (! $this->m_SubForms)
01995             return;
01996         foreach ($this->m_SubForms as $subForm)
01997         {
01998             $formObj = BizSystem::objectFactory()->getObject($subForm);
01999             $dataObj = $this->getDataObj()->getRefObject($formObj->m_DataObjName);
02000             if ($dataObj)
02001                 $formObj->setDataObj($dataObj);
02002             $formObj->rerender();
02003         }
02004         return;
02005     }
02006 
02012     protected function renderHTML()
02013     {                       
02014         include_once(OPENBIZ_BIN."/easy/FormRenderer.php");
02015         $formHTML = FormRenderer::render($this);
02016         $otherHTML = $this->rendercontextmenu();
02017         
02018         
02019         if(preg_match('/iPad/si',$_SERVER['HTTP_USER_AGENT']) || 
02020               preg_match('/iPhone/si',$_SERVER['HTTP_USER_AGENT'])){
02021                      $otherHTML.="
02022                      <script>
02023                             var a=document.getElementsByTagName('a');
02024                             for(var i=0;i<a.length;i++)
02025                             {
02026                                    if(a[i].getAttribute('href').indexOf('javascript:')==-1
02027                                    && a[i].getAttribute('href').indexOf('#')==-1)
02028                                           {
02029                                               a[i].onclick=function()
02030                                               {
02031                                                      try{
02032                                                         show_loader();
02033                                                  }catch(e){
02034                                                         
02035                                                  }
02036                                                   window.location=this.getAttribute('href');
02037                                                   return false
02038                                               }
02039                                           }else{
02040                                           }
02041                             } 
02042                             </script>                   
02043                      ";
02044               } 
02045         if(!$this->m_ParentFormName)
02046         {
02047               if (($viewObj = $this->getViewObject())!=null)
02048                 $viewObj->m_LastRenderedForm = $this->m_Name;
02049         }
02050         return $formHTML ."\n". $otherHTML;
02051     }
02052 
02058     protected function getEventLogMsg()
02059     {
02060         list($element, $eventHandler) = $this->getInvokingElement();
02061         $eventLogMsg = $eventHandler->m_EventLogMsg;
02062         if($eventLogMsg)
02063         {
02064             return $eventLogMsg;
02065         }
02066         else
02067         {
02068             return null;
02069         }
02070     }
02071 
02077     protected function getOnEventElements()
02078     {
02079         $elementList = array();
02080         foreach ($this->m_DataPanel as $element)
02081         {
02082             if ($element->m_OnEventLog=="Y")
02083                 $elementList[] = $element->m_Value;
02084         }
02085         return $elementList;
02086     }
02087 
02093     protected function runEventLog()
02094     {
02095         $logMessage = $this->getEventLogMsg();
02096         $eventName = $this->m_EventName;
02097         if($logMessage && $eventName)
02098         {
02099             $logElements = $this->getOnEventElements();
02100             $eventlog       = BizSystem::getService(EVENTLOG_SERVICE);
02101             $eventlog->log($eventName, $logMessage, $logElements);
02102         }
02103     }
02104 
02110     protected function getRedirectPage()
02111     {
02112         // get the control that issues the call
02113         // __this is elementName:eventHandlerName
02114         list($element, $eventHandler) = $this->getInvokingElement();
02115         $eventHandlerName = $eventHandler->m_Name;
02116         $redirectPage = $element->getRedirectPage($eventHandlerName); // need to get postaction of eventhandler
02117         $functionType = $element->getFunctionType($eventHandlerName);
02118         switch ($functionType)
02119         {
02120             case "Popup":
02121             case "Prop_Window":
02122             case "Prop_Dialog":
02123                 $target = "Popup";
02124                 break;
02125             default:
02126                 $target = "";
02127         }
02128         return array($redirectPage, $target);
02129     }
02130 
02139     public function switchForm($formName=null, $id=null, $params=null, $target=null)
02140     {         
02141        $paramFields = array();
02142        if($params){
02143               parse_str(urldecode($params),$paramFields);
02144        }
02145         if ($id!=null)
02146             $paramFields["Id"] = $id;
02147         $this->_showForm($formName, $target, $paramFields);
02148     }
02149     
02150     public function parentSwitchForm($formName=null, $id=null, $params=null, $target=null)
02151     {
02152        if($this->m_ParentFormName){
02153               $formObj = BizSystem::getObject($this->m_ParentFormName);
02154               return $formObj->switchForm($formName, $id, $params, $target);
02155        }
02156     }
02157 
02158        public function targetSwitchForm($targetForm, $formName=null, $id=null, $params=null, $target=null)
02159     {
02160        if($targetForm){
02161               $formObj = BizSystem::getObject($targetForm);
02162               if($formObj){
02163                      return $formObj->switchForm($formName, $id, $params, $target);
02164               }
02165        }
02166     }
02172     protected function getInvokingElement()
02173     {
02174        if ($this->m_InvokingElement)
02175               return $this->m_InvokingElement;
02176        // __this is elementName:eventHandlerName
02177         $elementAndEventName = BizSystem::clientProxy()->getFormInputs("__this");
02178         if (! $elementAndEventName)
02179               return array(null,null);
02180         list ($elementName, $eventHandlerName) = explode(":", $elementAndEventName);
02181         $element = $this->getElement($elementName);
02182         $eventHandler = $element->m_EventHandlers->get($eventHandlerName);
02183         $this->m_InvokingElement = array($element, $eventHandler);
02184         return $this->m_InvokingElement;
02185     }
02186 
02192     protected function processPostAction()
02193     {
02194         // get the $redirectPage from eventHandler
02195         list($redirectPage,$target) = $this->getRedirectPage();
02196         if ($redirectPage)
02197         {
02198             if($this->m_hasError==false)
02199             {
02200                 // if the redirectpage start with "form=", render the form to the target which is defined by FuntionType
02201                 if (strpos($redirectPage,"form=") === 0)
02202                 {
02203                     parse_str($redirectPage, $output);
02204                     $formName = $output['form'];
02205 
02206                     // parse query string. e.g. fld:Id=val&fld:name=val
02207                     $paramFields = array();
02208                     foreach ($output as $key=>$value)
02209                     {
02210                         if (substr($key, 0, 4) == "fld:")
02211                         {
02212                             $fieldName = substr($key, 4);
02213                             $paramFields[$fieldName] = $value;
02214                         }
02215                     }
02216 
02217                     $this->_showForm($formName, $target, $paramFields);
02218                 }
02219                 else
02220                 {
02221                     // otherwise, do page redirection
02222                     BizSystem::clientProxy()->ReDirectPage($redirectPage);
02223                 }
02224             }
02225         }
02226     }
02227 
02234     public function getActiveRecord($recId=null)
02235     {
02236         if ($this->m_ActiveRecord != null)
02237         {
02238             if($this->m_ActiveRecord['Id'] != null)
02239             {
02240                 return $this->m_ActiveRecord;
02241             }
02242         }
02243 
02244         if ($recId==null || $recId=='')
02245             $recId = BizSystem::clientProxy()->getFormInputs('_selectedId');
02246         if ($recId==null || $recId=='')
02247             return null;
02248         $this->m_RecordId = $recId;
02249 
02250         // TODO: may consider cache the current record in session
02251         if($this->getDataObj()){
02252                $this->getDataObj()->setActiveRecordId($this->m_RecordId);
02253                $rec = $this->getDataObj()->getActiveRecord();
02254        
02255                // update the record row
02256                $this->m_DataPanel->setRecordArr($rec);
02257        
02258                $this->m_ActiveRecord = $rec;
02259         }
02260         return $rec;
02261     }
02262 
02263        public function getRecordId(){
02264        return $this->m_RecordId;
02265     }
02266     public function setRecordId($val){
02267        $this->m_RecordId = $val;
02268        return $val; 
02269     }
02270     
02277     protected function setActiveRecord($record)
02278     {
02279         // update the record row
02280         $this->m_DataPanel->setRecordArr($record);           
02281         if(!isset($this->m_ActiveRecord["Id"]) && 
02282               $this->m_RecordId!=null && 
02283               (strtoupper($this->m_FormType) == 'EDIT' || $this->m_FormType==null )){     
02284                      if($this->getDataObj()){    
02285                             $this->m_ActiveRecord = $this->getDataObj()->fetchById($this->m_RecordId)->toArray();
02286                      }                    
02287         }
02288               if(is_array($record)){              
02289                foreach($record as $key=>$value){
02290                      if($key=='extend')continue;
02291                      $this->m_ActiveRecord[$key] = $record[$key];
02292                }        
02293               }
02294               
02295     }
02296 
02302     protected function setClientScripts()
02303     {
02304         // load custom js class
02305         if ($this->m_jsClass != "Openbiz.Form" && $this->m_jsClass != "Openbiz.TableForm" )
02306             BizSystem::clientProxy()->appendScripts($this->m_jsClass, $this->m_jsClass . ".js");
02307         /*
02308         if ($this->m_FormType == 'LIST')
02309         {
02310             BizSystem::clientProxy()->appendScripts("tablekit", "tablekit.js");
02311         }*/
02312     }
02313     
02314     protected function translate()
02315     {
02316        $module = $this->getModuleName($this->m_Name);
02317        if (!empty($this->m_Title))
02318        {
02319               $trans_string = I18n::t($this->m_Title, $this->getTransKey('Title'), $module);
02320               if($trans_string){
02321                      $this->m_Title = $trans_string;
02322               }
02323        }
02324        if (!empty($this->m_Icon))
02325        {
02326               $trans_string = I18n::t($this->m_Icon, $this->getTransKey('Icon'), $module);
02327               if($trans_string){
02328                      $this->m_Icon = $trans_string;
02329               }
02330        }
02331        if (!empty($this->m_Description))
02332        {
02333               $trans_string = I18n::t($this->m_Description, $this->getTransKey('Description'), $module);
02334               if($trans_string){
02335                      $this->m_Description = $trans_string;
02336               }
02337        }
02338     }
02339     
02340     protected function getTransKey($name)
02341     {
02342        $shortFormName = substr($this->m_Name,intval(strrpos($this->m_Name,'.'))+1);
02343        return strtoupper($shortFormName.'_'.$name);
02344     }
02345 }
02346 ?>

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