ImpromptuController.php 3.7 KB

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