ImpromptuController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class ImpromptuController
  3. {
  4. private $app;
  5. private $catFile;
  6. private $report;
  7. public static function Run ($argv)
  8. {
  9. $imr = $argv[1];
  10. $export = $argv[2];
  11. if (count($argv) > 4 && $argv[3] != "") {
  12. $user = $argv[3];
  13. $pw = $argv[4];
  14. } else {
  15. $user = "fn";
  16. $pw = "flowqp";
  17. }
  18. $cat = (count($argv) > 5) ? $argv[5] : "";
  19. $impCtrl = new ImpromptuController();
  20. if (is_dir($imr)) {
  21. $imrPath = $imr;
  22. $newCat = strtolower($export);
  23. $impCtrl->OpenCatalog($newCat, str_rot13($user), str_rot13($pw));
  24. $impCtrl->RenameCatalog($imrPath, strtolower($cat));
  25. $impCtrl->Quit();
  26. return;
  27. }
  28. try {
  29. $impCtrl->OpenReport($imr, str_rot13($user), str_rot13($pw), $cat);
  30. $impCtrl->SaveAsIqd($imr);
  31. switch ($export) {
  32. case "csv":
  33. $impCtrl->SaveAsCsv($imr);
  34. break;
  35. case "iqd":
  36. break;
  37. case "ims":
  38. default:
  39. $impCtrl->SaveAsIms($imr);
  40. break;
  41. }
  42. $impCtrl->Quit();
  43. $imr = basename($imr);
  44. echo " {$imr} als {$export} exportiert\r\n";
  45. } catch (Exception $e) {
  46. echo "Fehler in {$imr}:\r\n";
  47. echo $e->getMessage() . "\r\n";
  48. }
  49. }
  50. public function __construct ()
  51. {
  52. $this->app = new COM("CognosImpromptu.Application");
  53. }
  54. public function OpenCatalog ($catFile, $user, $pw)
  55. {
  56. if (strpos($catFile, "\\") <= 0) {
  57. $catFile = realpath(dirname(__FILE__)."/../../../../System/OPTIMA/Catalogs/"). "\\" . $catFile;
  58. }
  59. $this->app->OpenCatalog($catFile, "Ersteller", "", $user, $pw);
  60. $this->catFile = $catFile;
  61. }
  62. public function RenameCatalog ($imrPath, $oldCatFile = "")
  63. {
  64. $dir = dir($imrPath);
  65. while ($file = $dir->read()) {
  66. if ($file == "." || $file == "..") {
  67. continue;
  68. }
  69. $imrFile = $imrPath . "\\" . $file;
  70. if (stripos($file, ".imr")) {
  71. $catFile = $this->openFileAndExtractCatInfo($imrFile);
  72. if ($oldCatFile != "" && strtolower($catFile) != $oldCatFile) {
  73. echo "skipped: {$file} - {$catFile}\r\n";
  74. continue;
  75. }
  76. try {
  77. $this->report = $this->app->OpenReport($imrFile);
  78. $this->report->Save();
  79. $this->SaveAsIqd($imrFile);
  80. $this->CloseReport();
  81. echo " {$file}\r\n";
  82. } catch (Exception $e) {
  83. echo "Could not open file '{$file}'\r\n";
  84. }
  85. }
  86. if (is_dir($imrFile)) {
  87. $this->RenameCatalog($imrFile, $oldCatFile);
  88. }
  89. }
  90. }
  91. public function OpenReport ($imrFile, $user, $pw, $catFile = "")
  92. {
  93. if ($catFile == "") {
  94. $catFile = $this->openFileAndExtractCatInfo($imrFile);
  95. }
  96. $this->OpenCatalog($catFile, $user, $pw);
  97. $this->report = $this->app->OpenReport($imrFile);
  98. }
  99. public function CloseReport ()
  100. {
  101. if (isset($this->report)) {
  102. $this->report->CloseReport();
  103. }
  104. $this->report = null;
  105. }
  106. public function SaveAsCsv ($imrFile, $csvFile = false)
  107. {
  108. if (!$csvFile) {
  109. $csvFile = substr($imrFile, 0, -4) . ".csv";
  110. }
  111. $this->report->RetrieveAll();
  112. $this->report->Export($csvFile, "x_ascii.flt");
  113. }
  114. public function SaveAsIqd ($imrFile, $iqdFile = false)
  115. {
  116. if (!$iqdFile) {
  117. $iqdFile = substr($imrFile, 0, -4) . ".iqd";
  118. }
  119. $this->report->Export($iqdFile, "x_iqd.flt");
  120. }
  121. public function SaveAsIms ($imrFile, $imsFile = false)
  122. {
  123. if (!$imsFile) {
  124. $imsFile = substr($imrFile, 0, -4) . ".ims";
  125. }
  126. $report = $this->app->OpenReport($imrFile);
  127. $report->ExportHotFile($imsFile);
  128. }
  129. public function Quit ()
  130. {
  131. $this->CloseReport();
  132. $this->app->Quit();
  133. }
  134. private function openFileAndExtractCatInfo ($fileName)
  135. {
  136. $fh = fopen($fileName, "r");
  137. if (!$fh) return "";
  138. for ($i = 0; $i < 1000; $i++) {
  139. $row = fgets($fh);
  140. if (preg_match("/catalogs.(\w*)\.cat/i", $row, $matches)) {
  141. fclose($fh);
  142. return strtolower($matches[1]) . ".cat";
  143. }
  144. }
  145. fclose($fh);
  146. return "";
  147. }
  148. }