PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/modules/tool/ToolListbox.php

http://openbiz-cubi.googlecode.com/
PHP | 320 lines | 246 code | 26 blank | 48 comment | 28 complexity | 9b757fcb323a82b85ac38e719901c913 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?PHP
  2. /**
  3. * FieldControl - class FieldControl is the base class of field control who binds with a bizfield
  4. *
  5. * @package BizView
  6. * @author rocky swen
  7. * @copyright Copyright (c) 2005
  8. * @version 1.2
  9. * @access public
  10. */
  11. class ToolListbox extends Listbox
  12. {
  13. /* Special handling the SelectFrom
  14. dbs()
  15. tables(db)
  16. columns(db,table)
  17. fields(do)
  18. joins()
  19. references()
  20. ------------------
  21. dos()
  22. forms()
  23. */
  24. public function getFromList(&$list)
  25. {
  26. $selFrom = $this->getSelectFrom();
  27. $selFrom = $this->evalSelectFrom($selFrom);
  28. list($func,$body) = explode("(",$selFrom);
  29. $body = str_replace(")","",$body);
  30. if (strpos($body,",")>0)
  31. $args = explode(",",$body);
  32. else
  33. $args[0] = $body;
  34. if (method_exists($this, $func))
  35. $list = call_user_func_array(array($this, $func),$args);
  36. }
  37. protected function evalSelectFrom($selFrom)
  38. {
  39. return $selFrom;
  40. }
  41. protected function dbs()
  42. {
  43. $dbinfo = BizSystem::configuration()->getDatabaseInfo();
  44. $i = 0;
  45. foreach ($dbinfo as $db)
  46. {
  47. $list[$i]['val'] = $db['Name'];
  48. $list[$i]['txt'] = $db['Name'];
  49. $i++;
  50. }
  51. return $list;
  52. }
  53. protected function tables($dbStr)
  54. {
  55. if (!$dbStr)
  56. {
  57. $xp_DBName = "/BizDataObj/@DBName";
  58. $dbNameAttr = $this->getFormObj()->QueryXpath($xp_DBName);
  59. $dbName = $dbNameAttr->value;
  60. }
  61. else
  62. {
  63. if (strpos($dbStr,'$') === 0)
  64. {
  65. $db_fld = substr($dbStr, 1);
  66. $dbName = $this->getFormObj()->getElement($db_fld)->m_Value;
  67. }
  68. else
  69. {
  70. $xp_DBName = $dbStr;
  71. $dbNameAttr = $this->getFormObj()->QueryXpath($xp_DBName);
  72. $dbName = $dbNameAttr->value;
  73. }
  74. }
  75. global $g_BizSystem;
  76. $db = $g_BizSystem->getDBConnection($dbName);
  77. $tables = $db->listTables();
  78. $i = 0;
  79. foreach ($tables as $t)
  80. {
  81. $list[$i]['val'] = $t;
  82. $list[$i]['txt'] = $t;
  83. $i++;
  84. }
  85. return $list;
  86. }
  87. protected function columns($tableStr, $joinFld=null)
  88. {
  89. // get the join attribute first. If join found, use table of the join
  90. if ($joinFld)
  91. {
  92. $joinFld = substr($joinFld, 1);
  93. $fld_joinVal = $this->getFormObj()->getElement($joinFld)->m_Value;
  94. if ($fld_joinVal)
  95. {
  96. $xpathStr = "/BizDataObj/TableJoins/Join[@Name='".$fld_joinVal."']";
  97. $joinElem = $this->getFormObj()->QueryXpath($xpathStr);
  98. $table = $joinElem->getAttribute('Table');
  99. }
  100. }
  101. $xp_DBName = "/BizDataObj/@DBName";
  102. $dbNameAttr = $this->getFormObj()->QueryXpath($xp_DBName);
  103. $dbName = $dbNameAttr->value ? $dbNameAttr->value : "Default";
  104. if (!$table)
  105. {
  106. if (strpos($tableStr,'$') === 0)
  107. {
  108. $table_fld = substr($tableStr, 1);
  109. $table = $this->getFormObj()->getElement($table_fld)->m_Value;
  110. //print_r($this->getFormObj()->getElement($table_fld));
  111. }
  112. else
  113. {
  114. $xp_Table = $tableStr;
  115. $tableAttr = $this->getFormObj()->QueryXpath($xp_Table);
  116. $table = $tableAttr->value;
  117. }
  118. }
  119. if ($table == "")
  120. {
  121. $list[$i]['val'] = "";
  122. $list[$i]['txt'] = "";
  123. return $list;
  124. }
  125. global $g_BizSystem;
  126. $db = $g_BizSystem->getDBConnection($dbName);
  127. $tblCols = $db->describeTable($table);
  128. $i = 0;
  129. foreach ($tblCols as $colName=>$colAttrs)
  130. {
  131. $list[$i]['val'] = $colName;
  132. $list[$i]['txt'] = $colName . " (" . $colAttrs['DATA_TYPE'] . ")";
  133. $i++;
  134. }
  135. return $list;
  136. }
  137. // get fields of this DataObj or another DataObj
  138. protected function fields($xp_BizDataObj=null)
  139. {
  140. if (!$xp_BizDataObj)
  141. {
  142. // get fields of this DataObj
  143. $xpathStr = "/BizDataObj/BizFieldList/BizField";
  144. $elems = $this->getFormObj()->QueryXpath($xpathStr, false);
  145. }
  146. else
  147. {
  148. $doAttr = $this->getFormObj()->QueryXpath($xp_BizDataObj);
  149. $doName = $doAttr->value;
  150. // get the dataobj file
  151. $metaFileInfo = $this->getFormObj()->GetMetaFileInfo();
  152. if (!$metaFileInfo)
  153. return array();
  154. //echo "meta file info:"; print_r($metaFileInfo);
  155. if ($doName && !strpos($doName, ".") && ($metaFileInfo['package'])) // no package prefix as package.object, add it
  156. $doName = $metaFileInfo['package'].".".$doName;
  157. $doFile = $metaFileInfo['modules_path'].str_replace(".","/",$doName).'.xml';
  158. // get the fields of the dataobj
  159. $xpathStr = "/BizDataObj/BizFieldList/BizField";
  160. if (!file_exists($doFile))
  161. return array();
  162. $doc = new DomDocument();
  163. $ok = $doc->load($doFile);
  164. if (!$ok)
  165. return array();
  166. $xpath = new DOMXPath($doc);
  167. $elems = $xpath->query($xpathStr);
  168. }
  169. $i = 0;
  170. $list[$i]['val'] = "";
  171. $list[$i]['txt'] = "";
  172. $i++;
  173. foreach ($elems as $elem)
  174. {
  175. $list[$i]['val'] = $elem->getAttribute('Name');
  176. $list[$i]['txt'] = $elem->getAttribute('Name') . " (" . $elem->getAttribute('Column');
  177. $join = $elem->getAttribute('Join');
  178. if ($join && $join != "")
  179. $list[$i]['txt'] .= ", join: $join)";
  180. else
  181. $list[$i]['txt'] .= ")";
  182. $i++;
  183. }
  184. return $list;
  185. }
  186. protected function joins()
  187. {
  188. $xpathStr = "/BizDataObj/TableJoins/Join";
  189. $joinElems = $this->getFormObj()->QueryXpath($xpathStr, false); // return multiple
  190. $i = 0;
  191. $list[$i]['val'] = "";
  192. $list[$i]['txt'] = "";
  193. $i++;
  194. foreach ($joinElems as $join)
  195. {
  196. $list[$i]['val'] = $join->getAttribute('Name');
  197. $list[$i]['txt'] = $join->getAttribute('Name')." (".$join->getAttribute('Table').".".$join->getAttribute('Column').")";
  198. $i++;
  199. }
  200. return $list;
  201. }
  202. protected function references()
  203. {
  204. $xpathStr = "/EasyView/FormReferences/Reference";
  205. $refElems = $this->getFormObj()->QueryXpath($xpathStr, false); // return multiple
  206. $i = 0;
  207. $list[$i]['val'] = "";
  208. $list[$i]['txt'] = "";
  209. $i++;
  210. foreach ($refElems as $ref)
  211. {
  212. $list[$i]['val'] = $ref->getAttribute('Name');
  213. $list[$i]['txt'] = $ref->getAttribute('Name');
  214. $i++;
  215. }
  216. return $list;
  217. }
  218. /*// select do in form
  219. protected function dos()
  220. {
  221. $metaFileInfo = $this->getFormObj()->GetMetaFileInfo();
  222. $modulePath = $metaFileInfo['modules_path'];
  223. $modulePath = substr($modulePath,0,strlen($modulePath)-1);
  224. global $g_MetaFiles;
  225. php_grep("<BizDataObj", $modulePath);
  226. for ($i=0; $i<count($g_MetaFiles); $i++)
  227. {
  228. $g_MetaFiles[$i] = str_replace('/','.',str_replace(array($modulePath.'/','.xml'),'', $g_MetaFiles[$i]));
  229. $list[$i]['val'] = $g_MetaFiles[$i];
  230. $list[$i]['txt'] = $g_MetaFiles[$i];
  231. }
  232. return $list;
  233. }*/
  234. // select do in form
  235. protected function dos()
  236. {
  237. $metaFileInfo = $this->getFormObj()->GetMetaFileInfo();
  238. $modulePath = $metaFileInfo['modules_path'];
  239. $packs = explode(".",$metaFileInfo['package']);
  240. $moduleName = $packs[0];
  241. $modulePath = MODULE_PATH.'/'.$moduleName; //substr($modulePath,0,strlen($modulePath)-1);
  242. global $g_MetaFiles;
  243. php_grep("<BizDataObj", $modulePath);
  244. for ($i=0; $i<count($g_MetaFiles); $i++)
  245. {
  246. $g_MetaFiles[$i] = $moduleName.'.'.str_replace('/','.',str_replace(array($modulePath.'/','.xml'),'', $g_MetaFiles[$i]));
  247. $list[$i]['val'] = $g_MetaFiles[$i];
  248. $list[$i]['txt'] = $g_MetaFiles[$i];
  249. }
  250. return $list;
  251. }
  252. // select form in view
  253. protected function forms()
  254. {
  255. $metaFileInfo = $this->getFormObj()->GetMetaFileInfo();
  256. $modulePath = $metaFileInfo['modules_path'];
  257. $modulePath = substr($modulePath,0,strlen($modulePath)-1);
  258. global $g_MetaFiles;
  259. php_grep("<EasyForm", $modulePath);
  260. for ($i=0; $i<count($g_MetaFiles); $i++)
  261. {
  262. $g_MetaFiles[$i] = str_replace('/','.',str_replace(array($modulePath.'/','.xml'),'', $g_MetaFiles[$i]));
  263. $list[$i]['val'] = $g_MetaFiles[$i];
  264. $list[$i]['txt'] = $g_MetaFiles[$i];
  265. }
  266. return $list;
  267. }
  268. }
  269. $g_MetaFiles = array();
  270. function php_grep($q, $path)
  271. {
  272. global $g_MetaFiles;
  273. $fp = opendir($path);
  274. while($f = readdir($fp))
  275. {
  276. if ( preg_match("#^\.+$#", $f) ) continue; // ignore symbolic links
  277. $file_full_path = $path.'/'.$f;
  278. if(is_dir($file_full_path))
  279. {
  280. php_grep($q, $file_full_path);
  281. }
  282. else
  283. {
  284. $path_parts = pathinfo($f);
  285. if ($path_parts['extension'] != 'xml') continue; // consider only xml files
  286. //echo file_get_contents($file_full_path); exit;
  287. if( stristr(file_get_contents($file_full_path), $q) )
  288. $g_MetaFiles[] = $file_full_path;
  289. }
  290. }
  291. }
  292. ?>