mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
102 lines
3.2 KiB
PHP
102 lines
3.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);
|
|
$query = $this->db->table('chat_departments')
|
|
->select(
|
|
[
|
|
'chat_departments.id',
|
|
'chat_departments.name',
|
|
'chat_departments.display',
|
|
'chat_department_users.user_id'
|
|
]
|
|
)
|
|
->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.type",$type)
|
|
->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;
|
|
}
|
|
}
|