#!/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, 98]; $allNetworkData = []; $from = strtotime("-1 week"); // Simpler timestamp generation $to = time(); $newBuildings = []; 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); } // fetch new buildings $newBuildingsNetwork = BuildingModel::search([ 'network_id' => $networkID, '>create' => $from, ]); if ($newBuildingsNetwork) { foreach ($newBuildingsNetwork as $building) { if (!isset($newBuildings[$network->name])) { $newBuildings[$network->name] = []; } $newBuildings[$network->name][] = [ 'network_id' => $networkID, 'network_name' => $network->name, 'building_id' => $building->id, 'street' => $building->street, 'zip' => $building->zip, 'city' => $building->city, ]; } } } 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'); if (strpos($label, 'Tiefbau abgeschlossen') !== false) { $label = 'Tiefbau angeschlossen'; } if (strpos($label, 'Leitungsbau abgeschlossen') !== false) { $label = 'Leitungsbau angeschlossen'; } $editedAt = date("d.m.Y H:i", $item->last_edited_at); // Shortened format for space if ($item->item_object_type == 'Building') { $type = intval($item->item_num) > 150 ? 'Ist-Zustand' : 'Planung'; $type2 = 'Tiefbau'; } else if ($item->item_object_type == 'Termination') { if (intval($item->item_num) > 0 && intval($item->item_num) < 1005) $type = 'Planung'; else if (intval($item->item_num) > 1005 && intval($item->item_num) < 2005) $type = 'Ist-Zustand'; else $type = 'Zusätzlich'; $type2 = 'Leitungsbau'; } else { $type = 'Unbekannt'; $type2 = 'Unbekannt'; } $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; // Check if there are new buildings and show them if (!empty($newBuildings[$networkName])) { $html .= <<Neue Gebäude:
    HTML; foreach ($newBuildings[$networkName] as $building) { $buildingStreet = htmlspecialchars($building['street']); $buildingZip = htmlspecialchars($building['zip']); $buildingCity = htmlspecialchars($building['city']); $html .= <<{$buildingStreet}, {$buildingZip} {$buildingCity} HTML; } $html .= << HTML; } else { $noNewBuildingsMsg = htmlspecialchars("Keine neuen Gebäude für dieses Netzwerk."); $html .= <<{$noNewBuildingsMsg}

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

    HTML; } else { $html .= <<
Art Straße Ort Feld Wert Doku Editiert Von
{$type2} {$street} {$city} {$label} {$valueDisplay} {$type} {$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 }?>