/**
* Obsługa zgłoszeń Floty ALFA-BET (REST API -> Email)
*/
add_action(’rest_api_init’, function () {
register_rest_route(’panel-floty/v1′, '/zgloszenie’, array(
'methods’ => 'POST’,
'callback’ => 'obsluz_zgloszenie_floty’,
'permission_callback’ => '__return_true’,
));
});
function obsluz_zgloszenie_floty(WP_REST_Request $request) {
$data = $request->get_json_params();
// 📩 ADRESY E-MAIL ODBIORCÓW (połączone bezpiecznie przecinkami dla SMTP)
$recipients = array(
'krzysztof@kkrawczynski.pl’
);
$to = implode(’, ’, $recipients);
$type = isset($data[’type’]) ? sanitize_text_field($data[’type’]) : ”;
$plate = isset($data[’plate’]) ? sanitize_text_field($data[’plate’]) : 'Brak nr’;
$notes = isset($data[’notes’]) ? sanitize_textarea_field($data[’notes’]) : ”;
// ==========================================
// 1. OBSŁUGA DOKUMENTACJI ZDJĘCIOWEJ
// ==========================================
if ($type === 'PHOTOS_REPORT’) {
$subject = „📸 Dok. zdjęciowa – Pojazd: {$plate}”;
$message = „
Nowa dokumentacja zdjęciowa
„;
$message .= „
Numer rejestracyjny: ” . esc_html($plate) . „
„;
$message .= „
Uwagi / Opis:
” . nl2br(esc_html($notes)) . „
„;
$message .= „
Zdjęcia zostały dołączone jako załączniki do tej wiadomości.
„;
$headers = array(’Content-Type: text/html; charset=UTF-8′);
$attachments = array();
$temp_files = array();
if (!empty($data[’photosData’]) && is_array($data[’photosData’])) {
$upload_dir = wp_upload_dir();
$temp_dir = $upload_dir[’basedir’] . '/temp_flota/’;
if (!file_exists($temp_dir)) {
wp_mkdir_p($temp_dir);
}
foreach ($data[’photosData’] as $index => $photo) {
if (empty($photo[’data’])) continue;
$img_data = $photo[’data’];
if (preg_match(’/^data:image\/(\w+);base64,/’, $img_data, $type_match)) {
$img_data = substr($img_data, strpos($img_data, ’,’) + 1);
$ext = strtolower($type_match[1]);
} else {
$ext = 'jpg’;
}
$decoded_img = base64_decode($img_data);
if ($decoded_img === false) continue;
$file_name = 'foto_’ . sanitize_title($plate) . '_’ . time() . '_’ . ($index + 1) . ’.’ . $ext;
$file_path = $temp_dir . $file_name;
file_put_contents($file_path, $decoded_img);
$attachments[] = $file_path;
$temp_files[] = $file_path;
}
}
// Wysyłka maila
$sent = wp_mail($to, $subject, $message, $headers, $attachments);
// Usuwanie plików tymczasowych
foreach ($temp_files as $file) {
if (file_exists($file)) {
@unlink($file);
}
}
if ($sent) {
return new WP_REST_Response(array(’status’ => 'success’, 'message’ => 'Zdjęcia zostały wysłane!’), 200);
} else {
error_log(„FLOTA ERROR: Nie udało się wysłać zdjęć na adresy: {$to}”);
return new WP_REST_Response(array(’status’ => 'error’, 'message’ => 'Błąd wysyłania e-maila przez serwer.’), 500);
}
}
// ==========================================
// 2. OBSŁUGA ZGŁOSZENIA USTERKI
// ==========================================
if ($type === 'FAULT_REPORT’) {
$details = isset($data[’details’]) ? sanitize_textarea_field($data[’details’]) : ”;
$module = isset($data[’module’]) ? sanitize_text_field($data[’module’]) : ”;
$subject = „🛠️ Zgłoszenie Usterki – Pojazd: {$plate}”;
$message = „
Nowe zgłoszenie usterki
„;
$message .= „
Pojazd: ” . esc_html($plate) . „
„;
$message .= „
Moduł: ” . esc_html($module) . „
„;
$message .= „
Szczegóły usterki:
” . nl2br(esc_html($details)) . „
„;
$headers = array(’Content-Type: text/html; charset=UTF-8′);
$sent = wp_mail($to, $subject, $message, $headers);
if ($sent) {
return new WP_REST_Response(array(’status’ => 'success’, 'message’ => 'Zgłoszenie wysłane!’), 200);
} else {
error_log(„FLOTA ERROR: Nie udało się wysłać usterki na adresy: {$to}”);
return new WP_REST_Response(array(’status’ => 'error’, 'message’ => 'Błąd wysyłania e-maila przez serwer.’), 500);
}
}
return new WP_REST_Response(array(’status’ => 'error’, 'message’ => 'Nieznany typ zgłoszenia’), 400);
}