ImpromptuController.php 3.6 KB

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