mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Configuraciones iniciales del sistema de mensajeria
This commit is contained in:
138
ci4/app/Models/Mensajeria/ConversacionModel.php
Normal file
138
ci4/app/Models/Mensajeria/ConversacionModel.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
30
ci4/app/Models/Mensajeria/ParticipanteModel.php
Normal file
30
ci4/app/Models/Mensajeria/ParticipanteModel.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Mensajeria;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use App\Models\Customers\Customer;
|
||||
|
||||
class ParticipanteModel extends BaseModel
|
||||
{
|
||||
protected $table = 'chat_participantes';
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = 'App\Entities\Mensajeria\ParticipanteEntity';
|
||||
protected $useTimestamps = true;
|
||||
protected $useSoftDeletes = true;
|
||||
protected $allowedFields = [
|
||||
'conversacion_id',
|
||||
'usuario_id',
|
||||
'cliente_id',
|
||||
'email',
|
||||
'last_read',
|
||||
];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
|
||||
public function getCustomer()
|
||||
{
|
||||
//$customerModel = new Customer();
|
||||
//return $customerModel->find($this->attributes['customer_id']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user