Viewing File: /home/assersoft/public_html/audiogramnew/app/Controllers/PatientsController.php

<?php

namespace App\Controllers;

use App\Models\PatientsModel;
use App\Validation\ClinicRules;
use App\Validation\PatientRules;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;

class PatientsController extends ResourceController
{

    protected $modelName   = 'App\Models\PatientsModel';
    protected $format      = 'json';
    protected $rules;
    protected $clinicRules;

    public function __construct()
    {
        $this->rules       = new PatientRules();
        $this->clinicRules = new ClinicRules();
    }

    /**
     * Return an array of resource objects, themselves in array format.
     *
     * @return ResponseInterface
     */
    public function index()
    {
        if(!$this->validate($this->clinicRules->clinicId)) {
            return $this->failValidationErrors($this->validator->getErrors());
        }

        $data = (array) $this->request->getVar();

        $clinicId = $data['clinic_id'];
        $perPage  = $data['per_page'] ?? 10;
        $search   = $data['search'] ?? null;

        $patientsData = $this->model->getPagination($clinicId, $perPage, $search);
        return $this->respond($patientsData);
    }

    /**
     * Return the properties of a resource object.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function show($id = null)
    {
        if($id === null) {
            return $this->fail('Patient ID is required');
        }

        $patient = $this->model->find($id);
        if($patient === null) {
            return $this->failNotFound('Patient not found');
        }

        return $this->respond($patient);
    }

    /**
     * Create a new resource object, from "posted" parameters.
     *
     * @return ResponseInterface
     */
    public function create()
    {
        try {
        if(!$this->validate($this->rules->createPatient)) {
            return $this->failValidationErrors($this->validator->getErrors());
        }

        $patientData = (array) $this->request->getVar();

        $phoneExist = $this->model->where('patient_phone', $patientData['patient_phone'])
            ->where('clinic_id', $patientData['clinic_id'])
            ->first();

        if($phoneExist !== null) {
            return $this->fail('Patient with this phone number already exists');
        }

        $patientId = $this->model->insert($patientData);
        if($patientId === false) {
            return $this->fail('Failed to create patient');
        }
    } catch(\Exception $e) {
        return $this->fail($e->getMessage());
    }
        return $this->respondCreated(['id' => $patientId]);
    }

    /**
     * Add or update a model resource, from "posted" properties.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function update($id = null)
    {
        if($id === null) {
            return $this->fail('Patient ID is required');
        }

        if(!$this->validate($this->rules->updatePatient)) {
            return $this->fail($this->validator->getErrors());
        }

        $patientData = $this->request->getVar();
        $patientData['clinic_id'] = $this->request->getVar('clinic_id');

        $patient = $this->model->find($id);
        if($patient === null) {
            return $this->failNotFound('Patient not found');
        }

        $result = $this->model->update($id, $patientData);
        if($result === false) {
            return $this->fail('Failed to update patient');
        }

        return $this->respondUpdated(['id' => $id]);
    }

    /**
     * Delete the designated resource object from the model.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function delete($id = null)
    {
        if($id === null) {
            return $this->fail('Patient ID is required');
        }

        $patient = $this->model->find($id);
        if($patient === null) {
            return $this->failNotFound('Patient not found');
        }

        $result = $this->model->delete($id);
        if($result === false) {
            return $this->fail('Failed to delete patient');
        }

        return $this->respondDeleted(['id' => $id]);
    }

    public function getPatientsCount() {
        if(!$this->validate($this->clinicRules->clinicId)) {
            return $this->fail($this->validator->getErrors());
        }

        $clinicId = $this->request->getVar('clinic_id');

        $patientsCount = $this->model->where('clinic_id', $clinicId)->countAllResults();

        return $this->respond($patientsCount);
    }

    public function getPatientByPhone() {

        $phone = $this->request->getVar('phone');
        $clinicId = $this->request->getVar('clinic_id');

        if($phone === null) {
            return $this->fail('Phone number is required');
        }

        $patient = $this->model->where('phone', $phone)->where('clinic_id', $clinicId)->first();
        if($patient === null) {
            return $this->failNotFound('Patient not found');
        }

        return $this->respond($patient);
    }
}
Back to Directory File Manager