123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- class ImpromptuController
- {
- private $app;
- private $catFile;
- private $report;
- public static function Run ($argv)
- {
- $imr = $argv[1];
- $export = $argv[2];
- if (count($argv) > 4 && $argv[3] != "") {
- $user = $argv[3];
- $pw = $argv[4];
- } else {
- $user = "fn";
- $pw = "flowqp";
- }
- $cat = (count($argv) > 5) ? $argv[5] : "";
- $impCtrl = new ImpromptuController();
- if (is_dir($imr)) {
- $imrPath = $imr;
- $newCat = strtolower($export);
- $impCtrl->OpenCatalog($newCat, str_rot13($user), str_rot13($pw));
- $impCtrl->RenameCatalog($imrPath, strtolower($cat));
- $impCtrl->Quit();
- return;
- }
- try {
- $impCtrl->OpenReport($imr, str_rot13($user), str_rot13($pw), $cat);
- $impCtrl->SaveAsIqd($imr);
-
- switch ($export) {
- case "csv":
- $impCtrl->SaveAsCsv($imr);
- break;
- case "iqd":
- break;
- case "ims":
- default:
- $impCtrl->SaveAsIms($imr);
- break;
- }
- $impCtrl->Quit();
- $imr = basename($imr);
- echo " {$imr} als {$export} exportiert\r\n";
- } catch (Exception $e) {
- echo "Fehler in {$imr}:\r\n";
- echo $e->getMessage() . "\r\n";
- }
-
- }
- public function __construct ()
- {
- $this->app = new COM("CognosImpromptu.Application");
- }
- public function OpenCatalog ($catFile, $user, $pw)
- {
- if (strpos($catFile, "\\") <= 0) {
- $catFile = realpath(dirname(__FILE__)."/../../../../System/OPTIMA/Catalogs/"). "\\" . $catFile;
- }
- $this->app->OpenCatalog($catFile, "Ersteller", "", $user, $pw);
- $this->catFile = $catFile;
- }
- public function RenameCatalog ($imrPath, $oldCatFile = "")
- {
- $dir = dir($imrPath);
- while ($file = $dir->read()) {
- if ($file == "." || $file == "..") {
- continue;
- }
- $imrFile = $imrPath . "\\" . $file;
- if (stripos($file, ".imr")) {
- $catFile = $this->openFileAndExtractCatInfo($imrFile);
- if ($oldCatFile != "" && strtolower($catFile) != $oldCatFile) {
- echo "skipped: {$file} - {$catFile}\r\n";
- continue;
- }
- try {
- $this->report = $this->app->OpenReport($imrFile);
- $this->report->Save();
- $this->SaveAsIqd($imrFile);
- $this->CloseReport();
- echo " {$file}\r\n";
- } catch (Exception $e) {
- echo "Could not open file '{$file}'\r\n";
- }
- }
- if (is_dir($imrFile)) {
- $this->RenameCatalog($imrFile, $oldCatFile);
- }
- }
- }
- public function OpenReport ($imrFile, $user, $pw, $catFile = "")
- {
- if ($catFile == "") {
- $catFile = $this->openFileAndExtractCatInfo($imrFile);
- }
- $this->OpenCatalog($catFile, $user, $pw);
- $this->report = $this->app->OpenReport($imrFile);
- }
-
- public function CloseReport ()
- {
- if (isset($this->report)) {
- $this->report->CloseReport();
- }
- $this->report = null;
- }
-
- public function SaveAsCsv ($imrFile, $csvFile = false)
- {
- if (!$csvFile) {
- $csvFile = substr($imrFile, 0, -4) . ".csv";
- }
- $this->report->RetrieveAll();
- $this->report->Export($csvFile, "x_ascii.flt");
- }
- public function SaveAsIqd ($imrFile, $iqdFile = false)
- {
- if (!$iqdFile) {
- $iqdFile = substr($imrFile, 0, -4) . ".iqd";
- }
- $this->report->Export($iqdFile, "x_iqd.flt");
- }
- public function SaveAsIms ($imrFile, $imsFile = false)
- {
- if (!$imsFile) {
- $imsFile = substr($imrFile, 0, -4) . ".ims";
- }
- $report = $this->app->OpenReport($imrFile);
- $report->ExportHotFile($imsFile);
- }
-
- public function Quit ()
- {
- $this->CloseReport();
- $this->app->Quit();
- }
-
- private function openFileAndExtractCatInfo ($fileName)
- {
- $fh = fopen($fileName, "r");
- if (!$fh) return "";
-
- for ($i = 0; $i < 1000; $i++) {
- $row = fgets($fh);
- if (preg_match("/catalogs.(\w*)\.cat/i", $row, $matches)) {
- fclose($fh);
- return strtolower($matches[1]) . ".cat";
- }
- }
- fclose($fh);
- return "";
- }
- }
|