Viewing File: /home/assersoft/public_html/audiogramnew/app/Controllers/TympanogramTestsController.php
<?php
namespace App\Controllers;
use App\Models\FrequenciesModel;
use App\Models\PatientsModel;
use App\Validation\ClinicRules;
use App\Validation\FrequencyRules;
use App\Validation\PatientRules;
use App\Validation\TympanogramRules;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class TympanogramTestsController extends ResourceController
{
protected $modelName = 'App\Models\TympanogramTestsModel';
protected $format = 'json';
protected $frequencyModel;
protected $rules;
protected $frequencyRules;
protected $patientRules;
protected $patientsModel;
protected $clinicRules;
public function __construct()
{
$this->frequencyModel = new FrequenciesModel();
$this->rules = new TympanogramRules();
$this->frequencyRules = new FrequencyRules();
$this->patientRules = new PatientRules();
$this->patientsModel = new PatientsModel();
$this->clinicRules = new ClinicRules();
}
/**
* Return an array of resource objects, themselves in array format.
*
* @return ResponseInterface
*/
public function index()
{
if (!$this->validate($this->patientRules->patientId)) {
return $this->fail($this->validator->getErrors());
}
$patientId = $this->request->getVar('patient_id');
$patient = $this->patientsModel->find($patientId);
if ($patient === null) {
return $this->failNotFound('Patient not found');
}
$tympanogramTests = $this->model->where('patient_id', $patientId)->findAll();
return $this->respond($tympanogramTests);
}
/**
* 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('Tympanogram Test ID is required');
}
$tympanogramTest = $this->model->find($id);
if ($tympanogramTest === null) {
return $this->failNotFound('Tympanogram Test not found');
}
$frequencies = $this->frequencyModel->where('tympanogramtest_id', $id)->findAll();
$tympanogramTest['frequencies'] = $frequencies;
return $this->respond($tympanogramTest);
}
/**
* Create a new resource object, from "posted" parameters.
*
* @return ResponseInterface
*/
public function create()
{
if (!$this->validate($this->rules->createTympanogram)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$tympanogramData = (array) $this->request->getVar();
$tympanogramId = $this->model->insert($tympanogramData);
if (!$tympanogramId) {
return $this->failServerError("Failed to create tympanogram record.");
}
$frequencies = (array) $tympanogramData['frequencies'] ?? null;
if($frequencies != null && is_array($frequencies) && sizeof($frequencies) > 0) {
$frequenciesData = [];
foreach ($frequencies as $frequency) {
if (
is_array($frequency) &&
(!array_key_exists('pressure', $frequency) ||
!array_key_exists('frequency', $frequency) ||
!array_key_exists('level', $frequency))
) {
return $this->failValidationErrors("Invalid frequency data.");
}
$frequenciesData[] = [
'tympanogramtest_id' => $tympanogramId,
'pressure' => $frequency->pressure ?? null,
'frequency' => $frequency->frequency ?? null,
'level' => $frequency->level ?? null,
];
}
if (!empty($frequenciesData)) {
$this->frequencyModel->insertBatch($frequenciesData);
}
}
return $this->respondCreated(['tympanogramtest_id' => $tympanogramId]);
}
/**
* 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('Tympanogram Test ID is required');
}
if (!$this->validate($this->rules->updateTympanogram)) {
return $this->fail($this->validator->getErrors());
}
$tympanogramData = (array) $this->request->getVar();
$tympanogram = $this->model->find($id);
if ($tympanogram === null) {
return $this->failNotFound('Tympanogram Test not found');
}
$this->model->update($id, $tympanogramData);
$frequencies = (array) $tympanogramData['frequencies'] ?? []; // Ensure it is an array
if (!empty($frequencies) && is_array($frequencies)) {
$frequenciesData = [];
foreach ($frequencies as $frequency) {
if (
is_array($frequency) &&
(!array_key_exists('pressure', $frequency) ||
!array_key_exists('frequency', $frequency) ||
!array_key_exists('level', $frequency))
) {
return $this->failValidationErrors("Invalid frequency data.");
}
$frequenciesData[] = [
'tympanogramtest_id' => $id,
'pressure' => $frequency->pressure ?? null,
'frequency' => $frequency->frequency ?? null,
'level' => $frequency->level ?? null,
];
}
if (!empty($frequenciesData)) {
$this->frequencyModel->where('tympanogramtest_id', $id)->delete();
$this->frequencyModel->insertBatch($frequenciesData);
}
}
return $this->respondUpdated(['tympanogram_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('Tympanogram Test ID is required');
}
$tympanogram = $this->model->find($id);
if ($tympanogram === null) {
return $this->failNotFound('Tympanogram Test not found');
}
$this->frequencyModel->where('tympanogramtest_id', $id)->delete();
$this->model->delete($id);
return $this->respondDeleted(['tympanogram_id' => $id]);
}
public function getTympanogramTestsCount() {
if (!$this->validate($this->clinicRules->clinicId)) {
return $this->fail($this->validator->getErrors());
}
$clinicId = $this->request->getVar('clinic_id');
$tympanogramTestsCount = $this->model->where('clinic_id', $clinicId)->countAllResults();
return $this->respond(['count' => $tympanogramTestsCount]);
}
}
Back to Directory
File Manager