diff --git a/scripts/pipework/run-weekly-email.php b/scripts/pipework/run-weekly-email.php index 99c8d76bc..0dfc35938 100644 --- a/scripts/pipework/run-weekly-email.php +++ b/scripts/pipework/run-weekly-email.php @@ -1,109 +1,279 @@ #!/usr/bin/php id); define("INTERNAL_USER_USERNAME", $me->username); -$pipeworkController = new PipeworkController(false); +// --- Controller Instantiation (Keep if required) --- +$pipeworkController = new PipeworkController(false); // Assuming PipeworkController class is loaded +// --- Data Fetching --- $networkIDs = [41, 90, 22, 2, 25, 24, 7, 15, 6, 12, 13]; - $allNetworkData = []; +$from = strtotime("-1 week"); // Simpler timestamp generation +$to = time(); foreach ($networkIDs as $networkID) { - $network = new Network($networkID); - - $from = date("U", strtotime("-1 week")); - $to = date("U"); - - $pipeworkHistory = BuildingModel::getHistory($from, $to, $networkID, ''); - $allNetworkData[$network->name] = $pipeworkHistory; -} - -$html = ""; -$html .= "

Pipework History Report

"; - -foreach ($allNetworkData as $networkName => $networkData) { - $html .= "

Network: " . htmlspecialchars($networkName) . "

"; - if (empty($networkData)) { - $html .= '
' . htmlspecialchars("Keine Änderungen im Tiefbau") . '
'; - continue; - } - - $html .= ""; - $html .= ""; - foreach ($networkData as $item) { - $userName = UserModel::getOne($item->last_edited_by_user_id); - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; + } catch (Exception $e) { + // Optional: Log or handle exceptions during data fetching for a specific network + error_log("Error fetching data for Network ID " . $networkID . ": " . $e->getMessage()); } - - $html .= "
StraßeOrtFeldWertEditiertVon
" . htmlspecialchars($item->building_street) . "" . htmlspecialchars($item->building_city) . "" . htmlspecialchars($item->item_label) . ""; - switch ($item->item_type) { - case 'string': - $html .= htmlspecialchars($item->value_string); - break; - case 'int': - $html .= htmlspecialchars($item->value_int); - break; - case 'text': - $html .= nl2br(htmlspecialchars($item->value_text)); - break; - default: - $html .= "N/A"; + try { + $network = new Network($networkID); // Assuming Network class is loaded + if ($network->id) { // Basic check if network loaded successfully + $history = BuildingModel::getHistory($from, $to, $networkID, ''); // Assuming BuildingModel is loaded + $allNetworkData[$network->name] = $history; + } else { + // Optional: Log or handle cases where a Network object couldn't be created + error_log("Could not load Network with ID: " . $networkID); } - $html .= "" . date("Y-m-d H:i:s", $item->last_edited_at) . "" . htmlspecialchars($userName->username) . "

