#!/usr/bin/php id); define("INTERNAL_USER_USERNAME", $me->username); // --- 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) { 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); } } 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 Email Generation --- // 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("d.m.Y 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ße Ort Feld Wert Editiert Von
{$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 - 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 = 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('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 - ' . date('d.m.Y'); // Use PHP date function for dynamic date $mail->Body = $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 'Nachricht wurde erfolgreich gesendet.' . PHP_EOL; // German output } catch (Exception $e) { // 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 } ?>