Files
safekat/ci4/app/Models/Chat/ChatDeparmentModel.php
2024-10-13 23:10:17 +00:00

135 lines
4.2 KiB
PHP

<?php
namespace App\Models\Chat;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Model;
class ChatDeparmentModel extends Model
{
protected $table = 'chat_departments';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
"name",
"display",
"description",
"type"
];
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 = [];
public function getChatDepartments(string $type = "general"): array
{
$userModel = model(UserModel::class);
$chatMessageModel = model(ChatMessageModel::class);
$query = $this->db->table('chat_departments')
->select(
[
'chat_departments.id',
'chat_departments.name',
'chat_departments.display',
'chat_department_users.user_id',
'chats.id as chatId',
]
)
->join(
"chat_department_users",
"chat_department_users.chat_department_id = chat_departments.id",
'left'
)
->join("chats", "chats.chat_department_id = chat_departments.id", "left")
->join(
"users",
"chat_department_users.user_id = users.id",
'left'
)->join(
"chat_messages",
"chat_messages.chat_id = chats.id",
"left"
)
->where("chat_departments.type", $type);
if (auth()->user()->cliente_id == null) {
$query->where("chat_department_users.user_id", auth()->user()->id);
}
$results = $query->get()->getResultArray();
// Create the desired structure
$departments = [];
foreach ($results as $row) {
$departmentName = $row['name'];
// If the department is not yet added to the array, initialize it
if (!isset($departments[$departmentName])) {
$departments[$departmentName] = [
'id' => $row['id'],
'name' => $row['name'],
'display' => $row['display'],
'users' => [] // Initialize users as an empty array
];
}
// If user_id is not null, add the user to the department's 'users' array
if ($row['user_id']) {
$departments[$departmentName]['users'][] = $userModel->find($row["user_id"]);
}
}
return $departments;
}
public function getChatDepartmentUsers(int $chat_deparment_id)
{
$result = $this->db->table('chat_departments')
->select(
[
"users.*"
]
)
->join(
"chat_department_users",
"chat_department_users.chat_department_id = chat_departments.id",
'left'
)
->join(
"users",
"chat_department_users.user_id = users.id",
'left'
)->where("chat_departments.id", $chat_deparment_id)
->get()->getResultObject();
return $result;
}
}