PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/cubi/openbiz/bin/easy/EasyForm.php

http://openbiz-cubi.googlecode.com/
PHP | 2404 lines | 1658 code | 212 blank | 534 comment | 298 complexity | 26cbc3f42d50feb7ce3e51bd5605dc8e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @package openbiz.bin.easy
  11. * @copyright Copyright (c) 2005-2011, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id: EasyForm.php 4203 2011-06-01 07:33:23Z rockys $
  15. */
  16. //include_once(OPENBIZ_BIN."/easy/Panel.php");
  17. //include_once(OPENBIZ_BIN."/easy/FormRenderer.php");
  18. //include_once(OPENBIZ_BIN."/util/QueryStringParam.php");
  19. /**
  20. * EasyForm class - contains form object metadata functions
  21. *
  22. * @package openbiz.bin.easy
  23. * @author Rocky Swen
  24. * @copyright Copyright (c) 2005-2009
  25. * @access public
  26. */
  27. class EasyForm extends MetaObject implements iSessionObject
  28. {
  29. public $DATAFORMAT = 'RECORD';
  30. // metadata vars are public, necessary for metadata inheritance
  31. public $m_Title;
  32. public $m_Icon;
  33. public $m_Description;
  34. public $m_jsClass;
  35. public $m_DataObjName;
  36. public $m_Height;
  37. public $m_Width;
  38. public $m_DefaultForm;
  39. public $m_CanUpdateRecord;
  40. public $m_DirectMethodList = null; //list of method that can directly from browser
  41. public $m_Panels;
  42. /**
  43. * Name of inherited form (meta-form)
  44. *
  45. * @var string
  46. */
  47. public $m_InheritFrom;
  48. /**
  49. * Data Panel object
  50. *
  51. * @var Panel
  52. */
  53. public $m_DataPanel;
  54. /**
  55. * Action Panel object
  56. * @var Panel
  57. */
  58. public $m_ActionPanel;
  59. /**
  60. * Navigation Panel object
  61. * @var Panel
  62. */
  63. public $m_NavPanel;
  64. /**
  65. * Search Panel object
  66. * @var Panel
  67. */
  68. public $m_SearchPanel;
  69. public $m_TemplateEngine;
  70. public $m_TemplateFile;
  71. public $m_FormType;
  72. public $m_SubForms = null;
  73. public $m_EventName;
  74. public $m_Range = 10;
  75. public $m_CacheLifeTime = 0;
  76. public $m_FormParams;
  77. // parent form is the form that trigger the popup. "this" form is a popup form
  78. public $m_ParentFormName;
  79. // the form that drives navigation - the 1st form deplayed in the view
  80. public $m_DefaultFormName = null;
  81. public $m_Errors; // errors array (error_element, error_message)
  82. public $m_Notices; // list of notice messages
  83. // basic form vars
  84. protected $m_DataObj;
  85. protected $m_RecordId = null;
  86. public $m_ActiveRecord = null;
  87. public $m_FormInputs = null;
  88. public $m_SearchRule = null;
  89. public $m_FixSearchRule = null; // FixSearchRule is the search rule always applying on the search
  90. public $m_SortRule = null;
  91. protected $m_DefaultFixSearchRule = null;
  92. protected $m_Referer = "";
  93. public $m_MessageFile = null;
  94. protected $m_hasError = false;
  95. protected $m_ValidateErrors = array();
  96. protected $queryParams = array();
  97. // vars for grid(list)
  98. protected $m_CurrentPage = 1;
  99. protected $m_StartItem = 1;
  100. public $m_TotalPages = 1;
  101. protected $m_TotalRecords = 0;
  102. protected $m_RecordSet = null;
  103. protected $m_RefreshData = false;
  104. protected $m_Resource = "";
  105. protected $m_Messages;
  106. protected $m_InvokingElement = null;
  107. public $m_AutoRefresh=0;
  108. public $m_ReferenceFormName; //switch from which form
  109. protected $m_RecordAllowAccess = true;
  110. /**
  111. * Initialize BizForm with xml array
  112. *
  113. * @param array $xmlArr
  114. * @return void
  115. */
  116. function __construct(&$xmlArr)
  117. {
  118. $this->readMetadata($xmlArr);
  119. //echo $_GET['referer'];
  120. $this->inheritParentObj();
  121. }
  122. public function allowAccess($access=null)
  123. {
  124. if(!$this->m_RecordAllowAccess)
  125. {
  126. /**
  127. * if the record is now allowed to access, then deny form render
  128. * instead of display an empty form
  129. */
  130. return false;
  131. }
  132. $result = parent::allowAccess($access);
  133. return $result ;
  134. }
  135. /**
  136. * Read array meta data, and store to meta object
  137. *
  138. * @param array $xmlArr
  139. * @return void
  140. */
  141. protected function readMetadata(&$xmlArr)
  142. {
  143. parent::readMetaData($xmlArr);
  144. $this->m_InheritFrom = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["INHERITFROM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["INHERITFROM"] : null;
  145. $this->m_Title = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TITLE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TITLE"] : null;
  146. $this->m_Icon = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["ICON"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["ICON"] : null;
  147. $this->m_Description = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DESCRIPTION"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["DESCRIPTION"] : null;
  148. $this->m_jsClass = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["JSCLASS"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["JSCLASS"] : null;
  149. $this->m_Height = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["HEIGHT"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["HEIGHT"] : null;
  150. $this->m_Width = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["WIDTH"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["WIDTH"] : null;
  151. $this->m_DefaultForm = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DEFAULTFORM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["DEFAULTFORM"] : null;
  152. $this->m_TemplateEngine = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEENGINE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEENGINE"] : null;
  153. $this->m_TemplateFile = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEFILE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["TEMPLATEFILE"] : null;
  154. $this->m_FormType = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["FORMTYPE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["FORMTYPE"] : null;
  155. $this->m_Range = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["PAGESIZE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["PAGESIZE"] : $this->m_Range;
  156. $this->m_FixSearchRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"] : null;
  157. $this->m_SortRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SORTRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SORTRULE"] : null;
  158. $this->m_DefaultFixSearchRule = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["SEARCHRULE"] : null;
  159. $this->m_Name = $this->prefixPackage($this->m_Name);
  160. if ($this->m_InheritFrom == '@sourceMeta') $this->m_InheritFrom = '@'.$this->m_Name;
  161. else $this->m_InheritFrom = $this->prefixPackage($this->m_InheritFrom);
  162. $this->m_DataObjName = $this->prefixPackage($xmlArr["EASYFORM"]["ATTRIBUTES"]["BIZDATAOBJ"]);
  163. if (isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["DIRECTMETHOD"]))
  164. $this->m_DirectMethodList = explode(",", strtolower(str_replace(" ", "",$xmlArr["EASYFORM"]["ATTRIBUTES"]["DIRECTMETHOD"])));
  165. $this->m_DataPanel = new Panel($xmlArr["EASYFORM"]["DATAPANEL"]["ELEMENT"],"",$this);
  166. $this->m_ActionPanel = new Panel($xmlArr["EASYFORM"]["ACTIONPANEL"]["ELEMENT"],"",$this);
  167. $this->m_NavPanel = new Panel($xmlArr["EASYFORM"]["NAVPANEL"]["ELEMENT"],"",$this);
  168. $this->m_SearchPanel = new Panel($xmlArr["EASYFORM"]["SEARCHPANEL"]["ELEMENT"],"",$this);
  169. $this->m_Panels = array($this->m_DataPanel, $this->m_ActionPanel, $this->m_NavPanel, $this->m_SearchPanel);
  170. $this->m_FormType = strtoupper($this->m_FormType);
  171. $this->m_EventName = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["EVENTNAME"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["EVENTNAME"] : null;
  172. $this->m_MessageFile = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["MESSAGEFILE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["MESSAGEFILE"] : null;
  173. $this->m_Messages = Resource::loadMessage($this->m_MessageFile , $this->m_Package);
  174. $this->m_CacheLifeTime = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["CACHELIFETIME"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["CACHELIFETIME"] : "0";
  175. $this->m_CurrentPage = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTPAGE"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTPAGE"] : 1;
  176. $this->m_StartItem = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTITEM"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["STARTITEM"] : 1;
  177. $this->m_AutoRefresh = isset($xmlArr["EASYFORM"]["ATTRIBUTES"]["AUTOREFRESH"]) ? $xmlArr["EASYFORM"]["ATTRIBUTES"]["AUTOREFRESH"] : 0;
  178. // parse access
  179. if ($this->m_Access)
  180. {
  181. $arr = explode (".", $this->m_Access);
  182. $this->m_Resource = $arr[0];
  183. }
  184. if ($this->m_jsClass == "jbForm" && strtoupper($this->m_FormType) == "LIST") $this->m_jsClass = "Openbiz.TableForm";
  185. if ($this->m_jsClass == "jbForm") $this->m_jsClass = "Openbiz.Form";
  186. $this->translate(); // translate for multi-language support
  187. }
  188. /**
  189. * Inherit from parent object. Name, Package, Class cannot be inherited
  190. *
  191. * @return void
  192. */
  193. protected function inheritParentObj()
  194. {
  195. if (!$this->m_InheritFrom) return;
  196. $parentObj = BizSystem::getObject($this->m_InheritFrom);
  197. $this->m_Title = $this->m_Title ? $this->m_Title : $parentObj->m_Title;
  198. $this->m_Icon = $this->m_Icon ? $this->m_Icon : $parentObj->m_Icon;
  199. $this->m_Description = $this->m_Description ? $this->m_Description : $parentObj->m_Description;
  200. $this->m_jsClass = $this->m_jsClass ? $this->m_jsClass : $parentObj->m_jsClass;
  201. $this->m_Height = $this->m_Height ? $this->m_Height : $parentObj->m_Height;
  202. $this->m_Width = $this->m_Width ? $this->m_Width : $parentObj->m_Width;
  203. $this->m_DefaultForm = $this->m_DefaultForm ? $this->m_DefaultForm : $parentObj->m_DefaultForm;
  204. $this->m_TemplateEngine = $this->m_TemplateEngine ? $this->m_TemplateEngine : $parentObj->m_TemplateEngine;
  205. $this->m_TemplateFile = $this->m_TemplateFile ? $this->m_TemplateFile : $parentObj->m_TemplateFile;
  206. $this->m_FormType = $this->m_FormType ? $this->m_FormType : $parentObj->m_FormType;
  207. $this->m_Range = $this->m_Range ? $this->m_Range : $parentObj->m_Range;
  208. $this->m_FixSearchRule = $this->m_FixSearchRule ? $this->m_FixSearchRule : $parentObj->m_FixSearchRule;
  209. $this->m_DefaultFixSearchRule = $this->m_DefaultFixSearchRule ? $this->m_DefaultFixSearchRule : $parentObj->m_DefaultFixSearchRule;
  210. $this->m_DataObjName = $this->m_DataObjName ? $this->m_DataObjName : $parentObj->m_DataObjName;
  211. $this->m_DirectMethodList = $this->m_DirectMethodList ? $this->m_DirectMethodList : $parentObj->m_DirectMethodList;
  212. $this->m_EventName = $this->m_EventName ? $this->m_EventName : $parentObj->m_EventName;
  213. $this->m_MessageFile = $this->m_MessageFile ? $this->m_MessageFile : $parentObj->m_MessageFile;
  214. $this->m_Messages = Resource::loadMessage($this->m_MessageFile , $this->m_Package);
  215. $this->m_CacheLifeTime = $this->m_CacheLifeTime ? $this->m_CacheLifeTime : $parentObj->m_CacheLifeTime;
  216. $this->m_CurrentPage = $this->m_CurrentPage ? $this->m_CurrentPage : $parentObj->m_CurrentPage;
  217. $this->m_StartItem = $this->m_StartItem ? $this->m_StartItem : $parentObj->m_StartItem;
  218. $this->m_DataPanel->merge($parentObj->m_DataPanel);
  219. $this->m_ActionPanel->merge($parentObj->m_ActionPanel);
  220. $this->m_NavPanel->merge($parentObj->m_NavPanel);
  221. $this->m_SearchPanel->merge($parentObj->m_SearchPanel);
  222. if($this->m_DataPanel->current()){
  223. foreach ($this->m_DataPanel as $elem)
  224. $elem->adjustFormName($this->m_Name);
  225. }
  226. if($this->m_ActionPanel->current()){
  227. foreach ($this->m_ActionPanel as $elem)
  228. $elem->adjustFormName($this->m_Name);
  229. }
  230. if($this->m_NavPanel->current()){
  231. foreach ($this->m_NavPanel as $elem)
  232. $elem->adjustFormName($this->m_Name);
  233. }
  234. if($this->m_SearchPanel->current()){
  235. foreach ($this->m_SearchPanel as $elem)
  236. $elem->adjustFormName($this->m_Name);
  237. }
  238. $this->m_Panels = array($this->m_DataPanel, $this->m_ActionPanel, $this->m_NavPanel, $this->m_SearchPanel);
  239. }
  240. /**
  241. * Get message, and translate it
  242. *
  243. * @param string $messageId message Id
  244. * @param array $params
  245. * @return string message string
  246. */
  247. public function getMessage($messageId, $params=array())
  248. {
  249. $message = isset($this->m_Messages[$messageId]) ? $this->m_Messages[$messageId] : constant($messageId);
  250. //$message = I18n::getInstance()->translate($message);
  251. $message = I18n::t($message, $messageId, $this->getModuleName($this->m_Name));
  252. $msg = @vsprintf($message,$params);
  253. if(!$msg){ //maybe in translation missing some %s can cause it returns null
  254. $msg = $message;
  255. }
  256. return $msg;
  257. }
  258. public function canDisplayForm()
  259. {
  260. if($this->getDataObj()->m_DataPermControl=='Y')
  261. {
  262. switch(strtolower($this->m_FormType))
  263. {
  264. default:
  265. case 'list':
  266. return true;
  267. break;
  268. case 'detail':
  269. $permCode=1;
  270. break;
  271. case 'edit':
  272. $permCode=2;
  273. break;
  274. }
  275. $svcObj = BizSystem::GetService(DATAPERM_SERVICE);
  276. $result = $svcObj->checkDataPerm($this->fetchData(),$permCode,$this->getDataObj());
  277. if($result == false)
  278. {
  279. return false;
  280. }
  281. }
  282. return true;
  283. }
  284. public function canDeleteRecord($rec)
  285. {
  286. if($this->getDataObj()->m_DataPermControl=='Y')
  287. {
  288. $svcObj = BizSystem::GetService(DATAPERM_SERVICE);
  289. $result = $svcObj->checkDataPerm($rec,3,$this->getDataObj());
  290. if($result == false)
  291. {
  292. return false;
  293. }
  294. }
  295. return true;
  296. }
  297. /**
  298. * Get/Retrieve Session data of this object
  299. *
  300. * @param SessionContext $sessionContext
  301. * @return void
  302. */
  303. public function getSessionVars($sessionContext)
  304. {
  305. $sessionContext->getObjVar($this->m_Name, "RecordId", $this->m_RecordId);
  306. $sessionContext->getObjVar($this->m_Name, "FixSearchRule", $this->m_FixSearchRule);
  307. $sessionContext->getObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);
  308. $sessionContext->getObjVar($this->m_Name, "QueryParams", $this->queryParams);
  309. $sessionContext->getObjVar($this->m_Name, "SubForms", $this->m_SubForms);
  310. $sessionContext->getObjVar($this->m_Name, "ParentFormName", $this->m_ParentFormName);
  311. $sessionContext->getObjVar($this->m_Name, "DefaultFormName", $this->m_DefaultFormName);
  312. $sessionContext->getObjVar($this->m_Name, "CurrentPage", $this->m_CurrentPage);
  313. $sessionContext->getObjVar($this->m_Name, "PageSize", $this->m_Range);
  314. $sessionContext->getObjVar($this->m_Name, "ReferenceFormName", $this->m_ReferenceFormName);
  315. $sessionContext->getObjVar($this->m_Name, "SearchPanelValues", $this->m_SearchPanelValues);
  316. }
  317. /**
  318. * Save object variable to session context
  319. *
  320. * @param SessionContext $sessionContext
  321. * @return void
  322. */
  323. public function setSessionVars($sessionContext)
  324. {
  325. $sessionContext->setObjVar($this->m_Name, "RecordId", $this->m_RecordId);
  326. $sessionContext->setObjVar($this->m_Name, "FixSearchRule", $this->m_FixSearchRule);
  327. $sessionContext->setObjVar($this->m_Name, "SearchRule", $this->m_SearchRule);
  328. $sessionContext->setObjVar($this->m_Name, "QueryParams", $this->queryParams);
  329. $sessionContext->setObjVar($this->m_Name, "SubForms", $this->m_SubForms);
  330. $sessionContext->setObjVar($this->m_Name, "ParentFormName", $this->m_ParentFormName);
  331. $sessionContext->setObjVar($this->m_Name, "DefaultFormName", $this->m_DefaultFormName);
  332. $sessionContext->setObjVar($this->m_Name, "CurrentPage", $this->m_CurrentPage);
  333. $sessionContext->setObjVar($this->m_Name, "PageSize", $this->m_Range);
  334. $sessionContext->setObjVar($this->m_Name, "ReferenceFormName", $this->m_ReferenceFormName);
  335. $sessionContext->setObjVar($this->m_Name, "SearchPanelValues", $this->m_SearchPanelValues);
  336. }
  337. /**
  338. * Invoke the action passed from browser
  339. *
  340. * @return mixed the function result, or false on error.
  341. */
  342. public function invoke()
  343. {
  344. $argList = func_get_args();
  345. $param1 = array_shift($argList);
  346. // first one is element:eventhandler
  347. list ($elementName, $eventHandlerName) = explode(":", $param1);
  348. $element = $this->getElement($elementName);
  349. $eventHandler = $element->m_EventHandlers->get($eventHandlerName);
  350. $this->m_InvokingElement = array($element, $eventHandler);
  351. // find the matching function
  352. list($funcName, $funcParams) = $eventHandler->parseFunction($eventHandler->m_OrigFunction);
  353. // call the function with rest parameters
  354. return call_user_func_array(array($this, $funcName), $argList);
  355. }
  356. /**
  357. * Validate request from client (browser)
  358. *
  359. * @param string $methodName called from the client
  360. * @return boolean
  361. */
  362. public function validateRequest($methodName)
  363. {
  364. $methodName = strtolower($methodName);
  365. if ($methodName == "selectrecord" || $methodName == "invoke" || $methodName="sortrecord")
  366. return true;
  367. // element, eventhandler
  368. list($element, $eventHandler) = $this->getInvokingElement();
  369. if ($element && $eventHandler)
  370. {
  371. if (stripos($eventHandler->m_OrigFunction, $methodName)===0)
  372. return true;
  373. }
  374. // scan elements to match method
  375. foreach ($this->m_Panels as $panel)
  376. {
  377. foreach ($panel as $elem)
  378. if ($elem->matchRemoteMethod($methodName)) return true;
  379. }
  380. if (is_array($this->m_DirectMethodList))
  381. {
  382. foreach ($this->m_DirectMethodList as $value)
  383. {
  384. if ($methodName == $value) return true;
  385. }
  386. }
  387. return false;
  388. }
  389. /**
  390. * Get object property
  391. * This method get element object if propertyName is "Elements[elementName]" format.
  392. *
  393. * @param string $propertyName
  394. * @return <type>
  395. */
  396. public function getProperty($propertyName)
  397. {
  398. $ret = parent::getProperty($propertyName);
  399. if ($ret !== null) return $ret;
  400. $pos1 = strpos($propertyName, "[");
  401. $pos2 = strpos($propertyName, "]");
  402. if ($pos1>0 && $pos2>$pos1)
  403. {
  404. $propType = substr($propertyName, 0, $pos1);
  405. $elementName = substr($propertyName, $pos1+1,$pos2-$pos1-1);
  406. switch(strtolower($propType))
  407. {
  408. case 'param':
  409. case 'params':
  410. $result = $this->m_FormParams[$elementName];
  411. break;
  412. default:
  413. $result = $this->getElement($elementName);
  414. break;
  415. }
  416. return $result;
  417. }
  418. }
  419. /**
  420. * Get object instance of {@link BizDataObj} defined in it's metadata file
  421. *
  422. * @return BizDataObj
  423. */
  424. public function getDataObj()
  425. {
  426. if (!$this->m_DataObj)
  427. {
  428. if ($this->m_DataObjName)
  429. $this->m_DataObj = BizSystem::objectFactory()->getObject($this->m_DataObjName);
  430. if($this->m_DataObj)
  431. $this->m_DataObj->m_BizFormName = $this->m_Name;
  432. else
  433. {
  434. //BizSystem::clientProxy()->showErrorMessage("Cannot get DataObj of ".$this->m_DataObjName.", please check your metadata file.");
  435. return null;
  436. }
  437. }
  438. return $this->m_DataObj;
  439. }
  440. /**
  441. * Set data object {@link BizDataObj} with specified instant from parameter
  442. *
  443. * @param BizDataObj $dataObj
  444. * @return void
  445. */
  446. final public function setDataObj($dataObj)
  447. {
  448. $this->m_DataObj = $dataObj;
  449. }
  450. /**
  451. * Get output attributs as array
  452. *
  453. * @return array array of attributs
  454. * @todo rename to getOutputAttribute or getAttribute (2.5?)
  455. */
  456. public function outputAttrs()
  457. {
  458. $output['name'] = $this->m_Name;
  459. $output['title'] = Expression::evaluateExpression($this->m_Title, $this);
  460. $output['icon'] = $this->m_Icon;
  461. $output['hasSubform'] = $this->m_SubForms ? 1 : 0;
  462. $output['currentPage'] = $this->m_CurrentPage;
  463. $output['currentRecordId'] = $this->m_RecordId;
  464. $output['totalPages'] = $this->m_TotalPages;
  465. $output['totalRecords'] = $this->m_TotalRecords;
  466. $output['description'] = str_replace('\n', "<br />", Expression::evaluateExpression($this->m_Description,$this));
  467. $output['elementSets'] = $this->getElementSet();
  468. $output['tabSets'] = $this->getTabSet();
  469. $output['ActionElementSets'] = $this->getElementSet($this->m_ActionPanel);
  470. if($output['icon'])
  471. {
  472. if(preg_match("/{.*}/si",$output['icon']))
  473. {
  474. $output['icon'] = Expression::evaluateExpression($output['icon'], null);
  475. }
  476. else
  477. {
  478. $output['icon'] = THEME_URL . "/" . Resource::getCurrentTheme() . "/images/".$output['icon'];
  479. }
  480. }
  481. return $output;
  482. }
  483. /**
  484. * Handle the error from {@link BizDataObj::getErrorMessage} method,
  485. * report the error as an alert window and log.
  486. *
  487. * @param int $errCode
  488. * @return void
  489. */
  490. public function processDataObjError($errCode = 0)
  491. {
  492. $errorMsg = $this->getDataObj()->getErrorMessage();
  493. BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
  494. BizSystem::clientProxy()->showErrorMessage($errorMsg);
  495. }
  496. /**
  497. * Process error of form object
  498. *
  499. * @param array $errors
  500. * @return string - HTML text of this form's read mode
  501. */
  502. public function processFormObjError($errors)
  503. {
  504. $this->m_Errors = $errors;
  505. $this->m_hasError = true;
  506. return $this->rerender();
  507. }
  508. /**
  509. * Handle the exception from DataObj method,
  510. * report the error as an alert window
  511. *
  512. * @param int $errCode
  513. * @return string
  514. */
  515. public function processBDOException($e)
  516. {
  517. $errorMsg = $e->getMessage();
  518. BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
  519. //BizSystem::clientProxy()->showClientAlert($errorMsg); //showErrorMessage($errorMsg);
  520. //BizSystem::clientProxy()->showErrorMessage($errorMsg);
  521. $e->no_exit=true;
  522. OB_ErrorHandler::ExceptionHandler($e);
  523. }
  524. /**
  525. * Set the sub forms of this form. This form is parent of other forms
  526. *
  527. * @param string $subForms - sub controls string with format: ctrl1;ctrl2...
  528. * @return void
  529. */
  530. final public function setSubForms($subForms)
  531. {
  532. // sub controls string with format: ctrl1;ctrl2...
  533. if (!$subForms || strlen($subForms) < 1)
  534. {
  535. $this->m_SubForms = null;
  536. return;
  537. }
  538. $subFormArr = explode(";", $subForms);
  539. unset($this->m_SubForms);
  540. foreach ($subFormArr as $subForm)
  541. {
  542. $this->m_SubForms[] = $this->prefixPackage($subForm);
  543. }
  544. }
  545. /**
  546. * Get view object
  547. *
  548. * @global BizSystem $g_BizSystem
  549. * @return EasyView
  550. */
  551. public function getViewObject()
  552. {
  553. global $g_BizSystem;
  554. $viewName = $g_BizSystem->getCurrentViewName();
  555. if (!$viewName) return null;
  556. $viewObj = BizSystem::getObject($viewName);
  557. return $viewObj;
  558. }
  559. /**
  560. * Get sub form of this form
  561. *
  562. * @return EasyForm
  563. */
  564. public function getSubForms()
  565. {
  566. // ask view to give its subforms if not set yet
  567. return $this->m_SubForms;
  568. }
  569. /**
  570. * Get an element object
  571. *
  572. * @param string $elementName - name of the control
  573. * @return Element
  574. */
  575. public function getElement($elementName)
  576. {
  577. if ($this->m_DataPanel->get($elementName)) return $this->m_DataPanel->get($elementName);
  578. if ($this->m_ActionPanel->get($elementName)) return $this->m_ActionPanel->get($elementName);
  579. if ($this->m_NavPanel->get($elementName)) return $this->m_NavPanel->get($elementName);
  580. if ($this->m_SearchPanel->get($elementName)) return $this->m_SearchPanel->get($elementName);
  581. if($this->m_WizardPanel)
  582. {
  583. if ($this->m_WizardPanel->get($elementName)) return $this->m_WizardPanel->get($elementName);
  584. }
  585. }
  586. public function getElementSet($panel = null)
  587. {
  588. if(!$panel){
  589. $panel = $this->m_DataPanel;
  590. }
  591. $setArr = array();
  592. $panel->rewind();
  593. while($panel->valid())
  594. {
  595. $elem = $panel->current();
  596. $panel->next();
  597. if($elem->m_ElementSet && $elem->canDisplayed()){
  598. //is it in array
  599. if(in_array($elem->m_ElementSet,$setArr)){
  600. continue;
  601. }else{
  602. array_push($setArr,$elem->m_ElementSet);
  603. }
  604. }
  605. }
  606. return $setArr;
  607. }
  608. public function getTabSet($panel = null)
  609. {
  610. if(!$panel){
  611. $panel = $this->m_DataPanel;
  612. }
  613. $setArr = array();
  614. $tabSetArr = array();
  615. $panel->rewind();
  616. while($panel->valid())
  617. {
  618. $elem = $panel->current();
  619. $panel->next();
  620. if($elem->m_TabSet && $elem->canDisplayed()){
  621. //is it in array
  622. if(in_array($elem->m_TabSet,$setArr)){
  623. continue;
  624. }else{
  625. $setArr[$elem->m_TabSetCode]=$elem->m_TabSet;
  626. }
  627. }
  628. }
  629. foreach($setArr as $tabsetCode=>$tabset)
  630. {
  631. $elemSetArr = array();
  632. $panel->rewind();
  633. while($panel->valid())
  634. {
  635. $elem = $panel->current();
  636. $panel->next();
  637. if($elem->m_ElementSet && $elem->canDisplayed()){
  638. //is it in array
  639. if( $elem->m_TabSetCode!= $tabsetCode ||
  640. in_array($elem->m_ElementSet,$elemSetArr)){
  641. continue;
  642. }else{
  643. array_push($elemSetArr,$elem->m_ElementSet);
  644. }
  645. }
  646. }
  647. $tabSetArr[$tabsetCode]['SetName'] = $tabset;
  648. $tabSetArr[$tabsetCode]['Elems'] = $elemSetArr;
  649. }
  650. return $tabSetArr;
  651. }
  652. /**
  653. * Get error elements
  654. *
  655. * @param array $fields
  656. * @return array
  657. */
  658. public function getErrorElements($fields)
  659. {
  660. $errElements = array();
  661. foreach ($fields as $field=>$error)
  662. {
  663. $element = $this->m_DataPanel->getByField($field);
  664. $errElements[$element->m_Name]=$error;
  665. }
  666. return $errElements;
  667. }
  668. /**
  669. * Popup a selection EasyForm in a dynamically generated EasyView
  670. *
  671. * @param string $viewName
  672. * @param string $formName
  673. * @param string $elementName
  674. * @return void
  675. * @access remote
  676. */
  677. public function loadPicker($formName, $elementName="")
  678. {
  679. // set the ParentFormName and ParentCtrlName of the popup form
  680. /* @var $pickerForm EasyForm */
  681. $pickerForm = BizSystem::objectFactory()->getObject($formName);
  682. if ($elementName != "")
  683. {
  684. // set the picker map as well
  685. $element = $this->getElement($elementName);
  686. $pickerMap = $element->m_PickerMap;
  687. }
  688. $currentRecord = $this->readInputRecord();
  689. $pickerForm->setParentForm($this->m_Name);
  690. $pickerForm->setParentFormData($this->m_Name, $elementName, $pickerMap);
  691. $pickerForm->m_ParentFormRecord = $currentRecord;
  692. BizSystem::clientProxy()->redrawForm("DIALOG", $pickerForm->render());
  693. }
  694. public function loadDialog($formName, $id=null,$transId=false)
  695. {
  696. $paramFields = array();
  697. if ($id!=null)
  698. $paramFields["Id"] = $id;
  699. if ($transId!=false)
  700. $paramFields["Id"] = $this->m_RecordId;
  701. $this->_showForm($formName, "Dialog", $paramFields);
  702. }
  703. public function setParentForm($parentFormName)
  704. {
  705. $this->m_ParentFormName = $parentFormName;
  706. }
  707. /**
  708. * Call/Invoke service method, this EasyForm name is passed to the method
  709. *
  710. * @param string $class
  711. * @param string $method
  712. * @param string $param
  713. * @return mixed - return value of the service method
  714. */
  715. public function callService($class, $method, $param = null)
  716. {
  717. $service = BizSystem::getService($class);
  718. if($param){
  719. return $service->$method($param);
  720. }else{
  721. return $service->$method($this->m_Name);
  722. }
  723. }
  724. /**
  725. * Set request parameters
  726. *
  727. * @param array $paramFields
  728. * @return void
  729. */
  730. public function setRequestParams($paramFields)
  731. {
  732. if ($paramFields)
  733. {
  734. $this->m_FixSearchRule=null; // reset fixsearchrule to clean the previous one in session
  735. foreach($paramFields as $fieldName=>$val)
  736. {
  737. $element = $this->m_DataPanel->getByField($fieldName);
  738. if($element->m_AllowURLParam=='Y')
  739. {
  740. if(!$this->getDataObj())return;
  741. if($this->getDataObj()->getField($fieldName)){
  742. if($this->getDataObj()->getField($fieldName)->checkValueType($val))
  743. {
  744. $this->setFixSearchRule("[$fieldName]='$val'");
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. public function setCurrentPage($pageid)
  752. {
  753. $this->m_CurrentPage = $pageid;
  754. }
  755. /**
  756. * Close the popup window
  757. *
  758. * @return void
  759. */
  760. public function close()
  761. {
  762. BizSystem::clientProxy()->closePopup();
  763. }
  764. /**
  765. * Render parent form
  766. *
  767. * @return void
  768. */
  769. public function renderParent()
  770. {
  771. /* @var $parentForm EasyForm */
  772. $parentForm = BizSystem::objectFactory()->getObject($this->m_ParentFormName);
  773. $parentForm->rerender();
  774. }
  775. /**
  776. * Set the dependent search rule of the bizform, this search rule will apply on its BizDataObj.
  777. * The dependent search rule (session var) will always be with bizform until it get set to other value
  778. *
  779. * @param string $rule - search rule has format "[fieldName1] opr1 Value1 AND/OR [fieldName2] opr2 Value2"
  780. * @param boolean $cleanActualRule
  781. * @return void
  782. */
  783. public function setFixSearchRule($rule = null, $cleanActualRule = true)
  784. {
  785. if ($cleanActualRule)
  786. $this->m_FixSearchRule = $this->m_DefaultFixSearchRule;
  787. if ($this->m_FixSearchRule && $rule)
  788. {
  789. if (strpos($this->m_FixSearchRule, $rule) === false)
  790. {
  791. $this->m_FixSearchRule = $this->m_FixSearchRule . " AND " . $rule;
  792. }
  793. }
  794. if (!$this->m_FixSearchRule && $rule){
  795. $this->m_FixSearchRule = $rule;
  796. }
  797. }
  798. /**
  799. * Fetch record set
  800. *
  801. * @return array array of record
  802. */
  803. public function fetchDataSet()
  804. {
  805. $dataObj = $this->getDataObj();
  806. if (!$dataObj) return null;
  807. if ($this->m_RefreshData)
  808. $dataObj->resetRules();
  809. else
  810. $dataObj->clearSearchRule();
  811. if ($this->m_FixSearchRule)
  812. {
  813. if ($this->m_SearchRule)
  814. $searchRule = $this->m_SearchRule . " AND " . $this->m_FixSearchRule;
  815. else
  816. $searchRule = $this->m_FixSearchRule;
  817. }
  818. else
  819. $searchRule = $this->m_SearchRule;
  820. $dataObj->setQueryParameters($this->queryParams);
  821. $dataObj->setSearchRule($searchRule);
  822. if($this->m_StartItem>1)
  823. {
  824. $dataObj->setLimit($this->m_Range, $this->m_StartItem);
  825. }
  826. else
  827. {
  828. $dataObj->setLimit($this->m_Range, ($this->m_CurrentPage-1)*$this->m_Range);
  829. }
  830. if($this->m_SortRule && $this->m_SortRule != $this->getDataObj()->m_SortRule)
  831. {
  832. $dataObj->setSortRule($this->m_SortRule);
  833. }
  834. $resultRecords = $dataObj->fetch();
  835. $this->m_TotalRecords = $dataObj->count();
  836. if ($this->m_Range && $this->m_Range > 0)
  837. $this->m_TotalPages = ceil($this->m_TotalRecords/$this->m_Range);
  838. $selectedIndex = 0;
  839. //if current page is large than total pages ,then reset current page to last page
  840. if($this->m_CurrentPage>$this->m_TotalPages && $this->m_TotalPages>0)
  841. {
  842. $this->m_CurrentPage = $this->m_TotalPages;
  843. $dataObj->setLimit($this->m_Range, ($this->m_CurrentPage-1)*$this->m_Range);
  844. $resultRecords = $dataObj->fetch();
  845. }
  846. $this->getDataObj()->setActiveRecord($resultRecords[$selectedIndex]);
  847. if(!$this->m_RecordId)
  848. {
  849. $this->m_RecordId = $resultRecords[0]["Id"];
  850. }else{
  851. $foundRecordId = false;
  852. foreach($resultRecords as $record)
  853. {
  854. if($this->m_RecordId==$record['Id'])
  855. {
  856. $foundRecordId = true;
  857. }
  858. }
  859. if($foundRecordId == false)
  860. {
  861. $this->m_RecordId=$result[0]['Id'];
  862. }
  863. }
  864. return $resultRecords;
  865. }
  866. /**
  867. * Fetch single record
  868. *
  869. * @return array one record array
  870. */
  871. public function fetchData()
  872. {
  873. // if has valid active record, return it, otherwise do a query
  874. if ($this->m_ActiveRecord != null)
  875. return $this->m_ActiveRecord;
  876. $dataObj = $this->getDataObj();
  877. if ($dataObj == null) return;
  878. if (strtoupper($this->m_FormType) == "NEW")
  879. return $this->getNewRecord();
  880. if (!$this->m_FixSearchRule && !$this->m_SearchRule){
  881. //if its a default sub form,even no search rule, but can still fetch a default record
  882. if(!is_array($this->getDataObj()->m_Association)){
  883. //only if its a default sub form and without any association then return emply array
  884. return array();
  885. }
  886. }else{
  887. if ($this->m_RefreshData) $dataObj->resetRules();
  888. else $dataObj->clearSearchRule();
  889. if ($this->m_FixSearchRule)
  890. {
  891. if ($this->m_SearchRule)
  892. $searchRule = $this->m_SearchRule . " AND " . $this->m_FixSearchRule;
  893. else
  894. $searchRule = $this->m_FixSearchRule;
  895. }
  896. $dataObj->setSearchRule($searchRule);
  897. $dataObj->setLimit(1);
  898. }
  899. $resultRecords = $dataObj->fetch();
  900. if(!count($resultRecords))
  901. {
  902. $this->m_RecordAllowAccess=false;
  903. }
  904. $this->m_RecordId = $resultRecords[0]['Id'];
  905. $this->setActiveRecord($resultRecords[0]);
  906. if($this->getDataObj()){
  907. $this->m_CanUpdateRecord = (int)$this->getDataObj()->canUpdateRecord();
  908. }
  909. return $resultRecords[0];
  910. }
  911. /**
  912. * Goto page specified by $page parameter, and ReRender
  913. * If page not specified, goto page 1
  914. *
  915. * @param number $page
  916. */
  917. public function gotoPage($page=1)
  918. {
  919. $tgtPage = intval($page);
  920. if ($tgtPage == 0) $tgtPage = 1;
  921. $this->m_CurrentPage = $tgtPage;
  922. $this->rerender();
  923. }
  924. public function gotoSelectedPage($elemName)
  925. {
  926. $page = BizSystem::clientProxy()->getFormInputs(str_replace(".","_", $this->m_Name).'_'.$elemName);
  927. $this->gotoPage($page);
  928. }
  929. public function setPageSize($elemName)
  930. {
  931. $pagesize = BizSystem::clientProxy()->getFormInputs(str_replace(".","_", $this->m_Name).'_'.$elemName);
  932. $this->m_Range=$pagesize;
  933. $this->UpdateForm();
  934. }
  935. /**
  936. * Sort Record, for list form
  937. *
  938. * @param string $sortCol column name to sort
  939. * @param string $order 'dec' (decending) or 'asc' (ascending)
  940. * @access remote
  941. * @return void
  942. */
  943. public function sortRecord($sortCol, $order='ASC')
  944. {
  945. $element = $this->getElement($sortCol);
  946. // turn off the OnSort flag of the old onsort field
  947. $element->setSortFlag(null);
  948. // turn on the OnSort flag of the new onsort field
  949. if ($order == "ASC")
  950. $order = "DESC";
  951. else
  952. $order = "ASC";
  953. $element->setSortFlag($order);
  954. // change the sort rule and issue the query
  955. $this->getDataObj()->setSortRule("[" . $element->m_FieldName . "] " . $order);
  956. // move to 1st page
  957. $this->m_CurrentPage = 1;
  958. $this->m_SortRule = "";
  959. $this->rerender();
  960. }
  961. /**
  962. * Run Search
  963. *
  964. * @return void
  965. */
  966. public function runSearch()
  967. {
  968. static $isSearchHelperLoaded = false;
  969. if (!$isSearchHelperLoaded) {
  970. include_once(OPENBIZ_BIN."/easy/SearchHelper.php");
  971. $isSearchHelperLoaded = true;
  972. }
  973. $searchRule = "";
  974. $this->queryParams = array();
  975. foreach ($this->m_SearchPanel as $element)
  976. {
  977. $searchStr = '';
  978. if(method_exists($element,"getSearchRule")){
  979. $searchStr = $element->getSearchRule();
  980. }else{
  981. if (!$element->m_FieldName)
  982. continue;
  983. $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
  984. if($element->m_FuzzySearch=="Y")
  985. {
  986. $value="*$value*";
  987. }
  988. if ($value!='')
  989. {
  990. //$searchStr = inputValToRule($element->m_FieldName, $value, $this);
  991. $this->queryParams[$element->m_FieldName] = $value;
  992. }
  993. }
  994. //BizSystem::clientProxy()->showClientAlert($searchStr);
  995. if($searchStr){
  996. if ($searchRule == "")
  997. $searchRule .= $searchStr;
  998. else
  999. $searchRule .= " AND " . $searchStr;
  1000. }
  1001. }
  1002. $this->m_SearchRule = $searchRule;
  1003. $this->m_RefreshData = true;
  1004. $this->m_CurrentPage = 1;
  1005. BizSystem::log(LOG_DEBUG,"FORMOBJ",$this->m_Name."::runSearch(), SearchRule=".$this->m_SearchRule);
  1006. $recArr = $this->readInputRecord();
  1007. $this->m_SearchPanelValues = $recArr;
  1008. $this->runEventLog();
  1009. $this->rerender();
  1010. }
  1011. /**
  1012. * Reset search
  1013. *
  1014. * @return void
  1015. */
  1016. public function resetSearch()
  1017. {
  1018. $this->m_SearchRule = "";
  1019. $this->m_RefreshData = true;
  1020. $this->m_CurrentPage = 1;
  1021. $this->runEventLog();
  1022. $this->rerender();
  1023. }
  1024. public function setSearchRule($searchRule, $queryParams=null)
  1025. {
  1026. $this->m_SearchRule = $searchRule;
  1027. $this->queryParams = $queryParams;
  1028. $this->m_RefreshData = true;
  1029. $this->m_CurrentPage = 1;
  1030. }
  1031. /**
  1032. * New record, be default, just redirect to the new record page
  1033. *
  1034. * @return void
  1035. */
  1036. public function newRecord()
  1037. {
  1038. $this->processPostAction();
  1039. }
  1040. /**
  1041. * Copy record to new record *
  1042. *
  1043. * @param mixed $id id of record that want to copy,
  1044. * it parameter not passed, id is '_selectedId'
  1045. * @return void
  1046. */
  1047. public function copyRecord($id=null)
  1048. {
  1049. if ($id==null || $id=='')
  1050. $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
  1051. if (!$id)
  1052. {
  1053. BizSystem::clientProxy()->showClientAlert($this->getMessage("PLEASE_EDIT_A_RECORD"));
  1054. return;
  1055. }
  1056. $this->getActiveRecord($id);
  1057. $this->processPostAction();
  1058. }
  1059. /**
  1060. * Edit Record
  1061. * NOTE: append fld:Id=$id to the redirect page url
  1062. *
  1063. * @param mixed $id
  1064. * @return void
  1065. */
  1066. public function editRecord($id=null)
  1067. {
  1068. if ($id==null || $id=='')
  1069. $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
  1070. if (!isset($id))
  1071. {
  1072. BizSystem::clientProxy()->showClientAlert($this->getMessage("PLEASE_EDIT_A_RECORD"));
  1073. return;
  1074. }
  1075. // update the active record with new update record
  1076. $this->getActiveRecord($id);
  1077. $this->processPostAction();
  1078. }
  1079. /**
  1080. * Show form
  1081. *
  1082. * @param string $formName
  1083. * @param string $target target type: Popup or other
  1084. * @param array $paramFields
  1085. * @return void
  1086. */
  1087. protected function _showForm($formName, $target, $paramFields)
  1088. {
  1089. $formName_org = $formName;
  1090. if (!$this->m_DefaultFormName)
  1091. $this->m_DefaultFormName = $this->m_Name;
  1092. if ($formName == null)
  1093. {
  1094. if($this->m_ReferenceFormName == null)
  1095. {
  1096. $formName = $this->m_DefaultFormName;
  1097. }else{
  1098. if($formName = $this->m_ReferenceFormName){
  1099. //this judgement is for anti endless loop between swtich forms
  1100. $formObj = BizSystem::objectFactory()->getObject($this->m_ReferenceFormName);
  1101. if($formObj->m_ReferenceFormName == $this->m_Name){
  1102. $formName = $this->m_DefaultFormName;
  1103. }else{
  1104. $formName = $this->m_ReferenceFormName;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. //if($this->getViewObject()->isInFormRefLibs($formName))
  1110. {
  1111. // get the form object
  1112. /* @var $formObj EasyForm */
  1113. $formObj = BizSystem::objectFactory()->getObject($formName);
  1114. $formObj->m_DefaultFormName = $this->m_DefaultFormName;
  1115. if($formName_org){
  1116. //RefenerenceForm records where the from switch from
  1117. if( $this->m_FormType!='EDIT' &&
  1118. $this->m_FormType!='NEW' &&
  1119. $this->m_FormType!='COPY' ){
  1120. $formObj->m_ReferenceFormName = $this->m_Name;
  1121. }
  1122. }
  1123. //if has more than Id field as params then $clearFixSearchRule is false, means join all where rules
  1124. $paramTemp = $paramFields;
  1125. unset($paramTemp['Id']);
  1126. if(count($paramTemp)){
  1127. $clearFixSearchRule = false;
  1128. }else{
  1129. $clearFixSearchRule = true;
  1130. }
  1131. foreach($paramFields as $fieldName=>$val){
  1132. $formObj->m_FormParams[$fieldName] = $val;
  1133. $formObj->setFixSearchRule("[$fieldName]='$val'",$clearFixSearchRule);
  1134. if($fieldName=="Id"){
  1135. $formObj->setRecordId($val);
  1136. }
  1137. }
  1138. if(!$formObj->canDisplayForm())
  1139. {
  1140. $formObj->m_ErrorMessage = $this->getMessage("FORM_OPERATION_NOT_PERMITTED",$formObj->m_Name);
  1141. if (strtoupper($this->m_FormType) == "LIST"){
  1142. BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
  1143. BizSystem::clientProxy()->showClientAlert($formObj->m_ErrorMessage);
  1144. }else{
  1145. $this->processFormObjError(array($formObj->m_ErrorMessage));
  1146. }
  1147. return false;
  1148. }
  1149. switch ($target)
  1150. {
  1151. case "Popup":
  1152. $formObj->setParentForm($this->m_Name);
  1153. echo $formObj->render();
  1154. break;
  1155. case "Dialog":
  1156. $formObj->setParentForm($this->m_Name);
  1157. BizSystem::clientProxy()->redrawForm("DIALOG", $formObj->render());
  1158. break;
  1159. default:
  1160. BizSystem::clientProxy()->redrawForm($this->m_Name, $formObj->render());
  1161. }
  1162. }
  1163. }
  1164. /**
  1165. * Delete Record
  1166. * NOTE: use redirectpage attr of eventhandler to redirect or redirect to previous page by default
  1167. *
  1168. * @param string $id
  1169. * @return void
  1170. */
  1171. public function deleteRecord($id=null)
  1172. {
  1173. if ($id==null || $id=='')
  1174. $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
  1175. $selIds = BizSystem::clientProxy()->getFormInputs('row_selections', false);
  1176. if ($selIds == null)
  1177. $selIds[] = $id;
  1178. foreach ($selIds as $id)
  1179. {
  1180. $dataRec = $this->getDataObj()->fetchById($id);
  1181. $this->getDataObj()->setActiveRecord($dataRec);
  1182. if(!$this->canDeleteRecord($dataRec))
  1183. {
  1184. $this->m_ErrorMessage = $this->getMessage("FORM_OPERATION_NOT_PERMITTED",$this->m_Name);
  1185. if (strtoupper($this->m_FormType) == "LIST"){
  1186. BizSystem::log(LOG_ERR, "DATAOBJ", "DataObj error = ".$errorMsg);
  1187. BizSystem::clientProxy()->showClientAlert($this->m_ErrorMessage);
  1188. }else{
  1189. $this->processFormObjError(array($this->m_ErrorMessage));
  1190. }
  1191. return;
  1192. }
  1193. // take care of exception
  1194. try
  1195. {
  1196. $dataRec->delete();
  1197. } catch (BDOException $e)
  1198. {
  1199. // call $this->processBDOException($e);
  1200. $this->processBDOException($e);
  1201. return;
  1202. }
  1203. }
  1204. if (strtoupper($this->m_FormType) == "LIST")
  1205. $this->rerender();
  1206. $this->runEventLog();
  1207. $this->processPostAction();
  1208. }
  1209. /**
  1210. * Remove the record out of the associate relationship
  1211. *
  1212. * @return void
  1213. */
  1214. public function removeRecord ($id=null)
  1215. {
  1216. if ($id==null || $id=='')
  1217. $id = BizSystem::clientProxy()->getFormInputs('_selectedId');
  1218. $selIds = BizSystem::clientProxy()->getFormInputs('row_selections', false);
  1219. if ($selIds == null)
  1220. $selIds[] = $id;
  1221. foreach ($selIds as $id)
  1222. {
  1223. $rec = $this->getDataObj()->fetchById($id);
  1224. $ok = $this->getDataObj()->removeRecord($rec, $bPrtObjUpdated);
  1225. if (! $ok)
  1226. return $this->processDataObjError($ok);
  1227. }
  1228. $this->runEventLog();
  1229. $this->rerender();
  1230. if($this->m_ParentFormName)
  1231. {
  1232. $this->renderParent();
  1233. }
  1234. }
  1235. /**
  1236. * Select Record
  1237. *
  1238. * @param string $recId
  1239. * @access remote
  1240. * @return void
  1241. */
  1242. public function selectRecord($recId)
  1243. {
  1244. if ($recId==null || $recId=='')
  1245. $recId = BizSystem::clientProxy()->getFormInputs('_selectedId');
  1246. $this->m_RecordId = $recId;
  1247. if($this->getDataObj()){
  1248. $this->getDataObj()->setActiveRecordId($this->m_RecordId);
  1249. }
  1250. $this->rerender(false); // not redraw the this form, but draw the subforms
  1251. //$this->rerender();
  1252. }
  1253. /**
  1254. * Get element Id
  1255. *
  1256. * @return mixed
  1257. */
  1258. public function getElementID()
  1259. {
  1260. $id = $this->m_DataPanel->getByField('Id')->getValue();
  1261. if($id)
  1262. {
  1263. return (int)$id;
  1264. }
  1265. else
  1266. {
  1267. return (int)$this->m_RecordId;
  1268. }
  1269. }
  1270. /**
  1271. * Save input and redirect page to a new view
  1272. * use redirectpage attr of eventhandler to redirect or redirect to previous page by default
  1273. * NOTE: For Edit/New form type
  1274. *
  1275. * @return void
  1276. */
  1277. public function saveRecord()
  1278. {
  1279. if (strtoupper($this->m_FormType) == "NEW")
  1280. {
  1281. $this->insertRecord();
  1282. }
  1283. else
  1284. {
  1285. $this->updateRecord();
  1286. }
  1287. }
  1288. /**
  1289. * Update record
  1290. *
  1291. * @return mixed
  1292. */
  1293. public function updateRecord()
  1294. {
  1295. $currentRec = $this->fetchData();
  1296. $recArr = $this->readInputRecord();
  1297. $this->setActiveRecord($recArr);
  1298. if (count($recArr) != 0){
  1299. try
  1300. {
  1301. $this->ValidateForm();
  1302. }
  1303. catch (ValidationException $e)
  1304. {
  1305. $this->processFormObjError($e->m_Errors);
  1306. return;
  1307. }
  1308. if ($this->_doUpdate($recArr, $currentRec) == false)
  1309. return;
  1310. $this->commitFormElements(); // commit change in FormElement
  1311. }
  1312. // in case of popup form, close it, then rerender the parent form
  1313. if ($this->m_ParentFormName)
  1314. {
  1315. $this->close();
  1316. $this->renderParent();
  1317. }
  1318. $this->processPostAction();
  1319. }
  1320. public function updateFieldValueAdd($id,$fld_name,$value,$min,$max)
  1321. {
  1322. if($value>=$max){
  1323. $value = $min;
  1324. }else{
  1325. $value++;
  1326. }
  1327. return $this->updateFieldValue($id,$fld_name,$value);
  1328. }
  1329. public function updateFieldValueXor($id,$fld_name,$value)
  1330. {
  1331. if($value>0){
  1332. $value_xor = 0;
  1333. }else{
  1334. $value_xor = 1;
  1335. }
  1336. return $this->updateFieldValue($id,$fld_name,$value_xor);
  1337. }
  1338. /**
  1339. * Update record
  1340. *
  1341. * @return mixed
  1342. */
  1343. public function updateFieldValue($Id,$fld_name,$value)
  1344. {
  1345. $element = $this->m_DataPanel->get($fld_name);
  1346. $fieldname = $element->m_FieldName;
  1347. $currentRec = $this->getActiveRecord($Id);
  1348. $recArr = $this->getActiveRecord($Id);
  1349. $recArr[$fieldname]=$value;
  1350. if ($this->_doUpdate($recArr, $currentRec) == false)
  1351. return;
  1352. $this->UpdateForm();
  1353. }
  1354. /**
  1355. * Do update record
  1356. *
  1357. * @param array $inputRecord
  1358. * @param array $currentRecord
  1359. * @return void
  1360. */
  1361. protected function _doUpdate($inputRecord, $currentRecord)
  1362. {
  1363. $dataRec = new DataRecord($currentRecord, $this->getDataObj());
  1364. foreach ($inputRecord as $k => $v){
  1365. $dataRec[$k] = $v; // or $dataRec->$k = $v;
  1366. }
  1367. try
  1368. {
  1369. $dataRec->save();
  1370. }
  1371. catch (ValidationException $e)
  1372. {
  1373. $errElements = $this->getErrorElements($e->m_Errors);
  1374. if(count($e->m_Errors)==count($errElements)){
  1375. $this->processFormObjError($errElements);
  1376. }else{
  1377. $errmsg = implode("<br />",$e->m_Errors);
  1378. BizSystem::clientProxy()->showErrorMessage($errmsg);
  1379. }
  1380. return false;
  1381. }
  1382. catch (BDOException $e)
  1383. {
  1384. $this->processBDOException($e);
  1385. return false;
  1386. }
  1387. $this->m_ActiveRecord = null;
  1388. $this->getActiveRecord($dataRec["Id"]);
  1389. $this->runEventLog();
  1390. return true;
  1391. }
  1392. protected function commitFormElements()
  1393. {
  1394. foreach ($this->m_DataPanel as $element)
  1395. {
  1396. if (is_a($element, "FormElement")) {
  1397. $element->setValue('');
  1398. }
  1399. }
  1400. }
  1401. /**
  1402. * Insert new record
  1403. *
  1404. * @return mixed
  1405. */
  1406. public function insertRecord()
  1407. {
  1408. $recArr = $this->readInputRecord();
  1409. $this->setActiveRecord($recArr);
  1410. if (count($recArr) == 0)
  1411. return;
  1412. try
  1413. {
  1414. $this->ValidateForm();
  1415. }
  1416. catch (ValidationException $e)
  1417. {
  1418. $this->processFormObjError($e->m_Errors);
  1419. return;
  1420. }
  1421. $this->_doInsert($recArr);
  1422. $this->commitFormElements(); // commit change in FormElement
  1423. // in case of popup form, close it, then rerender the parent form
  1424. if ($this->m_ParentFormName)
  1425. {
  1426. $this->close();
  1427. $this->renderParent();
  1428. }
  1429. $this->processPostAction();
  1430. }
  1431. /**
  1432. * Do insert record
  1433. *
  1434. * @param array $inputRecord
  1435. * @return void
  1436. */
  1437. protected function _doInsert($inputRecord)
  1438. {
  1439. $dataRec = new DataRecord(null, $this->getDataObj());
  1440. // $inputRecord['Id'] = null; // comment it out for name PK case
  1441. foreach ($inputRecord as $k => $v)
  1442. $dataRec[$k] = $v; // or $dataRec->$k = $v;
  1443. try
  1444. {
  1445. $dataRec->save();
  1446. }
  1447. catch (ValidationException $e)
  1448. {
  1449. $errElements = $this->getErrorElements($e->m_Errors);
  1450. if(count($e->m_Errors)==count($errElements)){
  1451. $this->processFormObjError($errElements);
  1452. }else{
  1453. $errmsg = implode("<br />",$e->m_Errors);
  1454. BizSystem::clientProxy()->showErrorMessage($errmsg);
  1455. }
  1456. return;
  1457. }
  1458. catch (BDOException $e)
  1459. {
  1460. $this->processBDOException($e);
  1461. return;
  1462. }
  1463. $this->m_ActiveRecord = null;
  1464. $this->getActiveRecord($dataRec["Id"]);
  1465. $this->runEventLog();
  1466. return $dataRec["Id"];
  1467. }
  1468. /**
  1469. * Cancel input and do page redirection
  1470. *
  1471. * @return void
  1472. */
  1473. public function cancel()
  1474. {
  1475. $this->processPostAction();
  1476. }
  1477. /**
  1478. * Update form controls
  1479. *
  1480. * @return void
  1481. * @access remote
  1482. */
  1483. public function updateForm()
  1484. {
  1485. // read the input to form controls
  1486. //@todo: read inputs but should be skipp uploaders elements
  1487. $recArr = $this->readInputRecord();
  1488. $this->setActiveRecord($recArr);
  1489. $this->rerender();
  1490. }
  1491. /**
  1492. * Generate list for AutoSuggest element
  1493. * Also supports elements that have hidden values
  1494. *
  1495. * @param string $input - the search string used to filter the list
  1496. * @todo rename to createAutoSuggestList or createAutoSuggest(v2.5?)
  1497. * @return void
  1498. */
  1499. public function autoSuggest($input)
  1500. {
  1501. if (defined('JSLIB_BASE') && JSLIB_BASE == 'JQUERY') {
  1502. $value = $_GET["term"];
  1503. // get the select from list of the element
  1504. $element = $this->getElement($input);
  1505. $element->setValue($value);
  1506. $fromlist = array();
  1507. $element->getFromList($fromlist);
  1508. $arr = array();
  1509. $i = 0;
  1510. foreach ($fromlist as $item) {
  1511. $arr[$i++] = array('label'=>$item['txt'], 'value'=>$item['val']);
  1512. }
  1513. echo json_encode($arr);
  1514. return;
  1515. }
  1516. $foo = $_POST;
  1517. $hidden_flag = FALSE;
  1518. if (strpos($input, '_hidden'))
  1519. {
  1520. $realInput = str_replace('_hidden', '', $input);
  1521. $hidden_flag = TRUE;
  1522. } else
  1523. {
  1524. $realInput = $input;
  1525. }
  1526. $value = BizSystem::clientProxy()->getFormInputs($input);
  1527. // get the select from list of the element
  1528. $element = $this->getElement($realInput);
  1529. $element->setValue($value);
  1530. $fromlist = array();
  1531. $element->getFromList($fromlist);
  1532. echo "<ul>";
  1533. if ($fromlist)
  1534. {
  1535. if ($hidden_flag = TRUE)
  1536. {
  1537. $count=0;
  1538. foreach ($fromlist as $item)
  1539. {
  1540. echo "<li id=" . $item['txt'] . ">" . $item['val'] . "</li>";
  1541. $count++;
  1542. if($count>=5) break;
  1543. }
  1544. }
  1545. else
  1546. {
  1547. $count=0;
  1548. foreach ($fromlist as $item)
  1549. {
  1550. echo "<li>" . $item['txt'] . "</li>";
  1551. $count++;
  1552. if($count>=5) break;
  1553. }
  1554. }
  1555. }
  1556. echo "</ul>";
  1557. }
  1558. /**
  1559. * Validate input on EasyForm level
  1560. * default form validation do nothing.
  1561. * developers need to override this method to implement their logic
  1562. *
  1563. * @return boolean
  1564. */
  1565. protected function validateForm($cleanError = true)
  1566. {
  1567. if($cleanError == true)
  1568. {
  1569. $this->m_ValidateErrors = array();
  1570. }
  1571. $this->m_DataPanel->rewind();
  1572. while($this->m_DataPanel->valid())
  1573. {
  1574. /* @var $element Element */
  1575. $element = $this->m_DataPanel->current();
  1576. if($element->m_Label)
  1577. {
  1578. $elementName = $element->m_Label;
  1579. }
  1580. else
  1581. {
  1582. $elementName = $element->m_Text;
  1583. }
  1584. if ($element->checkRequired() === true &&
  1585. ($element->m_Value==null || $element->m_Value == ""))
  1586. {
  1587. $errorMessage = $this->getMessage("FORM_ELEMENT_REQUIRED",array($elementName));
  1588. $this->m_ValidateErrors[$element->m_Name] = $errorMessage;
  1589. //return false;
  1590. }
  1591. elseif ($element->m_Value!==null && $element->Validate() == false)
  1592. {
  1593. $validateService = BizSystem::getService(VALIDATE_SERVICE);
  1594. $errorMessage = $this->getMessage("FORM_ELEMENT_INVALID_INPUT",array($elementName,$value,$element->m_Validator));
  1595. if ($errorMessage == false)
  1596. { //Couldn't get a clear error message so let's try this
  1597. $errorMessage = $validateService->getErrorMessage($element->m_Validator, $elementName);
  1598. }
  1599. $this->m_ValidateErrors[$element->m_Name] = $errorMessage;
  1600. //return false;
  1601. }
  1602. $this->m_DataPanel->next() ;
  1603. }
  1604. if (count($this->m_ValidateErrors) > 0)
  1605. {
  1606. throw new ValidationException($this->m_ValidateErrors);
  1607. return false;
  1608. }
  1609. return true;
  1610. }
  1611. /**
  1612. * Read user input data from UI
  1613. *
  1614. * @return array - record array
  1615. */
  1616. protected function readInputRecord()
  1617. {
  1618. $recArr = array();
  1619. foreach ($this->m_DataPanel as $element)
  1620. {
  1621. $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
  1622. if ($value ===null && (
  1623. !is_a($element,"FileUploader")
  1624. && !is_subclass_of($element,"FileUploader")
  1625. && !is_a($element,"Checkbox")
  1626. && !is_a($element,"FormElement")
  1627. )){
  1628. continue;
  1629. }
  1630. $element->setValue($value);
  1631. $this->m_FormInputs[$element->m_Name] = $value;
  1632. $value = $element->getValue();
  1633. if ( $element->m_FieldName)
  1634. $recArr[$element->m_FieldName] = $value;
  1635. }
  1636. foreach ($this->m_SearchPanel as $element)
  1637. {
  1638. $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
  1639. $element->setValue($value);
  1640. $this->m_FormInputs[$element->m_Name] = $value;
  1641. $value = $element->getValue();
  1642. if ($value !== null && $element->m_FieldName)
  1643. $recArr[$element->m_FieldName] = $value;
  1644. }
  1645. return $recArr;
  1646. }
  1647. /**
  1648. * Read inputs
  1649. *
  1650. * @return array array of input
  1651. */
  1652. protected function readInputs()
  1653. {
  1654. $inputArr = array();
  1655. foreach ($this->m_DataPanel as $element)
  1656. {
  1657. $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
  1658. $element->setValue($value);
  1659. $inputArr[$element->m_Name] = $value;
  1660. }
  1661. foreach ($this->m_SearchPanel as $element)
  1662. {
  1663. $value = BizSystem::clientProxy()->getFormInputs($element->m_Name);
  1664. $element->setValue($value);
  1665. $inputArr[$element->m_Name] = $value;
  1666. }
  1667. return $inputArr;
  1668. }
  1669. public function setFormInputs($inputArr=null)
  1670. {
  1671. if(!$inputArr){
  1672. $inputArr = $this->m_FormInputs;
  1673. }
  1674. if(!is_array($inputArr)){
  1675. $inputArr = array();
  1676. }
  1677. foreach ($this->m_DataPanel as $element)
  1678. {
  1679. if (isset($inputArr[$element->m_Name]))
  1680. {
  1681. $element->setValue($inputArr[$element->m_Name]);
  1682. }
  1683. }
  1684. foreach ($this->m_SearchPanel as $element)
  1685. {
  1686. if (isset($inputArr[$element->m_Name]))
  1687. {
  1688. $element->setValue($inputArr[$element->m_Name]);
  1689. }
  1690. }
  1691. return $inputArr;
  1692. }
  1693. /**
  1694. * Get new record
  1695. *
  1696. * @return array
  1697. */
  1698. protected function getNewRecord()
  1699. {
  1700. if($this->getDataObj())
  1701. {
  1702. $recArr = $this->getDataObj()->newRecord();
  1703. }
  1704. if (! $recArr)
  1705. return null;
  1706. // load default values if new record value is empty
  1707. $defaultRecArr = array();
  1708. foreach ($this->m_DataPanel as $element)
  1709. {
  1710. if ($element->m_FieldName)
  1711. {
  1712. $defaultRecArr[$element->m_FieldName] = $element->getDefaultValue();
  1713. }
  1714. }
  1715. foreach ($recArr as $field => $val)
  1716. {
  1717. if ($val == "" && $defaultRecArr[$field] != "")
  1718. {
  1719. $recArr[$field] = $defaultRecArr[$field];
  1720. }
  1721. }
  1722. return $recArr;
  1723. }
  1724. /**
  1725. * Render this form (return html content),
  1726. * called by EasyView's render method (called when form is loaded).
  1727. * Query is issued before returning the html content.
  1728. *
  1729. * @return string - HTML text of this form's read mode
  1730. * @example ../../../example/FormObject.php
  1731. */
  1732. public function render()
  1733. {
  1734. if (!$this->allowAccess())
  1735. return "";
  1736. $this->setClientScripts();
  1737. if($this->m_CacheLifeTime>0 && $this->m_SubForms == null)
  1738. {
  1739. $cache_id = md5($this->m_Name);
  1740. //try to process cache service.
  1741. $cacheSvc = BizSystem::getService(CACHE_SERVICE,1);
  1742. $cacheSvc->init($this->m_Name,$this->m_CacheLifeTime);
  1743. if($cacheSvc->test($cache_id))
  1744. {
  1745. BizSystem::log(LOG_DEBUG, "FORM", "Cache Hit. form name = ".$this->m_Name);
  1746. $output = $cacheSvc->load($cache_id);
  1747. }
  1748. else
  1749. {
  1750. BizSystem::log(LOG_DEBUG, "FORM", "Set cache. form name = ".$this->m_Name);
  1751. $output = $this->renderHTML();
  1752. $cacheSvc->save($output, $cache_id);
  1753. }
  1754. return $output;
  1755. }
  1756. //Moved the renderHTML function infront of declaring subforms
  1757. $renderedHTML = $this->renderHTML();
  1758. // prepare the subforms' dataobjs, since the subform relates to parent form by dataobj association
  1759. if ($this->m_SubForms && $this->getDataObj())
  1760. {
  1761. foreach ($this->m_SubForms as $subForm)
  1762. {
  1763. $formObj = BizSystem::objectFactory()->getObject($subForm);
  1764. $dataObj = $this->getDataObj()->getRefObject($formObj->m_DataObjName);
  1765. if ($dataObj)
  1766. $formObj->setDataObj($dataObj);
  1767. }
  1768. }
  1769. if (!$this->allowAccess())
  1770. {
  1771. return "";
  1772. }
  1773. return $renderedHTML;
  1774. }
  1775. /**
  1776. * Render context menu code
  1777. *
  1778. * @return string html code for context menu
  1779. */
  1780. protected function renderContextMenu ()
  1781. {
  1782. $menuList = array();
  1783. foreach ($this->m_Panels as $panel)
  1784. {
  1785. $panel->rewind();
  1786. while ($element = $panel->current())
  1787. {
  1788. $panel->next();
  1789. if (method_exists($element,'getContextMenu') && $menus = $element->getContextMenu())
  1790. {
  1791. foreach ($menus as $m)
  1792. $menuList[] = $m;
  1793. }
  1794. }
  1795. }
  1796. if (count($menuList) == 0)
  1797. return "";
  1798. $str = "<div class='contextMenu' id='" . $this->m_Name . "_contextmenu'>\n";
  1799. $str .= "<div class=\"contextMenu_header\" ></div>\n";
  1800. $str .= "<ul>\n";
  1801. foreach ($menuList as $m)
  1802. {
  1803. $func = $m['func'];
  1804. $shortcutKey = isset($m['key']) ? " (".$m['key'].")" : "";
  1805. $str .= "<li><a href=\"javascript:void(0)\" onclick=\"$func\">".$m['text'].$shortcutKey."</a></li>\n";
  1806. }
  1807. $str .= "</ul>\n";
  1808. $str .= "<div class=\"contextMenu_footer\" ></div>\n";
  1809. $str .= "</div>\n";
  1810. if (defined('JSLIB_BASE') && JSLIB_BASE == 'JQUERY') {
  1811. $str .= "
  1812. <script>
  1813. $(jq('".$this->m_Name."')).removeAttr('onContextMenu');
  1814. $(jq('".$this->m_Name."'))[0].oncontextmenu=function(event){return Openbiz.Menu.show(event, '".$this->m_Name."_contextmenu');};
  1815. $(jq('".$this->m_Name."')).bind('click',Openbiz.Menu.hide);
  1816. </script>";
  1817. }
  1818. else {
  1819. $str .= "
  1820. <script>
  1821. $('".$this->m_Name."').removeAttribute('onContextMenu');
  1822. $('".$this->m_Name."').oncontextmenu=function(event){return Openbiz.Menu.show(event, '".$this->m_Name."_contextmenu');};
  1823. $('".$this->m_Name."').observe('click',Openbiz.Menu.hide);
  1824. </script>";
  1825. }
  1826. return $str;
  1827. }
  1828. /**
  1829. * Rerender this form (form is rendered already) .
  1830. *
  1831. * @param boolean $redrawForm - whether render this form again or not, optional default true
  1832. * @param boolean $hasRecordChange - if record change, need to render subforms, optional default true
  1833. * @return string - HTML text of this form's read mode
  1834. */
  1835. public function rerender($redrawForm=true, $hasRecordChange=true)
  1836. {
  1837. if ($redrawForm)
  1838. {
  1839. BizSystem::clientProxy()->redrawForm($this->m_Name, $this->renderHTML());
  1840. }
  1841. if ($hasRecordChange)
  1842. {
  1843. $this->rerenderSubForms();
  1844. }
  1845. }
  1846. /**
  1847. * Rerender sub forms who has dependecy on this form.
  1848. * This method is called when parent form's change affect the sub forms
  1849. *
  1850. * @return void
  1851. */
  1852. protected function rerenderSubForms()
  1853. {
  1854. if (! $this->m_SubForms)
  1855. return;
  1856. foreach ($this->m_SubForms as $subForm)
  1857. {
  1858. $formObj = BizSystem::objectFactory()->getObject($subForm);
  1859. if($this->getDataObj() && $formObj->m_DataObjName)
  1860. {
  1861. $dataObj = $this->getDataObj()->getRefObject($formObj->m_DataObjName);
  1862. if ($dataObj)
  1863. $formObj->setDataObj($dataObj);
  1864. }
  1865. $formObj->rerender();
  1866. }
  1867. return;
  1868. }
  1869. /**
  1870. * Render html content of this form
  1871. *
  1872. * @return string - HTML text of this form's read mode
  1873. */
  1874. protected function renderHTML()
  1875. {
  1876. $formHTML = FormRenderer::render($this);
  1877. $otherHTML = $this->rendercontextmenu();
  1878. if(preg_match('/iPad/si',$_SERVER['HTTP_USER_AGENT']) ||
  1879. preg_match('/iPhone/si',$_SERVER['HTTP_USER_AGENT'])){
  1880. $otherHTML.="
  1881. <script>
  1882. var a=document.getElementsByTagName('a');
  1883. for(var i=0;i<a.length;i++)
  1884. {
  1885. if(a[i].getAttribute('href').indexOf('javascript:')==-1
  1886. && a[i].getAttribute('href').indexOf('#')==-1)
  1887. {
  1888. a[i].onclick=function()
  1889. {
  1890. try{
  1891. show_loader();
  1892. }catch(e){
  1893. }
  1894. window.location=this.getAttribute('href');
  1895. return false
  1896. }
  1897. }else{
  1898. }
  1899. }
  1900. </script>
  1901. ";
  1902. }
  1903. if(!$this->m_ParentFormName)
  1904. {
  1905. if (($viewObj = $this->getViewObject())!=null)
  1906. $viewObj->m_LastRenderedForm = $this->m_Name;
  1907. }
  1908. return $formHTML ."\n". $otherHTML;
  1909. }
  1910. /**
  1911. * Get event log message
  1912. *
  1913. * @return mixed string or null
  1914. */
  1915. protected function getEventLogMsg()
  1916. {
  1917. list($element, $eventHandler) = $this->getInvokingElement();
  1918. $eventLogMsg = $eventHandler->m_EventLogMsg;
  1919. if($eventLogMsg)
  1920. {
  1921. return $eventLogMsg;
  1922. }
  1923. else
  1924. {
  1925. return null;
  1926. }
  1927. }
  1928. /**
  1929. * Get on event elements
  1930. *
  1931. * @return array element list
  1932. */
  1933. protected function getOnEventElements()
  1934. {
  1935. $elementList = array();
  1936. foreach ($this->m_DataPanel as $element)
  1937. {
  1938. if ($element->m_OnEventLog=="Y")
  1939. $elementList[] = $element->m_Value;
  1940. }
  1941. return $elementList;
  1942. }
  1943. /**
  1944. * Run event log
  1945. *
  1946. * @return void
  1947. */
  1948. protected function runEventLog()
  1949. {
  1950. $logMessage = $this->getEventLogMsg();
  1951. $eventName = $this->m_EventName;
  1952. if($logMessage && $eventName)
  1953. {
  1954. $logElements = $this->getOnEventElements();
  1955. $eventlog = BizSystem::getService(EVENTLOG_SERVICE);
  1956. $eventlog->log($eventName, $logMessage, $logElements);
  1957. }
  1958. }
  1959. /**
  1960. * return redirect page and target array
  1961. *
  1962. * @return array {redirectPage, $target}
  1963. */
  1964. protected function getRedirectPage()
  1965. {
  1966. // get the control that issues the call
  1967. // __this is elementName:eventHandlerName
  1968. list($element, $eventHandler) = $this->getInvokingElement();
  1969. $eventHandlerName = $eventHandler->m_Name;
  1970. $redirectPage = $element->getRedirectPage($eventHandlerName); // need to get postaction of eventhandler
  1971. $functionType = $element->getFunctionType($eventHandlerName);
  1972. switch ($functionType)
  1973. {
  1974. case "Popup":
  1975. case "Prop_Window":
  1976. case "Prop_Dialog":
  1977. $target = "Popup";
  1978. break;
  1979. default:
  1980. $target = "";
  1981. }
  1982. return array($redirectPage, $target);
  1983. }
  1984. /**
  1985. * Switch to other form
  1986. *
  1987. * @param string $formName to-be-swtiched form name. if empty, then switch to default form
  1988. * @param string $id id value of the target form
  1989. * @return void
  1990. * @access remote
  1991. */
  1992. public function switchForm($formName=null, $id=null, $params=null, $target=null)
  1993. {
  1994. $paramFields = array();
  1995. if($params){
  1996. parse_str(urldecode($params),$paramFields);
  1997. }
  1998. if ($id!=null)
  1999. $paramFields["Id"] = $id;
  2000. $this->_showForm($formName, $target, $paramFields);
  2001. }
  2002. public function parentSwitchForm($formName=null, $id=null, $params=null, $target=null)
  2003. {
  2004. if($this->m_ParentFormName){
  2005. $formObj = BizSystem::getObject($this->m_ParentFormName);
  2006. return $formObj->switchForm($formName, $id, $params, $target);
  2007. }
  2008. }
  2009. public function targetSwitchForm($targetForm, $formName=null, $id=null, $params=null, $target=null)
  2010. {
  2011. if($targetForm){
  2012. $formObj = BizSystem::getObject($targetForm);
  2013. if($formObj){
  2014. return $formObj->switchForm($formName, $id, $params, $target);
  2015. }
  2016. }
  2017. }
  2018. /**
  2019. * Get the element that issues the call.
  2020. *
  2021. * @return array element object and event handler name
  2022. */
  2023. protected function getInvokingElement()
  2024. {
  2025. if ($this->m_InvokingElement)
  2026. return $this->m_InvokingElement;
  2027. // __this is elementName:eventHandlerName
  2028. $elementAndEventName = BizSystem::clientProxy()->getFormInputs("__this");
  2029. if (! $elementAndEventName)
  2030. return array(null,null);
  2031. list ($elementName, $eventHandlerName) = explode(":", $elementAndEventName);
  2032. $element = $this->getElement($elementName);
  2033. $eventHandler = $element->m_EventHandlers->get($eventHandlerName);
  2034. $this->m_InvokingElement = array($element, $eventHandler);
  2035. return $this->m_InvokingElement;
  2036. }
  2037. /**
  2038. * Process Post Action
  2039. *
  2040. * @return void
  2041. */
  2042. protected function processPostAction()
  2043. {
  2044. // get the $redirectPage from eventHandler
  2045. list($redirectPage,$target) = $this->getRedirectPage();
  2046. if ($redirectPage)
  2047. {
  2048. if($this->m_hasError==false)
  2049. {
  2050. // if the redirectpage start with "form=", render the form to the target which is defined by FuntionType
  2051. if (strpos($redirectPage,"form=") === 0)
  2052. {
  2053. parse_str($redirectPage, $output);
  2054. $formName = $output['form'];
  2055. // parse query string. e.g. fld:Id=val&fld:name=val
  2056. $paramFields = array();
  2057. foreach ($output as $key=>$value)
  2058. {
  2059. if (substr($key, 0, 4) == "fld:")
  2060. {
  2061. $fieldName = substr($key, 4);
  2062. $paramFields[$fieldName] = $value;
  2063. }
  2064. }
  2065. $this->_showForm($formName, $target, $paramFields);
  2066. }
  2067. else
  2068. {
  2069. // otherwise, do page redirection
  2070. BizSystem::clientProxy()->ReDirectPage($redirectPage);
  2071. }
  2072. }
  2073. }
  2074. }
  2075. /**
  2076. * Get activeRecord
  2077. *
  2078. * @param mixed $recId
  2079. * @return array - record array
  2080. */
  2081. public function getActiveRecord($recId=null)
  2082. {
  2083. if ($this->m_ActiveRecord != null)
  2084. {
  2085. if($this->m_ActiveRecord['Id'] != null)
  2086. {
  2087. return $this->m_ActiveRecord;
  2088. }
  2089. }
  2090. if ($recId==null || $recId=='')
  2091. $recId = BizSystem::clientProxy()->getFormInputs('_selectedId');
  2092. if ($recId==null || $recId=='')
  2093. return null;
  2094. $this->m_RecordId = $recId;
  2095. // TODO: may consider cache the current record in session
  2096. if($this->getDataObj()){
  2097. $this->getDataObj()->setActiveRecordId($this->m_RecordId);
  2098. $rec = $this->getDataObj()->getActiveRecord();
  2099. // update the record row
  2100. $this->m_DataPanel->setRecordArr($rec);
  2101. $this->m_ActiveRecord = $rec;
  2102. }
  2103. return $rec;
  2104. }
  2105. public function getRecordId(){
  2106. return $this->m_RecordId;
  2107. }
  2108. public function setRecordId($val){
  2109. $this->m_RecordId = $val;
  2110. return $val;
  2111. }
  2112. /**
  2113. * Set active record
  2114. *
  2115. * @param array $record
  2116. * @return void
  2117. */
  2118. protected function setActiveRecord($record)
  2119. {
  2120. // update the record row
  2121. $this->m_DataPanel->setRecordArr($record);
  2122. if(!isset($this->m_ActiveRecord["Id"]) &&
  2123. $this->m_RecordId!=null &&
  2124. (strtoupper($this->m_FormType) == 'EDIT' || $this->m_FormType==null )){
  2125. if($this->getDataObj()){
  2126. $this->m_ActiveRecord = $this->getDataObj()->fetchById($this->m_RecordId)->toArray();
  2127. }
  2128. }
  2129. if(is_array($record)){
  2130. foreach($record as $key=>$value){
  2131. if($key=='extend')continue;
  2132. $this->m_ActiveRecord[$key] = $record[$key];
  2133. }
  2134. }
  2135. }
  2136. /**
  2137. * Set client scripts, auto add javascripts code to the page
  2138. *
  2139. * @return void
  2140. */
  2141. protected function setClientScripts()
  2142. {
  2143. // load custom js class
  2144. if ($this->m_jsClass != "Openbiz.Form" && $this->m_jsClass != "Openbiz.TableForm" && $this->m_jsClass != "" )
  2145. BizSystem::clientProxy()->appendScripts($this->m_jsClass, $this->m_jsClass . ".js");
  2146. /*
  2147. if ($this->m_FormType == 'LIST')
  2148. {
  2149. BizSystem::clientProxy()->appendScripts("tablekit", "tablekit.js");
  2150. }*/
  2151. }
  2152. protected function translate()
  2153. {
  2154. $module = $this->getModuleName($this->m_Name);
  2155. if (!empty($this->m_Title))
  2156. {
  2157. $trans_string = I18n::t($this->m_Title, $this->getTransKey('Title'), $module, $this->getTransPrefix());
  2158. if($trans_string){
  2159. $this->m_Title = $trans_string;
  2160. }
  2161. }
  2162. if (!empty($this->m_Icon))
  2163. {
  2164. $trans_string = I18n::t($this->m_Icon, $this->getTransKey('Icon'), $module, $this->getTransPrefix());
  2165. if($trans_string){
  2166. $this->m_Icon = $trans_string;
  2167. }
  2168. }
  2169. if (!empty($this->m_Description))
  2170. {
  2171. $trans_string = I18n::t($this->m_Description, $this->getTransKey('Description'), $module, $this->getTransPrefix());
  2172. if($trans_string){
  2173. $this->m_Description = $trans_string;
  2174. }
  2175. }
  2176. }
  2177. protected function getTransPrefix()
  2178. {
  2179. $nameArr = explode(".",$this->m_Name);
  2180. for($i=1;$i<count($nameArr)-1;$i++)
  2181. {
  2182. $prefix .= strtoupper($nameArr[$i])."_";
  2183. }
  2184. return $prefix;
  2185. }
  2186. protected function getTransKey($name)
  2187. {
  2188. $shortFormName = substr($this->m_Name,intval(strrpos($this->m_Name,'.'))+1);
  2189. return strtoupper($shortFormName.'_'.$name);
  2190. }
  2191. }
  2192. ?>