PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/openbiz/bin/service/reportService.php

http://openbiz-cubi.googlecode.com/
PHP | 173 lines | 99 code | 16 blank | 58 comment | 2 complexity | 91b1628edea6ca9cb0b5337f7837fa8a 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.service
  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: reportService.php 2553 2010-11-21 08:36:48Z mr_a_ton $
  15. */
  16. /**
  17. * reportService class is the plug-in service of generate report for BizDataobj
  18. *
  19. * @package openbiz.bin.service
  20. * @author Rocky Swen
  21. * @copyright Copyright (c) 2003-2009, Rocky Swen
  22. * @access public
  23. */
  24. class reportService extends MetaObject
  25. {
  26. public $m_targetReportPath; // = "D:\\Tomcat5\\webapps\\birt-viewer\\report\\";
  27. public $m_rptTemplate; // = "dataobj.rptdesign.tpl";
  28. public $m_birtViewer; // = "http://localhost:8080/birt-viewer";
  29. /**
  30. * Initialize reportService with xml array metadata
  31. *
  32. * @param array $xmlArr
  33. * @return void
  34. */
  35. function __construct(&$xmlArr)
  36. {
  37. $this->readMetadata($xmlArr);
  38. }
  39. /**
  40. * Read array meta data, and store to meta object
  41. *
  42. * @param array $xmlArr
  43. * @return void
  44. */
  45. protected function readMetadata(&$xmlArr)
  46. {
  47. parent::readMetaData($xmlArr);
  48. $this->m_targetReportPath = isset($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["TARGETREPORTPATH"]) ? $xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["TARGETREPORTPATH"] : null;
  49. $this->m_rptTemplate = isset($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["REPORTTEMPLATE"]) ? $xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["REPORTTEMPLATE"] : null;
  50. $this->m_birtViewer = isset($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["BIRTVIEWER"]) ? $xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["BIRTVIEWER"] : null;
  51. }
  52. /**
  53. * render the report output
  54. *
  55. * @param string $objName object name which is the bizform name
  56. * @return void
  57. */
  58. public function render($objName)
  59. {
  60. // get the current UI bizobj
  61. $bizform = BizSystem::getObject($objName); // get the existing bizform object
  62. $bizobj = $bizform->getDataObj();
  63. $h=opendir($this->m_targetReportPath);
  64. if (!$h)
  65. {
  66. echo "cannot read dir ".$this->m_targetReportPath;
  67. exit;
  68. }
  69. // create a tmp csv file for hold the data, then feed csv file to report engine
  70. $uid = $this->getUniqueString();
  71. $tmpfname = $this->m_targetReportPath . $uid . ".csv";
  72. //echo "csv file is at $tmpfname.<br>";
  73. $fp = fopen($tmpfname, 'w');
  74. $keyList = $bizform->m_RecordRow->GetSortControlKeys();
  75. $fieldNames = array();
  76. foreach($keyList as $key)
  77. {
  78. $fieldNames[] = $bizform->GetControl($key)->m_BizFieldName;
  79. }
  80. fputcsv($fp, $fieldNames);
  81. $recList = $bizobj->directFetch();
  82. foreach ($recList as $recArray)
  83. {
  84. unset($fieldValues);
  85. $fieldValues = array();
  86. $line = "";
  87. foreach($keyList as $key)
  88. {
  89. $fieldValues[] = $recArray[$bizform->GetControl($key)->m_BizFieldName];
  90. }
  91. fputcsv($fp, $fieldValues);
  92. }
  93. fclose($fp);
  94. $i = 0;
  95. foreach($keyList as $key)
  96. {
  97. $rpt_fields[$i]["name"] = $bizform->GetControl($key)->m_BizFieldName;
  98. $rpt_fields[$i]["type"] = $bizobj->getField($rpt_fields[$i]["name"])->m_Type;
  99. $i++;
  100. }
  101. // dataobj.rptdesign.tpl
  102. // $rpt_data_dir, $rpt_title, $rpt_csv_file, $rpt_fields[](name,type)
  103. $smarty = BizSystem::getSmartyTemplate();
  104. $smarty->assign("rpt_data_dir", $this->m_targetReportPath);
  105. $smarty->assign("rpt_title", $bizform->m_Title);
  106. $smarty->assign("rpt_csv_file", basename($tmpfname));
  107. $smarty->assign("rpt_fields", $rpt_fields);
  108. $reportContent = $smarty->fetch($this->m_rptTemplate);
  109. $tmpRptDsgn = $this->m_targetReportPath . $uid . ".rptdesign";
  110. //echo "temp rpt design file is at $tmpRptDsgn.<br>";
  111. $fp = fopen($tmpRptDsgn, 'w');
  112. fwrite($fp, $reportContent);
  113. fclose($fp);
  114. ob_clean();
  115. $designFileName = $uid . ".rptdesign";
  116. $content = "<div style='font-family:Arial; font-size:12px; background-color:#FCFCFC;'>";
  117. $content .= "Reports can be viewed as ";
  118. $content .= "<li><a href='".$this->m_birtViewer."/run?__report=report\\$designFileName' target='__blank'>HTML report</a></li>";
  119. $content .= "<li><a href='".$this->m_birtViewer."/run?__report=report\\$designFileName&__format=pdf' target='__blank'>PDF report</a></li>";
  120. $content .= "<li><a href='".$this->m_birtViewer."/frameset?__report=report\\$designFileName' target='__blank'>Interactive report</a></li>";
  121. $content .= "</div>";
  122. echo $content;
  123. exit;
  124. }
  125. /**
  126. * Clear files on specified directory
  127. *
  128. * @param string $dir
  129. * @param number $seconds
  130. */
  131. public function cleanFiles($dir, $seconds)
  132. {
  133. //Delete temporary files
  134. $currentTime = time();
  135. $dirHandle = opendir($dir);
  136. while($file = readdir($dirHandle))
  137. {
  138. $path=$dir.'/'.$file;
  139. if($currentTime - filemtime($path) > $seconds)
  140. unlink($path);
  141. }
  142. closedir($dirHandle);
  143. }
  144. /**
  145. * Get unique string by time and MD5
  146. *
  147. * @return string
  148. */
  149. public function getUniqueString()
  150. {
  151. $mdy = date("mdy");
  152. $hms = date("His");
  153. $rightnow = $mdy.$hms;
  154. return md5($rightnow);
  155. }
  156. }
  157. ?>