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