74 lines
1.6 KiB
PHP
74 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\UserModel;
|
|
use App\Models\PatientModel;
|
|
|
|
class Admin extends BaseController
|
|
{
|
|
public function dashboard()
|
|
{
|
|
if ($r = $this->requireRole('admin')) {
|
|
return $r;
|
|
}
|
|
|
|
|
|
$patientModel = new PatientModel();
|
|
|
|
|
|
|
|
$data['totalPatients'] = $patientModel->countAll();
|
|
|
|
|
|
return view('admin/dashboard', $data);
|
|
}
|
|
|
|
|
|
public function patients()
|
|
{
|
|
if ($r = $this->requireRole('admin')) {
|
|
return $r;
|
|
}
|
|
|
|
$db = \Config\Database::connect();
|
|
|
|
$query = $db->query("
|
|
SELECT users.id, users.name, users.email, patients.phone
|
|
FROM users
|
|
JOIN patients ON patients.user_id = users.id
|
|
WHERE users.role = 'patient'
|
|
");
|
|
|
|
$data['patients'] = $query->getResult();
|
|
|
|
return view('admin/patients', $data);
|
|
}
|
|
|
|
public function deletePatient($id)
|
|
{
|
|
if ($r = $this->requireRole('admin')) {
|
|
return $r;
|
|
}
|
|
|
|
$id = (int) $id;
|
|
if ($id < 1) {
|
|
return redirect()->to(site_url('admin/patients'));
|
|
}
|
|
|
|
$userModel = new UserModel();
|
|
$patientModel = new PatientModel();
|
|
$db = \Config\Database::connect();
|
|
|
|
$patient = $patientModel->where('user_id', $id)->first();
|
|
if ($patient) {
|
|
$db->table('appointments')->where('patient_id', $patient['id'])->delete();
|
|
$patientModel->delete($patient['id']);
|
|
}
|
|
|
|
$userModel->delete($id);
|
|
|
|
return redirect()->to(site_url('admin/patients'));
|
|
}
|
|
|
|
}
|