97 lines
3.2 KiB
PHP
97 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* Helper para crear y enviar notificaciones por SSE
|
|
*/
|
|
|
|
require_once __DIR__ . '/../config/config.php';
|
|
|
|
class NotificationHelper {
|
|
|
|
/**
|
|
* Crear una notificación y enviarla por SSE
|
|
*
|
|
* @param int $userId ID del usuario destinatario
|
|
* @param string $type Tipo de notificación (message, document, status, etc)
|
|
* @param string $message Mensaje de la notificación
|
|
* @param array $data Datos adicionales (opcional)
|
|
* @return int|false ID de la notificación creada o false si falla
|
|
*/
|
|
public static function create($userId, $type, $message, $data = []) {
|
|
try {
|
|
$db = Database::getInstance();
|
|
|
|
// Insertar en BD
|
|
$notificationId = $db->insert(
|
|
'notifications',
|
|
[
|
|
'user_id' => $userId,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'data' => json_encode($data),
|
|
'is_read' => 0,
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]
|
|
);
|
|
|
|
if (!$notificationId) {
|
|
error_log('NotificationHelper: Failed to insert notification');
|
|
return false;
|
|
}
|
|
|
|
// Leer la notificación completa para enviarla
|
|
$notification = $db->fetchOne(
|
|
"SELECT id, user_id, type, message, data, is_read, created_at
|
|
FROM notifications
|
|
WHERE id = ?",
|
|
[$notificationId]
|
|
);
|
|
|
|
if ($notification) {
|
|
// Enviar por SSE
|
|
self::pushSSE($notification);
|
|
}
|
|
|
|
return $notificationId;
|
|
|
|
} catch (Exception $e) {
|
|
error_log('NotificationHelper::create error: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enviar notificación existente por SSE
|
|
*
|
|
* @param array $notification Array con datos de la notificación
|
|
*/
|
|
public static function pushSSE($notification) {
|
|
try {
|
|
// Usar el sistema de push_event.php para broadcast
|
|
$pushUrl = 'http://localhost:' . ($_SERVER['SERVER_PORT'] ?? '8000') . '/api/push_event.php';
|
|
|
|
$payload = json_encode([
|
|
'event' => 'notification',
|
|
'data' => $notification,
|
|
'target' => 'all' // Enviar a todos los operadores conectados
|
|
]);
|
|
|
|
// Fire-and-forget (no esperar respuesta)
|
|
$ch = curl_init($pushUrl);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen($payload)
|
|
]);
|
|
|
|
@curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
} catch (Exception $e) {
|
|
error_log('NotificationHelper::pushSSE error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|