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

/cubi/modules/common/lib/MetaObjExport.php

http://openbiz-cubi.googlecode.com/
PHP | 306 lines | 249 code | 22 blank | 35 comment | 75 complexity | 9490dde7ce2b7427d1a4108cea874265 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. class MetaObjExport
  3. {
  4. protected $m_Object;
  5. protected $m_Doc;
  6. protected $m_XmlFile;
  7. protected $m_ObjType;
  8. protected $m_RelXmlFile;
  9. protected $comments = "<!--\n #object_type# Object '#object_name#', file path #object_file#. Please change the 'myproj' to your own module name. \n-->\n";
  10. protected $firstAttrs = array('Name','Class','Description','Title');
  11. protected $skipAttrs = array('Package','Percent','TotalPages','OrigFunction','FormName','HTMLAttr','BizObjName','Index','DATAFORMAT');
  12. protected $convertAttrs = array('DataObjName'=>'BizDataObj','MainTable'=>'Table','Range'=>'PageSize');
  13. public function __construct($object)
  14. {
  15. $this->m_Object = $object;
  16. }
  17. public function GetDocDocument()
  18. {
  19. if ($this->m_Doc)
  20. return $this->m_Doc;
  21. $this->m_XmlFile = MODULE_PATH."/".str_replace(".","/",$this->m_Object->m_Name).".xml";
  22. $this->m_RelXmlFile = "cubi/modules/".str_replace(".","/",$this->m_Object->m_Name).".xml";
  23. //if (!file_exists($this->m_XmlFile))
  24. // return null;
  25. $doc = new DomDocument();
  26. //$ok = $doc->load($this->m_XmlFile);
  27. //if (!$ok)
  28. // return null;
  29. $this->m_Doc = $doc;
  30. //$rootElem = $doc->documentElement;
  31. return $doc;
  32. }
  33. public function MetaObj2XML()
  34. {
  35. if (is_a($this->m_Object, "EasyForm")) {
  36. $this->m_ObjType = "Form";
  37. return $this->Form2XML();
  38. }
  39. else if (is_a($this->m_Object, "EasyView")) {
  40. $this->m_ObjType = "View";
  41. return $this->View2XML();
  42. }
  43. else if (is_a($this->m_Object, "BizDataObj")) {
  44. $this->m_ObjType = "DataObject";
  45. return $this->DataObj2XML();
  46. }
  47. }
  48. public function DataObj2XML()
  49. {
  50. $doc = $this->GetDocDocument();
  51. $docElem = $this->DataObj2XMLElement($this->m_Object);
  52. $doc->appendChild($docElem);
  53. $xmlStr = xmlpp($doc->saveXML());
  54. return $xmlStr;
  55. }
  56. protected function DataObj2XMLElement($obj, $clz='')
  57. {
  58. $doc = $this->GetDocDocument();
  59. $className = get_class($obj);
  60. $elemName = $className; // element use class name by default
  61. $vars = get_object_vars($obj);
  62. if ($className == "TableJoin") {
  63. $elemName = "Join";
  64. }
  65. if ($className == "BizRecord") {
  66. $vars = $obj;
  67. $elemName = "BizFieldList";
  68. }
  69. if ($className == "MetaIterator") {
  70. $vars = $obj;
  71. }
  72. if (is_subclass_of($obj, "BizDataObj")) {
  73. $elemName = "BizDataObj";
  74. }
  75. if ($clz!='') {
  76. $elemName = $clz;
  77. }
  78. // create an element
  79. $elem = $doc->createElement($elemName);
  80. // set input attributes
  81. $attrList = array();
  82. foreach ($vars as $name => $value)
  83. {
  84. if (is_object($value)) {
  85. $clz = "";
  86. if (get_class($value) == "MetaIterator") {
  87. $clz = str_replace('m_','',$name);
  88. }
  89. //echo "get child element of $name, $clz\n";
  90. $chldElem = $this->DataObj2XMLElement($value, $clz);
  91. $elem->appendChild($chldElem);
  92. //echo "-- get child element of $name\n";
  93. }
  94. else if (is_array($value)) {
  95. continue;
  96. }
  97. else if ($value!="") {
  98. //echo "set attr ($name, $value)\n";
  99. $attrName = str_replace('m_','',$name);
  100. $attrList[$attrName] = $value;
  101. if ($elemName == "BizDataObj" && $attrName == "Name") $attrList[$attrName] = $this->getShortName($value);
  102. }
  103. }
  104. $this->setElemAttrs($attrList, $elem);
  105. return $elem;
  106. }
  107. public function Form2XML()
  108. {
  109. //print_r($this->m_Object);
  110. $doc = $this->GetDocDocument();
  111. $docElem = $this->FormObj2XMLElement($this->m_Object);
  112. $doc->appendChild($docElem);
  113. //$xmlStr = str_replace(array('#object_type#','#object_name#','#object_file#'),array($this->m_ObjType,$this->m_Object->m_Name,$this->m_RelXmlFile),$this->comments);
  114. $xmlStr = xmlpp($doc->saveXML());
  115. return $xmlStr;
  116. }
  117. protected function FormObj2XMLElement($obj, $clz='')
  118. {
  119. $doc = $this->GetDocDocument();
  120. $className = get_class($obj);
  121. $elemName = $className; // element use class name by default
  122. $vars = get_object_vars($obj);
  123. if ($className == "Panel") {
  124. $vars = $obj;
  125. }
  126. if (is_subclass_of($obj, "Element")) {
  127. $elemName = "Element";
  128. }
  129. if (is_subclass_of($obj, "EasyForm")) {
  130. $elemName = "EasyForm";
  131. }
  132. if ($clz!='') {
  133. $elemName = $clz;
  134. }
  135. // create an element
  136. $elem = $doc->createElement($elemName);
  137. // set input attributes
  138. $attrList = array();
  139. foreach ($vars as $name => $value)
  140. {
  141. if (is_object($value)) {
  142. $clz = "";
  143. if (get_class($value) == "Panel") {
  144. $clz = str_replace('m_','',$name);
  145. }
  146. if ($name == "m_EventHandlers") {
  147. foreach ($value as $k1 => $v1) {
  148. //echo "get child element of $name, $clz\n";
  149. $chldElem = $this->FormObj2XMLElement($v1, $clz);
  150. $elem->appendChild($chldElem);
  151. //echo "-- get child element of $name\n";
  152. }
  153. } else {
  154. //echo "get child element of $name, $clz\n";
  155. $chldElem = $this->FormObj2XMLElement($value, $clz);
  156. $elem->appendChild($chldElem);
  157. //echo "-- get child element of $name\n";
  158. }
  159. }
  160. else if (is_array($value)) {
  161. continue;
  162. }
  163. else if ($value!="") {
  164. //echo "set attr ($name, $value)\n";
  165. if ($name == "m_Function") {
  166. if (preg_match("/\.([a-zA-Z1-9_]+\(.+)/",$value,$matches)) {
  167. $value = $matches[1];
  168. }
  169. }
  170. $attrName = str_replace('m_','',$name);
  171. $attrList[$attrName] = $value;
  172. if ($elemName == "EasyForm" && $attrName == "Name") $attrList[$attrName] = $this->getShortName($value);
  173. if ($elemName == "EasyForm" && $attrName == "TemplateFile") $attrList[$attrName] = $this->getShortName($value);
  174. }
  175. }
  176. $this->setElemAttrs($attrList, $elem);
  177. return $elem;
  178. }
  179. protected function setElemAttrs($attrList, $elem)
  180. {
  181. // set attributes with order Name, Class, Description, Title, ...
  182. foreach ($this->firstAttrs as $attrName) {
  183. if (isset($attrList[$attrName])) {
  184. $elem->setAttribute($attrName, $attrList[$attrName]);
  185. }
  186. }
  187. foreach ($attrList as $k=>$v) {
  188. if (in_array($k, $this->firstAttrs)) continue;
  189. if (in_array($k, $this->skipAttrs)) continue;
  190. if (isset($this->convertAttrs[$k])) $k = $this->convertAttrs[$k];
  191. $elem->setAttribute($k, $v);
  192. }
  193. }
  194. protected function getShortName($value)
  195. {
  196. if (strpos($value,'.')>0) {
  197. $parts = explode('.',$value);
  198. $value = $parts[count($parts)-1];
  199. }
  200. return $value;
  201. }
  202. public function View2XML()
  203. {
  204. $doc = $this->GetDocDocument();
  205. $docElem = $this->ViewObj2XMLElement($this->m_Object);
  206. $doc->appendChild($docElem);
  207. //$xmlStr = str_replace(array('#object_type#','#object_name#','#object_file#'),array($this->m_ObjType,$this->m_Object->m_Name,$this->m_RelXmlFile),$this->comments);
  208. $xmlStr = xmlpp($doc->saveXML());
  209. return $xmlStr;
  210. }
  211. protected function ViewObj2XMLElement($obj)
  212. {
  213. $doc = $this->GetDocDocument();
  214. $className = get_class($obj);
  215. $vars = get_object_vars($obj);
  216. $elemName = $className; // element use class name by default
  217. if ($className == "MetaIterator") {
  218. $vars = $obj;
  219. $elemName = "FormReferences";
  220. }
  221. else if ($className == "FormReference") {
  222. $elemName = "Reference";
  223. }
  224. if (is_subclass_of($obj, "EasyView")) {
  225. $elemName = "EasyView";
  226. }
  227. // create an element
  228. $elem = $doc->createElement($elemName);
  229. // set input attributes
  230. $attrList = array();
  231. foreach ($vars as $name => $value)
  232. {
  233. if (is_object($value)) {
  234. //echo "get child element of $name\n";
  235. $chldElem = $this->ViewObj2XMLElement($value);
  236. $elem->appendChild($chldElem);
  237. //echo "-- get child element of $name\n";
  238. }
  239. else if ($value!="") {
  240. //echo "set attr ($name, $value)\n";
  241. $attrName = str_replace('m_','',$name);
  242. $attrList[$attrName] = $value;
  243. if ($elemName == "EasyView" && $attrName == "Name") $attrList[$attrName] = $this->getShortName($value);
  244. }
  245. }
  246. $this->setElemAttrs($attrList, $elem);
  247. return $elem;
  248. }
  249. }
  250. /** Prettifies an XML string into a human-readable and indented work of art
  251. * @param string $xml The XML as a string
  252. * @param boolean $html_output True if the output should be escaped (for use in HTML)
  253. * http://gdatatips.blogspot.com/2008/11/xml-php-pretty-printer.html, Apache 2.0 License.
  254. */
  255. function xmlpp($xml, $html_output=false) {
  256. $xml_obj = new SimpleXMLElement($xml);
  257. $level = 4;
  258. $indent = 0; // current indentation level
  259. $pretty = array();
  260. // get an array containing each XML element
  261. $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
  262. // shift off opening XML tag if present
  263. if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
  264. $pretty[] = array_shift($xml);
  265. }
  266. foreach ($xml as $el) {
  267. if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
  268. // opening tag, increase indent
  269. $pretty[] = str_repeat(' ', $indent) . $el;
  270. $indent += $level;
  271. } else {
  272. if (preg_match('/^<\/.+>$/', $el)) {
  273. $indent -= $level; // closing tag, decrease indent
  274. }
  275. if ($indent < 0) {
  276. $indent += $level;
  277. }
  278. $pretty[] = str_repeat(' ', $indent) . $el;
  279. }
  280. }
  281. $xml = implode("\n", $pretty);
  282. return ($html_output) ? htmlentities($xml) : $xml;
  283. }
  284. ?>