mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
139 lines
3.7 KiB
PHP
139 lines
3.7 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();
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|