You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

90 lines
2.9 KiB

<?php
namespace App\Controllers;
use App\Models\Notification;
use App\Models\Groups;
use App\Models\Stores;
class NotificationController extends AdminController
{
public function __construct()
{
parent::__construct();
}
// Récupérer toutes les notifications selon les règles définies dans le modèle
public function getNotification()
{
$Notification = new Notification();
$notifications = $Notification->getNotifications();
return $this->response->setJSON($notifications);
}
// Marquer une notification comme lue
public function markAsRead(int $id)
{
$Notification = new Notification();
$Notification->markAsRead($id);
return $this->response->setJSON(['status' => 'success']);
}
// Créer une nouvelle notification
public function createNotification(string $message, string $group, ?int $store_id, ?string $link)
{
$Notification = new Notification();
$data = [
'message' => $message,
'is_read' => 0,
'forgroup' => $group,
'store_id' => $store_id,
'link' => $link,
'created_at' => date('Y-m-d H:i:s')
];
$Notification->insertNotification($data);
}
// Notifier tous les groupes ayant une permission donnée, pour un store spécifique
public function notifyGroupsByPermission(string $permission, string $message, ?int $store_id, ?string $link): void
{
$groupsModel = new Groups();
$groups = $groupsModel->getGroupsWithPermission($permission);
foreach ($groups as $group) {
$this->createNotification($message, $group['group_name'], $store_id, $link);
}
}
// Notifier tous les groupes ayant une permission donnée, pour TOUS les stores actifs
public function notifyGroupsByPermissionAllStores(string $permission, string $message, ?string $link): void
{
$storesModel = new Stores();
$allStores = $storesModel->getActiveStore();
if (!is_array($allStores) || empty($allStores)) return;
foreach ($allStores as $store) {
$this->notifyGroupsByPermission($permission, $message, (int)$store['id'], $link);
}
}
// Marquer toutes les notifications comme lues pour l'utilisateur connecté
public function markAllAsRead()
{
$Notification = new Notification();
$session = session();
$users = $session->get('user');
// Mettre à jour toutes les notifications non lues pour ce store et ce groupe
$builder = $Notification->builder();
$builder->where('store_id', $users['store_id'])
->groupStart()
->where('forgroup', $users['group_name'])
->orWhere('forgroup', strtolower('TOUS'))
->groupEnd()
->where('is_read', 0)
->set(['is_read' => 1])
->update();
return $this->response->setJSON(['status' => 'success']);
}
}