<?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();
		} else {
			$impCtrl->OpenReport($imr, str_rot13($user), str_rot13($pw), $cat);

			switch ($export) {
				case "csv":
					$impCtrl->SaveAsCsv($imr);
					break;
				case "iqd":
					$impCtrl->SaveAsIqd($imr);
					break;
				case "ims":
				default:
					$impCtrl->SaveAsIms($imr);
					break;
			}
			$impCtrl->Quit();
			$imr = basename($imr);
			echo "  {$imr} als {$export} exportiert\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/LOCOSOFT/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 "";
	}
} 