mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
202 lines
5.2 KiB
PHP
202 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Mensajeria;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
class ConversacionModel extends Model
|
|
{
|
|
protected $table = 'chat_conversaciones'; // Asegúrate de que este sea el nombre correcto de la tabla
|
|
protected $primaryKey = 'id';
|
|
protected $returnType = 'App\Entities\Mensajeria\ConversacionEntity';
|
|
protected $useTimestamps = true;
|
|
protected $useSoftDeletes = true;
|
|
protected $allowedFields = [
|
|
'asunto',
|
|
'pedido_libro_id',
|
|
'pedido_maquetacion_id',
|
|
'factura_id',
|
|
'departamento'
|
|
];
|
|
|
|
public function pedidoLibro()
|
|
{
|
|
return $this->db
|
|
->table('pedido_libros')
|
|
->where('id', $this->pedido_libro_id)
|
|
->get()
|
|
->getFirstRow();
|
|
}
|
|
|
|
public function pedidoMaquetacion()
|
|
{
|
|
return $this->db
|
|
->table('pedido_maquetaciones')
|
|
->where('id', $this->pedido_maquetacion_id)
|
|
->get()
|
|
->getFirstRow();
|
|
}
|
|
|
|
public function factura()
|
|
{
|
|
return $this->db
|
|
->table('facturas')
|
|
->where('id', $this->factura_id)
|
|
->get()
|
|
->getFirstRow();
|
|
}
|
|
|
|
|
|
public function getConversacion($convesacionId)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
//->select("t1.conversacion_id AS id, t2.asunto AS asunto")
|
|
->where("t1.id", $convesacionId)
|
|
->join("chat_participantes t2", "t2.conversacion_id = t1.id", "left")
|
|
->join("chat_mensajes t3", "t3.conversacion_id = t1.id", "left")
|
|
->orderBy('t1.created_at', 'DESC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
return $builder;
|
|
}
|
|
|
|
public function getChatParticipants($chatId)
|
|
{
|
|
return $this->db
|
|
->table($this->table . " t1")
|
|
->select("t2.usuario_id as user_id, t3.first_name AS nombre, t3.last_name AS apellidos")
|
|
->where("t1.id", $chatId)
|
|
->join("chat_participantes t2", "t2.conversacion_id = t1.id", "left")
|
|
->join("users t3", "t3.id = t2.usuario_id", "left")
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
public function getChatMessages($chatId)
|
|
{
|
|
return $this->db
|
|
->table($this->table . " t1")
|
|
->select("t2.mensaje AS mensaje, t2.usuario_id as user_id")
|
|
->select("t3.first_name AS nombre, t3.last_name AS apellidos")
|
|
->where("t1.id", intval($chatId))
|
|
->join("chat_mensajes t2", "t2.conversacion_id = t1.id", "left")
|
|
->join("users t3", "t3.id = t2.usuario_id", "left")
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Devuelve si la conversacion tiene algún cliente
|
|
*/
|
|
public function isClient()
|
|
{
|
|
$isCliente = false;
|
|
$participantes = $this->participantes(); // Asegúrate de implementar este método
|
|
foreach ($participantes as $p) {
|
|
if (($p->user && $p->user->customer_id) || $p->customer_id) {
|
|
$isCliente = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $isCliente;
|
|
}
|
|
|
|
/**
|
|
* Comprueba si la conversacion del mensaje tiene el cliente
|
|
*/
|
|
public function haveCliente($userCustomer)
|
|
{
|
|
$customer_id = $userCustomer->customer_id;
|
|
$participants = $this->participantes(); // Asegúrate de implementar este método
|
|
|
|
foreach ($participants as $p) {
|
|
if ($p->customer_id && $p->customer_id === $customer_id) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getParticipanteDesdeCliente($cliente_id)
|
|
{
|
|
return $this->db->table('chat_participantes')
|
|
->where('cliente_id', $cliente_id)
|
|
->where('conversacion_id', $this->id)
|
|
->get()
|
|
->getFirstRow();
|
|
}
|
|
|
|
public function marcarLeidoCliente($cliente_id)
|
|
{
|
|
try {
|
|
$participante = $this->getParticipanteDesdeCliente($cliente_id);
|
|
if ($participante) {
|
|
$this->db->table('chat_participantes')
|
|
->where('id', $participante->id)
|
|
->update(['last_read' => date('Y-m-d H:i:s')]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
public function marcarNoLeido($usuario_id)
|
|
{
|
|
try {
|
|
$participante = $this->getParticipanteDesdeUsuario($usuario_id); // Asegúrate de implementar este método
|
|
if ($participante) {
|
|
$this->db->table('chat_participantes')
|
|
->where('id', $participante->id)
|
|
->update(['last_read' => null]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
protected function participantes()
|
|
{
|
|
return $this->db
|
|
->table('chat_participantes')
|
|
->where('conversacion_id', $this->id)
|
|
->get()
|
|
->getResult();
|
|
}
|
|
|
|
protected function getParticipanteDesdeUsuario($usuario_id)
|
|
{
|
|
return $this->db->table('chat_participantes')
|
|
->where('usuario_id', $usuario_id)
|
|
->where('conversacion_id', $this->id)
|
|
->get()
|
|
->getFirstRow();
|
|
}
|
|
}
|