ImpromptuController.php 3.6 KB

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