"; } -$html .= ""; +// --- HTML Email Generation --- -// Instantiate PHPMailer -$mail = new PHPMailer\PHPMailer\PHPMailer(true); +// Logo paths - Ensure these are correct relative to the script location or absolute paths +$logoToolPath = LIBDIR . '/../public/assets/images/the-tool-logo.png'; +$logoXinonPath = LIBDIR . '/../public/assets/images/xinon-full.png'; + +// Check if logo files exist +$logoToolExists = file_exists($logoToolPath); +$logoXinonExists = file_exists($logoXinonPath); + +// --- Start HTML --- +$html = << + + + + + Tiefbau Report + + + + + + + +HTML; + } // End foreach network +} // End if empty allNetworkData + +// --- Footer --- +$html .= << + +
+ + + + + + + + + HTML; + +if (empty($allNetworkData)) { + $html .= << + + +HTML; +} else { + foreach ($allNetworkData as $networkName => $networkData) { + $networkNameHtml = htmlspecialchars($networkName); + $html .= << + + + + + + + + + + + +HTML; + foreach ($networkData as $item) { + // Determine the value to display + $valueDisplay = ''; + if (!empty($item->value_string)) { + $valueDisplay = htmlspecialchars($item->value_string); + } elseif (!empty($item->value_int)) { + $valueDisplay = htmlspecialchars($item->value_int); + } elseif (!empty($item->value_text)) { + $valueDisplay = nl2br(htmlspecialchars($item->value_text)); + } else { + // Optionally skip rows with no displayable value, or show N/A + // continue; // Uncomment to skip + $valueDisplay = 'N/A'; + } + + // Skip fully empty rows based on original logic if needed (though the check above is usually sufficient) + if (empty($item->value_string) && empty($item->value_int) && empty($item->value_text) && $valueDisplay === 'N/A') { + continue; + } + + + $userName = 'Unbekannt'; // Default username + try { + $user = UserModel::getOne($item->last_edited_by_user_id); // Assuming UserModel is loaded + if ($user && $user->username) { + $userName = htmlspecialchars($user->username); + } + } catch (Exception $e) { + error_log("Error fetching user ID " . $item->last_edited_by_user_id . ": " . $e->getMessage()); + } + + + $street = htmlspecialchars($item->building_street ?? 'N/A'); + $city = htmlspecialchars($item->building_city ?? 'N/A'); + $label = htmlspecialchars($item->item_label ?? 'N/A'); + $editedAt = date("Y-m-d H:i", $item->last_edited_at); // Shortened format for space + + $html .= << + + + + + + + +HTML; + } // End foreach item + $html .= << +
+ + + +
+HTML; +if ($logoToolExists) { + $html .= 'The Tool Logo'; +} else { + $html .= 'The Tool Logo not found'; +} +$html .= << + +HTML; +if ($logoXinonExists) { + $html .= 'Xinon Logo'; +} else { + $html .= 'Xinon Logo not found'; +} +$html .= << +
+
+

Tiefbau Report

+

Änderungen der letzten Woche

+
+

Keine Netzwerkdaten zum Anzeigen vorhanden.

+
+

Netzwerk: {$networkNameHtml}

+HTML; + + if (empty($networkData)) { + $noChangesMsg = htmlspecialchars("Keine Änderungen im Tiefbau für diesen Zeitraum."); + $html .= <<{$noChangesMsg}

+HTML; + } else { + $html .= << +
StraßeOrtFeldWertEditiertVon
{$street}{$city}{$label}{$valueDisplay}{$editedAt}{$userName}
+HTML; + } // End if empty networkData + $html .= << +
+ Automatisch generierter Report von XINON TheTool | © +HTML; +$html .= date("Y"); +$html .= << +
+ + + + + + +HTML; + +// --- Email Sending --- +$mail = new PHPMailer(true); try { - // Server settings + // Server settings - Ensure these constants are defined in config.php $mail->isSMTP(); $mail->Host = TT_PIPEWORK_SMTP_HOST; $mail->SMTPAuth = true; $mail->Username = TT_PIPEWORK_SMTP_USER; $mail->Password = TT_PIPEWORK_SMTP_PASS; - $mail->CharSet = "UTF-8"; - $mail->Encoding = 'base64'; - $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; + $mail->CharSet = PHPMailer::CHARSET_UTF8; // Use PHPMailer constant + $mail->Encoding = 'base64'; + $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; + // Embed Logos - Do this *before* setting the body + if ($logoToolExists) { + $mail->addEmbeddedImage($logoToolPath, 'logo_thetool'); + } + if ($logoXinonExists) { + $mail->addEmbeddedImage($logoXinonPath, 'logo_xinon'); + } + // Recipients - $mail->setFrom('einkauf@xinon.at', 'XINON Einkauf'); - $mail->addAddress('mark.zaff@xinon.eu', 'Mark Zaff'); + $mail->setFrom('thetool.xinon.at', 'XINON TheTool'); // Use sender address from config if available + $mail->addAddress('mark.zaff@xinon.eu', 'Mark Zaff'); $mail->addAddress('luca.haid@xinon.eu', 'Luca Haid'); // Content $mail->isHTML(true); - $mail->Subject = 'Tiefbau Report'; + $mail->Subject = 'Tiefbau Report - ' . date('d.m.Y'); // Use PHP date function for dynamic date $mail->Body = $html; - $mail->AltBody = strip_tags($html); + // Create a plain text version (simple approach) + $plainTextBody = strip_tags(str_replace('
', "\n", $html)); // Basic conversion + $plainTextBody = html_entity_decode($plainTextBody); // Decode HTML entities + $mail->AltBody = $plainTextBody; + $mail->send(); - echo 'Message has been sent'; + echo 'Nachricht wurde erfolgreich gesendet.' . PHP_EOL; // German output + } catch (Exception $e) { - echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; + // Log the detailed error + error_log("E-Mail konnte nicht gesendet werden. Mailer Error: {$mail->ErrorInfo}"); + // Output a user-friendly message + echo "E-Mail konnte nicht gesendet werden. Fehlerdetails wurden protokolliert. Mailer Error: {$mail->ErrorInfo}" . PHP_EOL; // German output } ?> \ No newline at end of file