mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
247 lines
9.7 KiB
PHP
247 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Chat;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Chat\ChatDeparmentModel;
|
|
use App\Models\Chat\ChatDeparmentUserModel;
|
|
use App\Models\Chat\ChatMessageModel;
|
|
use App\Models\Chat\ChatModel;
|
|
use App\Models\Clientes\ClienteModel;
|
|
use App\Models\Usuarios\UserModel;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\Log\Logger;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ChatController extends BaseController
|
|
{
|
|
protected ChatDeparmentModel $chatDeparmentModel;
|
|
protected ChatDeparmentUserModel $chatDeparmentUserModel;
|
|
protected ChatModel $chatModel;
|
|
protected ChatMessageModel $chatMessageModel;
|
|
protected UserModel $userModel;
|
|
protected ClienteModel $clienteModel;
|
|
|
|
|
|
|
|
|
|
public function initController(
|
|
RequestInterface $request,
|
|
ResponseInterface $response,
|
|
LoggerInterface $logger
|
|
) {
|
|
parent::initController($request, $response, $logger);
|
|
|
|
// Add your code here.
|
|
$this->chatDeparmentModel = model(ChatDeparmentModel::class);
|
|
$this->chatDeparmentUserModel = model(ChatDeparmentUserModel::class);
|
|
$this->chatModel = model(ChatModel::class);
|
|
$this->chatMessageModel = model(ChatMessageModel::class);
|
|
$this->userModel = model(UserModel::class);
|
|
$this->clienteModel = model(ClienteModel::class);
|
|
|
|
}
|
|
public function index() {}
|
|
public function get_chat_departments()
|
|
{
|
|
|
|
$data = $this->chatDeparmentModel->getChatDepartments();
|
|
return $this->response->setJSON($data);
|
|
}
|
|
public function get_chat_presupuesto(int $chat_department_id, int $presupuesto_id)
|
|
{
|
|
|
|
$data = [
|
|
"chat" => null,
|
|
"messages" => null,
|
|
];
|
|
$chat = $this->chatModel->getChatPresupuesto($chat_department_id, $presupuesto_id);
|
|
if ($chat) {
|
|
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
|
|
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
|
|
}
|
|
$data["chat"] = $chat;
|
|
return $this->response->setJSON($data);
|
|
}
|
|
public function get_chat_pedido(int $chat_department_id, int $pedido_id)
|
|
{
|
|
|
|
$data = [
|
|
"chat" => null,
|
|
"messages" => null,
|
|
];
|
|
$chat = $this->chatModel->getChatPedido($chat_department_id, $pedido_id);
|
|
if ($chat) {
|
|
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
|
|
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
|
|
|
|
}
|
|
$data["chat"] = $chat;
|
|
return $this->response->setJSON($data);
|
|
}
|
|
public function get_chat_factura(int $chat_department_id, int $factura_id)
|
|
{
|
|
|
|
$data = [
|
|
"chat" => null,
|
|
"messages" => null,
|
|
];
|
|
$chat = $this->chatModel->getChatFactura($chat_department_id, $factura_id);
|
|
if ($chat) {
|
|
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
|
|
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
|
|
|
|
}
|
|
$data["chat"] = $chat;
|
|
return $this->response->setJSON($data);
|
|
}
|
|
public function get_chat(int $chat_id)
|
|
{
|
|
|
|
$data = $this->chatModel->getChat($chat_id);
|
|
return $this->response->setJSON($data);
|
|
}
|
|
public function store_chat_message_presupuesto()
|
|
{
|
|
|
|
$data = $this->request->getPost();
|
|
// $data = $this->chatModel->createChatPresupuesto();
|
|
$existChat = $this->chatModel->existChatPresupuesto($data["chat_department_id"], $data["model_id"]);
|
|
if ($existChat == false) {
|
|
$chatId = $this->chatModel->createChatPresupuesto($data["chat_department_id"], $data["model_id"]);
|
|
} else {
|
|
$chat = $this->chatModel->getChatPresupuesto($data["chat_department_id"], $data["model_id"]);
|
|
$chatId = $chat->id;
|
|
}
|
|
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
|
|
$dataResponse = $this->chatMessageModel->find($chat_message_id);
|
|
return $this->response->setJSON($dataResponse);
|
|
}
|
|
public function store_chat_message_pedido()
|
|
{
|
|
|
|
$data = $this->request->getPost();
|
|
$existChat = $this->chatModel->existChatPedido($data["chat_department_id"], $data["model_id"]);
|
|
if ($existChat == false) {
|
|
$chatId = $this->chatModel->createChatPedido($data["chat_department_id"], $data["model_id"]);
|
|
} else {
|
|
$chat = $this->chatModel->getChatPedido($data["chat_department_id"], $data["model_id"]);
|
|
$chatId = $chat->id;
|
|
}
|
|
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
|
|
$dataResponse = $this->chatMessageModel->find($chat_message_id);
|
|
return $this->response->setJSON($dataResponse);
|
|
}
|
|
public function store_chat_message_factura()
|
|
{
|
|
|
|
$data = $this->request->getPost();
|
|
$existChat = $this->chatModel->existChatFactura($data["chat_department_id"], $data["model_id"]);
|
|
if ($existChat == false) {
|
|
$chatId = $this->chatModel->createChatFactura($data["chat_department_id"], $data["model_id"]);
|
|
} else {
|
|
$chat = $this->chatModel->getChatFactura($data["chat_department_id"], $data["model_id"]);
|
|
$chatId = $chat->id;
|
|
}
|
|
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
|
|
$dataResponse = $this->chatMessageModel->find($chat_message_id);
|
|
return $this->response->setJSON($dataResponse);
|
|
}
|
|
public function store_chat_message_single()
|
|
{
|
|
$data = $this->request->getPost();
|
|
|
|
$existChat = $this->chatMessageModel->get_chat_contact_messages($data["receiver_id"]);
|
|
if (count($existChat) > 0) {
|
|
$chatId = $existChat[0]->chat_id;
|
|
} else {
|
|
$chatId = $this->chatModel->createChatSingle();
|
|
}
|
|
$chat_message_id = $this->chatMessageModel->insert(
|
|
[
|
|
"chat_id" => $chatId,
|
|
"sender_id" => auth()->user()->id,
|
|
"message" => $data["message"],
|
|
"receiver_id" => $data["receiver_id"]
|
|
]
|
|
);
|
|
$dataResponse = $this->chatMessageModel->find($chat_message_id);
|
|
return $this->response->setJSON($dataResponse);
|
|
}
|
|
public function get_chat_internal_contacts()
|
|
{
|
|
if(auth()->user()->cliente_id){
|
|
return $this->response->setJSON([]);
|
|
}
|
|
$users = $this->userModel->builder()
|
|
->where("cliente_id", null)
|
|
->whereNotIn("id", [auth()->user()->id])
|
|
->where("deleted_at", null)
|
|
->get()->getResultObject();
|
|
foreach ($users as $user) {
|
|
$user->unreadMessages = $this->chatMessageModel->get_chat_unread_messages_count($user->id);
|
|
}
|
|
usort($users, fn($a, $b) => $a->unreadMessages < $b->unreadMessages);
|
|
return $this->response->setJSON($users);
|
|
}
|
|
public function get_chat_internal_contact(int $user_id)
|
|
{
|
|
if(auth()->user()->cliente_id){
|
|
return $this->response->setJSON([]);
|
|
}
|
|
$users = $this->userModel->builder()
|
|
->where("cliente_id", null)
|
|
->where("deleted_at", null)
|
|
->where("id", $user_id)
|
|
->get()->getFirstRow();
|
|
$this->chatMessageModel->set_chat_messages_as_read($user_id);
|
|
return $this->response->setJSON($users);
|
|
}
|
|
public function get_chat_internal_messages(int $user_id)
|
|
{
|
|
$conversation = $this->chatMessageModel->get_chat_contact_messages($user_id);
|
|
return $this->response->setJSON($conversation);
|
|
}
|
|
public function get_chat_cliente()
|
|
{
|
|
$cliente_id = auth()->user()->cliente_id;
|
|
$response = [];
|
|
if($cliente_id){
|
|
$data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id);
|
|
$response["totalMessages"] = 0;
|
|
$response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]);
|
|
foreach ($response["chatFacturas"] as $key => $value) {
|
|
$response["totalMessages"] += $value->unreadMessages;
|
|
}
|
|
$response["chatPresupuestos"] = $this->chatModel->getClienteChatPresupuestos($data["presupuestos"]);
|
|
foreach ($response["chatPresupuestos"] as $key => $value) {
|
|
$response["totalMessages"] += $value->unreadMessages;
|
|
}
|
|
$response["chatPedidos"] = $this->chatModel->getClienteChatPedidos($data["pedidos"]);
|
|
foreach ($response["chatPedidos"] as $key => $value) {
|
|
$response["totalMessages"] += $value->unreadMessages;
|
|
}
|
|
$response["data"] = $data;
|
|
|
|
}else{
|
|
$response["internals"] = $this->chatModel->getChatDepartmentNotifications();
|
|
$internal_notifications = $this->chatModel->getChatInternalNotifications();
|
|
foreach ($internal_notifications as $value) {
|
|
$response["internals"][] = $value;
|
|
}
|
|
$response["totalMessages"] = 0;
|
|
foreach ($response["internals"] as $key => $value) {
|
|
$response["totalMessages"] += $value->unreadMessages;
|
|
}
|
|
}
|
|
return $this->response->setJSON($response);
|
|
}
|
|
|
|
public function get_chat_department_users(int $chat_department_id)
|
|
{
|
|
$data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);
|
|
return $this->response->setJSON($data);
|
|
}
|
|
}
|