GapsXmlController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. require_once(dirname(__FILE__).'/GapsXmlInfoController.php');
  3. require_once(dirname(__FILE__).'/PowerplayController.php');
  4. require_once(dirname(__FILE__).'/MailController.php');
  5. require_once(dirname(__FILE__).'/../models/Logfile.php');
  6. class GapsXmlController
  7. {
  8. /** @var string */
  9. private $today;
  10. private $generateTypes;
  11. private $flagSeperateMail = false;
  12. private $flagSendMail = true;
  13. private $flagGenImages = true;
  14. /** @var string */
  15. private $publishImagePath;
  16. /** @var string */
  17. private $reportPath;
  18. /** @var PowerplayController */
  19. private $pprCtrl;
  20. private $cubePath;
  21. private $mailController;
  22. public static function Run ($argv)
  23. {
  24. $config = parse_ini_file(dirname(__FILE__) . "/../../../GAPS.ini");
  25. if ($argv[1] == "generate") {
  26. GapsXmlController::Generate($config, $argv);
  27. } else if ($argv[1] == "cube-pfad") {
  28. GapsXmlController::UpdateCubePath($config, $argv[2]);
  29. } else {
  30. GapsXmlController::Duration($config, $argv);
  31. }
  32. }
  33. public static function Generate ($config, $argv)
  34. {
  35. if (count($argv) < 3) {
  36. die("Aufruf: php Tools\\generate.php generate GAPS.xml [Benutzer,Mails] [Cubes]");
  37. }
  38. $gapsXmlCtrl = new GapsXmlController($config);
  39. $xml = $argv[2];
  40. if (count($argv) > 3 && ($argv[3] == "mail" || $argv[3] == "pdf")) {
  41. $gapsXmlCtrl->setAction($argv[3]);
  42. array_shift($argv);
  43. }
  44. $userFilter = (count($argv) > 3 && $argv[3] != "") ? explode(",", strtolower($argv[3])) : array("*");
  45. $cubeFilter = (count($argv) > 4 && $argv[4] != "") ? explode(",", strtolower($argv[4])) : array("*");
  46. $gapsXmlCtrl->generateAll($xml, $userFilter, $cubeFilter);
  47. }
  48. public static function Duration ($config, $argv)
  49. {
  50. if (count($argv) < 3) {
  51. die("Aufruf: php Tools\\generate.php [duration|summary] GAPS.xml Logdatei");
  52. }
  53. $action = $argv[1];
  54. $newStructure = (substr($action, -1) != "2");
  55. $xml = $argv[2];
  56. $xmlInfo = pathinfo($xml);
  57. $xmlBasename = $xmlInfo['basename'];
  58. $errorLog = (count($argv) > 3) ? $argv[3] : realpath(dirname(__FILE__) . "\\..\\..\\logs\\" . $xmlBasename . ".log");
  59. $gapsXmlCtrl = new GapsXmlController($config);
  60. $result = $gapsXmlCtrl->getStatistics($xml, $newStructure);
  61. switch ($action) {
  62. case "duration":
  63. case "duration2":
  64. $duration = $result['summary']->Categories;
  65. $estimate = date("H:i", strtotime("now") + $duration * 60);
  66. echo "Geschaetzte Dauer: {$duration} Minuten\r\n";
  67. echo "Geschaetztes Ende: {$estimate}\r\n";
  68. break;
  69. case "summary":
  70. case "summary2":
  71. default:
  72. file_put_contents($errorLog, json_encode($result));
  73. if (count($result['errors']) > 0) {
  74. echo "Fehlerhafte Dateien (". count($result['errors']) . "):\r\n";
  75. foreach ($result['errors'] as $e) {
  76. echo $e . "\r\n";
  77. }
  78. echo "\r\nDetails: " . $errorLog ."\r\n";
  79. }
  80. break;
  81. }
  82. }
  83. public static function Summary ($config, $argv) {
  84. GapsXmlController::Duration($config, $argv);
  85. }
  86. public static function UpdateCubePath ($config, $xmlFile) {
  87. $gapsXmlCtrl = new GapsXmlController($config);
  88. $gapsXmlCtrl->changeCubePath($xmlFile);
  89. return;
  90. }
  91. public function __construct ($config)
  92. {
  93. $this->today = strtotime("now") - 12 * 60 * 60;
  94. $this->generateTypes = array(".gif" => "GIFGenerate", ".pdf" => "PDFGenerate", ".xls" => "XLSGenerate"); //".jpg" => "JPGGenerate",
  95. $this->flagSeperateMail = (isset($config['VERSAND_SEPARAT']) && $config['VERSAND_SEPARAT'] != "N");
  96. $this->mailController = new MailController($config);
  97. }
  98. public function setAction ($flag) {
  99. $this->flagGenImages = ($flag != "mail");
  100. $this->flagSendMail = ($flag != "pdf");
  101. }
  102. public function getStatistics ($xml, $newFile = true)
  103. {
  104. $xmlInfo = pathinfo($xml);
  105. $xmlBasename = str_replace(".xml", "", $xmlInfo['basename']);
  106. $doc = simplexml_load_string(file_get_contents($xml));
  107. $gxiCtrl = new GapsXmlInfoController();
  108. $reports = $gxiCtrl->getExistingReportFiles($xmlBasename, $doc);
  109. date_default_timezone_set('UTC');
  110. $startTime = 0;
  111. $time = 0;
  112. $duration = "00:01:00";
  113. $result = array('errors' => array(), 'sources' => array(), 'summary' => null);
  114. $publishImagePath = (string)$doc->General->PublishImagePath;
  115. $reportPath = (string)$doc->General->ReportPath;
  116. foreach ($doc->Publishes->Publish as $publish) {
  117. /** @var SimpleXMLElement $publish */
  118. $attr = $publish->attributes();
  119. foreach ($publish->Images->Image as $image) {
  120. if ($newFile) {
  121. $dat = $publishImagePath . $xmlBasename . "\\" . (string)$image->Report . "_";
  122. } else {
  123. $dat = $publishImagePath . (string)$image->Directory . (string)$attr->Name . "\\" . (string)$image->Name;
  124. }
  125. $reportFile = $reportPath . (string)$image->Report;
  126. if (!file_exists($reportFile . ".ppx")) {
  127. $reportFile .= ".ppr";
  128. } else {
  129. $reportFile .= ".ppx";
  130. }
  131. if (stripos($reportFile, "_graf") && !file_exists($reportFile)) {
  132. continue;
  133. }
  134. foreach ($this->generateTypes as $extension => $value) {
  135. if ((string)$image->$value != "J")
  136. continue;
  137. $layer = ($extension == ".pdf") ? (string)$image->PDFImageLayer : (string)$image->ImageLayer;
  138. if ($newFile) {
  139. $imageFile = $dat . $layer . $extension;
  140. $imageFileUtf8 = utf8_decode($imageFile);
  141. } else {
  142. $imageFile = $dat . $extension;
  143. $imageFileUtf8 = utf8_decode($imageFile);
  144. }
  145. if (file_exists($imageFileUtf8) && filemtime($imageFileUtf8) >= $this->today) {
  146. if ($startTime) {
  147. $duration = date("H:i:s", intval(filemtime($imageFileUtf8)) - $time);
  148. } else {
  149. $startTime = intval(filemtime($imageFileUtf8));
  150. }
  151. $time = intval(filemtime($imageFileUtf8));
  152. } else {
  153. $duration = (file_exists($imageFileUtf8)) ? "alt" : "fehlt";
  154. $modified = (file_exists($imageFileUtf8)) ? date("d.m.Y H:i", filemtime($imageFileUtf8)) : "fehlt";
  155. $newError = new WorkflowError();
  156. $newError->Filename = $imageFile;
  157. $newError->User = substr((string)$image->Directory, 0, -1);
  158. $newError->Report = (string)$image->Report;
  159. $newError->ReportModified = (file_exists($reportFile)) ? date("d.m.Y H:i", filemtime($reportFile)) : "fehlt";
  160. $newError->Cube = (isset($reports[$newError->Report])) ? $reports[$newError->Report] : "";
  161. $newError->Layer = $layer;
  162. $newError->MailTo = (string)$image->Email;
  163. $newError->Modified = $modified;
  164. $result['errors'][] = $newError;
  165. }
  166. $source = new Source($imageFile);
  167. $source->Report = (string)$image->Report;
  168. $source->Layer = $layer;
  169. $source->Duration = $duration;
  170. $result['sources'][] = $source;
  171. /*
  172. <Name>1.2 Kostenstellenbericht aktueller Monat</Name>
  173. <Description>Kostenstellenbericht aktueller Monat</Description>
  174. <Directory>Global1\</Directory>
  175. <Report>1_2_Kostenstellenbericht_aktueller_Monat_Graf</Report>
  176. <GIFGenerate>J</GIFGenerate>
  177. <JPGGenerate>N</JPGGenerate>
  178. <PDFGenerate>N</PDFGenerate>
  179. <XLSGenerate>N</XLSGenerate>
  180. <Active>N</Active>
  181. <SendEmail>N</SendEmail>
  182. <ImageType>0</ImageType>
  183. <ImageLayer>0</ImageLayer>
  184. <PDFImageType>0</PDFImageType>
  185. <PDFImageLayer>0</PDFImageLayer>
  186. <Ansicht>Client</Ansicht>
  187. <Email />
  188. <ppx>N</ppx>
  189. */
  190. }
  191. }
  192. }
  193. $result['summary'] = new Summary(count($result['sources']), floor(($time - $startTime) / 60) + 1);
  194. $result['summary']->Duration = date("H:i:s", $time - $startTime);
  195. date_default_timezone_set('Europe/Berlin');
  196. return $result;
  197. }
  198. public function generateAll ($xmlFile, $userFilter, $cubeFilter)
  199. {
  200. $doc = $this->readXmlFile($xmlFile);
  201. $xmlShortFileName = substr(basename($xmlFile), 0, -4);
  202. $this->publishImagePath = (string)$doc->General->PublishImagePath . $xmlShortFileName . "\\";
  203. $this->pprCtrl = new PowerplayController($this->publishImagePath);
  204. $this->cubePath = (string)$doc->General->CubePath;
  205. $this->reportPath = (string)$doc->General->ReportPath;
  206. $reportMapping = $this->getReportMapping($doc, $userFilter, $cubeFilter);
  207. if ($this->flagGenImages) {
  208. $this->generateImages($reportMapping['report']);
  209. }
  210. if ($this->flagSendMail) {
  211. $this->sendMails($reportMapping['mail'], $xmlShortFileName);
  212. }
  213. $scorecardMapping = $this->getScorecardMapping($doc);
  214. if (count($scorecardMapping['report']) > 0) {
  215. $sc = $this->generateScorecards($scorecardMapping);
  216. $this->saveScorecards($sc);
  217. }
  218. }
  219. public function changeCubePath ($xmlFile)
  220. {
  221. $doc = $this->readXmlFile($xmlFile);
  222. $this->cubePath = (string)$doc->General->CubePath;
  223. $this->reportPath = (string)$doc->General->ReportPath;
  224. $this->publishImagePath = (string)$doc->General->PublishImagePath;
  225. $this->pprCtrl = new PowerplayController($this->publishImagePath);
  226. $gxiCtrl = new GapsXmlInfoController();
  227. $reports = $gxiCtrl->getExistingReportFiles("1", $this->reportPath, false, "");
  228. $reportMapping = $this->getReportMapping($doc);
  229. // $reportMapping['report'][$report][$extension] = 1
  230. foreach($reportMapping['report'] as $report => $reportArray) {
  231. $ppxFile = $this->reportPath . $report . ".ppx";
  232. $pprFile = $this->reportPath . $report . ".ppr";
  233. if (file_exists($ppxFile)) {
  234. $this->changeCubePathInFile($ppxFile);
  235. } else if (file_exists($ppxFile)) {
  236. if ($this->pprCtrl->OpenReport($pprFile, $this->cubePath . $reports[$report] . "mdc")) {
  237. $this->pprCtrl->Save();
  238. $this->pprCtrl->CloseReport();
  239. }
  240. }
  241. }
  242. }
  243. private function saveScorecards ($sc)
  244. {
  245. foreach($sc['data'] as $user => $userArray) {
  246. $xml = simplexml_load_string('<?xml version="1.0" encoding="iso-8859-1"?><Daten />');
  247. foreach ($userArray as $id => $idArray) {
  248. $cat = $xml->addChild("Kategorie");
  249. /** @var SimpleXMLElement $cat */
  250. $cat->addAttribute("ID", $id);
  251. $cat->addChild("Publish");
  252. $cat->addChild("Scorecards");
  253. foreach ($idArray as $e) {
  254. $entry = $cat->Scorecards->addChild("Bericht");
  255. /** @var SimpleXMLElement $entry */
  256. $scorecard = $sc['report'][$e[0]][$e[1]][$e[2]];
  257. foreach ($scorecard as $key => $value) {
  258. $entry->addChild($key, $value);
  259. }
  260. }
  261. }
  262. $xml->asXML($this->publishImagePath . "..\\" . $user . "\\data.xml");
  263. }
  264. /*
  265. $header = array("Report","Zeile","Spalte","Datum","Wert");
  266. $recentFile = $this->publishImagePath . "recent.csv";
  267. $scFile = $this->publishImagePath . "scorecards.csv";
  268. $handle = fopen($recentFile, "w+");
  269. fwrite($handle, implode(";", $header) . "\r\n");
  270. foreach ($sc as $row) {
  271. fwrite($handle, implode(";", $row) . "\r\n");
  272. }
  273. fclose($handle);
  274. if (!file_exists($scFile)) {
  275. array_unshift($sc, $header);
  276. }
  277. $handle = fopen($scFile, "a+");
  278. foreach ($sc as $row) {
  279. fwrite($handle, implode(";", $row) . "\r\n");
  280. }
  281. fclose($handle);
  282. */
  283. }
  284. public function getScorecardMapping($doc)
  285. {
  286. $result = array('report' => array(), 'data' => array());
  287. foreach ($doc->Scorecards->Scorecard as $sc) {
  288. /** @var SimpleXMLElement $sc */
  289. $attr = $sc->attributes();
  290. $id = (string)$attr->ID;
  291. $user = substr((string)$attr->Directory, 0, -1);
  292. // $category = (string)$attr->Name;
  293. foreach ($sc->Reports->Report as $cell) {
  294. $report = trim((string)$cell->ReportFile);
  295. $row = (string)$cell->RowData;
  296. $col = (string)$cell->ColData;
  297. $format = (string)$cell->Format;
  298. $result['report'][$report][$row][$col] = array(
  299. 'Titel' => (string)$cell->Name,
  300. 'Beschreibung' => (string)$cell->Description,
  301. 'Wert' => 0,
  302. 'Grafik' => (string)$cell->ReportFile,
  303. 'URL' => "",
  304. 'Format' => $format,
  305. 'HoherZielwert' => (string)$cell->HoherZielwert,
  306. 'NiedrigerWert' => (string)$cell->LowValue,
  307. 'HoherWert' => (string)$cell->HighValue,
  308. 'Koordinaten' => $row . "|" . $col,
  309. 'Toleranz' => (string)$cell->Tolerance,
  310. 'Gewichtung' => (string)$cell->Weight,
  311. 'Akkumulation' => 0,
  312. 'Email' => "",
  313. 'InfoURL' => ""
  314. );
  315. $result['data'][$user][$id][] = array($report, $row, $col);
  316. /* Quelle
  317. <Scorecard ID="36420" Directory="Global1\" Name="_4._Service_">
  318. <Reports>
  319. <Report ID="88670">
  320. <Name>Ergebnis Prognose Monat Service Hannuschka</Name>
  321. <Description>Ergebnis Prognose Monat Service Hannuschka</Description>
  322. <Active>J</Active>
  323. <ReportFile>Prog_Monat_Soll_Ist_Service</ReportFile>
  324. <RowData>1</RowData>
  325. <ColData>18</ColData>
  326. <HoherZielwert>J</HoherZielwert>
  327. <LowValue>0.9</LowValue>
  328. <HighValue>1.0</HighValue>
  329. <Tolerance>0</Tolerance>
  330. <Format>Prozent</Format>
  331. <Weight>0</Weight>
  332. <MonthlyAccumulate>N</MonthlyAccumulate>
  333. <Email/>
  334. <InfoURL/>
  335. */
  336. /*
  337. <Daten>
  338. <_1._Gesamtbetrieb_ ID="36420">
  339. <Scorecards>
  340. <Bericht>
  341. <Titel>Prognose Ergebnis Gesamt</Titel>
  342. <Beschreibung></Beschreibung>
  343. <Wert>109,14 %</Wert>
  344. <Grafik>images/</Grafik>
  345. <URL><![CDATA[http://10.6.174.10/GAPS_BMW/Report/1_15_Monatsprognose]]></URL>
  346. <HoherZielwert>J</HoherZielwert>
  347. <NiedrigerWert>90 %</NiedrigerWert>
  348. <HoherWert>100 %</HoherWert>
  349. <Koordinaten>1|15</Koordinaten>
  350. <Toleranz>0</Toleranz>
  351. <Gewichtung>0</Gewichtung>
  352. <Akkumulation>N</Akkumulation>
  353. <Email>hotline@freicon.de</Email>
  354. <InfoURL>www.freicon.de</InfoURL>
  355. </Bericht>
  356. */
  357. }
  358. }
  359. return $result;
  360. }
  361. private function readXmlFile ($xmlFile)
  362. {
  363. if (!file_exists($xmlFile)) {
  364. throw new InvalidArgumentException("File '{$xmlFile}' not found");
  365. }
  366. return simplexml_load_string(file_get_contents($xmlFile));
  367. }
  368. /**
  369. * @param SimpleXMLElement $doc
  370. * @param array $userFilter
  371. * @param array $cubeFilter
  372. * @return array
  373. */
  374. public function getReportMapping ($doc, $userFilter = array("*"), $cubeFilter = array("*"))
  375. {
  376. $result = array('report' => array(), 'mail' => array());
  377. $infoCtrl = new GapsXmlInfoController();
  378. $reportCubes = array();
  379. if ($cubeFilter[0] != "*") {
  380. $reportCubes = $infoCtrl->getExistingReportFiles("", $doc, false);
  381. }
  382. $flagMailFilter = ($userFilter[0] != "*" && preg_match("/.*@.*\.\w+/", $userFilter[0]));
  383. foreach ($doc->Publishes->Publish as $publish) {
  384. /** @var SimpleXMLElement $publish */
  385. foreach ($publish->Images->Image as $image) {
  386. $user = substr((string)$image->Directory, 0, -1);
  387. $mailAddresses = explode(";", strtolower((string)$image->Email));
  388. foreach ($mailAddresses as $i => $mailAddress) {
  389. $mailAddresses[$i] = trim($mailAddress);
  390. }
  391. if ($flagMailFilter) {
  392. $mailAddresses = array_intersect($mailAddresses, $userFilter);
  393. if (count($mailAddresses) == 0) {
  394. continue;
  395. }
  396. }
  397. $report = trim((string)$image->Report);
  398. if ($cubeFilter[0] != "*" && isset($reportCubes[$report]) && !in_array($reportCubes[$report], $cubeFilter)) {
  399. continue;
  400. }
  401. foreach ($this->generateTypes as $extension => $generateImageType) {
  402. if ((string)$image->$generateImageType != "J")
  403. continue;
  404. $desc = utf8_decode((string)$image->Description);
  405. $layer = ($extension == ".pdf") ? (string)$image->PDFImageLayer : (string)$image->ImageLayer;
  406. $imageFile = utf8_decode("{$report}_{$layer}{$extension}");
  407. $result['report'][$report][$extension][$layer] = 1;
  408. if ((string)$image->SendEmail == "J") {
  409. foreach ($mailAddresses as $mailAddress) {
  410. $result['mail'][$mailAddress][$this->publishImagePath.$imageFile] = array($desc, $mailAddresses);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. return $result;
  417. }
  418. private function generateImages ($reportMapping)
  419. {
  420. // $reportMapping[$report][$extension] = 1
  421. foreach($reportMapping as $report => $reportArray) {
  422. if (!$this->openReport($this->reportPath . $report)) {
  423. continue;
  424. }
  425. if (isset($reportArray['.jpg'])) {
  426. $this->pprCtrl->PublishImages();
  427. }
  428. if (isset($reportArray['.pdf'])) {
  429. $this->pprCtrl->SaveAsPdf("", array_keys($reportArray['.pdf']));
  430. }
  431. if (isset($reportArray['.xls'])) {
  432. $this->pprCtrl->SaveAsXls();
  433. }
  434. $this->pprCtrl->CloseReport();
  435. }
  436. }
  437. public function generateScorecards ($scorecardMapping)
  438. {
  439. $result = array();
  440. $now = date("Y-m-d");
  441. // $scorecardMapping[$report][$row][$column] = 1
  442. foreach($scorecardMapping['report'] as $report => $reportArray) {
  443. if (!$this->openReport($this->reportPath . $report)) {
  444. continue;
  445. }
  446. foreach ($reportArray as $row => $rowArray) {
  447. foreach ($rowArray as $col => $v) {
  448. $currentValue = $this->pprCtrl->GetCellValue($row, $col);
  449. $result[] = array(
  450. 'report' => $report,
  451. 'row' => $row,
  452. 'column' => $col,
  453. 'date' => $now,
  454. 'value' => $currentValue
  455. );
  456. $scorecardMapping['report'][$report][$row][$col]['Wert'] = $currentValue;
  457. }
  458. }
  459. $this->pprCtrl->CloseReport();
  460. }
  461. return $scorecardMapping;
  462. }
  463. private function openReport ($report)
  464. {
  465. $ppxFile = $report . ".ppx";
  466. $pprFile = $report . ".ppr";
  467. if (!file_exists($ppxFile)) {
  468. if (!file_exists($pprFile)) {
  469. return false;
  470. }
  471. $this->pprCtrl->OpenReport($pprFile);
  472. // $this->pprCtrl->SaveAsPpx();
  473. } else {
  474. $this->changeCubePathInFile($ppxFile);
  475. $this->pprCtrl->OpenReport($ppxFile);
  476. }
  477. return true;
  478. }
  479. public function changeCubePathInFile ($ppxFile)
  480. {
  481. $doc = simplexml_load_file($ppxFile);
  482. /** @var SimpleXMLElement $attr */
  483. $attr = $doc->PowerPlayReport->PPDSConnection->attributes();
  484. if ($this->cubePath != (string)$attr->Path) {
  485. $attr->Path = $this->cubePath;
  486. $doc->saveXML($ppxFile);
  487. }
  488. }
  489. public function getXmlFileParameters ($xmlFile)
  490. {
  491. $doc = $this->readXmlFile($xmlFile);
  492. $infoCtrl = new GapsXmlInfoController();
  493. $reportCubes = $infoCtrl->getExistingReportFiles("", $doc, false);
  494. $reportMapping = $this->getReportMapping($doc);
  495. $reports = array();
  496. $users = array();
  497. $mails = array();
  498. // $reportMapping[$extension][$report][$layer][$user] = ($file, $mail)
  499. foreach ($reportMapping as $ext => $extArray) {
  500. foreach($extArray as $report => $reportArray) {
  501. foreach ($reportArray as $layerArray) {
  502. foreach ($layerArray as $user => $userArray) {
  503. if ($ext != '.gif') {
  504. $reports[$report] = @$reportCubes[$report];
  505. }
  506. $users[$user] = 1;
  507. $m = array_flip(explode(";", $userArray['mail']));
  508. $mails = array_merge($mails, $m);
  509. }
  510. }
  511. }
  512. }
  513. return array('users' => array_keys($users), 'mails' => array_keys($mails), 'cubes' => array_unique(array_values($reports)), 'reports' => array_keys($reports));
  514. }
  515. private function sendMails ($reportMapping, $xmlFile)
  516. {
  517. $xmlFile = str_replace("_", " ", $xmlFile);
  518. $xmlFile = preg_replace("/gaps/i", "GAPS", $xmlFile);
  519. // $reportMapping[$mailAddress][$image] = array($desc, $mailAddresses);
  520. foreach ($reportMapping as $mailAddress => $image) {
  521. if ($mailAddress == "") continue;
  522. echo $mailAddress . "\r\n";
  523. if (!preg_match("/^[a-z0-9_.+-]+@[a-z0-9-]+\.[a-z0-9-.]+$/i", $mailAddress)) {
  524. echo "Keine gueltige E-Mail-Adresse!\r\n";
  525. continue;
  526. }
  527. if ($this->flagSeperateMail) {
  528. foreach ($image as $filename => $desc) {
  529. if (file_exists($filename) && abs(filemtime($filename) - strtotime("now")) < 24 * 60 * 60) {
  530. $sourceFile = "";
  531. preg_match("/(.*)_(\d+)\.\w{3}/", basename($filename), $sourceFile);
  532. $imageFile = $desc[0] . substr($filename, -4);
  533. $imageDate = date("d.m.Y H:i", filemtime($filename));
  534. $attachments = array(realpath($filename), $imageFile);
  535. $descriptions = "<tr><td>{$imageFile}</td><td>{$sourceFile[1]}</td><td>{$sourceFile[2]}</td><td>{$imageDate}</td><td>" . implode("<br/>", $desc[1]) . "</td></tr>";
  536. $this->mailController->sendMail($mailAddress, $desc[0], $this->getMailBody($descriptions), "", array($attachments));
  537. }
  538. }
  539. } else {
  540. $attachments = array();
  541. $descriptions = array();
  542. foreach ($image as $filename => $desc) {
  543. $sourceFile = "";
  544. preg_match("/(.*)_(\d+)\.\w{3}/", basename($filename), $sourceFile);
  545. $imageFile = $desc[0] . substr($filename, -4);
  546. $imageDate = "Heute nicht verf&uuml;gbar.";
  547. if (file_exists($filename) && abs(filemtime($filename) - strtotime("now")) < 24 * 60 * 60) {
  548. $attachments[] = array(realpath($filename), $imageFile);
  549. $imageDate = date("d.m.Y H:i", filemtime($filename));
  550. }
  551. $descriptions[] = "<tr><td>{$imageFile}</td><td>{$sourceFile[1]}</td><td>{$sourceFile[2]}</td><td>{$imageDate}</td><td>" . implode("<br/>", $desc[1]) . "</td></tr>";
  552. }
  553. $count = count($attachments);
  554. $subject = ($count != 1) ? "{$xmlFile}: {$count} Dateien" : $desc[0];
  555. $this->mailController->sendMail($mailAddress, $subject, $this->getMailBody(implode("\r\n", $descriptions)), "", $attachments);
  556. }
  557. }
  558. }
  559. private function getMailBody ($content) {
  560. return "<!DOCTYPE html>\r\n" .
  561. "<html><head>\r\n" .
  562. '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"><style>' .
  563. "\r\n.liste { border: 1px solid #888888; border-collapse: collapse; } \r\n" .
  564. ".liste td, .liste th { padding: 3px 15px; border: 1px solid #888888; } \r\n" .
  565. "</style></head><body>\r\n" .
  566. '<table class="liste"><tr><th>Datei</th><th>Bericht</th><th>Schicht</th><th>Stand</th><th>Empf&auml;nger</th></tr>' .
  567. $content .
  568. "</table></body></html>";
  569. }
  570. }
  571. ?>