From 74867ca1acdc21900a1dd857dd9b88f6bb7edeef Mon Sep 17 00:00:00 2001 From: amazuecos Date: Mon, 24 Mar 2025 22:45:02 +0100 Subject: [PATCH] send email --- ci4/app/Config/Email.php | 4 +- ci4/app/Config/Services.php | 2 + ci4/app/Language/es/Chat.php | 4 +- ci4/app/Services/ChatService.php | 35 ++++++-- .../vuexy/form/mensajes/mensajesView.php | 18 +--- .../Views/themes/vuexy/mail/mail_layout.php | 84 +++++++++++++++++++ .../themes/vuexy/mail/messageNotification.php | 30 +++++++ 7 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 ci4/app/Views/themes/vuexy/mail/mail_layout.php create mode 100644 ci4/app/Views/themes/vuexy/mail/messageNotification.php diff --git a/ci4/app/Config/Email.php b/ci4/app/Config/Email.php index 9447a491..09d27f0b 100755 --- a/ci4/app/Config/Email.php +++ b/ci4/app/Config/Email.php @@ -28,7 +28,7 @@ class Email extends BaseConfig /** * SMTP Server Hostname */ - public string $SMTPHost = 'smtp.ionos.es'; + public string $SMTPHost = 'localhost'; /** * SMTP Username @@ -38,7 +38,7 @@ class Email extends BaseConfig /** * SMTP Password */ - public string $SMTPPass = 'H%5&qDkDkWnfLTGN'; + public string $SMTPPass = ''; /** * SMTP Port diff --git a/ci4/app/Config/Services.php b/ci4/app/Config/Services.php index 9309734f..02632cdf 100755 --- a/ci4/app/Config/Services.php +++ b/ci4/app/Config/Services.php @@ -10,6 +10,7 @@ use App\Services\PapelImpresionService; use CodeIgniter\Config\BaseService; use App\Services\ProductionService; use App\Services\TarifaMaquinaService; +use CodeIgniter\Email\Email; /** * Services Configuration file. @@ -36,6 +37,7 @@ class Services extends BaseService * return new \CodeIgniter\Example(); * } */ + public static function production(){ return new ProductionService(); } diff --git a/ci4/app/Language/es/Chat.php b/ci4/app/Language/es/Chat.php index b1ba0e4e..10458366 100644 --- a/ci4/app/Language/es/Chat.php +++ b/ci4/app/Language/es/Chat.php @@ -60,6 +60,8 @@ return [ "subscribe_admin_chat_wrong" => "Tienes que seleccionar un usuario.", "help_select_chat_department_user" => "Solamente son listados los usuarios que pertenecen al personal. Los clientes no son listados, para añadirlos a la conversación se realiza desde la sección de mensajería de las diferentes secciones(presupuesto,pedido,factura ...)", "store_department" => "Crear departamento", - + "mail" => [ + "mail_subject" => "Nuevo mensaje" + ] ]; \ No newline at end of file diff --git a/ci4/app/Services/ChatService.php b/ci4/app/Services/ChatService.php index 66277625..d5d8f9fb 100644 --- a/ci4/app/Services/ChatService.php +++ b/ci4/app/Services/ChatService.php @@ -17,12 +17,13 @@ use App\Models\Configuracion\ConfigVariableModel; use App\Models\Presupuestos\PresupuestoModel; use App\Models\Usuarios\UserModel; use CodeIgniter\Config\BaseService; - - +use CodeIgniter\Email\Email; class ChatService extends BaseService { protected ?ChatEntity $chatEntity; + protected Email $emailService; + protected UserModel $userModel; protected ChatModel $chatModel; protected ChatMessageModel $chatMessageModel; protected ChatUser $chatUserModel; @@ -39,6 +40,8 @@ class ChatService extends BaseService protected array $modelClassMap; public function __construct() { + $this->emailService = service('email'); + $this->userModel = model(UserModel::class); $this->chatModel = model(ChatModel::class); $this->chatMessageModel = model(ChatMessageModel::class); $this->chatNotificationModel = model(ChatNotification::class); @@ -87,6 +90,12 @@ class ChatService extends BaseService } $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $this->chatEntity->id, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $chatMessageEntity = $this->chatMessageModel->find($chat_message_id); + if ($data["client"]) { + $userClient = $this->userModel->find($data['client']); + if ($userClient) { + $this->sendMessageNotificationEmail($chatMessageEntity, $userClient); + } + } return $chatMessageEntity; } public function createChat(int $chatDepartmentId, string $model, int $modelId): bool|int|string @@ -112,9 +121,9 @@ class ChatService extends BaseService { $chatDepartments = $this->chatDepartmentModel->getModelChatDepartments($this->modelFkMap[$model], $modelId); $departmentWithChat = array_map(fn($q) => $q["name"], $chatDepartments); - if(count($departmentWithChat) > 0){ + if (count($departmentWithChat) > 0) { $departmentWithoutChat = $this->chatDepartmentModel->whereNotIn('name', $departmentWithChat)->findAll(); - }else{ + } else { $departmentWithoutChat = $this->chatDepartmentModel->findAll(); } foreach ($departmentWithoutChat as $value) { @@ -153,18 +162,18 @@ class ChatService extends BaseService ->where($this->modelFkMap[$model], $modelId) ->findAll(); foreach ($chats as $c) { - $this->chatModel->setAsViewedChatUserNotifications($c->id,auth()->user()->id); + $this->chatModel->setAsViewedChatUserNotifications($c->id, auth()->user()->id); } $chatWithHebraData = array_map(fn(ChatEntity $c) => $c->withHebra(), $chats); return $chatWithHebraData ?? []; } - public function getAuthUserNotifications(): array + public function getAuthUserNotifications(): array { $departmentNotifications = $this->chatModel->getChatDepartmentNotifications(); $internalNotifications = $this->chatModel->getChatInternalNotifications(); $totalMessages = 0; - $countNotificiationDepartments = array_map(fn($n) => $n->unreadMessages,$departmentNotifications); - $countNotificationInternal = array_map(fn($n) => $n->unreadMessages,$internalNotifications); + $countNotificiationDepartments = array_map(fn($n) => $n->unreadMessages, $departmentNotifications); + $countNotificationInternal = array_map(fn($n) => $n->unreadMessages, $internalNotifications); $totalMessages = array_sum($countNotificiationDepartments) + array_sum($countNotificationInternal); return [ @@ -173,4 +182,14 @@ class ChatService extends BaseService "totalMessages" => $totalMessages ]; } + public function sendMessageNotificationEmail(ChatMessageEntity $chatMessageEntity, UserEntity $user): bool + { + $users = auth()->getProvider(); + $toEmail = $users->find($user->id)->getEmail(); + $this->emailService->setTo($toEmail); + $subject = '[Safekat]' . lang('Chat.mail.mail_subject') . '-' . $chatMessageEntity->chat()->title; + $this->emailService->setSubject($subject); + $this->emailService->setMessage(view('themes/vuexy/mail/messageNotification', ["header" => lang('Chat.mail.mail_subject'), "data" => $chatMessageEntity->withUser()])); + return $this->emailService->send(); + } } diff --git a/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php b/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php index 85cf1b96..3ef4954e 100644 --- a/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php +++ b/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php @@ -10,11 +10,11 @@