PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/openbiz/bin/easy/element/OptionElement.php

http://openbiz-cubi.googlecode.com/
PHP | 280 lines | 201 code | 22 blank | 57 comment | 28 complexity | 13ace5039de4a8b21f29ce4b331e7181 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.element
  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: OptionElement.php 3561 2011-03-30 06:15:47Z jixian2003 $
  15. */
  16. //include_once("InputElement.php");
  17. /**
  18. * OptionElement is the base class of element that render list (from Selection.xml)
  19. * Used by :
  20. * - {@link AutoSuggest}
  21. * - {@link Checkbox}
  22. * - {@link ColumnList}
  23. * - {@link EditCombobox}
  24. * - {@link LabelList}
  25. * - {@link Listbox}
  26. *
  27. * @package openbiz.bin.easy.element
  28. * @author Rocky Swen
  29. * @copyright Copyright (c) 2005-2009
  30. * @access public
  31. */
  32. class OptionElement extends InputElement
  33. {
  34. public $m_SelectFrom;
  35. public $m_SelectFromSQL;
  36. public $m_SelectedList;
  37. /**
  38. * Read metadata info from metadata array and store to class variable
  39. *
  40. * @param array $xmlArr metadata array
  41. * @return void
  42. */
  43. protected function readMetaData(&$xmlArr)
  44. {
  45. parent::readMetaData($xmlArr);
  46. $this->m_SelectFrom = isset($xmlArr["ATTRIBUTES"]["SELECTFROM"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROM"] : null;
  47. $this->m_SelectedList = isset($xmlArr["ATTRIBUTES"]["SELECTEDLIST"]) ? $xmlArr["ATTRIBUTES"]["SELECTEDLIST"] : null;
  48. $this->m_SelectFromSQL = isset($xmlArr["ATTRIBUTES"]["SELECTFROMSQL"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROMSQL"] : null;
  49. }
  50. /**
  51. * Get select from
  52. *
  53. * @return string
  54. */
  55. protected function getSelectFrom()
  56. {
  57. $formobj = $this->getFormObj();
  58. return Expression::evaluateExpression($this->m_SelectFrom, $formobj);
  59. }
  60. protected function getSelectedList()
  61. {
  62. $formobj = $this->getFormObj();
  63. return Expression::evaluateExpression($this->m_SelectedList, $formobj);
  64. }
  65. protected function getSelectFromSQL()
  66. {
  67. $formobj = $this->getFormObj();
  68. return Expression::evaluateExpression($this->m_SelectFromSQL, $formobj);
  69. }
  70. /**
  71. * Render, draw the control according to the mode
  72. *
  73. * @return string HTML text
  74. */
  75. public function render()
  76. {
  77. return "";
  78. }
  79. /**
  80. * Get from list
  81. *
  82. * @param array $list
  83. * @return void
  84. */
  85. public function getFromList(&$list, $selectFrom=null)
  86. {
  87. if (!$selectFrom) {
  88. $selectFrom = $this->getSelectFrom();
  89. }
  90. if (!$selectFrom) {
  91. return $this->getSQLFromList($list);
  92. }
  93. $this->getXMLFromList($list, $selectFrom);
  94. if ($list != null)
  95. return;
  96. $this->getDOFromList($list, $selectFrom);
  97. if ($list != null)
  98. return;
  99. $this->getSimpleFromList($list, $selectFrom);
  100. if ($list != null)
  101. return;
  102. return;
  103. }
  104. protected function getXMLFromList(&$list, $selectFrom)
  105. {
  106. $pos0 = strpos($selectFrom, "(");
  107. $pos1 = strpos($selectFrom, ")");
  108. if ($pos0>0 && $pos1 > $pos0)
  109. { // select from xml file
  110. $xmlFile = substr($selectFrom, 0, $pos0);
  111. $tag = substr($selectFrom, $pos0 + 1, $pos1 - $pos0-1);
  112. $tag = strtoupper($tag);
  113. $xmlFile = BizSystem::GetXmlFileWithPath ($xmlFile);
  114. if (!$xmlFile) return false;
  115. $xmlArr = &BizSystem::getXmlArray($xmlFile);
  116. if ($xmlArr)
  117. {
  118. $i = 0;
  119. if (!key_exists($tag, $xmlArr["SELECTION"]))
  120. return false;
  121. if(!$xmlArr["SELECTION"][$tag][0]){
  122. $array = $xmlArr["SELECTION"][$tag];
  123. unset($xmlArr["SELECTION"][$tag]);
  124. $xmlArr["SELECTION"][$tag][0]=$array;
  125. }
  126. foreach($xmlArr["SELECTION"][$tag] as $node)
  127. {
  128. $list[$i]['val'] = $node["ATTRIBUTES"]["VALUE"];
  129. $list[$i]['pic'] = $node["ATTRIBUTES"]["PICTURE"];
  130. if ($node["ATTRIBUTES"]["TEXT"])
  131. {
  132. $list[$i]['txt'] = $node["ATTRIBUTES"]["TEXT"];
  133. }
  134. else
  135. {
  136. $list[$i]['txt'] = $list[$i]['val'];
  137. }
  138. $i++;
  139. }
  140. $this->translateList($list, $tag); // supprot multi-language
  141. }
  142. return true;
  143. }
  144. return false;
  145. }
  146. protected function getDOFromList(&$list, $selectFrom)
  147. {
  148. $pos0 = strpos($selectFrom, "[");
  149. $pos1 = strpos($selectFrom, "]");
  150. if ($pos0 > 0 && $pos1 > $pos0)
  151. { // select from bizObj
  152. // support BizObjName[BizFieldName] or
  153. // BizObjName[BizFieldName4Text:BizFieldName4Value] or
  154. // BizObjName[BizFieldName4Text:BizFieldName4Value:BizFieldName4Pic]
  155. $bizObjName = substr($selectFrom, 0, $pos0);
  156. $pos3 = strpos($selectFrom, ":");
  157. if($pos3 > $pos0 && $pos3 < $pos1)
  158. {
  159. $fieldName = substr($selectFrom, $pos0 + 1, $pos3 - $pos0 - 1);
  160. $fieldName_v = substr($selectFrom, $pos3 + 1, $pos1 - $pos3 - 1);
  161. }
  162. else
  163. {
  164. $fieldName = substr($selectFrom, $pos0 + 1, $pos1 - $pos0 - 1);
  165. $fieldName_v = $fieldName;
  166. }
  167. $pos4 = strpos($fieldName_v, ":");
  168. if($pos4){
  169. $fieldName_v_mixed = $fieldName_v;
  170. $fieldName_v = substr($fieldName_v_mixed,0,$pos4);
  171. $fieldName_p = substr($fieldName_v_mixed, $pos4+1, strlen($fieldName_v_mixed)-$pos4-1);
  172. unset($fieldName_v_mixed);
  173. }
  174. $commaPos = strpos($selectFrom, ",", $pos1);
  175. if ($commaPos > $pos1)
  176. $searchRule = trim(substr($selectFrom, $commaPos + 1));
  177. /* @var $bizObj BizDataObj */
  178. $bizObj = BizSystem::getObject($bizObjName);
  179. if (!$bizObj)
  180. return false;
  181. $recList = array();
  182. $oldAssoc = $bizObj->m_Association;
  183. $bizObj->m_Association = null;
  184. QueryStringParam::reset();
  185. $recList = $bizObj->directFetch($searchRule);
  186. $bizObj->m_Association = $oldAssoc;
  187. foreach ($recList as $rec)
  188. {
  189. $list[$i]['val'] = $rec[$fieldName_v];
  190. $list[$i]['txt'] = $rec[$fieldName];
  191. $list[$i]['pic'] = $rec[$fieldName_p];
  192. $i++;
  193. }
  194. return true;
  195. }
  196. return false;
  197. }
  198. protected function getSimpleFromList(&$list, $selectFrom)
  199. {
  200. // in case of a|b|c
  201. if (strpos($selectFrom, "[") > 0 || strpos($selectFrom, "(") > 0)
  202. return;
  203. $recList = explode('|',$selectFrom);
  204. foreach ($recList as $rec)
  205. {
  206. $list[$i]['val'] = $rec;
  207. $list[$i]['txt'] = $rec;
  208. $list[$i]['pic'] = $rec;
  209. $i++;
  210. }
  211. }
  212. public function getSQLFromList(&$list)
  213. {
  214. $sql = $this->getSelectFromSQL();
  215. if (!$sql) return;
  216. $formObj = $this->getFormObj();
  217. $do = $formObj->getDataObj();
  218. $db = $do->getDBConnection();
  219. try {
  220. $resultSet = $db->query($sql);
  221. $recList = $resultSet->fetchAll();
  222. foreach ($recList as $rec)
  223. {
  224. $list[$i]['val'] = $rec[0];
  225. $list[$i]['txt'] = isset($rec[1]) ? $rec[1] : $rec[0];
  226. $i++;
  227. }
  228. }
  229. catch (Exception $e)
  230. {
  231. BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: ".$e->getMessage());
  232. $this->m_ErrorMessage = "Error in SQL query: ".$sql.". ".$e->getMessage();
  233. throw new BDOException($this->m_ErrorMessage);
  234. return null;
  235. }
  236. }
  237. protected function translateList(&$list, $tag)
  238. {
  239. $module = $this->getModuleName($this->m_SelectFrom);
  240. if (empty($module))
  241. $module = $this->getModuleName($this->m_FormName);
  242. for ($i=0; $i<count($list); $i++)
  243. {
  244. $key = 'SELECTION_'.strtoupper($tag).'_'.$i.'_TEXT';
  245. $list[$i]['txt'] = I18n::t($list[$i]['txt'], $key, $module, $this->getTransLOVPrefix());
  246. }
  247. }
  248. protected function getTransLOVPrefix()
  249. {
  250. $nameArr = explode(".",$this->m_SelectFrom);
  251. for($i=1;$i<count($nameArr)-1;$i++)
  252. {
  253. $prefix .= strtoupper($nameArr[$i])."_";
  254. }
  255. return $prefix;
  256. }
  257. }
  258. ?>