EVOLUTION-NINJA
Edit File: Notification.php
<?php namespace App\Controllers; class Notification extends BaseController{ public function __construct() { $this->db = \Config\Database::connect(); date_default_timezone_set('Asia/Kolkata'); } public function index() { $currentUserId = session()->get('id'); $builder = $this->db->table('notifications'); $notifications = $builder->select('*') ->where('user_id', $currentUserId) // Filter by the current user's ID ->where('status', '0') // Only unread notifications ->orderBy('created_at', 'DESC') ->get() ->getResultArray(); $unreadCount = count($notifications); // Return the response as JSON return $this->response->setJSON([ 'notifications' => $notifications, 'unreadCount' => $unreadCount ]); } public function mark_read(){ $id = session()->get('id'); // print_r($id);die(); $builder = $this->db->table('notifications'); $builder->where('status', '0'); $builder->where('user_id',$id); $builder->update(['status' => '1']); return $this->response->setJSON(['result' => 1, 'message' => 'Notifications marked as read']); } public function getUsersByRole() { $role = $this->request->getPost('role'); $currentUserRole = session()->get('role'); $currentUserId = session()->get('id'); // print_r( $currentUserId);die(); $builder = $this->db->table('users'); $builderRole = $this->db->table('roles_creation'); if ($currentUserRole === '0' || $currentUserRole === '1') { if ($role === 'all' || empty($role)) { $users = $builder->select('id, username')->get()->getResultArray(); } else { $users = $builder->select('id, username') ->where('role', $role) ->get() ->getResultArray(); } } elseif ($currentUserRole === '2') { $builder = $this->db->table('users'); $builder->select('users.id,users.username'); $builder->join('roles_creation','users.username = roles_creation.first_name') ->where('roles_creation.role',$role) ->where('roles_creation.apm',$currentUserId); $query = $builder->get(); $users = $query->getResultArray(); } else { return $this->response->setJSON(['error' => 'Access Denied']); } $response = []; foreach ($users as $user) { $response[] = [ 'id' => $user['id'], 'username' => $user['username'] ]; } return $this->response->setJSON($response); } public function send_Notification() { $selectedUsers = $this->request->getPost('users'); $message = $this->request->getPost('message'); if (!empty($selectedUsers)) { foreach ($selectedUsers as $userId) { $data = [ 'user_id' => $userId, 'message' => $message, 'status' => 0, // Unread 'created_at' => date('Y-m-d H:i:s') ]; $this->db->table('notifications')->insert($data); } return $this->response->setJSON(['result'=>1, 'message' => 'Notification sent successfully!']); } else { return $this->response->setJSON(['status' => 'error', 'message' => 'No users selected']); } } }?>