PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/php/PHPReports/PHPReportMaker.php

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