PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/phpreports/PHPReportMaker.php

http://simpleinvoices.googlecode.com/
PHP | 857 lines | 457 code | 96 blank | 304 comment | 43 complexity | 79a22c6304143a4618b8dd5926480ff2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0, LGPL-3.0
  1. <?php
  2. include("PHPReportsUtil.php");
  3. /******************************************************************************
  4. * *
  5. * PHPReportMaker *
  6. * This is the main class of PHPReports *
  7. * *
  8. * Use like this: *
  9. * $oRpt = new PHPReportMaker(); *
  10. * $oRpt->setXML("test.xml"); *
  11. * $oRpt->setXSLT("test.xsl"); *
  12. * $oRpt->setUser("john"); *
  13. * $oRpt->setPassword("doe"); *
  14. * $oRpt->setConnection("mydatabaseaddr"); *
  15. * $oRpt->setDatabaseInterface("oracle"); *
  16. * $oRpt->setSQL("select * from mytable"); *
  17. * $oRpt->run(); *
  18. * *
  19. ******************************************************************************/
  20. class PHPReportMaker {
  21. var $_sPath; // PHPReports path
  22. var $sXML; // XML report file
  23. var $sXSLT; // XSLT file
  24. var $sUser; // user name
  25. var $sPass; // password
  26. var $sCon; // connection name
  27. var $sDataI; // database interface
  28. var $sSQL; // sql query command
  29. var $_oParm; // parameters
  30. var $sDatabase; // database
  31. var $sCodeOut; // code output
  32. var $sOut; // HTML result file
  33. var $bDebug; // debug report
  34. var $_sNoDataMsg; // no data message - NEW!!! on 0.2.0
  35. var $_sOutputPlugin; // output plugin name - NEW!!! on 0.2.0
  36. var $_oOutputPlugin; // output plugin - NEW!!! on 0.2.0
  37. var $_sXMLOutputFile;// XML output file with the data
  38. var $_sSaveTo; // save to file - NEW!!! 0.2.0
  39. var $_oProc; // XSLT processor
  40. var $_aEnv; // enviroment vars
  41. var $_sClassName; // report class name to create
  42. var $_sTmp; // temporary dir
  43. var $_oCon; // database connection handle
  44. var $_oQuery; // executed query
  45. var $_iPageSize; // page size
  46. var $_aBench; // benchmark registers
  47. var $_sLang; // language
  48. var $_bBody; // no HTML BODY shown on the report
  49. var $_bDeleteXML; // if needs to delete the XML file after using it
  50. var $_oError;
  51. var $_oInput; // input plugins
  52. /***************************************************************************
  53. * *
  54. * Constructor - remember to set the PHPREPORTS *
  55. * environment variable *
  56. * *
  57. ***************************************************************************/
  58. function PHPReportMaker() {
  59. $this->_sPath = getPHPReportsFilePath();
  60. // $this->_sTmp = getPHPReportsTmpPath();
  61. $this->_sTmp = dirname(dirname(dirname(__FILE__))) . "/tmp/cache"; // done for SI to work - Ap.Muthu
  62. $this->sXML = null;
  63. $this->sXSLT = $this->_sPath."/xslt/PHPReport.xsl";
  64. $this->sUser = null;
  65. $this->sPass = null;
  66. $this->sCon = null;
  67. $this->sDataI = null;
  68. $this->sSQL = null;
  69. $this->_oParm = Array();
  70. $this->sDatabase = null;
  71. $this->sCodeOut = null;
  72. $this->sOut = null;
  73. $this->bDebug = false;
  74. $this->_sNoDataMsg = "";
  75. $this->_sNoDataFunc = "";
  76. $this->_sOutputPlugin = "default";
  77. $this->_oOutputPlugin = null;
  78. $this->_sSaveTo = null;
  79. $this->_aEnv = Array();
  80. $this->_sClassName = "PHPReport";
  81. $this->_iPageSize = 0;
  82. $this->_aBench = Array();
  83. $this->_sLang = "default";
  84. $this->_bBody = true;
  85. $this->_bDeleteXML = false;
  86. $this->_oError = new PHPReportsErrorTr();
  87. $this->_oInput = Array();
  88. /*
  89. Now we get the XSLT processor
  90. new code on the 0.2.8 version, because PHP5 have XSL
  91. support with libxslt, by default
  92. */
  93. $oProcFactory = new XSLTProcessorFactory();
  94. $this->_oProc = $oProcFactory->get();
  95. if(is_null($this->_oProc))
  96. $this->_oError->showMsg("NOXSLT");
  97. // check path stuff
  98. if(is_null(getPHPReportsFilePath()))
  99. $this->_oError->showMsg("NOPATH");
  100. }
  101. /******************************************************************************
  102. * *
  103. * Create a quick report from a layout file and some parameters. *
  104. * *
  105. ******************************************************************************/
  106. function createFromTemplate($sTitle="NO DEFINED TITLE",$sFile=null,$oParms=null,$oDocument=null,$oGroups=null){
  107. $sPath = getPHPReportsFilePath();
  108. $oError = new PHPReportsErrorTr();
  109. $sIf = $this->getDatabaseInterface();
  110. $sFile = $sFile ? $sFile : realpath($sPath."/template.xml");
  111. // if the file does not exist
  112. if(!file_exists($sFile))
  113. $oError->showMsg("NOTEMPLATE",$sFile);
  114. // get the template contents
  115. $sFileContents = file_get_contents($sFile);
  116. // check if there are some parameters here ...
  117. if($oParms){
  118. $sParms = null;
  119. if(is_object($oParms)) $sParms = $oParms->write();
  120. if(is_string($oParms)) $sParms = $oParms;
  121. if($sParms)
  122. $sFileContents = str_replace("<REPLACE_WITH_PARAMETERS/>",$sParms,$sFileContents);
  123. }
  124. // check if there is some document info
  125. if($oDocument){
  126. $sDocument = null;
  127. if(is_object($oDocument)) $sDocument = $oDocument->write();
  128. if(is_string($oDocument)) $sDocument = $oDocument;
  129. if($sDocument)
  130. $sFileContents = str_replace("<REPLACE_WITH_DOCUMENT_INFO/>",$sDocument,$sFileContents);
  131. }
  132. // if no groups info was specified, follow the default behaviour: all the fields from the query,
  133. // with no group break
  134. if(!$oGroups){
  135. // include the database interface and try to open the connection and execute the query
  136. $sIfFile = realpath($sPath."/database/db_".$sIf.".php");
  137. if(!file_exists($sIfFile))
  138. $oError->showMsg("NOIF",$sIf);
  139. include_once $sIfFile;
  140. // if the database connection is null, open it
  141. if(is_null($this->_oCon))
  142. $this->_oCon = @PHPReportsDBI::db_connect(Array($this->sUser,$this->sPass,$this->sCon,$this->sDatabase)) or $oError->showMsg("REFUSEDCON");
  143. // if there are some input filters ...
  144. if($this->_oInput){
  145. foreach($this->_oInput as $oFilter){
  146. $oFilter->setConnection($this->_oCon);
  147. $oFilter->setSQL(trim($this->sSQL));
  148. $this->sSQL = trim($oFilter->run());
  149. }
  150. // there is no need to run the filters again
  151. $this->_oInput = null;
  152. }
  153. // run the query
  154. $this->_oQuery = @PHPReportsDBI::db_query($this->_oCon,trim($this->sSQL)) or $oError->showMsg("QUERYERROR");
  155. // insert the column names
  156. $sNames = "";
  157. $sReplacedNames = "";
  158. $iColNum = PHPREportsDBI::db_colnum($this->_oQuery);
  159. for($i=1; $i<=$iColNum; $i++){
  160. $sName = PHPReportsDBI::db_columnName($this->_oQuery,$i);
  161. $sExtra = isNumericType(PHPReportsDBI::db_columnType($this->_oQuery,$i))?" ALIGN=\"RIGHT\"":"";
  162. $sReplacedNames .= "<COL CELLCLASS=\"bold\"$sExtra>".ucfirst(strtolower(str_replace("_"," ",$sName)))."</COL>";
  163. $sNames .= "<COL TYPE=\"FIELD\"$sExtra>$sName</COL>";
  164. }
  165. // build the group info
  166. $sGroup = "<GROUP REPRINT_HEADER_ON_PAGEBREAK='TRUE'><HEADER><ROW><REPLACE_WITH_REPLACED_COLUMN_NAMES/></ROW></HEADER><FIELDS><ROW><REPLACE_WITH_COLUMN_NAMES/></ROW></FIELDS></GROUP>";
  167. $sGroup = str_replace("<REPLACE_WITH_REPLACED_COLUMN_NAMES/>",$sReplacedNames,$sGroup);
  168. $sGroup = str_replace("<REPLACE_WITH_COLUMN_NAMES/>",$sNames,$sGroup);
  169. $sFileContents = str_replace("<REPLACE_WITH_GROUP_INFO/>",$sGroup,$sFileContents);
  170. }else{
  171. $sGroups = null;
  172. if(is_object($oGroups)) $sGroups = $oGroups->write();
  173. if(is_string($oGroups)) $sGroups = $oGroups;
  174. if($sGroups)
  175. $sFileContents = str_replace("<REPLACE_WITH_GROUP_INFO/>",$sGroups,$sFileContents);
  176. }
  177. // replace the report title
  178. $sFileContents = str_replace("<REPLACE_WITH_TITLE/>",$sTitle,$sFileContents);
  179. // print htmlspecialchars($sFileContents);
  180. // create the temporary XML file
  181. $sTemp = tempnam($this->_sTmp,"tempphprpt");
  182. // this is just for PHP4 compability
  183. $fHand = fopen($sTemp,"w");
  184. fwrite($fHand,$sFileContents);
  185. fclose($fHand);
  186. $this->_bDeleteXML = true; // flag to delete the temporary file
  187. $this->sXML = $sTemp; // the XML layout file is the temporary file now
  188. }
  189. /******************************************************************************
  190. * *
  191. * Run report *
  192. * Here is where things happens. :-) *
  193. * *
  194. ******************************************************************************/
  195. function run() {
  196. $iReportStart = time();
  197. // create the parameters array
  198. $aParm["user" ] = $this->sUser; // set user
  199. $aParm["pass" ] = $this->sPass; // set password
  200. $aParm["conn" ] = $this->sCon; // set connection name
  201. $aParm["interface"] = $this->sDataI; // set database interface
  202. $aParm["database" ] = $this->sDatabase; // set database
  203. $aParm["classname"] = $this->_sClassName; // ALWAYS use this class to run the report
  204. $aParm["sql" ] = $this->sSQL; // set the sql query
  205. $aParm["nodatamsg"] = $this->_sNoDataMsg; // no data msg
  206. $aParm["nodatafunc"] = $this->_sNoDataFunc; // no data function
  207. $aParm["pagesize"] = $this->_iPageSize>0?$this->_iPageSize:"";
  208. $aParm["language"] = $this->_sLang;
  209. // create the parameters keys array - with element numbers or element keys
  210. $aKeys = null;
  211. if(is_array($this->_oParm)){
  212. $aKeys = array_keys($this->_oParm);
  213. $iSize = sizeof($this->_oParm);
  214. for($i=0; $i<$iSize; $i++){
  215. $sOkey = $aKeys[$i]; // original key
  216. $sKey = $sOkey; // reference key
  217. // check if its a numeric key - if so, add 1 to
  218. // it to keep the parameters based on 1 and not on 0
  219. if(is_numeric($sOkey))
  220. $sKey = intval($sOkey)+1;
  221. $aParm["parameter".($i+1)] = $this->_oParm[$sOkey];
  222. $aParm["reference".($i+1)] = $sKey;
  223. }
  224. }
  225. // if there is not a file to create the code,
  226. // create it on the memory (faster, use file just for
  227. // debugging stuff)
  228. if(is_null($this->sCodeOut)) {
  229. $sOut = null;
  230. $aParm["output_format"]="memory";
  231. }else{
  232. $sOut = $this->sCodeOut;
  233. $aParm["output_format"]="file";
  234. }
  235. // XSLT processing
  236. $this->_aBench["code_start"] = time();
  237. $this->_oProc->setXML($this->sXML);
  238. $this->_oProc->setXSLT($this->sXSLT);
  239. $this->_oProc->setOutput($sOut);
  240. $this->_oProc->setParms($aParm);
  241. $sRst = $this->_oProc->run();
  242. $this->_aBench["code_end"] = time();
  243. $this->_aBench["code_eval_start"] = time();
  244. // if its created on the memory ...
  245. if(is_null($sOut))
  246. eval($sRst);
  247. else {
  248. // include the generated classes, if it was created
  249. if(!file_exists($sOut))
  250. $this->_oError->showMsg("NOCODE",array($sOut));
  251. require_once($sOut);
  252. }
  253. $this->_aBench["code_eval_end"] = time();
  254. // include the generated class
  255. $oReport = new $this->_sClassName;
  256. // set the database connection handle, if there is one
  257. $oReport->setDatabaseConnection($this->_oCon);
  258. $oReport->setInputFilters($this->_oInput);
  259. $oReport->setQuery($this->_oQuery);
  260. // run the generated class
  261. $this->_sXMLOutputFile = $oReport->run($this->_sXMLOutputFile,$this->_aEnv);
  262. $this->_aBench = array_merge($this->_aBench,$oReport->getBenchmarks());
  263. // check if the XML file exists, we need data!
  264. if(!file_exists($this->_sXMLOutputFile))
  265. $this->_oError->showMsg("NOXML",array($this->_sXMLOutputFile));
  266. /*
  267. Now we have a XML file with the report contents ... what to to with it???
  268. Let's call the output plugin!
  269. */
  270. // if there is no one, create a new default plugin
  271. $oOut = null;
  272. if(is_null($this->_oOutputPlugin)) {
  273. $oOut = $this->createOutputPlugin("default");
  274. $oOut->setInput ($this->_sXMLOutputFile);
  275. $oOut->setOutput($this->sOut);
  276. $oOut->setBody($this->_bBody);
  277. $this->setOutputPlugin($oOut);
  278. }else{
  279. $oOut = $this->_oOutputPlugin;
  280. $oOut->setBody($this->_bBody);
  281. $oOut->setInput($this->_sXMLOutputFile);
  282. if(!is_null($this->sOut))
  283. $oOut->setOutput($this->sOut);
  284. }
  285. // if need to save it
  286. if(!is_null($this->_sSaveTo))
  287. $this->save();
  288. // run
  289. $oOut->run();
  290. $this->_aBench["output_end"] = time();
  291. $this->_aBench["report_start"] = $iReportStart;
  292. $this->_aBench["report_end"] = time();
  293. // if needs to delete the XML file
  294. if($this->_bDeleteXML)
  295. unlink($this->sXML);
  296. return $this->_sXMLOutputFile;
  297. }
  298. /******************************************************************************
  299. * Return a (or all) benchmark index. *
  300. ******************************************************************************/
  301. function getBenchmark($sId=null){
  302. if(!$sId)
  303. return $this->_aBench;
  304. return $this->_aBench[$sId];
  305. }
  306. /******************************************************************************
  307. * Set the page size (overrides XML value) *
  308. ******************************************************************************/
  309. function setPageSize($iSize=50){
  310. $this->_iPageSize=$iSize;
  311. }
  312. function getPageSize(){
  313. return $this->_iPageSize;
  314. }
  315. /******************************************************************************
  316. * *
  317. * Set the XML file path *
  318. * @param String file path *
  319. * *
  320. ******************************************************************************/
  321. function setXML($sXML_) {
  322. if(!file_exists($sXML_))
  323. $this->_oError->showMsg("NOXMLSET",array($sXML_));
  324. $this->sXML = $sXML_;
  325. }
  326. /******************************************************************************
  327. * *
  328. * Returns the XML file path *
  329. * @return String file path *
  330. * *
  331. ******************************************************************************/
  332. function getXML() {
  333. return $this->sXML;
  334. }
  335. /******************************************************************************
  336. * *
  337. * Sets the XSLT file path *
  338. * @param String file path *
  339. * *
  340. ******************************************************************************/
  341. function setXSLT($sXSLT_) {
  342. if(!file_exists($sXSLT_))
  343. $this->_oError->showMsg("NOXSLTSET",array($sXSLT_));
  344. $this->sXSLT = $sXSLT_;
  345. }
  346. /******************************************************************************
  347. * *
  348. * Returns the XSLT file path *
  349. * @return String file path *
  350. * *
  351. ******************************************************************************/
  352. function getXSLT() {
  353. return $this->sXSLT;
  354. }
  355. /******************************************************************************
  356. * *
  357. * Set the user name *
  358. * @param String user name *
  359. * *
  360. ******************************************************************************/
  361. function setUser($sUser_) {
  362. $this->sUser = $sUser_;
  363. }
  364. /******************************************************************************
  365. * *
  366. * Returns the user name *
  367. * @return String user name *
  368. * *
  369. ******************************************************************************/
  370. function getUser() {
  371. return $this->sUser;
  372. }
  373. /******************************************************************************
  374. * *
  375. * Sets the password *
  376. * *
  377. ******************************************************************************/
  378. function setPassword($sPass_) {
  379. $this->sPass = $sPass_;
  380. }
  381. /******************************************************************************
  382. * *
  383. * Returns the password *
  384. * *
  385. ******************************************************************************/
  386. function getPassword() {
  387. return $this->sPass;
  388. }
  389. /******************************************************************************
  390. * *
  391. * Sets the database connection *
  392. * *
  393. ******************************************************************************/
  394. function setConnection($sCon_) {
  395. $this->sCon = $sCon_;
  396. }
  397. /******************************************************************************
  398. * *
  399. * Returns the password *
  400. * *
  401. ******************************************************************************/
  402. function getConnection() {
  403. return $this->sCon;
  404. }
  405. /******************************************************************************
  406. * *
  407. * Sets the database interface *
  408. * *
  409. ******************************************************************************/
  410. function setDatabaseInterface($sData_) {
  411. $this->sDataI = $sData_;
  412. }
  413. /******************************************************************************
  414. * *
  415. * Returns the database interface *
  416. * *
  417. ******************************************************************************/
  418. function getDatabaseInterface() {
  419. return $this->sDataI;
  420. }
  421. /******************************************************************************
  422. * *
  423. * Sets the SQL query *
  424. * *
  425. ******************************************************************************/
  426. function setSQL($sSQL_) {
  427. $this->sSQL = $sSQL_;
  428. }
  429. /******************************************************************************
  430. * *
  431. * Returns the SQL query *
  432. * *
  433. ******************************************************************************/
  434. function getSQL() {
  435. return $this->sSQL;
  436. }
  437. /******************************************************************************
  438. * *
  439. * Sets the parameters *
  440. * *
  441. ******************************************************************************/
  442. function setParameters($oParm_) {
  443. $this->_oParm = $oParm_;
  444. }
  445. /******************************************************************************
  446. * *
  447. * Returns the parameters *
  448. * *
  449. ******************************************************************************/
  450. function getParameters() {
  451. return $this->_oParm;
  452. }
  453. /******************************************************************************
  454. * *
  455. * Sets the database *
  456. * *
  457. ******************************************************************************/
  458. function setDatabase($sData_) {
  459. $this->sDatabase = $sData_;
  460. }
  461. /******************************************************************************
  462. * *
  463. * Returns the database *
  464. * *
  465. ******************************************************************************/
  466. function getDatabase() {
  467. return $this->sDatabase;
  468. }
  469. /******************************************************************************
  470. * *
  471. * Sets the code output file *
  472. * *
  473. ******************************************************************************/
  474. function setCodeOutput($sFile_) {
  475. $this->sCodeOut = $sFile_;
  476. }
  477. /******************************************************************************
  478. * *
  479. * Returns the database *
  480. * *
  481. ******************************************************************************/
  482. function getCodeOutput() {
  483. return $this->sCodeOut;
  484. }
  485. /******************************************************************************
  486. * *
  487. * Sets the output path *
  488. * *
  489. ******************************************************************************/
  490. function setOutput($sOut_) {
  491. $this->sOut = $sOut_;
  492. }
  493. /******************************************************************************
  494. * *
  495. * Returns output path *
  496. * *
  497. ******************************************************************************/
  498. function getOutput() {
  499. return $this->sOut;
  500. }
  501. /******************************************************************************
  502. * *
  503. * Sets if the report will generate debug info after it runs *
  504. * *
  505. ******************************************************************************/
  506. function setDebug($bDesc) {
  507. $this->bDebug = $bDesc;
  508. }
  509. /******************************************************************************
  510. * *
  511. * Returns if will debug *
  512. * *
  513. ******************************************************************************/
  514. function getDebug() {
  515. return $this->bDebug;
  516. }
  517. /******************************************************************************
  518. * *
  519. * Sets message to be shown when no data returns from the query *
  520. * @param String message *
  521. * *
  522. ******************************************************************************/
  523. function setNoDataMsg($sMsg_="") {
  524. $this->_sNoDataMsg=$sMsg_;
  525. }
  526. /******************************************************************************
  527. * *
  528. * Returns the no data message *
  529. * @return String message *
  530. * *
  531. ******************************************************************************/
  532. function getNoDataMsg() {
  533. return $this->_sNoDataMsg;
  534. }
  535. function setNoDataFunc($sFunc=""){
  536. $this->_sNoDataFunc=$sFunc;
  537. }
  538. function getNoDataFunc(){
  539. return $this->_sNoDataFunc;
  540. }
  541. /******************************************************************************
  542. * *
  543. * Create the output plugin *
  544. * @param name *
  545. * *
  546. ******************************************************************************/
  547. function createOutputPlugin($sName_) {
  548. $sFullPath = $this->_sPath."/output/$sName_/PHPReportOutput.php";
  549. // check if the required plugin exists
  550. if(!file_exists($sFullPath))
  551. $this->_oError->showMsg("NOPLUGIN",array($sName_,$sFullPath));
  552. include $sFullPath;
  553. $oOut = new PHPReportOutput($this->sXML);
  554. return $oOut;
  555. }
  556. function addInputPlugin($sName_,$oGroupDesc_,$sGroupKey_,$oOptions_=null){
  557. $sName = ucwords(strtolower($sName_));
  558. $sClass = "PHPReportInput$sName";
  559. $sFullPath = $this->_sPath."/input/PHPReportInput$sName.php";
  560. // check if the required plugin exists
  561. if(!file_exists($sFullPath))
  562. $this->_oError->showMsg("NOPLUGIN",array($sName_,$sFullPath));
  563. include $sFullPath;
  564. $oIn = new $sClass($oGroupDesc_,$sGroupKey_,$oOptions_);
  565. array_push($this->_oInput,$oIn);
  566. }
  567. /******************************************************************************
  568. * *
  569. * Output plugin for the final format *
  570. * @param plugin *
  571. * *
  572. ******************************************************************************/
  573. function setOutputPlugin($oPlugin_) {
  574. $this->_oOutputPlugin=$oPlugin_;
  575. }
  576. /******************************************************************************
  577. * *
  578. * Returns the output plugin *
  579. * @return plugin *
  580. * *
  581. ******************************************************************************/
  582. function getOutputPlugin() {
  583. return $this->_oOutputPlugin;
  584. }
  585. /******************************************************************************
  586. * *
  587. * Set the XML output/data file *
  588. * *
  589. ******************************************************************************/
  590. function setXMLOutputFile($sFile_=null){
  591. $this->_sXMLOutputFile=$sFile_;
  592. }
  593. /******************************************************************************
  594. * *
  595. * Returns the XML output/data file *
  596. * *
  597. ******************************************************************************/
  598. function getXMLOutputFile(){
  599. return $this->_sXMLOutputFile;
  600. }
  601. /******************************************************************************
  602. * *
  603. * File path to save the report *
  604. * Please remember to use a writable path! *
  605. * *
  606. ******************************************************************************/
  607. function saveTo($sFile_=null){
  608. if(is_null($sFile_))
  609. return;
  610. $this->_sSaveTo=$sFile_;
  611. }
  612. /******************************************************************************
  613. * *
  614. * Save report *
  615. * *
  616. ******************************************************************************/
  617. function save(){
  618. if(is_null($this->_sSaveTo))
  619. return;
  620. $sIn = $this->_sXMLOutputFile;
  621. $sMD5 = md5_file($sIn); // calculate the md5 checksum
  622. $sMD5 = str_pad($sMD5,50); // padding
  623. $sOut = "compress.zlib://".$this->_sSaveTo;
  624. $fIn = fopen($sIn,"r");
  625. $fOut = fopen($sOut,"w");
  626. // write the md5sum
  627. fwrite($fOut,$sMD5);
  628. while($sStr=fread($fIn,1024))
  629. fwrite($fOut,$sStr);
  630. fclose($fOut);
  631. fclose($fIn);
  632. }
  633. /******************************************************************************
  634. * *
  635. * Preview report *
  636. * *
  637. ******************************************************************************/
  638. function preview($sXML_=null){
  639. if(is_null($sXML_))
  640. return;
  641. if(!file_exists($sXML_)){
  642. print "<b>The file $sXML_ doesn't exists.</b><br>";
  643. return;
  644. }
  645. $sPath = getPHPReportsFilePath();
  646. $sXSLT = "$sPath/xslt/PHPReportPreview.xsl";
  647. // XSLT processing
  648. $this->_oProc->setXML($sXML_);
  649. $this->_oProc->setXSLT($sXSLT);
  650. print $this->_oProc->run();
  651. }
  652. /******************************************************************************
  653. * *
  654. * Put an object to the environment array. *
  655. * You can use this function to expose any kind of variable or class to your *
  656. * report (using <COL>$this->getEnv("id")</COL>). Note that for using objects *
  657. * returned by this function directly as *
  658. * <COL>$this->getEnv("id")->myFunction()</COL> *
  659. * you'll need PHP5. *
  660. * *
  661. ******************************************************************************/
  662. function putEnvObj($sKey_=null,$oObj_=null){
  663. if(is_null($sKey_) ||
  664. is_null($oObj_))
  665. return;
  666. $this->_aEnv[$sKey_]=$oObj_;
  667. }
  668. /******************************************************************************
  669. * *
  670. * Returns an object from the environment array. *
  671. * *
  672. ******************************************************************************/
  673. function getEnvObj($sKey_){
  674. return $this->_aEnv[$sKey_];
  675. }
  676. /******************************************************************************
  677. * *
  678. * Set the name of the class that will be created *
  679. * to run the report. *
  680. * To see where this name is used, please check xslt/PHPReport.xsl *
  681. * *
  682. ******************************************************************************/
  683. function setClassName($sClassName_="PHPReport"){
  684. $this->_sClassName=$sClassName_;
  685. }
  686. /******************************************************************************
  687. * *
  688. * Returns the name of the class that will be created *
  689. * to run the report. *
  690. * *
  691. ******************************************************************************/
  692. function getClassName(){
  693. return is_null($this->_sClassName)?"PHPReport":$this->_sClassName;
  694. }
  695. /******************************************************************************
  696. * *
  697. * Set the database connection handle *
  698. * *
  699. ******************************************************************************/
  700. function setDatabaseConnection(&$_oCon){
  701. $this->_oCon =& $_oCon;
  702. }
  703. /******************************************************************************
  704. * *
  705. * Here's the deal: if the user have a session opened, the language will be *
  706. * stored there. If more than one user is using the system with different *
  707. * languages, each one will see the specified language (no way to run two *
  708. * reports with different languages for each user). If no session is opened, *
  709. * the language value is already there on GLOBALS, so we can retrieve from *
  710. * there, but this will allow just one language for all the reports. *
  711. * *
  712. ******************************************************************************/
  713. function setLanguage($sLang_="default"){
  714. $this->_sLang=$sLang_;
  715. $_SESSION["phpReportsLanguage"] = $sLang_;
  716. $GLOBALS["phpReportsLanguage"] = $sLang_;
  717. }
  718. function getLanguage(){
  719. return $this->_sLang;
  720. }
  721. function setBody($b=true){
  722. $this->_bBody=$b;
  723. }
  724. function getBody(){
  725. return $this->_bBody;
  726. }
  727. }
  728. class PHPReportTemplateElement {
  729. var $_aAttrs;
  730. var $_aChildren;
  731. var $_sType;
  732. function PHPReportTemplateElement($sType=null,$aAttrs=null){
  733. $this->_sType = $sType;
  734. $this->_aChildren = array();
  735. $this->_aAttrs = $aAttrs ? $aAttrs : array();
  736. }
  737. function addAttr($sKey,$sValue){
  738. $this->_aAttrs[$sKey]=$sValue;
  739. }
  740. function addChild($oChild){
  741. array_push($this->_aChildren,$oChild);
  742. }
  743. function write(){
  744. if($this->_sType){
  745. $sStr = "<".$this->_sType;
  746. foreach($this->_aAttrs as $sKey=>$sValue)
  747. $sStr .= strtoupper($sKey)=="VALUE"?"":" $sKey=\"$sValue\" ";
  748. $sStr .= ">";
  749. }
  750. $sStr .= $this->_aAttrs["VALUE"];
  751. foreach($this->_aChildren as $oChild)
  752. $sStr .= $oChild->write();
  753. if($this->_sType)
  754. $sStr .= "</".$this->_sType.">";
  755. return $sStr;
  756. }
  757. }
  758. ?>