GapsXmlInfoController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. class GapsXmlInfoController
  3. {
  4. private $xmlConfigPath;
  5. private $reportFiles;
  6. private $reportPaths;
  7. public static function Run ($argv)
  8. {
  9. echo "* Erstelle Info-Dateien fuer Portal und Versand\r\n";
  10. $gapsXmlCtrl = new GapsXmlInfoController($argv[1]);
  11. $gapsXmlCtrl->execute();
  12. }
  13. public function __construct ($path = "")
  14. {
  15. $this->xmlConfigPath = $path;
  16. @mkdir("{$this->xmlConfigPath}\\info");
  17. $this->reportFiles = array();
  18. $this->reportPaths = array();
  19. ini_set("default_charset", "iso-8859-1");
  20. }
  21. public function execute ()
  22. {
  23. $this->applyToAllXmlFiles("getExistingReportFiles");
  24. $result = array();
  25. $result['reports'] = $this->applyToAllXmlFiles("reports");
  26. $this->writeCsvFile($this->getFileName("reports"), $result['reports']);
  27. $result['versand'] = $this->applyToAllXmlFiles("versand");
  28. $this->writeCsvFile($this->getFileName("versand"), $result['versand']);
  29. $result['scorecards'] = $this->applyToAllXmlFiles("scorecards");
  30. $this->writeCsvFile($this->getFileName("scorecards"), $result['scorecards']);
  31. $result['ppr'] = $this->applyToAllXmlFiles("ppr");
  32. $result['ppr'] = array_values(array_merge($result['ppr'], $this->getUnusedReportFiles($result['ppr'])));
  33. $this->writeCsvFile($this->getFileName("ppr"), $result['ppr']);
  34. $this->writeJsonFile($this->getFileName("info", ".json"), $result);
  35. }
  36. private function applyToAllXmlFiles ($callback)
  37. {
  38. $configDir = dir($this->xmlConfigPath);
  39. $result = array();
  40. while ($xmlFile = $configDir->read()) {
  41. if (!preg_match('/\.xml$/i', $xmlFile) || $xmlFile == "auth.xml") {
  42. continue;
  43. }
  44. $doc = simplexml_load_file($this->xmlConfigPath . "\\" . $xmlFile);
  45. $add = @call_user_func(array($this, $callback), $xmlFile, $doc);
  46. $result = array_merge($result, $add);
  47. }
  48. return $result;
  49. }
  50. private function reports ($fileName, $doc)
  51. {
  52. $result = array();
  53. $subPath = $this->getReportsSubDir($doc);
  54. foreach ($doc->Publishes->Publish as $publish) {
  55. /** @var SimpleXMLElement $publish */
  56. $attr = $publish->attributes();
  57. foreach ($publish->Images->Image as $image) {
  58. $rep = strtolower(trim($subPath . (string)$image->Report));
  59. $r = array();
  60. $r['Datei'] = $fileName;
  61. $r['Benutzer'] = substr((string)$image->Directory, 0, -1);
  62. $r['Struktur'] = (string)$attr->Name;
  63. $r['Report'] = (string)$image->Report;
  64. $r['Name'] = str_replace(";", ",", (string)$image->Name);
  65. $r['GIF'] = (string)$image->GIFGenerate;
  66. $r['Schicht'] = (string)$image->ImageLayer;
  67. $r['PDF'] = (string)$image->PDFGenerate;
  68. $r['PDF-Schicht'] = (string)$image->PDFImageLayer;
  69. $r['XLS'] = (string)$image->XLSGenerate;
  70. $r['Versand'] = (string)$image->SendEmail;
  71. $r['Empfaenger'] = str_replace(";", ",", (string)$image->Email);
  72. $r['Cube'] = (isset($this->reportFiles[$rep])) ? $this->reportFiles[$rep] : "";
  73. $result[] = $r;
  74. }
  75. }
  76. return $result;
  77. }
  78. private function versand ($fileName, $doc)
  79. {
  80. $result = array();
  81. $subPath = $this->getReportsSubDir($doc);
  82. foreach ($doc->Publishes->Publish as $publish) {
  83. /** @var SimpleXMLElement $publish */
  84. $attr = $publish->attributes();
  85. foreach ($publish->Images->Image as $image) {
  86. if (stripos((string)$image->Report, "_graf")) {
  87. continue;
  88. }
  89. $empfaenger = explode(";", (string)$image->Email);
  90. if ($empfaenger[0] == "") {
  91. continue;
  92. }
  93. $rep = strtolower(trim($subPath . (string)$image->Report));
  94. foreach ($empfaenger as $eintrag) {
  95. $e = array();
  96. $e['Datei'] = $fileName;
  97. $e['Benutzer'] = substr((string)$image->Directory, 0, -1);
  98. $e['Struktur'] = (string)$attr->Name;
  99. $e['Empfaenger'] = strtolower($eintrag);
  100. $e['Report'] = (string)$image->Report;
  101. $e['Name'] = str_replace(";", ",", (string)$image->Name);
  102. $e['Versand'] = (string)$image->SendEmail;
  103. $e['PDF'] = (string)$image->PDFGenerate;
  104. $e['PDF-Schicht'] = (string)$image->PDFImageLayer;
  105. $e['XLS'] = (string)$image->XLSGenerate;
  106. $e['Cube'] = (isset($this->reportFiles[$rep])) ? $this->reportFiles[$rep] : "";
  107. $result[] = $e;
  108. }
  109. }
  110. }
  111. return $result;
  112. }
  113. private function scorecards ($fileName, $doc)
  114. {
  115. $result = array();
  116. foreach ($doc->Scorecards->Scorecard as $publish) {
  117. /** @var SimpleXMLElement $publish */
  118. $attr = $publish->attributes();
  119. foreach ($publish->Reports->Report as $sc) {
  120. $r = array();
  121. $r['Datei'] = $fileName;
  122. $r['Benutzer'] = substr((string)$attr->Directory, 0, -1);
  123. $r['Struktur'] = (string)$attr->Name;
  124. $r['Report'] = (string)$sc->ReportFile;
  125. $r['Name'] = str_replace(";", ",", (string)$sc->Name);
  126. $r['Zeile'] = (string)$sc->RowData;
  127. $r['Spalte'] = (string)$sc->ColData;
  128. $r['Aktiv'] = (string)$sc->Active;
  129. $result[] = $r;
  130. }
  131. }
  132. return $result;
  133. }
  134. public function getExistingReportFiles ($fileName, $doc, $getSubPath = true, $cubefilter = false)
  135. {
  136. $pprPath = (is_dir($doc)) ? $doc : (string)$doc->General->ReportPath;
  137. $this->reportPaths[$pprPath][$fileName] = 1;
  138. if (count($this->reportPaths[$pprPath]) > 1) {
  139. return array();
  140. }
  141. if (!is_dir($pprPath)) return array();
  142. $pprDir = dir($pprPath);
  143. $subPath = ($getSubPath) ? $this->getReportsSubDir($doc) : "";
  144. while ($file = $pprDir->read()) {
  145. if (preg_match('/(.*)\.pp[rx]$/i', $file, $matches)) {
  146. $cube = GapsXmlInfoController::openFileAndExtractCubeInfo($pprPath.$file);
  147. $rep = strtolower($subPath.$matches[1]);
  148. if (!$cubefilter || $cubefilter == $cube) {
  149. $this->reportFiles[$rep] = $cube;
  150. }
  151. }
  152. }
  153. return $this->reportFiles;
  154. }
  155. public static function openFileAndExtractCubeInfo ($fileName)
  156. {
  157. $fh = fopen($fileName, "r");
  158. for ($i = 0; $i < 200; $i++) {
  159. $row = fread($fh, 300);
  160. if (preg_match("/cube_out.([\w\s\d]*)\.mdc/i", $row, $matches)) {
  161. fclose($fh);
  162. return strtolower($matches[1]);
  163. }
  164. }
  165. fclose($fh);
  166. return "";
  167. }
  168. private function ppr ($fileName, $doc)
  169. {
  170. $subPath = $this->getReportsSubDir($doc);
  171. $linksArray = array();
  172. $result = array();
  173. foreach ($doc->Publishes->Publish as $publish) {
  174. /** @var SimpleXMLElement $publish */
  175. $attr = $publish->attributes();
  176. foreach ($publish->Images->Image as $image) {
  177. $rep = strtolower(trim($subPath . (string)$image->Report));
  178. $layer = 0 + (int)$image->ImageLayer + (int)$image->PDFImageLayer;
  179. if (isset($linksArray[$rep])) {
  180. $linksArray[$rep][$layer] = 1;
  181. continue;
  182. }
  183. $linksArray[$rep][$layer] = 1;
  184. $r = array();
  185. $r['Datei'] = $fileName;
  186. $r['Report'] = $rep;
  187. $r['Name'] = trim(str_replace(";", ",", (string)$image->Name));
  188. $r['PPR'] = (isset($this->reportFiles[$rep])) ? "" : "fehlt";
  189. $r['Cube'] = (isset($this->reportFiles[$rep])) ? $this->reportFiles[$rep] : "";
  190. $r['Typ'] = $this->getImageType($image);
  191. $r['Schichten'] = "";
  192. $result[$rep] = $r;
  193. }
  194. }
  195. foreach ($doc->Scorecards->Scorecard as $publish) {
  196. /** @var SimpleXMLElement $publish */
  197. $attr = $publish->attributes();
  198. foreach ($publish->Reports->Report as $sc) {
  199. $rep = strtolower(trim($subPath . (string)$sc->ReportFile));
  200. if (isset($linksArray[$rep])) {
  201. continue;
  202. }
  203. $linksArray[$rep][0] = 1;
  204. $r = array();
  205. $r['Datei'] = $fileName;
  206. $r['Report'] = $rep;
  207. $r['Name'] = trim(str_replace(";", ",", (string)$sc->Name));
  208. $r['PPR'] = (isset($this->reportFiles[$rep])) ? "" : "fehlt";
  209. $r['Cube'] = (isset($this->reportFiles[$rep])) ? $this->reportFiles[$rep] : "";
  210. $r['Typ'] = "SC";
  211. $r['Schichten'] = "";
  212. $result[$rep] = $r;
  213. }
  214. }
  215. foreach ($linksArray as $rep => $layer) {
  216. $lay = array_keys($layer);
  217. sort($lay);
  218. $result[$rep]['Schichten'] = "[" . implode(",", $lay) . "]";
  219. }
  220. return $result;
  221. }
  222. private function getUnusedReportFiles ($linksArray)
  223. {
  224. $result = array();
  225. $rest = array_diff(array_keys($this->reportFiles), array_keys($linksArray));
  226. foreach ($rest as $rep) {
  227. $r = array();
  228. $r['Datei'] = "nicht verwendet";
  229. $r['Report'] = $rep;
  230. $r['Name'] = "";
  231. $r['PPR'] = "";
  232. $r['Cube'] = $this->reportFiles[$rep];
  233. $r['Typ'] = "";
  234. $r['Schichten'] = "";
  235. $result[$rep] = $r;
  236. }
  237. return $result;
  238. }
  239. private function writeCsvFile ($fileName, $content)
  240. {
  241. if (count($content) > 0) {
  242. $c = array();
  243. $c[] = implode(";", array_keys(current($content)));
  244. foreach($content as $row) {
  245. $c[] = implode(";", $row);
  246. }
  247. file_put_contents($fileName, mb_convert_encoding(implode("\r\n", $c), "ISO-8859-1", "UTF-8"));
  248. echo "* Schreibe Datei '{$fileName}'\r\n";
  249. }
  250. }
  251. private function writeJsonFile ($fileName, $content)
  252. {
  253. file_put_contents($fileName, json_encode($content));
  254. echo "* Schreibe Datei '{$fileName}'\r\n";
  255. }
  256. private function getFileName ($fileName, $extension = ".csv")
  257. {
  258. $fileName = "{$this->xmlConfigPath}\\info\\{$fileName}{$extension}";
  259. return $fileName;
  260. }
  261. private function getReportsSubDir ($doc)
  262. {
  263. $pprPath = (string)$doc->General->ReportPath;
  264. $parentDir = dirname($pprPath);
  265. $subDir = str_replace($parentDir . "\\", "", $pprPath);
  266. return $subDir;
  267. }
  268. private function getImageType ($image)
  269. {
  270. if ((string)$image->PDFGenerate == "J") return "PDF";
  271. if ((string)$image->GIFGenerate == "J") return "GIF";
  272. if ((string)$image->XLSGenerate == "J") return "XLS";
  273. if ((string)$image->Active == "J") return "PPR";
  274. return "?";
  275. }
  276. }
  277. ?>