2025-10-08 14:23:07 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Core\Model;
|
|
|
|
|
|
|
|
|
|
class NotificationGroup extends Model
|
|
|
|
|
{
|
|
|
|
|
protected static string $table = 'notification_groups';
|
|
|
|
|
|
2025-10-20 17:25:02 +03:00
|
|
|
/**
|
|
|
|
|
* Get User model instance
|
|
|
|
|
*/
|
|
|
|
|
private function getUserModel(): \App\Models\User
|
|
|
|
|
{
|
|
|
|
|
return new \App\Models\User();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 14:23:07 +03:00
|
|
|
/**
|
|
|
|
|
* Get all groups with channel count
|
|
|
|
|
*/
|
2025-10-20 17:04:13 +03:00
|
|
|
public function getAllWithChannelCount(?int $userId = null): array
|
2025-10-08 14:23:07 +03:00
|
|
|
{
|
|
|
|
|
$sql = "SELECT ng.*,
|
|
|
|
|
COUNT(DISTINCT nc.id) as channel_count,
|
|
|
|
|
COUNT(DISTINCT d.id) as domain_count
|
|
|
|
|
FROM notification_groups ng
|
|
|
|
|
LEFT JOIN notification_channels nc ON ng.id = nc.notification_group_id
|
2025-10-20 17:04:13 +03:00
|
|
|
LEFT JOIN domains d ON ng.id = d.notification_group_id";
|
|
|
|
|
|
2025-10-20 17:25:02 +03:00
|
|
|
if ($userId && !$this->getUserModel()->isAdmin($userId)) {
|
2025-10-20 17:04:13 +03:00
|
|
|
$sql .= " WHERE ng.user_id = ?";
|
|
|
|
|
$stmt = $this->db->prepare($sql);
|
|
|
|
|
$stmt->execute([$userId]);
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= " GROUP BY ng.id ORDER BY ng.name ASC";
|
|
|
|
|
$stmt = $this->db->query($sql);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 14:23:07 +03:00
|
|
|
return $stmt->fetchAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get group with channels and domains
|
|
|
|
|
*/
|
2025-10-20 17:04:13 +03:00
|
|
|
public function getWithDetails(int $id, ?int $userId = null): ?array
|
2025-10-08 14:23:07 +03:00
|
|
|
{
|
|
|
|
|
$group = $this->find($id);
|
|
|
|
|
|
|
|
|
|
if (!$group) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 17:04:13 +03:00
|
|
|
// Check if user has access to this group
|
2025-10-20 17:25:02 +03:00
|
|
|
if ($userId && !$this->getUserModel()->isAdmin($userId) && $group['user_id'] != $userId) {
|
2025-10-20 17:04:13 +03:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 14:23:07 +03:00
|
|
|
// Get channels
|
|
|
|
|
$channelModel = new NotificationChannel();
|
|
|
|
|
$group['channels'] = $channelModel->getByGroupId($id);
|
|
|
|
|
|
2025-10-20 17:04:13 +03:00
|
|
|
// Get domains (filtered by user if needed)
|
2025-10-08 14:23:07 +03:00
|
|
|
$domainModel = new Domain();
|
2025-10-20 17:25:02 +03:00
|
|
|
if ($userId && !$this->getUserModel()->isAdmin($userId)) {
|
2025-10-20 17:04:13 +03:00
|
|
|
$group['domains'] = $domainModel->where('notification_group_id', $id, $userId);
|
|
|
|
|
} else {
|
|
|
|
|
$group['domains'] = $domainModel->where('notification_group_id', $id);
|
|
|
|
|
}
|
2025-10-08 14:23:07 +03:00
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete group and handle relationships
|
|
|
|
|
*/
|
|
|
|
|
public function deleteWithRelations(int $id): bool
|
|
|
|
|
{
|
|
|
|
|
// The database CASCADE will handle channels
|
|
|
|
|
// But we need to set domains to NULL
|
|
|
|
|
$sql = "UPDATE domains SET notification_group_id = NULL WHERE notification_group_id = ?";
|
|
|
|
|
$stmt = $this->db->prepare($sql);
|
|
|
|
|
$stmt->execute([$id]);
|
|
|
|
|
|
|
|
|
|
return $this->delete($id);
|
|
|
|
|
}
|
2025-10-20 17:04:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-20 17:25:02 +03:00
|
|
|
* Assign all notification groups without user_id to a specific user
|
2025-10-20 17:04:13 +03:00
|
|
|
*/
|
2025-10-20 17:25:02 +03:00
|
|
|
public function assignUnassignedGroupsToUser(int $userId): int
|
2025-10-20 17:04:13 +03:00
|
|
|
{
|
2025-10-20 17:25:02 +03:00
|
|
|
$stmt = $this->db->prepare("UPDATE notification_groups SET user_id = ? WHERE user_id IS NULL");
|
|
|
|
|
$stmt->execute([$userId]);
|
|
|
|
|
return $stmt->rowCount();
|
2025-10-20 17:04:13 +03:00
|
|
|
}
|
2025-10-08 14:23:07 +03:00
|
|
|
}
|
|
|
|
|
|