mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
122 lines
4.0 KiB
PHP
122 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Chat;
|
|
|
|
use App\Models\Usuarios\UserModel;
|
|
use CodeIgniter\Model;
|
|
|
|
class ChatMessageModel extends Model
|
|
{
|
|
protected $table = 'chat_messages';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
"message",
|
|
"chat_id",
|
|
"sender_id",
|
|
"receiver_id",
|
|
"viewed"
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected array $casts = [];
|
|
protected array $castHandlers = [];
|
|
|
|
// Dates
|
|
protected $useTimestamps = false;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
// Validation
|
|
protected $validationRules = [];
|
|
protected $validationMessages = [];
|
|
protected $skipValidation = false;
|
|
protected $cleanValidationRules = true;
|
|
|
|
// Callbacks
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = [];
|
|
protected $afterInsert = [];
|
|
protected $beforeUpdate = [];
|
|
protected $afterUpdate = [];
|
|
protected $beforeFind = [];
|
|
protected $afterFind = [];
|
|
protected $beforeDelete = [];
|
|
protected $afterDelete = [];
|
|
|
|
/**
|
|
* Devuelve los mensajes del chat
|
|
*
|
|
* @param integer $chat_id
|
|
* @return object
|
|
*/
|
|
public function get_chat_messages(int $chat_id): array
|
|
{
|
|
$user = model(UserModel::class);
|
|
$messages = $this->builder()->where("chat_id", $chat_id)->orderBy("created_at", "asc")->get()->getResultObject();
|
|
foreach ($messages as $message) {
|
|
$message->pos = auth()->user()->id == $message->sender_id ? "right" : "left";
|
|
if (auth()->user()->id == $message->sender_id) {
|
|
$message->user = auth()->user();
|
|
} else {
|
|
$message->user = $user->find($message->sender_id);
|
|
}
|
|
}
|
|
return $messages;
|
|
}
|
|
public function get_chat_contact_messages(int $receiver_id): array
|
|
{
|
|
$conversationArray = [];
|
|
$userModel = model(UserModel::class);
|
|
$receiverUser = $userModel->find($receiver_id);
|
|
$messagesFromClient = $this->builder()
|
|
->where("sender_id", auth()->user()->id)
|
|
->where("receiver_id", $receiverUser->id)
|
|
->get()->getResultObject();
|
|
$messagesFromReceiver = $this->builder()
|
|
->where("sender_id", $receiver_id)
|
|
->where("receiver_id", auth()->user()->id)
|
|
->get()->getResultObject();
|
|
foreach ($messagesFromClient as $message) {
|
|
$message->pos = "right";
|
|
$message->user = auth()->user();
|
|
$conversationArray[] = $message;
|
|
}
|
|
foreach ($messagesFromReceiver as $message) {
|
|
$message->pos = "left";
|
|
$message->user = $receiverUser;
|
|
$conversationArray[] = $message;
|
|
}
|
|
$dates = array();
|
|
foreach ($conversationArray as $key => $row) {
|
|
$dates[$key] = strtotime($row->created_at);
|
|
}
|
|
array_multisort($dates, SORT_ASC, $conversationArray);
|
|
return $conversationArray;
|
|
}
|
|
|
|
public function get_chat_unread_messages_count(int $sender_id): int
|
|
{
|
|
$messagesFromReceiver = $this->builder()
|
|
->where("sender_id", $sender_id)
|
|
->where("viewed", false)
|
|
->where("receiver_id", auth()->user()->id)->countAllResults();
|
|
return $messagesFromReceiver;
|
|
}
|
|
public function set_chat_messages_as_read(int $sender_id): int
|
|
{
|
|
$messagesFromReceiver = $this->builder()
|
|
->set("viewed", true)
|
|
->where("sender_id", $sender_id)
|
|
->where("receiver_id", auth()->user()->id)->update();
|
|
return $messagesFromReceiver;
|
|
}
|
|
}
|