PowerplayController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. require_once(dirname(__FILE__)."/AscParseController.php");
  3. require_once(dirname(__FILE__)."/GapsXmlInfoController.php");
  4. class PowerplayController
  5. {
  6. private $publishDir;
  7. private $tempDir;
  8. private $ppxFile;
  9. private $report;
  10. private $app;
  11. private $cubeFile;
  12. public $ReportName;
  13. public static function Run ($argv)
  14. {
  15. if (count($argv) <= 2) die();
  16. $action = $argv[1];
  17. switch ($action) {
  18. case "remove-queries":
  19. PowerplayController::RemoveQueries($argv);
  20. break;
  21. case "add-queries":
  22. case "untermengen":
  23. PowerplayController::AddQueries($argv);
  24. break;
  25. case "cube-name":
  26. PowerplayController::CubeName($argv[2]);
  27. break;
  28. case "rename":
  29. PowerplayController::Rename($argv);
  30. break;
  31. default:
  32. case "export":
  33. PowerplayController::Export($argv);
  34. break;
  35. }
  36. }
  37. public static function Export ($params)
  38. {
  39. $pprFile = $params[2];
  40. $export = (count($params) > 3) ? $params[3] : "jpg";
  41. $publishDir = (count($params) > 4) ? $params[4] : "";
  42. $exportFile = (count($params) > 5) ? $params[5] : "";
  43. $cubeName = (count($params) > 6) ? $params[6] : "";
  44. $pprCtrl = new PowerplayController($publishDir);
  45. if (!$pprCtrl->OpenReport($pprFile, $cubeName)) {
  46. echo "{$pprFile} kann nicht geoeffnet werden!\r\n";
  47. die();
  48. }
  49. switch ($export) {
  50. case "asc":
  51. $pprCtrl->SaveAsAsc();
  52. break;
  53. case "json":
  54. $ascFile = $pprCtrl->SaveAsAsc();
  55. $apCtrl = new AscParseController();
  56. $apCtrl->Run($ascFile, $exportFile);
  57. break;
  58. case "pdf":
  59. $pprCtrl->SaveAsPdf($exportFile);
  60. break;
  61. case "ppr":
  62. $pprCtrl->SaveAsPpr($exportFile);
  63. break;
  64. case "ppx":
  65. $pprCtrl->SaveAsPpx($exportFile);
  66. break;
  67. case "xls":
  68. $pprCtrl->SaveAsXls($exportFile);
  69. break;
  70. case "jpg":
  71. default:
  72. $pprCtrl->PublishImages();
  73. break;
  74. }
  75. $pprCtrl->CloseReport();
  76. $pprCtrl->Quit();
  77. }
  78. public static function RemoveQueries ($params)
  79. {
  80. $reportDir = realpath($params[2]);
  81. $suffix = $params[3] . ".";
  82. $dimensions = explode(",", $params[4]);
  83. $pprCtrl = new PowerplayController($reportDir);
  84. $dir = dir($reportDir);
  85. while ($file = $dir->read()) {
  86. if (stripos($file, $suffix)) {
  87. $fullFilename = $reportDir . "\\" . $file;
  88. if ($pprCtrl->OpenReport($fullFilename)) {
  89. $pprCtrl->RemoveReportQueryItems($dimensions);
  90. $pprCtrl->SaveAsPpr(str_replace(".ppx", ".ppr", str_replace($suffix, ".", $fullFilename)));
  91. $pprCtrl->CloseReport();
  92. echo " {$file}: Untermengen entfernt.\r\n";
  93. }
  94. }
  95. }
  96. $pprCtrl->Quit();
  97. }
  98. public static function AddQueries ($params)
  99. {
  100. $pprCtrl = new PowerplayController("");
  101. $pprCtrl->OpenReport($params[2]);
  102. $pprCtrl->AddReportQueryItems("AH-Gruppe", "Serviceberater", "Mainz - Kastel");
  103. $pprCtrl->CloseReport();
  104. $pprCtrl->Quit();
  105. }
  106. public static function CubeName ($file)
  107. {
  108. $pprCtrl = new PowerplayController(dirname(__FILE__));
  109. $pprCtrl->OpenReport($file);
  110. $cubeFile = $pprCtrl->GetCubeFilename();
  111. $pprCtrl->CloseReport();
  112. $pprCtrl->Quit();
  113. return $cubeFile;
  114. }
  115. public static function Rename ($params)
  116. {
  117. if (count($params) < 5) {
  118. echo "Aufruf: powerplay.bat rename [Report-Pfad] [Cubename] [Neuer Pfad und Name]\r\n";
  119. return;
  120. }
  121. $reportPath = realpath($params[2]);
  122. $cubeName = $params[3];
  123. $newPathAndName = realpath($params[4]);
  124. if (substr($cubeName, -4) == ".mdc") {
  125. $cubeName = substr($cubeName, 0, -4);
  126. }
  127. $pprCtrl = new PowerplayController($reportPath);
  128. $gxiCtrl = new GapsXmlInfoController();
  129. $reports = $gxiCtrl->getExistingReportFiles("1", $reportPath . "\\", false, $cubeName);
  130. foreach ($reports as $report => $cube) {
  131. $reportFile = $reportPath . "\\" . $report . ".ppr";
  132. if (!file_exists($reportFile)) {
  133. $reportFile = $reportPath . "\\" . $report . ".ppx";
  134. }
  135. if (!file_exists($reportFile)) {
  136. continue;
  137. }
  138. if ($pprCtrl->OpenReport($reportFile, $newPathAndName)) {
  139. $pprCtrl->Save();
  140. $pprCtrl->CloseReport();
  141. }
  142. }
  143. $pprCtrl->Quit();
  144. }
  145. public function __construct ($publishDir)
  146. {
  147. $this->publishDir = $publishDir;
  148. $this->tempDir = $this->publishDir ."\\temp\\";
  149. @mkdir($this->publishDir, 0777, true);
  150. @mkdir($this->tempDir);
  151. $this->app = new COM("CognosPowerplay.Application");
  152. }
  153. public function OpenReport ($ppxFile, $cubeName = "")
  154. {
  155. $this->ppxFile = $ppxFile;
  156. if (preg_match('/[\\\\\\/]([^\\\\\\/]*)\.pp[rx]$/i', $this->ppxFile, $match)) {
  157. $this->ReportName = $match[1];
  158. } else {
  159. $this->ReportName = substr($this->ppxFile, 0, -4);
  160. }
  161. if (preg_match("/_graf$/i", $this->ReportName)) {
  162. $this->ReportName = substr($this->ReportName, 0, -5);
  163. }
  164. if (!file_exists($this->ppxFile)) {
  165. return false;
  166. }
  167. try {
  168. if ($cubeName != "") {
  169. $this->report = new COM("CognosPowerplay.Report");
  170. $this->report->Open($this->ppxFile, $cubeName);
  171. } else {
  172. $this->report = $this->app->Reports->Open($this->ppxFile);
  173. }
  174. }
  175. catch (Exception $e) {
  176. echo "Could not open file '" . $this->ppxFile . "'\r\n";
  177. echo $e->getMessage() . "\r\n";
  178. return false;
  179. }
  180. return true;
  181. }
  182. public function CloseReport ()
  183. {
  184. $this->report->Close();
  185. }
  186. public function Quit ()
  187. {
  188. $this->app->Quit();
  189. }
  190. public function RemoveReportQueryItems ($dimensions)
  191. {
  192. $items = $this->report->ReportQueries->Count;
  193. $i = 1;
  194. while ($i <= $items) {
  195. $dim = $this->report->ReportQueries->Item($i)->Dimension;
  196. if (in_array($dim, $dimensions)) {
  197. $this->report->ReportQueries->Item($i)->Remove(false);
  198. $items -= 1;
  199. } else {
  200. $i += 1;
  201. }
  202. }
  203. }
  204. public function AddReportQueryItems ($dimension, $level, $category)
  205. {
  206. $parent = $this->report->ReportQueries->Add(2);
  207. $parent->Category($category);
  208. $parent->LowestLevel = false;
  209. $parent->Execute();
  210. $query = $this->report->ReportQueries->Add(3);
  211. $query->Name = $level . " " . $category;
  212. $query->Dimension = $dimension;
  213. $query->Level($level);
  214. $query->Find($parent);
  215. $query->Execute();
  216. $query->AddToReport(1, 1, 3);
  217. }
  218. public function GetCubeFilename ()
  219. {
  220. $this->cubeFile = $this->report->CubeName;
  221. return $this->cubeFile;
  222. }
  223. public function GetCellValue ($row, $col)
  224. {
  225. try {
  226. return $this->report->CellValue($row, $col);
  227. }
  228. catch (Exception $e) {
  229. echo $e->getMessage() . "\r\n";
  230. return 0;
  231. }
  232. }
  233. public function SaveAsPdf ($filename = "", $layers = null)
  234. {
  235. if ($filename == "") {
  236. $filename = $this->ReportName;
  237. }
  238. $layerCount = $this->report->Layers->Count;
  239. if ($layers == null) {
  240. $layers = range(0, $layerCount);
  241. }
  242. foreach ($layers as $layer) {
  243. if ($layer > $layerCount) {
  244. continue;
  245. }
  246. $pdfFile = "{$this->publishDir}\\{$filename}_{$layer}.pdf";
  247. $pdf = $this->report->PDFFile($pdfFile, true);
  248. if ($layer == 0) {
  249. $pdf->SaveEntireReport = true;
  250. $pdf->SaveAllCharts = true;
  251. $pdf->IncludeLegend = false;
  252. $pdf->AxisOnAllPages = true;
  253. $pdf->ChartTitleOnAllPages = false;
  254. } else {
  255. $this->report->Layers->Item($layer)->Activate();
  256. if ($this->report->Rows->Count == 0) {
  257. continue;
  258. }
  259. $pdf->SaveEntireReport = false;
  260. $pdf->SaveAllCharts = false;
  261. $pdf->IncludeLegend = true;
  262. $pdf->ChartTitleOnAllPages = true;
  263. $pdf->AxisOnAllPages = true;
  264. $pdf->SetListOfLayersToSave($this->report->Layers->Subset($layer, $layer));
  265. $pdf->SetListOfRowsToSave($this->report->Rows);
  266. }
  267. @unlink($pdfFile);
  268. $pdf->Save();
  269. }
  270. }
  271. public function SaveAsXls ($filename = "")
  272. {
  273. if ($filename == "") {
  274. $filename = "{$this->publishDir}\\{$this->ReportName}_0.xls";
  275. }
  276. @unlink($filename);
  277. $this->report->SaveAs($filename, 4);
  278. return $filename;
  279. }
  280. public function SaveAsPpx ($filename = "")
  281. {
  282. if ($filename == "") {
  283. $filename = substr($this->ppxFile, 0, -4) . ".ppx";
  284. }
  285. @unlink($filename);
  286. $this->report->SaveAs($filename, 5);
  287. return $filename;
  288. }
  289. public function SaveAsAsc ($filename = "")
  290. {
  291. if ($filename == "") {
  292. $filename = "{$this->publishDir}\\{$this->ReportName}.asc";
  293. }
  294. @unlink($filename);
  295. $this->report->SaveAs($filename, 3);
  296. return $filename;
  297. }
  298. public function Save ()
  299. {
  300. if (substr($this->ppxFile, -4) == ".ppr") {
  301. $this->SaveAsPpr();
  302. } else {
  303. $this->SaveAsPpx();
  304. }
  305. }
  306. public function SaveAsPpr ($filename = "")
  307. {
  308. if ($filename == "") {
  309. $filename = substr($this->ppxFile, 0, -4) . ".ppr";
  310. }
  311. @unlink($filename);
  312. $this->report->SaveAs($filename, 1);
  313. return $filename;
  314. }
  315. public function PublishImages ()
  316. {
  317. $this->clearTempDir();
  318. $this->report->Visible = true;
  319. $this->report->Publish($this->tempDir, false, true, true);
  320. $files = glob($this->tempDir . "*.jpg");
  321. foreach ($files as $i => $filename) {
  322. $layer = $i + 1;
  323. $renameTo = "{$this->publishDir}\\{$this->ReportName}_{$layer}.jpg";
  324. @unlink($renameTo);
  325. rename($filename, $renameTo);
  326. $imageObject = imagecreatefromjpeg($renameTo);
  327. imagegif($imageObject, substr($renameTo, 0, -4) . '.gif');
  328. }
  329. }
  330. private function clearTempDir ()
  331. {
  332. shell_exec("del /F /Q " . $this->tempDir . "*.*");
  333. }
  334. }